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
30 * Bug in LCC complier wchar.h that incorrectly says it defines stat struct
34 # include <sys/stat.h>
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
50 * Static pointers to language-specific error messages
51 * IF THIS EVER CHANGES, ALSO CHANGE THE SAME TABLE IN error.c
53 static const char *errlang
[] =
59 "pt", /* portuguese */
64 "en","ca","cs","da","de","el","es","fi","fr","he","hu","is","it","ja","ko",
65 "lt","nl","no","pl","pt","ru","sk","sl","sv","th","zh",
69 unsigned int getlanguage( char *lang
)
73 size
= sizeof(errlang
) / sizeof(errlang
[0]);
74 for ( i
= 0; i
< size
; i
++)
76 if ( strcmp( errlang
[i
], lang
) == 0 )
82 int main( int argc
, char *argv
[] )
87 int i
,j
,len
,end
=0,start
=0,text_size
;
90 struct textindex
*pti
,*ti
;
91 unsigned int count
,offset
,lang
;
94 ti
= (struct textindex
*)malloc( 500*sizeof(struct textindex
) );
97 fprintf( stderr
, "Unable to allocate memory for 500 message structures.\n" );
100 for ( i
= 1; i
< argc
; i
++ )
103 infp
= fopen( fn
, "r" );
106 fprintf( stderr
, "Unable to open %s for reading.\n", fn
);
110 stat( fn
, &statbuf
);
111 text
= (char *)malloc( statbuf
.st_size
);
114 fprintf( stderr
, "Unable to allocate %d bytes of memory.\n", (int) statbuf
.st_size
);
119 * Read each line from the file...
125 if ( fgets( line
, 511, infp
) == NULL
)
127 if ( line
[0] != '#' )
131 pti
->errorno
= atol( line
);
132 pti
->suberrorno
= atol( line
+4 );
133 pti
->fileoffset
= text_size
;
134 pti
->textlength
= strlen( line
+8)-1;
135 memcpy( text
+text_size
, line
+8, pti
->textlength
);
136 text_size
+= pti
->textlength
;
142 if ( last_count
!= 0 && count
!= (unsigned) last_count
)
144 fprintf( stderr
, "Inconsistent numbers of error messages (%d) between this file %s and the previous one (%d).\n", count
, fn
, last_count
);
149 /* generate output file name */
150 len
= strlen( argv
[i
] );
151 for ( j
= len
-1; j
>= 0; j
-- )
153 if ( argv
[i
][j
] == '.' )
155 if ( argv
[i
][j
] == '/' || argv
[i
][j
] == '\\')
161 memcpy( line
, fn
+start
+1, end
-start
);
162 line
[end
-start
-1] = '\0';
163 if ( ( lang
= getlanguage( line
) ) == 255 )
165 fprintf( stderr
, "Unknown language file name %s.\n", line
);
169 strcat( line
, ".mtb" );
170 outfp
= fopen( line
, "wb" );
173 fprintf( stderr
, "Unable to open %s for writing.\n", line
);
177 if ( fwrite( (void *)&count
, sizeof(unsigned int), 1, outfp
) != 1 )
179 fprintf( stderr
, "Unable to write message count to %s.\n", line
);
183 if ( fwrite( (void *)&lang
, sizeof(unsigned int), 1, outfp
) != 1 )
185 fprintf( stderr
, "Unable to write message lang to %s.\n", line
);
189 /* update the file offsets */
190 offset
= count
* sizeof( struct textindex
) + ( 2 * sizeof( unsigned int ) );
192 for ( j
= 0; (unsigned) j
< count
; j
++ )
194 pti
->fileoffset
+= offset
;
197 /* write the index structs */
199 for ( j
= 0; (unsigned) j
< count
; j
++ )
201 if ( fwrite( (void *)pti
, sizeof(struct textindex
), 1, outfp
) != 1 )
203 fprintf( stderr
, "Unable to write index struct %d to %s.\n", j
, line
);
209 /* write the text messages */
210 if ( (int) fwrite( text
, sizeof(char), text_size
, outfp
) != text_size
)
212 fprintf( stderr
, "Unable to write text messages to %s.\n", line
);
220 printf( "%d error messages compiled\n", last_count
);