gparam.h gvalue.h gparam.c Shut up Sun's picky compiler.
[platform/upstream/glib.git] / 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     }
105   mask = g_strdup_printf ("%s\\*", result->dir_name);
106
107   result->find_file_handle = (guint) FindFirstFile (mask,
108                                              (LPWIN32_FIND_DATA) result->find_file_data);
109   g_free (mask);
110
111   if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
112     {
113       int error = GetLastError ();
114
115       g_free (result->dir_name);
116       g_free (result->find_file_data);
117       g_free (result);
118       switch (error)
119         {
120         default:
121           errno = EIO;
122           return NULL;
123         }
124     }
125   result->just_opened = TRUE;
126
127   return result;
128 }
129
130 struct dirent*
131 g_win32_readdir (DIR *dir)
132 {
133   static struct dirent result;
134
135   g_return_val_if_fail (dir != NULL, NULL);
136
137   if (dir->just_opened)
138     dir->just_opened = FALSE;
139   else
140     {
141       if (!FindNextFile ((HANDLE) dir->find_file_handle,
142                          (LPWIN32_FIND_DATA) dir->find_file_data))
143         {
144           int error = GetLastError ();
145
146           switch (error)
147             {
148             case ERROR_NO_MORE_FILES:
149               return NULL;
150             default:
151               errno = EIO;
152               return NULL;
153             }
154         }
155     }
156   strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
157       
158   return &result;
159 }
160
161 void
162 g_win32_rewinddir (DIR *dir)
163 {
164   gchar *mask;
165
166   g_return_if_fail (dir != NULL);
167
168   if (!FindClose ((HANDLE) dir->find_file_handle))
169     g_warning ("gwin_rewinddir(): FindClose() failed\n");
170
171   mask = g_strdup_printf ("%s\\*", dir->dir_name);
172   dir->find_file_handle = (guint) FindFirstFile (mask,
173                                           (LPWIN32_FIND_DATA) dir->find_file_data);
174   g_free (mask);
175
176   if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
177     {
178       int error = GetLastError ();
179
180       switch (error)
181         {
182         default:
183           errno = EIO;
184           return;
185         }
186     }
187   dir->just_opened = TRUE;
188 }  
189
190 gint
191 g_win32_closedir (DIR *dir)
192 {
193   g_return_val_if_fail (dir != NULL, -1);
194
195   if (!FindClose ((HANDLE) dir->find_file_handle))
196     {
197       int error = GetLastError ();
198
199       switch (error)
200         {
201         default:
202           errno = EIO; return -1;
203         }
204     }
205
206   g_free (dir->dir_name);
207   g_free (dir->find_file_data);
208   g_free (dir);
209
210   return 0;
211 }
212
213 /* Mingw32 headers don't have latest language and sublanguage codes */
214 #ifndef LANG_AFRIKAANS
215 #define LANG_AFRIKAANS 0x36
216 #endif
217 #ifndef LANG_ALBANIAN
218 #define LANG_ALBANIAN 0x1c
219 #endif
220 #ifndef LANG_ARABIC
221 #define LANG_ARABIC 0x01
222 #endif
223 #ifndef LANG_ARMENIAN
224 #define LANG_ARMENIAN 0x2b
225 #endif
226 #ifndef LANG_ASSAMESE
227 #define LANG_ASSAMESE 0x4d
228 #endif
229 #ifndef LANG_AZERI
230 #define LANG_AZERI 0x2c
231 #endif
232 #ifndef LANG_BASQUE
233 #define LANG_BASQUE 0x2d
234 #endif
235 #ifndef LANG_BELARUSIAN
236 #define LANG_BELARUSIAN 0x23
237 #endif
238 #ifndef LANG_BENGALI
239 #define LANG_BENGALI 0x45
240 #endif
241 #ifndef LANG_CATALAN
242 #define LANG_CATALAN 0x03
243 #endif
244 #ifndef LANG_ESTONIAN
245 #define LANG_ESTONIAN 0x25
246 #endif
247 #ifndef LANG_FAEROESE
248 #define LANG_FAEROESE 0x38
249 #endif
250 #ifndef LANG_FARSI
251 #define LANG_FARSI 0x29
252 #endif
253 #ifndef LANG_GEORGIAN
254 #define LANG_GEORGIAN 0x37
255 #endif
256 #ifndef LANG_GUJARATI
257 #define LANG_GUJARATI 0x47
258 #endif
259 #ifndef LANG_HEBREW
260 #define LANG_HEBREW 0x0d
261 #endif
262 #ifndef LANG_HINDI
263 #define LANG_HINDI 0x39
264 #endif
265 #ifndef LANG_INDONESIAN
266 #define LANG_INDONESIAN 0x21
267 #endif
268 #ifndef LANG_KANNADA
269 #define LANG_KANNADA 0x4b
270 #endif
271 #ifndef LANG_KASHMIRI
272 #define LANG_KASHMIRI 0x60
273 #endif
274 #ifndef LANG_KAZAK
275 #define LANG_KAZAK 0x3f
276 #endif
277 #ifndef LANG_KONKANI
278 #define LANG_KONKANI 0x57
279 #endif
280 #ifndef LANG_LATVIAN
281 #define LANG_LATVIAN 0x26
282 #endif
283 #ifndef LANG_LITHUANIAN
284 #define LANG_LITHUANIAN 0x27
285 #endif
286 #ifndef LANG_MACEDONIAN
287 #define LANG_MACEDONIAN 0x2f
288 #endif
289 #ifndef LANG_MALAY
290 #define LANG_MALAY 0x3e
291 #endif
292 #ifndef LANG_MALAYALAM
293 #define LANG_MALAYALAM 0x4c
294 #endif
295 #ifndef LANG_MANIPURI
296 #define LANG_MANIPURI 0x58
297 #endif
298 #ifndef LANG_MARATHI
299 #define LANG_MARATHI 0x4e
300 #endif
301 #ifndef LANG_NEPALI
302 #define LANG_NEPALI 0x61
303 #endif
304 #ifndef LANG_ORIYA
305 #define LANG_ORIYA 0x48
306 #endif
307 #ifndef LANG_PUNJABI
308 #define LANG_PUNJABI 0x46
309 #endif
310 #ifndef LANG_SANSKRIT
311 #define LANG_SANSKRIT 0x4f
312 #endif
313 #ifndef LANG_SERBIAN
314 #define LANG_SERBIAN 0x1a
315 #endif
316 #ifndef LANG_SINDHI
317 #define LANG_SINDHI 0x59
318 #endif
319 #ifndef LANG_SLOVAK
320 #define LANG_SLOVAK 0x1b
321 #endif
322 #ifndef LANG_SWAHILI
323 #define LANG_SWAHILI 0x41
324 #endif
325 #ifndef LANG_TAMIL
326 #define LANG_TAMIL 0x49
327 #endif
328 #ifndef LANG_TATAR
329 #define LANG_TATAR 0x44
330 #endif
331 #ifndef LANG_TELUGU
332 #define LANG_TELUGU 0x4a
333 #endif
334 #ifndef LANG_THAI
335 #define LANG_THAI 0x1e
336 #endif
337 #ifndef LANG_UKRAINIAN
338 #define LANG_UKRAINIAN 0x22
339 #endif
340 #ifndef LANG_URDU
341 #define LANG_URDU 0x20
342 #endif
343 #ifndef LANG_UZBEK
344 #define LANG_UZBEK 0x43
345 #endif
346 #ifndef LANG_VIETNAMESE
347 #define LANG_VIETNAMESE 0x2a
348 #endif
349 #ifndef SUBLANG_ARABIC_SAUDI_ARABIA
350 #define SUBLANG_ARABIC_SAUDI_ARABIA 0x01
351 #endif
352 #ifndef SUBLANG_ARABIC_IRAQ
353 #define SUBLANG_ARABIC_IRAQ 0x02
354 #endif
355 #ifndef SUBLANG_ARABIC_EGYPT
356 #define SUBLANG_ARABIC_EGYPT 0x03
357 #endif
358 #ifndef SUBLANG_ARABIC_LIBYA
359 #define SUBLANG_ARABIC_LIBYA 0x04
360 #endif
361 #ifndef SUBLANG_ARABIC_ALGERIA
362 #define SUBLANG_ARABIC_ALGERIA 0x05
363 #endif
364 #ifndef SUBLANG_ARABIC_MOROCCO
365 #define SUBLANG_ARABIC_MOROCCO 0x06
366 #endif
367 #ifndef SUBLANG_ARABIC_TUNISIA
368 #define SUBLANG_ARABIC_TUNISIA 0x07
369 #endif
370 #ifndef SUBLANG_ARABIC_OMAN
371 #define SUBLANG_ARABIC_OMAN 0x08
372 #endif
373 #ifndef SUBLANG_ARABIC_YEMEN
374 #define SUBLANG_ARABIC_YEMEN 0x09
375 #endif
376 #ifndef SUBLANG_ARABIC_SYRIA
377 #define SUBLANG_ARABIC_SYRIA 0x0a
378 #endif
379 #ifndef SUBLANG_ARABIC_JORDAN
380 #define SUBLANG_ARABIC_JORDAN 0x0b
381 #endif
382 #ifndef SUBLANG_ARABIC_LEBANON
383 #define SUBLANG_ARABIC_LEBANON 0x0c
384 #endif
385 #ifndef SUBLANG_ARABIC_KUWAIT
386 #define SUBLANG_ARABIC_KUWAIT 0x0d
387 #endif
388 #ifndef SUBLANG_ARABIC_UAE
389 #define SUBLANG_ARABIC_UAE 0x0e
390 #endif
391 #ifndef SUBLANG_ARABIC_BAHRAIN
392 #define SUBLANG_ARABIC_BAHRAIN 0x0f
393 #endif
394 #ifndef SUBLANG_ARABIC_QATAR
395 #define SUBLANG_ARABIC_QATAR 0x10
396 #endif
397 #ifndef SUBLANG_AZERI_LATIN
398 #define SUBLANG_AZERI_LATIN 0x01
399 #endif
400 #ifndef SUBLANG_AZERI_CYRILLIC
401 #define SUBLANG_AZERI_CYRILLIC 0x02
402 #endif
403 #ifndef SUBLANG_CHINESE_MACAU
404 #define SUBLANG_CHINESE_MACAU 0x05
405 #endif
406 #ifndef SUBLANG_ENGLISH_SOUTH_AFRICA
407 #define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07
408 #endif
409 #ifndef SUBLANG_ENGLISH_JAMAICA
410 #define SUBLANG_ENGLISH_JAMAICA 0x08
411 #endif
412 #ifndef SUBLANG_ENGLISH_CARIBBEAN
413 #define SUBLANG_ENGLISH_CARIBBEAN 0x09
414 #endif
415 #ifndef SUBLANG_ENGLISH_BELIZE
416 #define SUBLANG_ENGLISH_BELIZE 0x0a
417 #endif
418 #ifndef SUBLANG_ENGLISH_TRINIDAD
419 #define SUBLANG_ENGLISH_TRINIDAD 0x0b
420 #endif
421 #ifndef SUBLANG_ENGLISH_ZIMBABWE
422 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
423 #endif
424 #ifndef SUBLANG_ENGLISH_PHILIPPINES
425 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
426 #endif
427 #ifndef SUBLANG_FRENCH_LUXEMBOURG
428 #define SUBLANG_FRENCH_LUXEMBOURG 0x05
429 #endif
430 #ifndef SUBLANG_FRENCH_MONACO
431 #define SUBLANG_FRENCH_MONACO 0x06
432 #endif
433 #ifndef SUBLANG_GERMAN_LUXEMBOURG
434 #define SUBLANG_GERMAN_LUXEMBOURG 0x04
435 #endif
436 #ifndef SUBLANG_GERMAN_LIECHTENSTEIN
437 #define SUBLANG_GERMAN_LIECHTENSTEIN 0x05
438 #endif
439 #ifndef SUBLANG_KASHMIRI_INDIA
440 #define SUBLANG_KASHMIRI_INDIA 0x02
441 #endif
442 #ifndef SUBLANG_MALAY_MALAYSIA
443 #define SUBLANG_MALAY_MALAYSIA 0x01
444 #endif
445 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
446 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
447 #endif
448 #ifndef SUBLANG_NEPALI_INDIA
449 #define SUBLANG_NEPALI_INDIA 0x02
450 #endif
451 #ifndef SUBLANG_SERBIAN_LATIN
452 #define SUBLANG_SERBIAN_LATIN 0x02
453 #endif
454 #ifndef SUBLANG_SERBIAN_CYRILLIC
455 #define SUBLANG_SERBIAN_CYRILLIC 0x03
456 #endif
457 #ifndef SUBLANG_SPANISH_GUATEMALA
458 #define SUBLANG_SPANISH_GUATEMALA 0x04
459 #endif
460 #ifndef SUBLANG_SPANISH_COSTA_RICA
461 #define SUBLANG_SPANISH_COSTA_RICA 0x05
462 #endif
463 #ifndef SUBLANG_SPANISH_PANAMA
464 #define SUBLANG_SPANISH_PANAMA 0x06
465 #endif
466 #ifndef SUBLANG_SPANISH_DOMINICAN_REPUBLIC
467 #define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07
468 #endif
469 #ifndef SUBLANG_SPANISH_VENEZUELA
470 #define SUBLANG_SPANISH_VENEZUELA 0x08
471 #endif
472 #ifndef SUBLANG_SPANISH_COLOMBIA
473 #define SUBLANG_SPANISH_COLOMBIA 0x09
474 #endif
475 #ifndef SUBLANG_SPANISH_PERU
476 #define SUBLANG_SPANISH_PERU 0x0a
477 #endif
478 #ifndef SUBLANG_SPANISH_ARGENTINA
479 #define SUBLANG_SPANISH_ARGENTINA 0x0b
480 #endif
481 #ifndef SUBLANG_SPANISH_ECUADOR
482 #define SUBLANG_SPANISH_ECUADOR 0x0c
483 #endif
484 #ifndef SUBLANG_SPANISH_CHILE
485 #define SUBLANG_SPANISH_CHILE 0x0d
486 #endif
487 #ifndef SUBLANG_SPANISH_URUGUAY
488 #define SUBLANG_SPANISH_URUGUAY 0x0e
489 #endif
490 #ifndef SUBLANG_SPANISH_PARAGUAY
491 #define SUBLANG_SPANISH_PARAGUAY 0x0f
492 #endif
493 #ifndef SUBLANG_SPANISH_BOLIVIA
494 #define SUBLANG_SPANISH_BOLIVIA 0x10
495 #endif
496 #ifndef SUBLANG_SPANISH_EL_SALVADOR
497 #define SUBLANG_SPANISH_EL_SALVADOR 0x11
498 #endif
499 #ifndef SUBLANG_SPANISH_HONDURAS
500 #define SUBLANG_SPANISH_HONDURAS 0x12
501 #endif
502 #ifndef SUBLANG_SPANISH_NICARAGUA
503 #define SUBLANG_SPANISH_NICARAGUA 0x13
504 #endif
505 #ifndef SUBLANG_SPANISH_PUERTO_RICO
506 #define SUBLANG_SPANISH_PUERTO_RICO 0x14
507 #endif
508 #ifndef SUBLANG_SWEDISH_FINLAND
509 #define SUBLANG_SWEDISH_FINLAND 0x02
510 #endif
511 #ifndef SUBLANG_URDU_PAKISTAN
512 #define SUBLANG_URDU_PAKISTAN 0x01
513 #endif
514 #ifndef SUBLANG_URDU_INDIA
515 #define SUBLANG_URDU_INDIA 0x02
516 #endif
517 #ifndef SUBLANG_UZBEK_LATIN
518 #define SUBLANG_UZBEK_LATIN 0x01
519 #endif
520 #ifndef SUBLANG_UZBEK_CYRILLIC
521 #define SUBLANG_UZBEK_CYRILLIC 0x02
522 #endif
523
524 gchar *
525 g_win32_getlocale (void)
526 {
527   LCID lcid;
528   gchar *ev;
529   gint primary, sub;
530   gchar *l = NULL, *sl = NULL;
531   gchar bfr[20];
532
533   if ((ev = getenv ("LC_ALL")) != NULL
534       || (ev = getenv ("LC_CTYPE")) != NULL
535       || (ev = getenv ("LANG")) != NULL)
536     return g_strdup (ev);
537
538   lcid = GetThreadLocale ();
539   primary = PRIMARYLANGID (LANGIDFROMLCID (lcid));
540   sub = SUBLANGID (LANGIDFROMLCID (lcid));
541   switch (primary)
542     {
543     case LANG_AFRIKAANS: l = "af"; break;
544     case LANG_ALBANIAN: l = "sq"; break;
545     case LANG_ARABIC:
546       l = "ar";
547       switch (sub)
548         {
549         case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
550         case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
551         case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
552         case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
553         case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
554         case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
555         case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
556         case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
557         case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
558         case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
559         case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
560         case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
561         case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
562         case SUBLANG_ARABIC_UAE: sl = "AE"; break;
563         case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
564         case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
565         }
566       break;
567     case LANG_ARMENIAN: l = "hy"; break;
568     case LANG_ASSAMESE: l = "as"; break;
569     case LANG_AZERI: l = "az"; break;
570     case LANG_BASQUE: l = "eu"; break;
571     case LANG_BELARUSIAN: l = "be"; break;
572     case LANG_BENGALI: l = "bn"; break;
573     case LANG_BULGARIAN: l = "bg"; break;
574     case LANG_CATALAN: l = "ca"; break;
575     case LANG_CHINESE:
576       l = "zh";
577       switch (sub)
578         {
579         case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
580         case SUBLANG_CHINESE_SIMPLIFIED: sl = "CH"; break;
581         case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
582         case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
583         case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
584         }
585       break;
586     case LANG_CROATIAN:         /* LANG_CROATIAN == LANG_SERBIAN
587                                  * What used to be called Serbo-Croatian
588                                  * should really now be two separate
589                                  * languages because of political reasons.
590                                  * (Says tml, who knows nothing about Serbian
591                                  * or Croatian.)
592                                  * (I can feel those flames coming already.)
593                                  */
594       switch (sub)
595         {
596         case SUBLANG_SERBIAN_LATIN: l = "hr"; break;
597         case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
598         default: l = "hr";      /* ??? */
599         }
600       break;
601     case LANG_CZECH: l = "cs"; break;
602     case LANG_DANISH: l = "da"; break;
603     case LANG_DUTCH:
604       l = "nl";
605       switch (sub)
606         {
607         case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
608         }
609       break;
610     case LANG_ENGLISH:
611       l = "en";
612       switch (sub)
613         {
614         /* SUBLANG_ENGLISH_US == SUBLANG_DEFAULT. Heh. I thought
615          * English was the language spoken in England.
616          * Oh well.
617          */
618         case SUBLANG_ENGLISH_UK: sl = "GB"; break;
619         case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
620         case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
621         case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
622         case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
623         case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "SA"; break;
624         case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
625         case SUBLANG_ENGLISH_CARIBBEAN: sl = "@caribbean"; break; /* ??? */
626         case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
627         case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
628         case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
629         case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
630         }
631       break;
632     case LANG_ESTONIAN: l = "et"; break;
633     case LANG_FAEROESE: l = "fo"; break;
634     case LANG_FARSI: l = "fa"; break;
635     case LANG_FINNISH: l = "fi"; break;
636     case LANG_FRENCH:
637       l = "fr";
638       switch (sub)
639         {
640         case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
641         case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
642         case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
643         case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
644         case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
645         }
646       break;
647     case LANG_GEORGIAN: l = "ka"; break;
648     case LANG_GERMAN:
649       l = "de";
650       switch (sub)
651         {
652         case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
653         case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
654         case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
655         case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
656         }
657       break;
658     case LANG_GREEK: l = "el"; break;
659     case LANG_GUJARATI: l = "gu"; break;
660     case LANG_HEBREW: l = "he"; break;
661     case LANG_HINDI: l = "hi"; break;
662     case LANG_HUNGARIAN: l = "hu"; break;
663     case LANG_ICELANDIC: l = "is"; break;
664     case LANG_INDONESIAN: l = "id"; break;
665     case LANG_ITALIAN:
666       l = "it";
667       switch (sub)
668         {
669         case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
670         }
671       break;
672     case LANG_JAPANESE: l = "ja"; break;
673     case LANG_KANNADA: l = "kn"; break;
674     case LANG_KASHMIRI:
675       l = "ks";
676       switch (sub)
677         {
678         case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
679         }
680       break;
681     case LANG_KAZAK: l = "kk"; break;
682     case LANG_KONKANI: l = "kok"; break; /* ??? */
683     case LANG_KOREAN: l = "ko"; break;
684     case LANG_LATVIAN: l = "lv"; break;
685     case LANG_LITHUANIAN: l = "lt"; break;
686     case LANG_MACEDONIAN: l = "mk"; break;
687     case LANG_MALAY:
688       l = "ms";
689       switch (sub)
690         {
691         case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
692         }
693       break;
694     case LANG_MALAYALAM: l = "ml"; break;
695     case LANG_MANIPURI: l = "mni"; break;
696     case LANG_MARATHI: l = "mr"; break;
697     case LANG_NEPALI:
698       l = "ne";
699       switch (sub)
700         {
701         case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
702         }
703       break;
704     case LANG_NORWEGIAN:
705       l = "no";
706       switch (sub)
707         {
708         /* SUBLANG_NORWEGIAN_BOKMAL == SUBLANG_DEFAULT */
709         case SUBLANG_NORWEGIAN_NYNORSK: sl = "@nynorsk"; break;
710         }
711       break;
712     case LANG_ORIYA: l = "or"; break;
713     case LANG_POLISH: l = "pl"; break;
714     case LANG_PORTUGUESE:
715       l = "pt";
716       switch (sub)
717         {
718         /* Hmm. SUBLANG_PORTUGUESE_BRAZILIAN == SUBLANG_DEFAULT */
719         case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
720         }
721       break;
722     case LANG_PUNJABI: l = "pa"; break;
723     case LANG_ROMANIAN: l = "ro"; break;
724     case LANG_RUSSIAN: l = "ru"; break;
725     case LANG_SANSKRIT: l = "sa"; break;
726     case LANG_SINDHI: l = "sd"; break;
727     case LANG_SLOVAK: l = "sk"; break;
728     case LANG_SLOVENIAN: l = "sl"; break;
729     case LANG_SPANISH:
730       l = "es";
731       switch (sub)
732         {
733         case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
734         case SUBLANG_SPANISH_MODERN: sl = "@modern"; break;     /* ??? */
735         case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
736         case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
737         case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
738         case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
739         case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
740         case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
741         case SUBLANG_SPANISH_PERU: sl = "PE"; break;
742         case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
743         case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
744         case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
745         case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
746         case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
747         case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
748         case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
749         case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
750         case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
751         case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
752         }
753       break;
754     case LANG_SWAHILI: l = "sw"; break;
755     case LANG_SWEDISH:
756       l = "sv";
757       switch (sub)
758         {
759         case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
760         }
761       break;
762     case LANG_TAMIL: l = "ta"; break;
763     case LANG_TATAR: l = "tt"; break;
764     case LANG_TELUGU: l = "te"; break;
765     case LANG_THAI: l = "th"; break;
766     case LANG_TURKISH: l = "tr"; break;
767     case LANG_UKRAINIAN: l = "uk"; break;
768     case LANG_URDU:
769       l = "ur";
770       switch (sub)
771         {
772         case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
773         case SUBLANG_URDU_INDIA: sl = "IN"; break;
774         }
775       break;
776     case LANG_UZBEK:
777       l = "uz";
778       switch (sub)
779         {
780         case SUBLANG_UZBEK_CYRILLIC: sl = "@cyrillic"; break;
781         }
782       break;
783     case LANG_VIETNAMESE: l = "vi"; break;
784     default: l = "xx"; break;
785     }
786   strcpy (bfr, l);
787   if (sl != NULL)
788     {
789       strcat (bfr, "_");
790       strcat (bfr, sl);
791     }
792
793   return g_strdup (bfr);
794 }
795
796 gchar *
797 g_win32_error_message (gint error)
798 {
799   gchar *msg;
800   gchar *retval;
801   int nbytes;
802
803   nbytes = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
804                           |FORMAT_MESSAGE_IGNORE_INSERTS
805                           |FORMAT_MESSAGE_FROM_SYSTEM,
806                           NULL, error, 0,
807                           (LPTSTR) &msg, 0, NULL);
808   if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
809     msg[nbytes-2] = '\0';
810   
811   retval = g_strdup (msg);
812
813   if (msg != NULL)
814     LocalFree (msg);
815
816   return retval;
817 }