/* **++ ** FACILITY: SCA Example ** ** MODULE DESCRIPTION: ** ** This module contains the code to copy one file to another, doing ** translation in the process. ** ** AUTHORS: ** ** Bill ** ** CREATION DATE: 26-Jul-1991 ** ** DESIGN ISSUES: ** ** None ** ** MODIFICATION HISTORY: ** ** {@tbs@}... **-- */ /* ** ** INCLUDE FILES ** */ #include #include #include "types.h" /* **++ ** FUNCTIONAL DESCRIPTION: ** ** Copy the input file to the output file, with character translation. ** ** FORMAL PARAMETERS: ** ** in_file: ** The input file. ** ** out_file: ** The output file. ** ** table: ** The translation table. ** ** SIDE EFFECTS: ** ** Data is read from the input file, and written to the output file. ** ** DESIGN: ** ** Use a simple while loop to move through the input file. Each ** iteration of the while loop represents the processing of a single ** character or end of line. ** ** IMPLICIT INPUT PARAMETERS: ** ** max_record_len: ** A constant that indicates the maximum rcord length. ** ** newline: ** A constant that is used to indicate a new line character. **-- */ void copy_file ( FILE *in_file, FILE *out_file, trans_table table) { char in_line[max_record_len]; char out_line[max_record_len]; int in_index, out_index; code_value code; boolean copying; int i; /* ** Initialize variables */ copying = TRUE; in_index = -1; out_index = -1; /* ** Loop until we reach the end of the file. */ while (fgets (in_line, max_record_len, in_file) != NULL) { /* ** Get next character */ if (in_index < strlen (in_line)) /* more characters on this line */ { /* ** Move to the next character */ in_index = in_index + 1; code = in_line[in_index]; } else { /* ** Move to the next line */ code = newline; in_index = -1; }; /* ** Translate or compress current character */ if (copying | (!table[code].compress)) /* it has a translation */ { /* ** Put the translated character into the output */ if (table[code].trans_value <= 255) { out_index = out_index + 1; out_line[out_index] = table[code].trans_value; } else if (table[code].trans_value == newline) { /* ** The current character is treated as a new-line - write the ** line. */ for (i = 0; i <= out_index; i++) { putc (out_line[i], out_file); } out_index = -1; }; /* ** Set flag to indicate whether we're copying or compressing ** characters. */ copying = !table[code].compress; } }; /* ** Do any necessary cleanup */ if (out_index != -1) { for (i = 0; i <= out_index; i++) { putc (out_line[i], out_file); } } }