/******************************************************************************/ /* */ /* R E P L C R B */ /* */ /* Program: REPLCRB */ /* */ /* Programmer: Dr. David G. Simpson */ /* NASA Goddard Space Flight Center */ /* Greenbelt, Maryland 20771 */ /* */ /* Date: September 27, 2006 */ /* */ /* Language: C */ /* */ /* Version: 1.00a */ /* */ /* Description: This program is a "batch" version of program REPLCR.C. */ /* It converts a batch of ASCII text file from any of the */ /* three possible formats to any other format: */ /* */ /* 1. Lines end with CR (e.g. Macintosh) */ /* 2. Lines end with LF (e.g. UNIX, VAX) */ /* 3. Lines end with CR/LF (e.g. PC) */ /* */ /* Input is a file containing a list of the files to be */ /* converted. The output files will have the same names as */ /* the input files, but with ".out" appended. */ /* */ /******************************************************************************/ /******************************************************************************/ /* #includes */ /******************************************************************************/ #include /* standard i/o */ #include /* standard library */ #include /* string functions */ /******************************************************************************/ /* #defines */ /******************************************************************************/ #define CR 0x0D /* carriage return */ #define LF 0x0A /* line feed */ /******************************************************************************/ /* function prototype */ /******************************************************************************/ void chomp (char *str); /* remove final \n from string */ /******************************************************************************/ /* global variables */ /******************************************************************************/ char filefilename[133]; /* file containing list of files */ char filename[133]; /* used to form file names */ char infilename[133]; /* input file name */ char outfilename[133]; /* output file name */ char line[133]; /* used to read in input line */ int fromfmt; /* format to convert from */ int tofmt; /* format to convert to */ int choice; /* both "from" and "to" formats */ unsigned char b, c, l; /* temp storage for bytes */ FILE *infp, *outfp, *filefp; /* file pointers */ /******************************************************************************/ /* main() */ /******************************************************************************/ int main (void) { /* Get name of file containing list of files to be converted. */ fputs("Enter name of file containing list of input files: ", stdout); fgets(filefilename, 132, stdin); chomp(filefilename); /* Get "from" format. */ fputs("\nConvert FROM format:\n", stdout); fputs(" 1. CR (e.g. Macintosh)\n", stdout); fputs(" 2. LF (e.g. UNIX, VAX)\n", stdout); fputs(" 3. CR/LF (e.g. PC)\n", stdout); fputs("? ", stdout); fgets(line, 132, stdin); sscanf(line,"%d", &fromfmt); if ((fromfmt<1) || (fromfmt>3)) { fputs("Invalid selection.\n", stdout); exit(1); } /* Get "to" format. */ fputs("\nConvert TO format:\n", stdout); fputs(" 1. CR (e.g. Macintosh)\n", stdout); fputs(" 2. LF (e.g. UNIX, VAX)\n", stdout); fputs(" 3. CR/LF (e.g. PC)\n", stdout); fputs("? ", stdout); fgets(line, 132, stdin); sscanf(line,"%d", &tofmt); if ((tofmt<1) || (tofmt>3)) { fputs("Invalid selection.\n", stdout); exit(1); } /* Combine "from" and "to" choices into a single integer. */ choice = (fromfmt<<4) | tofmt; /* Open file containing list of file names. */ if ((filefp = fopen(filefilename, "r"))==NULL) { printf("Error opening file %s\n", filefilename); exit(1); } /* Do the conversions. */ for (;;) /* loop for all input files */ { fgets(filename, 132, filefp); chomp(filename); if (feof(filefp)) break; strcpy(infilename, filename); strcpy(outfilename, filename); strcat(outfilename, ".out"); if ((infp = fopen(infilename, "rb"))==NULL) { printf("Error opening file %s\n", infilename); exit(1); } if ((outfp = fopen(outfilename, "wb"))==NULL) { printf("Error opening file %s\n", outfilename); exit(1); } switch (choice) { case 0x11: /* CR -> CR */ case 0x22: /* LF -> LF */ case 0x33: /* CR/LF -> CR/LF */ for (;;) { fread(&b, 1, 1, infp); if (feof(infp)) break; fwrite(&b, 1, 1, outfp); } break; case 0x12: /* CR -> LF */ for (;;) { fread(&b, 1, 1, infp); if (feof(infp)) break; if (b==CR) b = LF; fwrite(&b, 1, 1, outfp); } break; case 0x13: /* CR -> CR/LF */ l = LF; for (;;) { fread(&b, 1, 1, infp); if (feof(infp)) break; fwrite(&b, 1, 1, outfp); if (b==CR) fwrite(&l, 1, 1, outfp); } break; case 0x21: /* LF -> CR */ for (;;) { fread(&b, 1, 1, infp); if (feof(infp)) break; if (b==LF) b = CR; fwrite(&b, 1, 1, outfp); } break; case 0x23: /* LF -> CR/LF */ c = CR; for (;;) { fread(&b, 1, 1, infp); if (feof(infp)) break; if (b==LF) fwrite(&c, 1, 1, outfp); fwrite(&b, 1, 1, outfp); } break; case 0x31: /* CR/LF -> CR */ for (;;) { fread(&b, 1, 1, infp); if (feof(infp)) break; if (b==LF) continue; fwrite(&b, 1, 1, outfp); } break; case 0x32: /* CR/LF -> LF */ for (;;) { fread(&b, 1, 1, infp); if (feof(infp)) break; if (b==CR) continue; fwrite(&b, 1, 1, outfp); } break; default: fputs("\nProgram error.\n", stdout); break; } fclose (outfp); fclose (infp); } fclose (filefp); /* Close files and exit */ fputs("\nDone.\n", stdout); return 0; } /*****************************************************************************/ /* chomp() */ /* */ /* Remove final \n from end of string. */ /*****************************************************************************/ void chomp (char *str) { int len; /* length of str (incl \n) */ len = strlen (str); /* get length of str incl \n */ if (str[len-1] == '\n') /* if final char is \n .. */ str[len-1] = '\0'; /* ..then remove it */ }