grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / locale / countries / makecountry / makecountry.c
blob3d3a63cc968734815fce336a67dbc323cca001e6
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Program that makes country files
6 Lang: english
7 */
9 #include <locale.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <iconv.h>
15 #define D(x)
17 struct CountryEntry
19 char *ca_Name;
20 struct IntCountryPrefs *ca_Data;
23 extern struct IntCountryPrefs
24 andorraPrefs,
25 angolaPrefs,
26 argentinaPrefs,
27 australiaPrefs,
28 austriaPrefs,
29 belgiumPrefs,
30 bosnia_herzegovinaPrefs,
31 brazilPrefs,
32 bulgariaPrefs,
33 canadaPrefs,
34 cape_verdePrefs,
35 chinaPrefs,
36 costa_ricaPrefs,
37 croatiaPrefs,
38 czech_republicPrefs,
39 denmarkPrefs,
40 east_timorPrefs,
41 estoniaPrefs,
42 finlandPrefs,
43 francePrefs,
44 germanyPrefs,
45 great_britainPrefs,
46 greecePrefs,
47 haitiPrefs,
48 hungaryPrefs,
49 icelandPrefs,
50 indonesiaPrefs,
51 iranPrefs,
52 irelandPrefs,
53 italyPrefs,
54 japanPrefs,
55 latviaPrefs,
56 liechtensteinPrefs,
57 lithuaniaPrefs,
58 luxembourgPrefs,
59 maltaPrefs,
60 moldovaPrefs,
61 monacoPrefs,
62 montenegroPrefs,
63 new_zealandPrefs,
64 netherlandsPrefs,
65 norwayPrefs,
66 polandPrefs,
67 portugalPrefs,
68 romaniaPrefs,
69 russiaPrefs,
70 san_marinoPrefs,
71 slovakiaPrefs,
72 sloveniaPrefs,
73 south_koreaPrefs,
74 spainPrefs,
75 swedenPrefs,
76 switzerlandPrefs,
77 taiwanPrefs,
78 turkeyPrefs,
79 ukrainePrefs,
80 united_kingdomPrefs,
81 united_statesPrefs,
82 vatican_cityPrefs,
83 yugoslaviaPrefs;
85 /* Please keep this in alphabetical order, ie the order of Latin 1.
86 Character set of this file is also Latin 1 */
88 struct CountryEntry CountryArray[] =
90 { "andorra" , &andorraPrefs },
91 { "angola" , &angolaPrefs },
92 { "argentina" , &argentinaPrefs },
93 { "australia" , &australiaPrefs },
94 { "austria" , &austriaPrefs },
95 { "belgium" , &belgiumPrefs },
96 { "bosnia_herzegovina", &bosnia_herzegovinaPrefs},
97 { "brazil" , &brazilPrefs },
98 { "bulgaria" , &bulgariaPrefs },
99 { "canada" , &canadaPrefs },
100 { "cape_verde" , &cape_verdePrefs },
101 { "china" , &chinaPrefs },
102 { "costa_rica" , &costa_ricaPrefs },
103 { "croatia" , &croatiaPrefs },
104 { "czech_republic" , &czech_republicPrefs },
105 { "denmark" , &denmarkPrefs },
106 { "east_timor" , &east_timorPrefs },
107 { "estonia" , &estoniaPrefs },
108 { "finland" , &finlandPrefs },
109 { "france" , &francePrefs },
110 { "germany" , &germanyPrefs },
111 { "great_britain" , &great_britainPrefs },
112 { "greece" , &greecePrefs },
113 { "haiti" , &haitiPrefs },
114 { "hungary" , &hungaryPrefs },
115 { "iceland" , &icelandPrefs },
116 { "indonesia" , &indonesiaPrefs },
117 { "iran" , &iranPrefs },
118 { "ireland" , &irelandPrefs },
119 { "italy" , &italyPrefs },
120 { "japan" , &japanPrefs },
121 { "latvia" , &latviaPrefs },
122 { "liechtenstein" , &liechtensteinPrefs },
123 { "lithuania" , &lithuaniaPrefs },
124 { "luxembourg" , &luxembourgPrefs },
125 { "malta" , &maltaPrefs },
126 { "moldova" , &moldovaPrefs },
127 { "monaco" , &monacoPrefs },
128 { "montenegro" , &montenegroPrefs },
129 { "new_zealand" , &new_zealandPrefs },
130 { "netherlands" , &netherlandsPrefs },
131 { "norway" , &norwayPrefs },
132 { "poland" , &polandPrefs },
133 { "portugal" , &portugalPrefs },
134 { "romania" , &romaniaPrefs },
135 { "russia" , &russiaPrefs },
136 { "san_marino" , &san_marinoPrefs },
137 { "slovakia" , &slovakiaPrefs },
138 { "slovenia" , &sloveniaPrefs },
139 { "south_korea" , &south_koreaPrefs },
140 { "spain" , &spainPrefs },
141 { "sweden" , &swedenPrefs },
142 { "switzerland" , &switzerlandPrefs },
143 { "taiwan" , &taiwanPrefs },
144 { "turkey" , &turkeyPrefs },
145 { "ukraine" , &ukrainePrefs },
146 { "united_kingdom" , &united_kingdomPrefs },
147 { "united_states" , &united_statesPrefs },
148 { "vatican_city" , &vatican_cityPrefs },
149 { "yugoslavia" , &yugoslaviaPrefs },
150 { NULL , NULL }
153 /* This is equivalent to the start of the catalog file.
154 It is a series of strings, so that the endianness is
155 correct either way
157 char iffheader[] =
159 "FORM" "\x00\x00\x00\x00" "PREF"
161 "PRHD" "\x00\x00\x00\x06"
162 "\x00\x00\x00\x00\x00\x00"
165 char ctryheader[] =
167 "CTRY" "\x00\x00\x00\x00"
170 char versheader[] =
172 "FVER" "\x00\x00\x00\x00"
175 char nnameheader[] =
177 "NNAM" "\x00\x00\x00\x00"
180 char flagheader[] =
182 "FLAG" "\x00\x00\x00\x00"
186 char iffpad[] = "\x00";
188 void convertEndianness(struct CountryPrefs *cp);
189 unsigned long getCountryPrefsSize(void);
190 unsigned long getCountryPrefsVers(struct IntCountryPrefs *cp);
191 unsigned long getCountryPrefsFlag(struct IntCountryPrefs *cp);
192 unsigned long getCountryNames(struct IntCountryPrefs *cp);
194 int writeChunk(FILE *fp, char *header, void *buffer, int len,
195 char *progname, char *filename)
197 header[4] = (len & (0xFF000000)) >> 24;
198 header[5] = (len & (0x00FF0000)) >> 16;
199 header[6] = (len & (0x0000FF00)) >> 8;
200 header[7] = (len & (0x000000FF));
202 /* Write out the IFF header... */
203 if(fwrite(header, 8, 1, fp) < 1)
205 printf("%s: Error writing chunk header for %s.\n", progname, filename);
206 fclose(fp);
207 return 20;
210 if(fwrite(buffer, len, 1, fp) < 1)
212 printf("%s: Error writing chunk data for %s.\n", progname, filename);
213 fclose(fp);
214 return 20;
216 if ((len & 1) && (fwrite(iffpad, 1, 1, fp) < 1))
218 printf("%s: Error padding chunk for %s.\n", progname, filename);
219 fclose(fp);
220 return 20;
222 return 0;
225 int doCountry(struct IntCountryPrefs *cp, char *progname, char *filename)
227 FILE *fp;
228 int size = 530;
229 char *cpVers, *cpFlag, *cpNames;
231 fp = fopen(filename, "w");
232 if(fp == NULL)
234 printf("%s: Could not open file %s\n", progname, filename);
235 return 20;
238 /* Adjust the size of the IFF file if necessary ... */
239 if ((cpVers = getCountryPrefsVers(cp)) != NULL)
241 size += strlen(cpVers);
242 size += 8;
244 if ((cpFlag = getCountryPrefsFlag(cp)) != NULL)
246 size += strlen(cpFlag);
247 size += 8;
249 if ((cpNames = getCountryNames(cp)) != NULL)
251 size += strlen(cpNames);
252 size += 8;
255 iffheader[4] = (size & (0xFF000000)) >> 24;
256 iffheader[5] = (size & (0x00FF0000)) >> 16;
257 iffheader[6] = (size & (0x0000FF00)) >> 8;
258 iffheader[7] = (size & (0x000000FF));
260 /* Write out the IFF header... */
261 if(fwrite(iffheader, 26, 1, fp) < 1)
263 printf("%s: Error writing IFF header for %s.\n", progname, filename);
264 fclose(fp);
265 return 20;
268 /* Write out the main Country Prefs Chunk ... */
269 convertEndianness((struct CountryPrefs *)cp);
271 if(writeChunk(fp, ctryheader, cp, getCountryPrefsSize(), progname, filename))
273 printf("%s: Error writing country data chunk %s.\n", progname, filename);
274 return 20;
277 /* Write out the Version String Chunk if appropriate ... */
278 if (cpVers)
280 if(writeChunk(fp, versheader, cpVers, strlen(cpVers) + 1, progname, filename))
282 printf("%s: Error writing country version string chunk %s.\n", progname, filename);
283 return 20;
287 /* Write out the NativeNames Chunk if appropriate ... */
288 if (cpNames)
290 if(writeChunk(fp, nnameheader, cpNames, strlen(cpNames) + 1, progname, filename))
292 printf("%s: Error writing country native-names string chunk %s.\n", progname, filename);
293 return 20;
297 /* Write out the Flag Chunk if appropriate ... */
298 if (cpFlag)
300 if(writeChunk(fp, flagheader, cpFlag, strlen(cpFlag) + 1, progname, filename))
302 printf("%s: Error writing country flag string chunk %s.\n", progname, filename);
303 return 20;
307 fclose(fp);
308 return 0;
311 int main(int argc, char **argv)
313 int i, j, skipCountry = 0;
314 int do_specific;
315 char buffer[64];
316 char *name;
317 iconv_t cd = (iconv_t)-1;
319 if(argc < 3)
321 printf("%s: Wrong number of arguments\n", argv[0]);
322 return 20;
325 do_specific = strcmp("--all", argv[2]);
327 /* Check host OS locale. If it's UTF-8, use UTF-8 file names. Otherwise use Latin-1 names. */
328 name = setlocale(LC_CTYPE, "");
329 D(printf("System locale: %s\n", name));
331 if (!name)
333 printf("%s: System locale is invalid\n",argv[0]);
334 return 20;
337 /* Use strstr() because for example on Linux this will be "en_US.UTF-8" while on MacOS it's just "UTF-8" */
338 if ((strstr(name, "UTF-8")) || (strstr(name, "utf8")))
340 cd = iconv_open("UTF-8", "ISO-8859-1");
341 if (cd == (iconv_t)(-1))
343 printf("%s: Error converting character sets\n", argv[0]);
344 return 20;
348 for(j=0; CountryArray[j].ca_Name != NULL; j++)
350 name = CountryArray[j].ca_Name;
352 if (cd != (iconv_t)-1)
354 char *out = buffer;
355 size_t inbytes, outbytes;
357 /* Convert country name to local character set */
358 inbytes = strlen(name) + 1;
359 outbytes = sizeof(buffer);
360 iconv(cd, &name, &inbytes, &out, &outbytes);
361 name = buffer;
364 if (do_specific)
366 /* locate the specified country */
367 for(i=2; i < argc; i++)
369 skipCountry = strcmp(name, argv[i]);
370 if (!skipCountry)
371 break;
375 if (!skipCountry)
377 char path[1024];
379 printf("Generating %s.country\n", name);
381 strcpy(path, argv[1]);
382 strcat(path, name);
383 strcat(path, ".country");
384 doCountry(CountryArray[j].ca_Data, argv[0], path);
388 return 0;