2 * Compile text message files into binary files
4 * Expects any number of files specified on the command line
5 * and outputs an equivalent number of binary files with the
6 * same name (with .mtb extension) in the local directory.
8 * Format of input files:
9 * xxx,yyy,ttttttttttttttttttttttttt
13 * ttttt is message text
17 * allocate this much memory for text strings
18 * allocate 1000 structs for
19 * read all lines into memory counting number of rows and putting
20 * text lines into memory chunk, and mesage lengths into struct
21 * array, and offsets (0 being start of memory chunk)
22 * write out number of entries
23 * iterate through struct arrays, adding file offset of start of
24 * text messages based on n number of structs
25 * write out array of structs
26 * write out memory chuck
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
42 * Static pointers to language-specific error messages
43 * IF THIS EVER CHANGES, ALSO CHANGE THE SAME TABLE IN error.c
45 static const char *errlang
[] =
51 "pt", /* portuguese */
53 "en","ca","cs","da","de","el","es","fi","fr","he","hu","is","it","ja","ko",
54 "lt","nl","no","pl","pt","ru","sk","sl","sv","th","tr","zh",
58 unsigned int getlanguage( char *lang
)
62 size
= sizeof(errlang
) / sizeof(errlang
[0]);
63 for ( i
= 0; i
< size
; i
++)
65 if ( strcmp( errlang
[i
], lang
) == 0 )
71 int main( int argc
, char *argv
[] )
76 int i
,j
,len
,end
=0,start
=0,text_size
;
79 struct textindex
*pti
,*ti
;
80 unsigned int count
,offset
,lang
;
83 ti
= (struct textindex
*)malloc( 500*sizeof(struct textindex
) );
86 fprintf( stderr
, "Unable to allocate memory for 500 mesage structures.\n" );
89 for ( i
= 1; i
< argc
; i
++ )
92 infp
= fopen( fn
, "r" );
95 fprintf( stderr
, "Unable to open %s for reading.\n", fn
);
99 stat( fn
, &stat_buf
);
100 text
= (char *)malloc( stat_buf
.st_size
);
103 fprintf( stderr
, "Unable to allocate stat_buf.st_size bytes of memory.\n" );
108 * Read each line from the file...
114 if ( fgets( line
, 511, infp
) == NULL
)
116 if ( line
[0] != '#' )
120 pti
->errorno
= atol( line
);
121 pti
->suberrorno
= atol( line
+4 );
122 pti
->fileoffset
= text_size
;
123 pti
->textlength
= strlen( line
+8)-1;
124 memcpy( text
+text_size
, line
+8, pti
->textlength
);
125 text_size
+= pti
->textlength
;
131 if ( last_count
!= 0 && count
!= (unsigned) last_count
)
133 fprintf( stderr
, "Inconsistent numbers of error messages (%d) between this file %s and the previous one (%d).\n", count
, fn
, last_count
);
138 /* generate output file name */
139 len
= strlen( argv
[i
] );
140 for ( j
= len
-1; j
>= 0; j
-- )
142 if ( argv
[i
][j
] == '.' )
144 if ( argv
[i
][j
] == '/' || argv
[i
][j
] == '\\')
150 memcpy( line
, fn
+start
+1, end
-start
);
151 line
[end
-start
-1] = '\0';
152 if ( ( lang
= getlanguage( line
) ) == 255 )
154 fprintf( stderr
, "Unknown language file name %s.\n", line
);
158 strcat( line
, ".mtb" );
159 outfp
= fopen( line
, "wb" );
162 fprintf( stderr
, "Unable to open %s for writing.\n", line
);
166 if ( fwrite( (void *)&count
, sizeof(unsigned int), 1, outfp
) != 1 )
168 fprintf( stderr
, "Unable to write message count to %s.\n", line
);
172 if ( fwrite( (void *)&lang
, sizeof(unsigned int), 1, outfp
) != 1 )
174 fprintf( stderr
, "Unable to write message lang to %s.\n", line
);
178 /* update the file offsets */
179 offset
= count
* sizeof( struct textindex
) + ( 2 * sizeof( unsigned int ) );
181 for ( j
= 0; (unsigned) j
< count
; j
++ )
183 pti
->fileoffset
+= offset
;
186 /* write the index structs */
188 for ( j
= 0; (unsigned) j
< count
; j
++ )
190 if ( fwrite( (void *)pti
, sizeof(struct textindex
), 1, outfp
) != 1 )
192 fprintf( stderr
, "Unable to write index struct %d to %s.\n", j
, line
);
198 /* write the text messages */
199 if ( (int) fwrite( text
, sizeof(char), text_size
, outfp
) != text_size
)
201 fprintf( stderr
, "Unable to write text messages to %s.\n", line
);
209 printf( "%d error messages compiled\n", last_count
);