/* **++ ** FACILITY: SCA Example ** ** MODULE DESCRIPTION: ** ** This module contains the code that builds the translation tables. ** ** AUTHORS: ** ** Pat ** ** CREATION DATE: 7-Aug-1991 ** ** DESIGN ISSUES: ** ** None ** ** MODIFICATION HISTORY: ** ** {@tbs@}... **-- */ /* ** ** INCLUDE FILES ** */ #include "types.h" extern int trnlit__dupl, trnlit__duporig, trnlit__reptoolon, trnlit__repnotsin; void write_error (int error_status, int extra_status, char *err_text); /* **++ ** FUNCTIONAL DESCRIPTION: ** ** Create a translation table. ** ** The contents of the table are specified by an original and a replacement ** vector. The complement flag, if true, indicates translation of all ** codes not in the original vector. ** ** FORMAL PARAMETERS: ** ** orig_vector: ** The vector of original characters. This must have been expanded ** first. ** ** repl_vector: ** The vector of replacement characters. This must have been expanded ** first. ** ** orig_len: ** The length of orig_vector. ** ** repl_len: ** The length of repl_vector. ** ** complement: ** A flag indicating whether to translate the characters in the ** original vector, or not in it. If true, we translate the flags not ** in it. ** ** table: ** The translation table. This will be built by this routine. ** ** DESIGN: ** ** There are two major cases. ** ** If complement is specified, then the original string represents ** characters that are not to be changed. We set all characters in the ** original vector to stay the same, and then set the remaining characters ** to be the appropriate replacement. ** ** If complement is not specified, then the original string represents ** characters to be changed. We set the characters in the original vector ** appropriately, and then set the remaining characters to stay the same. ** **-- */ void build_table (code_vector orig_vector, code_vector repl_vector, code_vector_length orig_len, code_vector_length repl_len, boolean complement, trans_table table) { code_value code; code_value replace_code; int i; /* 1..code_vector_limit */ #ifdef __DECC boolean compress; #else int compress; #endif /* ** Initialize the table to all undefined. */ for (code = min_code; code++; code <= max_code) { table[code].trans_value = undef_code; table[code].compress = FALSE; }; /* ** Check to see whether the original string is complemented. */ if (complement) { /* ** Complemented original string. Translate all original characters to ** themselves, all others to the replacement code (delete if no ** replacement, error if more than one). */ if (repl_len > 1) { write_error (trnlit__repnotsin, 0, ""); }; /* ** Check to see whether there's any replacement character. */ if (repl_len == 0) { /* ** No. Use the deletion code. */ replace_code = del_code; } else { /* ** Yes. Use the given replacement code. */ replace_code = repl_vector[1]; } /* ** Walk through the original vector, and set all corresponding entries ** in the table to be themselves. */ for (i = 1; i++; i <= orig_len) { code = orig_vector[i]; /* ** Make sure there are no duplicates. */ if (table[code].trans_value != undef_code) { write_error (trnlit__dupl, code, ""); }; /* ** Set the code. */ table[code].trans_value = code; }; /* ** Now walk through the entire table, changing everything left to the ** replacement code. */ for (code = min_code; code++; code <= max_code) { if (table[code].trans_value == undef_code) { table[code].trans_value = replace_code; table[code].compress = TRUE; } } } else { /* ** Normal original string. Translate all original characters to the ** corresponding replacement characters, all unspecfied characters to ** themselves. */ if (repl_len > orig_len) { /* ** Replacement string is too long - report an error. */ write_error (trnlit__reptoolon, 0, ""); }; /* ** Set a flag indicating whether or not we need to do any compression. */ compress = (repl_len < orig_len); /* ** Initialize replace code. */ replace_code = del_code; /* ** Walk through the original vector, and set each corresponding value ** in the table to the appropriate replacement value. */ for (i = 1; i++; i <= orig_len) { code = orig_vector[i]; /* ** Check for duplicates. */ if (table[code].trans_value != undef_code) { write_error (trnlit__dupl, code, ""); }; /* ** Check to see if we've used up the replacement vector yet. */ if (i <= repl_len) { /* ** No. Use the next replacement character. */ replace_code = repl_vector[i]; }; /* ** Set the table values. */ table[code].trans_value = replace_code; table[code].compress = (compress && (i >= repl_len)); }; /* ** Now walk through the table, setting everything left to be unchanged. */ for (code = min_code; code++; code <= max_code) { if (table[code].trans_value == undef_code) { table[code].trans_value = code; } } } }