Previous attempt to remove some compiler warnings was
[AROS-Contrib.git] / regina / msgcmp.c
blob08e0cdf2cfbe8d9b4ec4f6c764c56da88e0e54c9
1 /*
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
11 * where xxx is errno
12 * yyy is suberrno
13 * ttttt is message text
15 * For each file:
16 * get file size
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
31 * but doesn't
33 #if defined(__LCC__)
34 # include <sys/stat.h>
35 #endif
37 #include "rexx.h"
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
45 #endif
47 #include "rexxmsg.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[] =
55 "en", /* english */
56 "de", /* german */
57 "es", /* spanish */
58 "no", /* norwegian */
59 "pt", /* portuguese */
60 "pl", /* polish */
61 "sv", /* swedish */
62 "tr", /* turkish */
63 #if 0
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",
66 #endif
67 } ;
69 unsigned int getlanguage( char *lang )
71 int i,size;
73 size = sizeof(errlang) / sizeof(errlang[0]);
74 for ( i = 0; i < size; i++)
76 if ( strcmp( errlang[i], lang ) == 0 )
77 return i;
79 return 255;
82 int main( int argc, char *argv[] )
84 struct stat statbuf;
85 FILE *infp, *outfp;
86 char line[512];
87 int i,j,len,end=0,start=0,text_size;
88 char *fn;
89 char *text;
90 struct textindex *pti,*ti;
91 unsigned int count,offset,lang;
92 int last_count=0;
94 ti = (struct textindex *)malloc( 500*sizeof(struct textindex) );
95 if ( ti == NULL )
97 fprintf( stderr, "Unable to allocate memory for 500 message structures.\n" );
98 exit(1);
100 for ( i = 1; i < argc; i++ )
102 fn = argv[i];
103 infp = fopen( fn, "r" );
104 if ( infp == NULL )
106 fprintf( stderr, "Unable to open %s for reading.\n", fn );
107 free(ti);
108 exit(1);
110 stat( fn, &statbuf );
111 text = (char *)malloc( statbuf.st_size );
112 if ( text == NULL )
114 fprintf( stderr, "Unable to allocate %d bytes of memory.\n", (int) statbuf.st_size );
115 free(ti);
116 exit(1);
119 * Read each line from the file...
121 text_size = 0;
122 pti = ti;
123 for ( count = 0; ; )
125 if ( fgets( line, 511, infp ) == NULL )
126 break;
127 if ( line[0] != '#' )
129 line[3] = '\0';
130 line[7] = '\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;
137 count++;
138 pti++;
141 fclose( infp );
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 );
145 free(ti);
146 exit(1);
148 last_count = count;
149 /* generate output file name */
150 len = strlen( argv[i] );
151 for ( j = len-1; j >= 0; j-- )
153 if ( argv[i][j] == '.' )
154 end = j;
155 if ( argv[i][j] == '/' || argv[i][j] == '\\')
157 start = j;
158 break;
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 );
166 free(ti);
167 exit(1);
169 strcat( line, ".mtb" );
170 outfp = fopen( line, "wb" );
171 if ( outfp == NULL )
173 fprintf( stderr, "Unable to open %s for writing.\n", line );
174 free(ti);
175 exit(1);
177 if ( fwrite( (void *)&count, sizeof(unsigned int), 1, outfp ) != 1 )
179 fprintf( stderr, "Unable to write message count to %s.\n", line );
180 free(ti);
181 exit(1);
183 if ( fwrite( (void *)&lang, sizeof(unsigned int), 1, outfp ) != 1 )
185 fprintf( stderr, "Unable to write message lang to %s.\n", line );
186 free(ti);
187 exit(1);
189 /* update the file offsets */
190 offset = count * sizeof( struct textindex ) + ( 2 * sizeof( unsigned int ) );
191 pti = ti;
192 for ( j = 0; (unsigned) j < count; j++ )
194 pti->fileoffset += offset;
195 pti++;
197 /* write the index structs */
198 pti = ti;
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 );
204 free(ti);
205 exit(1);
207 pti++;
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 );
213 free(ti);
214 exit(1);
216 fclose( outfp );
217 free( text );
219 free(ti);
220 printf( "%d error messages compiled\n", last_count );
221 return 0;