/**
  * Simple conversion utility to go from a Linux to win32 text file format, or
  * vice versa.
  *
  * $Id: linwin.c,v 1.2 1999/10/11 06:42:11 tommut Exp tommut $
  *
  * $Log: linwin.c,v $
  * Revision 1.2  1999/10/11 06:42:11  tommut
  * *** empty log message ***
  *
  * Revision 1.1  1999/10/11 06:41:59  tommut
  * Initial revision
  *
  */

#include <stdio.h>
#include <stdlib.h>

int convert( char *filename, char *output );
int getLine( FILE *fp, char *destination, int maxlen );
void printHelp();

int maxLineNum = 67;
int convertToLinux = 0;

/**
  * convert the text file from Linux to Windows formatting 
  * return 1 on success, 0 otherwise
  */
int convert( char *filename, char *output ) 
{
    char str[256], line[82];
    FILE *in, *out;
    int count = 0;

    if ( (in = fopen( filename, "r") ) == NULL ) {
        sprintf( str, "%s%s%s","Could not open file ",filename," for reading"); 
        perror( str );
        return 0;
    }
    if ( (out = fopen( output, "w") ) == NULL ) {
        sprintf( str, "%s%s%s","Could not open file ",filename," for writing"); 
        perror( str );
        return 0;
    }
    
    memset( line, '\0', 82 );
    /** if converting from win32 to Linux, simply remove all '\r's */
    if ( convertToLinux ) {
        while ( (count = getLine( in, line, 81 )) > 0 ) {
            if ( line[count-1] == '\n' ) {
                line[count-2] = '\n';
                line[count-1] = '\0';
            }
            fprintf( out, "%s", line    );
            memset( line, '\0', 82 );
        }
    }
    else {
        while ( (count = getLine( in, line, 81 )) > 0 ) {
            if ( count > maxLineNum ) {
                if ( line[count-1] == '\n' ) {
                    line[count-1] = ' ';
                }
            }
            /** this '\n' is probably desired - prepend it with a '\r' */
            else if ( line[count-1] == '\n' ) {
                line[count-1] = '\r';
                line[count] = '\n';
                line[count+1] = '\0';
            }
            fprintf( out, "%s", line    );
            memset( line, '\0', 82 );
        }
    }
    return 1;
}

/**
  * read maxlen-1 chars from FILE fp and place in destination
  * return the number of chars read, 0 on EOF
  */
int getLine( FILE *fp, char *destination, int maxlen ) 
{
    char in;
    int index = 0;
    
    while ( ( index < maxlen - 1 ) && ( (in = fgetc( fp ) ) != EOF ) && 
        in != '\n' ) { 
        destination[index++] = in;
    }
    if ( in == '\n' ) {
        destination[index] = in;
        index++;
    }
    destination[index] = '\0';
    return index;
}

/**
  * parse arguments and set globals accordingly
  */
int main( int argc, char **argv ) 
{
    int i, inputted = 0, outputted = 0, wait = 0;
    char input[256], output[256];

    if ( argc > 1 ) {
        for ( i = 1; i < argc; i++ ) {
            if ( strcmp( argv[i], "-n" ) == 0 ) {
                if ( i+1 < argc ) {
                    maxLineNum = atoi( argv[i+1] );
                    wait = 1;
                }
            }
            else if ( strcmp( argv[i], "-l" ) == 0 ) {
                convertToLinux = 1;
            }
            else if ( (strcmp( argv[i], "-h" ) == 0) || 
                ( strcmp( argv[i], "--help" ) == 0 ) ) {
                printHelp();
                exit( 0 );
            }
            else if ( argv[i][0] == '-' ) {
                printHelp();
                exit( 0 );
            }
            else {
                if ( !wait ) {
                    if ( !inputted ) {
                        strcpy( input, argv[i] );
                        inputted = 1;
                    }
                    else if ( !outputted ) {
                        strcpy( output, argv[i] );
                        outputted = 1;
                    }
                }
                else {
                    wait = 0;
                }
            }
        }
    }
    else { 
        printHelp();
        exit( 0 );
    }
    convert( input, output );
    return 0;
}
    
void printHelp() 
{
    printf( "%s%s%-27s%s%-27s%s%-27s%s%-27s%s%-27s%s", 
        "Usage: linwin [OPTION]... INPUTFILE OUTPUTFILE\n",
        "Convert a text file from Linux formatting to Win32, or vice versa.\n",
        " -n num",
        "The maximum number of chars where a '\\n' is copied\n",  
        "",
        "  to the output file.\n",
        " -l", 
        "Convert text file from win32 to Linux formatting\n",
        "",
        "  (remove '\\r's).\n",
        " -h, --help",
        "Print help (this screen).\n\n" );
}

