glib.h Rename Win32-only functions from gwin_* to g_win32_* to match the
[platform/upstream/glib.git] / glib / gwin32.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 1998-1999  Tor Lillqvist
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
23  * file for a list of people on the GLib Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 /* 
29  * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "glibconfig.h"
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
42
43 #define STRICT                  /* Strict typing, please */
44 #include <windows.h>
45 #include <direct.h>
46 #include <errno.h>
47 #include <ctype.h>
48 #ifdef _MSC_VER
49 #  include <io.h>
50 #endif /* _MSC_VER */
51
52 #include "glib.h"
53
54 int
55 g_win32_ftruncate (gint  fd,
56                    guint size)
57 {
58   HANDLE hfile;
59   guint curpos;
60
61   g_return_val_if_fail (fd >= 0, -1);
62   
63   hfile = (HANDLE) _get_osfhandle (fd);
64   curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
65   if (curpos == 0xFFFFFFFF
66       || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
67       || !SetEndOfFile (hfile))
68     {
69       gint error = GetLastError ();
70
71       switch (error)
72         {
73         case ERROR_INVALID_HANDLE:
74           errno = EBADF;
75           break;
76         default:
77           errno = EIO;
78           break;
79         }
80
81       return -1;
82     }
83
84   return 0;
85 }
86
87 DIR*
88 g_win32_opendir (const char *dirname)
89 {
90   DIR *result;
91   gchar *mask;
92   guint k;
93
94   g_return_val_if_fail (dirname != NULL, NULL);
95
96   result = g_new0 (DIR, 1);
97   result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
98   result->dir_name = g_strdup (dirname);
99   
100   k = strlen (result->dir_name);
101   if (k && result->dir_name[k - 1] == '\\')
102     {
103       result->dir_name[k - 1] = '\0';
104       k--;
105     }
106   mask = g_strdup_printf ("%s\\*", result->dir_name);
107
108   result->find_file_handle = (guint) FindFirstFile (mask,
109                                              (LPWIN32_FIND_DATA) result->find_file_data);
110   g_free (mask);
111
112   if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
113     {
114       int error = GetLastError ();
115
116       g_free (result->dir_name);
117       g_free (result->find_file_data);
118       g_free (result);
119       switch (error)
120         {
121         default:
122           errno = EIO;
123           return NULL;
124         }
125     }
126   result->just_opened = TRUE;
127
128   return result;
129 }
130
131 struct dirent*
132 g_win32_readdir (DIR *dir)
133 {
134   static struct dirent result;
135
136   g_return_val_if_fail (dir != NULL, NULL);
137
138   if (dir->just_opened)
139     dir->just_opened = FALSE;
140   else
141     {
142       if (!FindNextFile ((HANDLE) dir->find_file_handle,
143                          (LPWIN32_FIND_DATA) dir->find_file_data))
144         {
145           int error = GetLastError ();
146
147           switch (error)
148             {
149             case ERROR_NO_MORE_FILES:
150               return NULL;
151             default:
152               errno = EIO;
153               return NULL;
154             }
155         }
156     }
157   strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
158       
159   return &result;
160 }
161
162 void
163 g_win32_rewinddir (DIR *dir)
164 {
165   gchar *mask;
166
167   g_return_if_fail (dir != NULL);
168
169   if (!FindClose ((HANDLE) dir->find_file_handle))
170     g_warning ("gwin_rewinddir(): FindClose() failed\n");
171
172   mask = g_strdup_printf ("%s\\*", dir->dir_name);
173   dir->find_file_handle = (guint) FindFirstFile (mask,
174                                           (LPWIN32_FIND_DATA) dir->find_file_data);
175   g_free (mask);
176
177   if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
178     {
179       int error = GetLastError ();
180
181       switch (error)
182         {
183         default:
184           errno = EIO;
185           return;
186         }
187     }
188   dir->just_opened = TRUE;
189 }  
190
191 gint
192 g_win32_closedir (DIR *dir)
193 {
194   g_return_val_if_fail (dir != NULL, -1);
195
196   if (!FindClose ((HANDLE) dir->find_file_handle))
197     {
198       int error = GetLastError ();
199
200       switch (error)
201         {
202         default:
203           errno = EIO; return -1;
204         }
205     }
206
207   g_free (dir->dir_name);
208   g_free (dir->find_file_data);
209   g_free (dir);
210
211   return 0;
212 }
213
214 /* Mingw32 headers don't have latest language and sublanguage codes */
215 #ifndef LANG_AFRIKAANS
216 #define LANG_AFRIKAANS 0x36
217 #endif
218 #ifndef LANG_ALBANIAN
219 #define LANG_ALBANIAN 0x1c
220 #endif
221 #ifndef LANG_ARABIC
222 #define LANG_ARABIC 0x01
223 #endif
224 #ifndef LANG_ARMENIAN
225 #define LANG_ARMENIAN 0x2b
226 #endif
227 #ifndef LANG_ASSAMESE
228 #define LANG_ASSAMESE 0x4d
229 #endif
230 #ifndef LANG_AZERI
231 #define LANG_AZERI 0x2c
232 #endif
233 #ifndef LANG_BASQUE
234 #define LANG_BASQUE 0x2d
235 #endif
236 #ifndef LANG_BELARUSIAN
237 #define LANG_BELARUSIAN 0x23
238 #endif
239 #ifndef LANG_BENGALI
240 #define LANG_BENGALI 0x45
241 #endif
242 #ifndef LANG_CATALAN
243 #define LANG_CATALAN 0x03
244 #endif
245 #ifndef LANG_ESTONIAN
246 #define LANG_ESTONIAN 0x25
247 #endif
248 #ifndef LANG_FAEROESE
249 #define LANG_FAEROESE 0x38
250 #endif
251 #ifndef LANG_FARSI
252 #define LANG_FARSI 0x29
253 #endif
254 #ifndef LANG_GEORGIAN
255 #define LANG_GEORGIAN 0x37
256 #endif
257 #ifndef LANG_GUJARATI
258 #define LANG_GUJARATI 0x47
259 #endif
260 #ifndef LANG_HEBREW
261 #define LANG_HEBREW 0x0d
262 #endif
263 #ifndef LANG_HINDI
264 #define LANG_HINDI 0x39
265 #endif
266 #ifndef LANG_INDONESIAN
267 #define LANG_INDONESIAN 0x21
268 #endif
269 #ifndef LANG_KANNADA
270 #define LANG_KANNADA 0x4b
271 #endif
272 #ifndef LANG_KASHMIRI
273 #define LANG_KASHMIRI 0x60
274 #endif
275 #ifndef LANG_KAZAK
276 #define LANG_KAZAK 0x3f
277 #endif
278 #ifndef LANG_KONKANI
279 #define LANG_KONKANI 0x57
280 #endif
281 #ifndef LANG_LATVIAN
282 #define LANG_LATVIAN 0x26
283 #endif
284 #ifndef LANG_LITHUANIAN
285 #define LANG_LITHUANIAN 0x27
286 #endif
287 #ifndef LANG_MACEDONIAN
288 #define LANG_MACEDONIAN 0x2f
289 #endif
290 #ifndef LANG_MALAY
291 #define LANG_MALAY 0x3e
292 #endif
293 #ifndef LANG_MALAYALAM
294 #define LANG_MALAYALAM 0x4c
295 #endif
296 #ifndef LANG_MANIPURI
297 #define LANG_MANIPURI 0x58
298 #endif
299 #ifndef LANG_MARATHI
300 #define LANG_MARATHI 0x4e
301 #endif
302 #ifndef LANG_NEPALI
303 #define LANG_NEPALI 0x61
304 #endif
305 #ifndef LANG_ORIYA
306 #define LANG_ORIYA 0x48
307 #endif
308 #ifndef LANG_PUNJABI
309 #define LANG_PUNJABI 0x46
310 #endif
311 #ifndef LANG_SANSKRIT
312 #define LANG_SANSKRIT 0x4f
313 #endif
314 #ifndef LANG_SERBIAN
315 #define LANG_SERBIAN 0x1a
316 #endif
317 #ifndef LANG_SINDHI
318 #define LANG_SINDHI 0x59
319 #endif
320 #ifndef LANG_SLOVAK
321 #define LANG_SLOVAK 0x1b
322 #endif
323 #ifndef LANG_SWAHILI
324 #define LANG_SWAHILI 0x41
325 #endif
326 #ifndef LANG_TAMIL
327 #define LANG_TAMIL 0x49
328 #endif
329 #ifndef LANG_TATAR
330 #define LANG_TATAR 0x44
331 #endif
332 #ifndef LANG_TELUGU
333 #define LANG_TELUGU 0x4a
334 #endif
335 #ifndef LANG_THAI
336 #define LANG_THAI 0x1e
337 #endif
338 #ifndef LANG_UKRAINIAN
339 #define LANG_UKRAINIAN 0x22
340 #endif
341 #ifndef LANG_URDU
342 #define LANG_URDU 0x20
343 #endif
344 #ifndef LANG_UZBEK
345 #define LANG_UZBEK 0x43
346 #endif
347 #ifndef LANG_VIETNAMESE
348 #define LANG_VIETNAMESE 0x2a
349 #endif
350 #ifndef SUBLANG_ARABIC_SAUDI_ARABIA
351 #define SUBLANG_ARABIC_SAUDI_ARABIA 0x01
352 #endif
353 #ifndef SUBLANG_ARABIC_IRAQ
354 #define SUBLANG_ARABIC_IRAQ 0x02
355 #endif
356 #ifndef SUBLANG_ARABIC_EGYPT
357 #define SUBLANG_ARABIC_EGYPT 0x03
358 #endif
359 #ifndef SUBLANG_ARABIC_LIBYA
360 #define SUBLANG_ARABIC_LIBYA 0x04
361 #endif
362 #ifndef SUBLANG_ARABIC_ALGERIA
363 #define SUBLANG_ARABIC_ALGERIA 0x05
364 #endif
365 #ifndef SUBLANG_ARABIC_MOROCCO
366 #define SUBLANG_ARABIC_MOROCCO 0x06
367 #endif
368 #ifndef SUBLANG_ARABIC_TUNISIA
369 #define SUBLANG_ARABIC_TUNISIA 0x07
370 #endif
371 #ifndef SUBLANG_ARABIC_OMAN
372 #define SUBLANG_ARABIC_OMAN 0x08
373 #endif
374 #ifndef SUBLANG_ARABIC_YEMEN
375 #define SUBLANG_ARABIC_YEMEN 0x09
376 #endif
377 #ifndef SUBLANG_ARABIC_SYRIA
378 #define SUBLANG_ARABIC_SYRIA 0x0a
379 #endif
380 #ifndef SUBLANG_ARABIC_JORDAN
381 #define SUBLANG_ARABIC_JORDAN 0x0b
382 #endif
383 #ifndef SUBLANG_ARABIC_LEBANON
384 #define SUBLANG_ARABIC_LEBANON 0x0c
385 #endif
386 #ifndef SUBLANG_ARABIC_KUWAIT
387 #define SUBLANG_ARABIC_KUWAIT 0x0d
388 #endif
389 #ifndef SUBLANG_ARABIC_UAE
390 #define SUBLANG_ARABIC_UAE 0x0e
391 #endif
392 #ifndef SUBLANG_ARABIC_BAHRAIN
393 #define SUBLANG_ARABIC_BAHRAIN 0x0f
394 #endif
395 #ifndef SUBLANG_ARABIC_QATAR
396 #define SUBLANG_ARABIC_QATAR 0x10
397 #endif
398 #ifndef SUBLANG_AZERI_LATIN
399 #define SUBLANG_AZERI_LATIN 0x01
400 #endif
401 #ifndef SUBLANG_AZERI_CYRILLIC
402 #define SUBLANG_AZERI_CYRILLIC 0x02
403 #endif
404 #ifndef SUBLANG_CHINESE_MACAU
405 #define SUBLANG_CHINESE_MACAU 0x05
406 #endif
407 #ifndef SUBLANG_ENGLISH_SOUTH_AFRICA
408 #define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07
409 #endif
410 #ifndef SUBLANG_ENGLISH_JAMAICA
411 #define SUBLANG_ENGLISH_JAMAICA 0x08
412 #endif
413 #ifndef SUBLANG_ENGLISH_CARIBBEAN
414 #define SUBLANG_ENGLISH_CARIBBEAN 0x09
415 #endif
416 #ifndef SUBLANG_ENGLISH_BELIZE
417 #define SUBLANG_ENGLISH_BELIZE 0x0a
418 #endif
419 #ifndef SUBLANG_ENGLISH_TRINIDAD
420 #define SUBLANG_ENGLISH_TRINIDAD 0x0b
421 #endif
422 #ifndef SUBLANG_ENGLISH_ZIMBABWE
423 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
424 #endif
425 #ifndef SUBLANG_ENGLISH_PHILIPPINES
426 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
427 #endif
428 #ifndef SUBLANG_FRENCH_LUXEMBOURG
429 #define SUBLANG_FRENCH_LUXEMBOURG 0x05
430 #endif
431 #ifndef SUBLANG_FRENCH_MONACO
432 #define SUBLANG_FRENCH_MONACO 0x06
433 #endif
434 #ifndef SUBLANG_GERMAN_LUXEMBOURG
435 #define SUBLANG_GERMAN_LUXEMBOURG 0x04
436 #endif
437 #ifndef SUBLANG_GERMAN_LIECHTENSTEIN
438 #define SUBLANG_GERMAN_LIECHTENSTEIN 0x05
439 #endif
440 #ifndef SUBLANG_KASHMIRI_INDIA
441 #define SUBLANG_KASHMIRI_INDIA 0x02
442 #endif
443 #ifndef SUBLANG_MALAY_MALAYSIA
444 #define SUBLANG_MALAY_MALAYSIA 0x01
445 #endif
446 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
447 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
448 #endif
449 #ifndef SUBLANG_NEPALI_INDIA
450 #define SUBLANG_NEPALI_INDIA 0x02
451 #endif
452 #ifndef SUBLANG_SERBIAN_LATIN
453 #define SUBLANG_SERBIAN_LATIN 0x02
454 #endif
455 #ifndef SUBLANG_SERBIAN_CYRILLIC
456 #define SUBLANG_SERBIAN_CYRILLIC 0x03
457 #endif
458 #ifndef SUBLANG_SPANISH_GUATEMALA
459 #define SUBLANG_SPANISH_GUATEMALA 0x04
460 #endif
461 #ifndef SUBLANG_SPANISH_COSTA_RICA
462 #define SUBLANG_SPANISH_COSTA_RICA 0x05
463 #endif
464 #ifndef SUBLANG_SPANISH_PANAMA
465 #define SUBLANG_SPANISH_PANAMA 0x06
466 #endif
467 #ifndef SUBLANG_SPANISH_DOMINICAN_REPUBLIC
468 #define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07
469 #endif
470 #ifndef SUBLANG_SPANISH_VENEZUELA
471 #define SUBLANG_SPANISH_VENEZUELA 0x08
472 #endif
473 #ifndef SUBLANG_SPANISH_COLOMBIA
474 #define SUBLANG_SPANISH_COLOMBIA 0x09
475 #endif
476 #ifndef SUBLANG_SPANISH_PERU
477 #define SUBLANG_SPANISH_PERU 0x0a
478 #endif
479 #ifndef SUBLANG_SPANISH_ARGENTINA
480 #define SUBLANG_SPANISH_ARGENTINA 0x0b
481 #endif
482 #ifndef SUBLANG_SPANISH_ECUADOR
483 #define SUBLANG_SPANISH_ECUADOR 0x0c
484 #endif
485 #ifndef SUBLANG_SPANISH_CHILE
486 #define SUBLANG_SPANISH_CHILE 0x0d
487 #endif
488 #ifndef SUBLANG_SPANISH_URUGUAY
489 #define SUBLANG_SPANISH_URUGUAY 0x0e
490 #endif
491 #ifndef SUBLANG_SPANISH_PARAGUAY
492 #define SUBLANG_SPANISH_PARAGUAY 0x0f
493 #endif
494 #ifndef SUBLANG_SPANISH_BOLIVIA
495 #define SUBLANG_SPANISH_BOLIVIA 0x10
496 #endif
497 #ifndef SUBLANG_SPANISH_EL_SALVADOR
498 #define SUBLANG_SPANISH_EL_SALVADOR 0x11
499 #endif
500 #ifndef SUBLANG_SPANISH_HONDURAS
501 #define SUBLANG_SPANISH_HONDURAS 0x12
502 #endif
503 #ifndef SUBLANG_SPANISH_NICARAGUA
504 #define SUBLANG_SPANISH_NICARAGUA 0x13
505 #endif
506 #ifndef SUBLANG_SPANISH_PUERTO_RICO
507 #define SUBLANG_SPANISH_PUERTO_RICO 0x14
508 #endif
509 #ifndef SUBLANG_SWEDISH_FINLAND
510 #define SUBLANG_SWEDISH_FINLAND 0x02
511 #endif
512 #ifndef SUBLANG_URDU_PAKISTAN
513 #define SUBLANG_URDU_PAKISTAN 0x01
514 #endif
515 #ifndef SUBLANG_URDU_INDIA
516 #define SUBLANG_URDU_INDIA 0x02
517 #endif
518 #ifndef SUBLANG_UZBEK_LATIN
519 #define SUBLANG_UZBEK_LATIN 0x01
520 #endif
521 #ifndef SUBLANG_UZBEK_CYRILLIC
522 #define SUBLANG_UZBEK_CYRILLIC 0x02
523 #endif
524
525 gchar *
526 g_win32_getlocale (void)
527 {
528   LCID lcid = GetThreadLocale ();
529   gint primary, sub;
530   gchar *l = NULL, *sl = NULL;
531   gchar bfr[20];
532
533   primary = PRIMARYLANGID (LANGIDFROMLCID (lcid));
534   sub = SUBLANGID (LANGIDFROMLCID (lcid));
535   switch (primary)
536     {
537     case LANG_AFRIKAANS: l = "af"; break;
538     case LANG_ALBANIAN: l = "sq"; break;
539     case LANG_ARABIC:
540       l = "ar";
541       switch (sub)
542         {
543         case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
544         case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
545         case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
546         case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
547         case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
548         case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
549         case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
550         case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
551         case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
552         case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
553         case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
554         case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
555         case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
556         case SUBLANG_ARABIC_UAE: sl = "AE"; break;
557         case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
558         case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
559         }
560       break;
561     case LANG_ARMENIAN: l = "hy"; break;
562     case LANG_ASSAMESE: l = "as"; break;
563     case LANG_AZERI: l = "az"; break;
564     case LANG_BASQUE: l = "eu"; break;
565     case LANG_BELARUSIAN: l = "be"; break;
566     case LANG_BENGALI: l = "bn"; break;
567     case LANG_BULGARIAN: l = "bg"; break;
568     case LANG_CATALAN: l = "ca"; break;
569     case LANG_CHINESE:
570       l = "zh";
571       switch (sub)
572         {
573         case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
574         case SUBLANG_CHINESE_SIMPLIFIED: sl = "CH"; break;
575         case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
576         case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
577         case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
578         }
579       break;
580     case LANG_CROATIAN:         /* LANG_CROATIAN == LANG_SERBIAN
581                                  * What used to be called Serbo-Croatian
582                                  * should really now be two separate
583                                  * languages because of political reasons.
584                                  * (Says tml, who knows nothing about Serbian
585                                  * or Croatian.)
586                                  * (I can feel those flames coming already.)
587                                  */
588       switch (sub)
589         {
590         case SUBLANG_SERBIAN_LATIN: l = "hr"; break;
591         case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
592         default: l = "hr";      /* ??? */
593         }
594       break;
595     case LANG_CZECH: l = "cs"; break;
596     case LANG_DANISH: l = "da"; break;
597     case LANG_DUTCH:
598       l = "nl";
599       switch (sub)
600         {
601         case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
602         }
603       break;
604     case LANG_ENGLISH:
605       l = "en";
606       switch (sub)
607         {
608         case SUBLANG_ENGLISH_US: sl = "US"; break;
609         case SUBLANG_ENGLISH_UK: sl = "GB"; break;
610         case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
611         case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
612         case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
613         case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
614         case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "SA"; break;
615         case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
616         case SUBLANG_ENGLISH_CARIBBEAN: sl = "@caribbean"; break; /* ??? */
617         case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
618         case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
619         case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
620         case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
621         }
622       break;
623     case LANG_ESTONIAN: l = "et"; break;
624     case LANG_FAEROESE: l = "fo"; break;
625     case LANG_FARSI: l = "fa"; break;
626     case LANG_FINNISH: l = "fi"; break;
627     case LANG_FRENCH:
628       l = "fr";
629       switch (sub)
630         {
631         case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
632         case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
633         case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
634         case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
635         case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
636         }
637       break;
638     case LANG_GEORGIAN: l = "ka"; break;
639     case LANG_GERMAN:
640       l = "de";
641       switch (sub)
642         {
643         case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
644         case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
645         case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
646         case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
647         }
648       break;
649     case LANG_GREEK: l = "el"; break;
650     case LANG_GUJARATI: l = "gu"; break;
651     case LANG_HEBREW: l = "he"; break;
652     case LANG_HINDI: l = "hi"; break;
653     case LANG_HUNGARIAN: l = "hu"; break;
654     case LANG_ICELANDIC: l = "is"; break;
655     case LANG_INDONESIAN: l = "id"; break;
656     case LANG_ITALIAN:
657       l = "it";
658       switch (sub)
659         {
660         case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
661         }
662       break;
663     case LANG_JAPANESE: l = "ja"; break;
664     case LANG_KANNADA: l = "kn"; break;
665     case LANG_KASHMIRI:
666       l = "ks";
667       switch (sub)
668         {
669         case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
670         }
671       break;
672     case LANG_KAZAK: l = "kk"; break;
673     case LANG_KONKANI: l = "kok"; break; /* ??? */
674     case LANG_KOREAN: l = "ko"; break;
675     case LANG_LATVIAN: l = "lv"; break;
676     case LANG_LITHUANIAN: l = "lt"; break;
677     case LANG_MACEDONIAN: l = "mk"; break;
678     case LANG_MALAY:
679       l = "ms";
680       switch (sub)
681         {
682         case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
683         case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
684         }
685       break;
686     case LANG_MALAYALAM: l = "ml"; break;
687     case LANG_MANIPURI: l = "mni"; break;
688     case LANG_MARATHI: l = "mr"; break;
689     case LANG_NEPALI:
690       l = "ne";
691       switch (sub)
692         {
693         case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
694         }
695       break;
696     case LANG_NORWEGIAN:
697       l = "no";
698       switch (sub)
699         {
700         case SUBLANG_NORWEGIAN_BOKMAL: sl = "@bokmal"; break;
701         case SUBLANG_NORWEGIAN_NYNORSK: sl = "@nynorsk"; break;
702         }
703       break;
704     case LANG_ORIYA: l = "or"; break;
705     case LANG_POLISH: l = "pl"; break;
706     case LANG_PORTUGUESE:
707       l = "pt";
708       switch (sub)
709         {
710         case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
711         }
712       break;
713     case LANG_PUNJABI: l = "pa"; break;
714     case LANG_ROMANIAN: l = "ro"; break;
715     case LANG_RUSSIAN: l = "ru"; break;
716     case LANG_SANSKRIT: l = "sa"; break;
717     case LANG_SINDHI: l = "sd"; break;
718     case LANG_SLOVAK: l = "sk"; break;
719     case LANG_SLOVENIAN: l = "sl"; break;
720     case LANG_SPANISH:
721       l = "es";
722       switch (sub)
723         {
724         case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
725         case SUBLANG_SPANISH_MODERN: sl = "@modern"; break;     /* ??? */
726         case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
727         case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
728         case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
729         case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
730         case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
731         case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
732         case SUBLANG_SPANISH_PERU: sl = "PE"; break;
733         case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
734         case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
735         case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
736         case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
737         case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
738         case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
739         case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
740         case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
741         case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
742         case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
743         }
744       break;
745     case LANG_SWAHILI: l = "sw"; break;
746     case LANG_SWEDISH:
747       l = "sv";
748       switch (sub)
749         {
750         case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
751         }
752       break;
753     case LANG_TAMIL: l = "ta"; break;
754     case LANG_TATAR: l = "tt"; break;
755     case LANG_TELUGU: l = "te"; break;
756     case LANG_THAI: l = "th"; break;
757     case LANG_TURKISH: l = "tr"; break;
758     case LANG_UKRAINIAN: l = "uk"; break;
759     case LANG_URDU:
760       l = "ur";
761       switch (sub)
762         {
763         case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
764         case SUBLANG_URDU_INDIA: sl = "IN"; break;
765         }
766       break;
767     case LANG_UZBEK:
768       l = "uz";
769       switch (sub)
770         {
771         case SUBLANG_UZBEK_LATIN: sl = "2latin"; break;
772         case SUBLANG_UZBEK_CYRILLIC: sl = "@cyrillic"; break;
773         }
774       break;
775     case LANG_VIETNAMESE: l = "vi"; break;
776     default: l = "xx"; break;
777     }
778   strcpy (bfr, l);
779   if (sl != NULL)
780     {
781       strcat (bfr, "_");
782       strcat (bfr, sl);
783     }
784
785   return g_strdup (bfr);
786 }