bringing SDL 1.2.14 from vendor into the main branch
[AROS-Contrib.git] / regina / msgcmp.c
blob79d3da0a0484519676c230b02281862d10c46997
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
29 #include "rexx.h"
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #ifdef HAVE_SYS_STAT_H
36 # include <sys/stat.h>
37 #endif
39 #include "rexxmsg.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[] =
47 "en", /* english */
48 "de", /* german */
49 "es", /* spanish */
50 "no", /* norwegian */
51 "pt", /* portuguese */
52 #if 0
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",
55 #endif
56 } ;
58 unsigned int getlanguage( char *lang )
60 int i,size;
62 size = sizeof(errlang) / sizeof(errlang[0]);
63 for ( i = 0; i < size; i++)
65 if ( strcmp( errlang[i], lang ) == 0 )
66 return i;
68 return 255;
71 int main( int argc, char *argv[] )
73 struct stat stat_buf;
74 FILE *infp, *outfp;
75 char line[512];
76 int i,j,len,end=0,start=0,text_size;
77 char *fn;
78 char *text;
79 struct textindex *pti,*ti;
80 unsigned int count,offset,lang;
81 int last_count=0;
83 ti = (struct textindex *)malloc( 500*sizeof(struct textindex) );
84 if ( ti == NULL )
86 fprintf( stderr, "Unable to allocate memory for 500 mesage structures.\n" );
87 exit(1);
89 for ( i = 1; i < argc; i++ )
91 fn = argv[i];
92 infp = fopen( fn, "r" );
93 if ( infp == NULL )
95 fprintf( stderr, "Unable to open %s for reading.\n", fn );
96 free(ti);
97 exit(1);
99 stat( fn, &stat_buf );
100 text = (char *)malloc( stat_buf.st_size );
101 if ( text == NULL )
103 fprintf( stderr, "Unable to allocate stat_buf.st_size bytes of memory.\n" );
104 free(ti);
105 exit(1);
108 * Read each line from the file...
110 text_size = 0;
111 pti = ti;
112 for ( count = 0; ; )
114 if ( fgets( line, 511, infp ) == NULL )
115 break;
116 if ( line[0] != '#' )
118 line[3] = '\0';
119 line[7] = '\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;
126 count++;
127 pti++;
130 fclose( infp );
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 );
134 free(ti);
135 exit(1);
137 last_count = count;
138 /* generate output file name */
139 len = strlen( argv[i] );
140 for ( j = len-1; j >= 0; j-- )
142 if ( argv[i][j] == '.' )
143 end = j;
144 if ( argv[i][j] == '/' || argv[i][j] == '\\')
146 start = j;
147 break;
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 );
155 free(ti);
156 exit(1);
158 strcat( line, ".mtb" );
159 outfp = fopen( line, "wb" );
160 if ( outfp == NULL )
162 fprintf( stderr, "Unable to open %s for writing.\n", line );
163 free(ti);
164 exit(1);
166 if ( fwrite( (void *)&count, sizeof(unsigned int), 1, outfp ) != 1 )
168 fprintf( stderr, "Unable to write message count to %s.\n", line );
169 free(ti);
170 exit(1);
172 if ( fwrite( (void *)&lang, sizeof(unsigned int), 1, outfp ) != 1 )
174 fprintf( stderr, "Unable to write message lang to %s.\n", line );
175 free(ti);
176 exit(1);
178 /* update the file offsets */
179 offset = count * sizeof( struct textindex ) + ( 2 * sizeof( unsigned int ) );
180 pti = ti;
181 for ( j = 0; (unsigned) j < count; j++ )
183 pti->fileoffset += offset;
184 pti++;
186 /* write the index structs */
187 pti = ti;
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 );
193 free(ti);
194 exit(1);
196 pti++;
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 );
202 free(ti);
203 exit(1);
205 fclose( outfp );
206 free( text );
208 free(ti);
209 printf( "%d error messages compiled\n", last_count );
210 exit(0);