Annotation of gpl/axl/babel/gen-table.c, revision 1.1.1.1

1.1       misho       1: #include <stdio.h>
                      2: #include <axl.h>
                      3: 
                      4: int main (int argc, char ** argv)
                      5: {
                      6:        FILE * file;
                      7:        FILE * file2;
                      8:        FILE * output;
                      9:        char   buffer[2];
                     10:        char   buffer2[4];
                     11:        int    iterator;
                     12:        char * tolower_encoding;
                     13:        char * toupper_encoding;
                     14:        char * encoding;
                     15:        char * string_aux;
                     16:        int    max_value;
                     17: 
                     18:        if (argc != 4) {
                     19:                printf ("Please ejecute: ./gen-table ORIGINAL_FILE UTF8-TRANSLATE-FILE CODING-NAME\n");
                     20:                return -1;
                     21:        }
                     22: 
                     23:        file = fopen (argv[1], "r");
                     24:        if (file == NULL) {
                     25:                printf ("failed to open: %s..\n", argv[1]);
                     26:                return -1;
                     27:        }
                     28: 
                     29:        file2 = fopen (argv[2], "r");
                     30:        if (file2 == NULL) {
                     31:                printf ("failed to open: %s..\n", argv[2]);
                     32:                return -1;
                     33:        }
                     34:        
                     35: 
                     36:        encoding          = (char *) argv[3];
                     37:        tolower_encoding  = axl_stream_to_lower_copy (encoding);
                     38:        axl_stream_remove (tolower_encoding, "-", axl_false);
                     39:        axl_stream_remove (tolower_encoding, ".", axl_false);
                     40: 
                     41:        toupper_encoding  = axl_stream_to_upper_copy (encoding);
                     42:        axl_stream_remove (toupper_encoding, "-", axl_false);
                     43:        axl_stream_remove (toupper_encoding, ".", axl_false);
                     44: 
                     45:        /* open output */
                     46:        string_aux = axl_strdup_printf ("axl_babel_%s.c", tolower_encoding);
                     47:        output = fopen (string_aux, "w");
                     48:        if (output == NULL) {
                     49:                printf ("failed to open output: %s..\n", string_aux);
                     50:                axl_free (string_aux);
                     51:                return -1;
                     52:        }
                     53:        
                     54: 
                     55:        fprintf (output, "/**\n");
                     56:        fprintf (output, " * Axl Babel: encoding support for axl: %s coding\n", encoding);
                     57:        fprintf (output, " * Copyright (C) 2008 Advanced Software Production Line, S.L.\n");
                     58:        fprintf (output, " */\n\n");
                     59:        fprintf (output, "/* include base header */\n");
                     60:        fprintf (output, "#include <axl_babel_%s.h>\n\n", tolower_encoding);
                     61: 
                     62:        fprintf (output, "/**\n");
                     63:        fprintf (output, " * @brief Creates the translation table for %s representation\n",
                     64:                encoding);
                     65:        fprintf (output, " *  to move information from %s to utf-8 and viceversa.\n", encoding);
                     66:        fprintf (output, " */\n");
                     67:        fprintf (output, "axlBabelTable * axl_babel_build_%s_table (void) {\n\n", tolower_encoding);
                     68: 
                     69:        fprintf (output, "\taxlBabelTable * table;\n\n");
                     70: 
                     71:        /* foreach item at the original array do */
                     72:        iterator = 0;
                     73:        while (fread (buffer, 1, 1, file) == 1) {
                     74: 
                     75:                /* read the terminator */
                     76:                if (fread (&(buffer[1]), 1, 1, file) != 1) {
                     77:                        break;
                     78:                }
                     79:                iterator++;
                     80:        } /* end file */
                     81:        fclose (file);
                     82: 
                     83:        /* record max value */
                     84:        max_value = iterator;
                     85: 
                     86:        file = fopen (argv[1], "r");
                     87:        if (file == NULL) {
                     88:                printf ("failed to open: %s..\n", argv[1]);
                     89:                return -1;
                     90:        }
                     91: 
                     92:        fprintf (output, "\t/* create the table to hold information translate %s encoding */\n", encoding);
                     93:        fprintf (output, "\ttable = axl_new (axlBabelTable, %d);\n\n", iterator);
                     94:        
                     95:        /* foreach item at the original array do */
                     96:        iterator = 0;
                     97:        while (fread (buffer, 1, 1, file) == 1) {
                     98: 
                     99:                /* read character from base file */
                    100:                if (fread (&(buffer[1]), 1, 1, file) != 1) {
                    101:                        break;
                    102:                }
                    103: 
                    104:                if (buffer[1] != '\n') {
                    105:                        printf ("ERROR (1): Expected to find new line character at=%d..\n", iterator);
                    106:                        return -1;
                    107:                }   
                    108: 
                    109:                /* check max value here */
                    110:                if (iterator >= max_value) {
                    111:                        printf ("ERROR (2): Reached max value expected at the translated file. Translation table must have a 1:1 relation.\n");
                    112:                        return -1;
                    113:                }
                    114: 
                    115:                /* read content from translated file */
                    116:                if (fread (buffer2, 1, 1, file2) != 1) {
                    117:                        printf ("ERROR (3): failed to read utf-8 file at iterator=%d (while handling buffer[0]='%c' (%u)..\n", 
                    118:                                iterator, buffer[0], (unsigned char) buffer[0]);
                    119:                        return -1;
                    120:                } /* end if */
                    121: 
                    122:                if (buffer2[0] == '\n' && buffer[0] != '\n') {
                    123:                        printf ("WARN: found no definition for buffer[0]='%c' (%u), iterator=%d..\n",
                    124:                                buffer[0], (unsigned char) buffer[0], iterator);
                    125:                        fprintf (output, "\t/* store item associated to code %d */\n", iterator);
                    126:                        fprintf (output, "\ttable[%d].size      = 1;\n", iterator);
                    127:                        fprintf (output, "\ttable[%d].buffer[0] = 0; /* unsupported translation */\n\n", iterator);
                    128:                        iterator++;
                    129:                        continue;
                    130:                }
                    131: 
                    132:                if (fread (&(buffer2[1]), 1, 1, file2) != 1) {
                    133:                        printf ("ERROR (4): failed to read utf-8 file (while handling buffer[0]='%c'..\n", iterator);
                    134:                        return -1;
                    135:                } /* end if */
                    136: 
                    137:                if (buffer2[1] == '\n') {
                    138:                        fprintf (output, "\t/* store item associated to code %d */\n", iterator);
                    139:                        fprintf (output, "\ttable[%d].size      = 1;\n", iterator);
                    140:                        fprintf (output, "\ttable[%d].buffer[0] = (unsigned char) %d; /* 0x%x */\n\n", iterator, (unsigned char) iterator, (unsigned char) iterator);
                    141:                        iterator++;
                    142:                        continue;
                    143:                }
                    144: 
                    145:                if (fread (&(buffer2[2]), 1, 1, file2) != 1) {
                    146:                        printf ("ERROR (5): failed to read utf-8 file (2 type unit)..\n");
                    147:                        return -1;
                    148:                } /* end if */
                    149:                
                    150:                if (buffer2[2] == '\n') {
                    151:                        fprintf (output, "\t/* store item associated to code %d */\n", iterator);
                    152:                        fprintf (output, "\ttable[%d].size      = 2;\n", iterator);
                    153:                        fprintf (output, "\ttable[%d].buffer[0] = (unsigned char) %u; /* 0x%x */\n", iterator, (unsigned char) buffer2[0], (unsigned char) buffer2[0]);
                    154:                        fprintf (output, "\ttable[%d].buffer[1] = (unsigned char) %u; /* 0x%x */\n\n", iterator, (unsigned char) buffer2[1], (unsigned char) buffer2[1]);
                    155:                        iterator++;
                    156:                        continue;
                    157:                }
                    158: 
                    159:                if (fread (&(buffer2[3]), 1, 1, file2) != 1) {
                    160:                        printf ("ERROR (6): failed to read utf-8 file (3 type unit)..\n");
                    161:                        return -1;
                    162:                } /* end if */
                    163: 
                    164:                fprintf (output, "\t/* store item associated to code %d */\n", iterator);
                    165:                fprintf (output, "\ttable[%d].size      = 3;\n", iterator);
                    166:                fprintf (output, "\ttable[%d].buffer[0] = (unsigned char) %u; /* 0x%x */\n",   iterator, (unsigned char) buffer2[0], (unsigned char) buffer2[0]);
                    167:                fprintf (output, "\ttable[%d].buffer[1] = (unsigned char) %u; /* 0x%x */\n",   iterator, (unsigned char) buffer2[1], (unsigned char) buffer2[1]);
                    168:                fprintf (output, "\ttable[%d].buffer[2] = (unsigned char) %u; /* 0x%x */\n\n", iterator, (unsigned char) buffer2[2], (unsigned char) buffer2[2]);
                    169:                iterator++;
                    170:                continue;
                    171:                
                    172:        }
                    173:        fclose (file);
                    174: 
                    175:        fprintf (output, "\t/* return table created */\n");
                    176:        fprintf (output, "\treturn table;\n\n");
                    177: 
                    178:        fprintf (output, "} /* end axl_babel_build_%s_table */\n\n", tolower_encoding);
                    179:        
                    180:        /* close file */
                    181:        fclose (output);
                    182:        printf ("ok, result at: %s..\n", string_aux);
                    183:        axl_free (string_aux);
                    184: 
                    185:        /* now write header */
                    186:        string_aux = axl_strdup_printf ("axl_babel_%s.h", tolower_encoding);
                    187:        output = fopen (string_aux, "w");
                    188:        if (output == NULL) {
                    189:                printf ("failed to open output: %s..\n", string_aux);
                    190:                axl_free (string_aux);
                    191:                return -1;
                    192:        }
                    193: 
                    194:        fprintf (output, "/**\n");
                    195:        fprintf (output, " * Axl Babel: encoding support for axl: %s coding\n", encoding);
                    196:        fprintf (output, " * Copyright (C) 2008 Advanced Software Production Line, S.L.\n");
                    197:        fprintf (output, " */\n\n");
                    198:        fprintf (output, "/* include base header */\n");
                    199:        fprintf (output, "#include <axl_babel.h>\n\n");
                    200: 
                    201:        fprintf (output, "#ifndef __AXL_BABEL_%s_H__\n", toupper_encoding);
                    202:        fprintf (output, "#define __AXL_BABEL_%s_H__\n\n", toupper_encoding);
                    203: 
                    204:        fprintf (output, "/**\n");
                    205:        fprintf (output, " * @brief Creates the translation table for %s representation to move information\n",
                    206:                encoding);
                    207:        fprintf (output, " * from %s to utf-8 and viceversa.\n", encoding);
                    208:        fprintf (output, " */\n");
                    209:        fprintf (output, "axlBabelTable * axl_babel_build_%s_table (void);\n", tolower_encoding);
                    210: 
                    211:        fprintf (output, "\n");
                    212:        fprintf (output, "#endif /* end __AXL_BABEL_%s_H__ */\n\n", toupper_encoding);
                    213: 
                    214:        /* close file */
                    215:        fclose (output);
                    216:        printf ("ok, result at: %s..\n", string_aux);
                    217:        axl_free (string_aux);
                    218:        
                    219:        /* free values */
                    220:        axl_free (toupper_encoding);
                    221:        axl_free (tolower_encoding);
                    222: 
                    223:        return 0;
                    224: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>