--- trunk/commandLine/commandLine.c 2003/05/05 22:01:36 520 +++ trunk/commandLine/commandLine.c 2003/05/06 19:42:41 521 @@ -2,76 +2,170 @@ #include #include +#define STR_BUFFER_SIZE 500 -char *program_name; /*the name of the program */ +// ******************************************************* +// Uncomment the following two tyedefs to support multiple +// input files +// ******************************************************* + +typedef struct{ + char fileName[STR_BUFFER_SIZE]; + FILE* filePtr; +} fileStruct; + +typedef struct linkedNameTag { + char name[STR_BUFFER_SIZE]; + struct linkedNameTag* next; +} linkedName; + +// ******************************************************* +// end multiple file support +// ******************************************************* + + +char *programName; /*the name of the program */ void usage(void); -int main(argc, argv) - int argc; - char *argv[]; +int main(argC, argV) + int argC; + char *argV[]; { int i,j; // loop counters - char *in_name = NULL; // name of the input file - char* out_prefix = NULL; // the output prefix - char current_flag; // used in parsing the flags + char* outPrefix; // the output prefix + + char* conversionCheck; + int conversionError; + int optionError; + + char currentFlag; // used in parsing the flags int done = 0; // multipurpose boolean - int have_prefix; // boolean for the output prefix + int havePrefix; // boolean for the output prefix + int flag1; + char flag1Arg1[STR_BUFFER_SIZE]; + char flag1Arg2[STR_BUFFER_SIZE]; + int flag2; + double flag2Val; + int flag3; + int rFlag; + int iFlag, iVal; + // ********************************************************** + // to support single or multiple input files, uncomment the + // appropriate declarations + // ********************************************************** + // single: + + char* inName; + FILE* inFile; + + // *********************************************************** + // multiple: + + fileStruct* inputArray; + linkedName* headName; + linkedName* currentName; + int nInputs; + + // end input file declarations + // *********************************************************** + + + + // intialize default values + + + // ******************************************** + // single input: + + inName = NULL; + inFile = NULL; + + // ********************************************* + // multiple input: + + nInputs = 0; + inputArray = NULL; + headName = NULL; + currentName = NULL; + + // end file initialization + // ********************************************** + + + + outPrefix = NULL; + + conversionError = 0; + optionError = 0; + + havePrefix = 0; + flag1 = 0; + flag2 = 0; + flag3 = 0; + rFlag = 0; + iFlag = 0; + + + iVal = 0; + + + // here we set the name of the program need by the usage message - program_name = argv[0]; + programName = argV[0]; - for( i = 1; i < argc; i++){ + for( i = 1; i < argC; i++){ - if(argv[i][0] =='-'){ + if(argV[i][0] =='-'){ // parse the option - if(argv[i][1] == '-' ){ + if(argV[i][1] == '-' ){ // parse long word options - if( !strcmp( argv[i], "--GofR" ) ){ - calcGofR = 1; + if( !strcmp( argV[i], "--flag1" ) ){ + flag1 = 1; // set flag1 to true + i++; - strcpy( gofR1, argv[i] ); + strcpy( flag1Arg1, argV[i] ); i++; - strcpy( gofR2, argv[i] ); + strcpy( flag1Arg2, argV[i] ); } - else if( !strcmp( argv[i], "--CosCorr" ) ){ - calcCosCorr = 1; + else if( !strcmp( argV[i], "--flag2" ) ){ + flag2 = 1; // set the flag2 to true; + i++; - strcpy( cosCorr1, argv[i] ); - i++; - strcpy( cosCorr2, argv[i] ); + flag2Val = strtod( argV[i], &conversionCheck ); + if( conversionCheck == argV[i] ) conversionError = 1; + if( *conversionCheck != '\0' ) conversionError = 1; + + if( conversionError ){ + + fprintf( stderr, + "Error converting \"%s\" to a double\n", argV[i] ); + usage(); + exit(0); + } + } - else if( !strcmp( argv[i], "--MuCorr") ){ - calcMuCorr = 1; - i++; - strcpy( muCorr, argv[i] ); + else if( !strcmp( argV[i], "--flag3") ){ + flag3 = 1; // set flag3 to be true + } - else if( !strcmp( argv[i], "--startFrame" ) ){ - haveStartFrame = 1; - i++; - startFrame = atoi(argv[i]); - startFrame--; - } + // anything else is an error - else if( !strcmp( argv[i], "--endFrame" ) ){ - haveEndFrame = 1; - i++; - endFrame = atoi(argv[i]); - } - else{ fprintf( stderr, - "Invalid option \"%s\"\n", argv[i] ); + "Invalid option \"%s\"\n", argV[i] ); usage(); + exit(0); } } @@ -81,43 +175,101 @@ int main(argc, argv) done =0; j = 1; - current_flag = argv[i][j]; + currentFlag = argv[i][j]; while( (current_flag != '\0') && (!done) ){ switch(current_flag){ + case 'h': + // -h => give the usage help message + + usage(); + exit(0); + break; + case 'o': // -o => the output prefix. + j++; + currentFlag = argV[i][j]; + + if( currentFlag != '\0' ) optionError = 1; + + if( optionError ){ + fprintf( stderr, + "\n" + "The -o flag should end an option sequence.\n" + " example: -ro *NOT* -or \n" ); + usage(); + exit(0); + } + i++; - out_prefix = argv[i]; - have_prefix = 1; + outPrefix = argV[i]; + if( outPrefix[0] == '-' ) optionError = 1; + + if( optionError ){ + fprintf( stderr, + "\n" + "\"%s\" is not a valid out prefix/name.\n" + "Out prefix/name should not begin with a dash.\n", + outPrefix ); + usage(); + exit(0); + } + + havePrefix = 1; done = 1; break; - case 'h': - // -h => give the usage - - usage(); - break; + case 'r': - // calculates the rmsd + // the r flag - calcRMSD = 1; + rFlag = 1; // set rflag to true break; - case 'g': - // calculate all to all g(r) + case 'i': + // -i set to the iVal - calcGofR = 1; - strcpy( gofR1, "all" ); - strcpy( gofR2, "all" ); + iFlag = 1; // set iFlag to true + j++; + currentFlag = argV[i][j]; + + if( currentFlag != '\0' ) optionError = 1; + + if( optionError ){ + fprintf( stderr, + "\n" + "The -i flag should end an option sequence.\n" + " example: -ri *NOT* -ir \n" ); + usage(); + exit(0); + } + + i++; + iVal = (int)strtol( argV[i], &conversionCheck, 10 ); + if( conversionCheck == argV[i] ) conversionError = 1; + if( *conversionCheck != '\0' ) conversionError = 1; + + if( conversionError ){ + + fprintf( stderr, + "Error converting \"%s\" to a int\n", argV[i] ); + usage(); + exit(0); + } + + done = 1; + break; default: - (void)fprintf(stderr, "Bad option \"-%c\"\n", current_flag); + (void)fprintf(stderr, + "\n" + "Bad option \"-%c\"\n", current_flag); usage(); } j++; @@ -128,25 +280,175 @@ int main(argc, argv) else{ - if( in_name != NULL ){ + + // ******************************************************** + // for only a single input file, leave this as it is. + // ******************************************************** + + if( inName != NULL ){ fprintf( stderr, + "\n" "Error at \"%s\", program does not currently support\n" "more than one input file.\n" "\n", - argv[i]); + argV[i]); usage(); + exit(0); } + + inName = argvV[i]; + + // ************************************************************* + + + // ************************************************************ + // To support more than one input file, uncomment the following + // section. + // ************************************************************ - in_name = argv[i]; + + nInputs++; + currentName = (linkedName *) malloc( sizeof( linkedName ) ); + if( currentName == NULL ){ + fprintf( stderr, + "\n" + "Ran out of memory\n" ); + exit(0); + } + + strcpy( currentName->name, argV[i] ); + + currentName->next = headName; + headName = currentName; + + // ********************************************************** + // end multiple input files + // ********************************************************** } } - if(in_name == NULL){ + + + + // initialize the input file(s) + + // *********************************************************** + // single file: + + if(inName == NULL){ + + fprintf( stderr, + "\n" + "Error, no input file was given\n"); usage(); + exit(0); } - if( !have_prefix ) out_prefix = in_name; + inFile = fopen( inName, "r" ); + if( inFile == NULL ){ + + fprintf( stderr, + "\n" + "Error trying to open \"%s\" for reading\n", + inName ); + exit(0); + } + // ************************************************************** + // multiple files: + + if( !nInputs ){ + + fprintf( stderr, + "\n" + "Error, no input files were given\n"); + usage(); + exit(0); + } + + // create the input array + + inputArray = (fileStruct *) calloc( nInputs, sizeof( fileStruct ) ); + if( inputArray == NULL ){ + fprintf(stderr, + "\n" + "Ran out of memory\n" ); + exit(0); + } + + j = nInputs - 1; + currentName = headName; + while( currentName != NULL ){ + + strcopy( inputArray[j].fileName, currentName->name ); + inputArray[i].filePtr = NULL; + j--; + currentName = currentName->next; + } + + // delete linked list + + while( headName != NULL ){ + + currentName = headName; + headName = currentName->next; + + free( currentName ); + } + + + // open the files for reading + + for(i=0; i The output name/prefix\n" " -r the r flag\n" - " -i <#> set number to <#>\n" + " -i set number to <#>\n" "\n" " long:\n" " -----\n" " --flag1 does flag 1 for arg1 and 2\n" - " --flag2 does flag 2 for arg1\n" + " --flag2 does flag 2 for double\n" " --flag3 does flag 3\n" "\n" "\n", - program_name); - exit(8); + program_name) + exit(0); }