/* Program: David.c Revision: 18-AUG-1995 This program reads an input file and writes an output file. The output file is exactly the same as the input file with the following changes: 1. If the input file contains a "control Y" character (decimal ASCII code 25) this is changed to a "Y" character (decimal ASCII code 89). 2. If the input file contains the two character sequence hex ASCII code BD followed by A0 (these are sometimes displayed on termials as "1/2" followed by "" then: o the character hex ASCII code BD is removed from the file (this line in the output file will be one character shorter then it was in the input file o the character hex ASCII code AO is changed to the character "&" which is decimal ASCII code 38. 3. If the input file contains the character hex ASCII code A0 (this is sometimes displayed on terminals are "") then this is changed to the character "&" which is decimal ASCII code 38. The following is the general outline of this program: Open the input file for reading. Open the output file for writing. Loop over all lines (records). Loop over all the characters in this line (record). For each character: IF the character is a "cntrl Y" then change it to a "Y". IF the character is a " hex BD " followed by " hex A0 " then remove the "hex BD" character and replace the "hex A0" with a "&". IF the character is a " hex A0 " then change it to a "&". ELSE just leave the character as is in the work buffer. Write the work buffer to the output file and then clear the work buffer. EndLoop EndLoop Close all files. For reference this is the "printable" ASCII code table: Character Decimal Hex Character Decimal Hex --------- ------- --- --------- ------- --- line feed = 10 0a : = 58 3a form feed = 12 0c ; = 59 3b return = 13 0d < = 60 3c space = 32 20 = = 61 3d > = 62 3e ! = 33 21 ? = 63 3f " = 34 22 @ = 64 40 # = 35 23 $ = 36 24 A:Z = 65:90 41:5a % = 37 25 & = 38 26 [ = 91 5b ' = 39 27 \ = 92 5c ] = 93 5d ( = 40 28 ^ = 94 5e ) = 41 29 _ = 95 5f * = 42 2a ` = 96 60 + = 43 2b , = 44 2c a:z = 97:122 61:7a - = 45 2d . = 46 2e { = 123 7b / = 47 2f | = 124 7c } = 125 7d 0:9 = 48:57 30:39 ~ = 126 7e */ #include #define MAXBUFF 1000 #define MAXLINE 80 void exit ( int location ) ; void clearbuf ( char working_line_buffer[] ) ; void map_chars ( char working_line_buffer[] ) ; main () { char filename[MAXLINE] ; char workbuf[MAXBUFF] ; FILE *fpin, *fpout; /* Get the filename of the input and output files */ printf("Enter the filename of the file to be MAPPED: "); gets(filename); if ((fpin = fopen(filename, "r")) == NULL) { printf("\nERROR: Can't open input file %s\n", filename); exit (1); } printf("Enter the filename of the file to receive MAPPED output: "); gets(filename); if ((fpout = fopen(filename, "w", "rat=cr", "rfm=var")) == NULL) { printf("\nERROR: Can't open output file %s\n", filename); exit (2); } /** We now have all the information that we need to start the actual work **/ clearbuf(workbuf) ; fgets(workbuf, MAXLINE, fpin); while (workbuf[0] != '\0') { map_chars( workbuf ) ; fputs(workbuf, fpout); clearbuf(workbuf); fgets(workbuf, MAXLINE, fpin); } fclose(fpin); fclose(fpout); } /* This is the CLEARBUF function. It is used to set all elements of the workbuf to '\0'. Note that because the arguments passed to this function are the names of two arrays, this function works on the actual arrays, not on temporary local copies of the arrays */ void clearbuf( char linebuf[] ) { int local_index = 0; while (local_index < MAXLINE) { linebuf[local_index] = '\0'; ++local_index; } } /* This is the MAP_CHARS function. It maps the characters in the line buffer: cntr Y --> Y, hex A0 --> &, combination hex BD followed by hex A0 then remove the BD from the line buffer and change the A0 to a decimal 38 character i.e. a "&" character. Note that the values that are compared to below are in decimal and the 2's compliment must be used for the 8 bit characters with the MSBit set. E.G. to search for hex A0 (which is decimal 160) one actually compares to decimal -96. */ void map_chars ( char buffer[] ) { int local_index, press_index ; local_index = 0 ; while (buffer[local_index] != '\0') { if ( buffer[local_index] == 25 ) { /* look for ctrl Y */ buffer[local_index] = 89 ; /* replace with Y */ ++local_index ; } /* look for hex BD */ else if ( buffer[local_index] == -67 && /* + hex A0 remove */ buffer[(local_index + 1)] == -96) { /* the hex BD and */ buffer[local_index] = 38 ; /* replace A0 with */ press_index = local_index + 1 ; /* &. */ while (buffer[press_index] != '\0') { buffer[press_index] = buffer[(press_index + 1)] ; ++press_index ; } ++local_index ; } else if ( buffer[local_index] == -96 ) { /* look for hex A0 */ buffer[local_index] = 38 ; /* replace charc & */ ++local_index ; } else { ++local_index ; } } }