Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / locale / countries / makecountry / makecountry.c
blobbdf427cb29bb618a285f3748d5ab9f3dfdb1f656
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Program that makes country files
6 Lang: english
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <iconv.h>
14 struct CountryEntry
16 char *ca_Name;
17 struct CountryPrefs *ca_Data;
20 extern struct CountryPrefs
21 andorraPrefs,
22 angolaPrefs,
23 argentinaPrefs,
24 australiaPrefs,
25 belgiePrefs,
26 belgiquePrefs,
27 bosna_i_hercegovinaPrefs,
28 bulgarijaPrefs,
29 brasilPrefs,
30 cabo_verdePrefs,
31 canadaPrefs,
32 canada_francaisPrefs,
33 czech_republicPrefs,
34 danmarkPrefs,
35 deutschPrefs,
36 eirePrefs,
37 espanaPrefs,
38 francePrefs,
39 greatBritainPrefs,
40 hrvatskaPrefs,
41 iranPrefs,
42 irelandPrefs,
43 islandPrefs,
44 italiaPrefs,
45 jugoslavijaPrefs,
46 letzebuergPrefs,
47 liechtensteinPrefs,
48 lietuvaPrefs,
49 magyarorszagPrefs,
50 monacoPrefs,
51 nihonPrefs,
52 nederlandPrefs,
53 norgePrefs,
54 osterreichPrefs,
55 polskaPrefs,
56 portugalPrefs,
57 romaniaPrefs,
58 rossijaPrefs,
59 san_marinoPrefs,
60 schweizPrefs,
61 slovakiaPrefs,
62 slovenijaPrefs,
63 suissePrefs,
64 suomiPrefs,
65 sverigePrefs,
66 svizzeraPrefs,
67 timor_lestePrefs,
68 turkiyePrefs,
69 ukrajinaPrefs,
70 united_kingdomPrefs,
71 united_statesPrefs,
72 vaticanoPrefs;
74 /* Please keep this in alphabetical order, ie the order of Latin 1 */
76 struct CountryEntry CountryArray[] =
78 { "andorra" , &andorraPrefs },
79 { "angola" , &angolaPrefs },
80 { "argentina" , &argentinaPrefs },
81 { "australia" , &australiaPrefs },
82 { "belgië" , &belgiePrefs },
83 { "belgique" , &belgiquePrefs },
84 { "bosna_i_hercegovina", &bosna_i_hercegovinaPrefs},
85 { "brasil" , &brasilPrefs },
86 { "bulgarija" , &bulgarijaPrefs },
87 { "cabo_verde" , &cabo_verdePrefs },
88 { "canada" , &canadaPrefs },
89 { "canada_français" , &canada_francaisPrefs },
90 { "czech_republic" , &czech_republicPrefs },
91 { "danmark" , &danmarkPrefs },
92 { "deutschland" , &deutschPrefs },
93 { "éire" , &eirePrefs },
94 { "españa" , &espanaPrefs },
95 { "france" , &francePrefs },
96 { "great_britain" , &greatBritainPrefs },
97 { "hrvatska" , &hrvatskaPrefs },
98 { "iran" , &iranPrefs },
99 { "ireland" , &irelandPrefs },
100 { "ísland" , &islandPrefs },
101 { "italia" , &italiaPrefs },
102 { "jugoslavija" , &jugoslavijaPrefs },
103 { "liechtenstein" , &liechtensteinPrefs },
104 { "lëtzebuerg" , &letzebuergPrefs },
105 { "lietuva" , &lietuvaPrefs },
106 { "magyarország" , &magyarorszagPrefs },
107 { "monaco" , &monacoPrefs },
108 { "nihon" , &nihonPrefs },
109 { "nederland" , &nederlandPrefs },
110 { "norge" , &norgePrefs },
111 { "österreich" , &osterreichPrefs },
112 { "polska" , &polskaPrefs },
113 { "portugal" , &portugalPrefs },
114 { "românia" , &romaniaPrefs },
115 { "rossija" , &rossijaPrefs },
116 { "san_marino" , &san_marinoPrefs },
117 { "schweiz" , &schweizPrefs },
118 { "slovakia" , &slovakiaPrefs },
119 { "slovenija" , &slovenijaPrefs },
120 { "suisse" , &suissePrefs },
121 { "suomi" , &suomiPrefs },
122 { "sverige" , &sverigePrefs },
123 { "svizzera" , &svizzeraPrefs },
124 { "timor-leste" , &timor_lestePrefs },
125 { "türkiye" , &turkiyePrefs },
126 { "ukrajina" , &ukrajinaPrefs },
127 { "united_kingdom" , &united_kingdomPrefs },
128 { "united_states" , &united_statesPrefs },
129 { "vaticano" , &vaticanoPrefs },
130 { NULL , NULL }
133 /* This is equivalent to the start of the catalog file.
134 It is a series of strings, so that the endianness is
135 correct either way
137 char preamble[] =
139 "FORM" "\x00\x00\x02\x12" "PREF"
141 "PRHD" "\x00\x00\x00\x06"
142 "\x00\x00\x00\x00\x00\x00"
144 "CTRY" "\x00\x00\x01\xF8"
147 void convertEndianness(struct CountryPrefs *cp);
148 unsigned long getCountryPrefsSize(void);
150 int doCountry(struct CountryPrefs *cp, char *progname, char *filename)
152 FILE *fp;
154 fp = fopen(filename, "w");
155 if(fp == NULL)
157 printf("%s: Could not open file %s\n", progname, filename);
158 return (20);
161 /* Write the preamble...
162 FORM 0x00000212 PREF
163 PRHD 0x00000006
165 CTRY 0x000001F8
167 if(fwrite(preamble, 34, 1, fp) < 1)
169 printf("%s: Write error during preable of %s.\n", progname, filename);
170 fclose(fp);
171 return(20);
174 convertEndianness(cp);
176 if(fwrite(cp, getCountryPrefsSize(), 1, fp) < 1)
178 printf("%s: Write error during data for %s.\n", progname, filename);
179 fclose(fp);
180 return(20);
183 fclose(fp);
184 return 0;
187 int main(int argc, char **argv)
189 int i,j,res = 0;
190 char buffer[1024];
191 char *inpos;
192 char *outpos;
193 size_t inbytes, outbytes;
194 iconv_t cd;
196 if(argc < 3)
198 printf("%s: Wrong number of arguments\n", argv[0]);
199 return(20);
202 cd = iconv_open("ISO-8859-1", "ISO-8859-1");
203 if(cd == (iconv_t)(-1))
205 printf("%s: Error converting character sets\n", argv[0]);
206 return(20);
209 for(i=2; i < argc; i++)
211 for(j=0; CountryArray[j].ca_Name != NULL; j++)
213 /* Convert country name to local character set */
214 inpos = CountryArray[j].ca_Name;
215 inbytes = strlen(inpos) + 1;
216 outpos = buffer;
217 outbytes = 1024;
218 iconv(cd, &inpos, &inbytes, &outpos, &outbytes);
220 res = strcmp(buffer, argv[i]);
221 if(res == 0)
223 strcpy(buffer, argv[1]);
224 strcat(buffer, argv[i]);
225 strcat(buffer, ".country");
226 doCountry(CountryArray[j].ca_Data, argv[0], buffer);
227 break;
229 #if 0
230 /* stegerg: does not work because of 'ö' in österreich */
231 /* If countryArray < argv[] don't bother searching */
232 else if(res > 0)
233 break;
234 #endif
237 if(res != 0)
239 printf("Unknown country %s\n", argv[i]);
240 //return(20);
243 return 0;