When the sublangid is SUBLANG_DEFAULT, return the locale of the language's
[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 Lesser 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser 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-2000.  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 #undef STRICT
46 #ifndef G_WITH_CYGWIN
47 #include <direct.h>
48 #endif
49 #include <errno.h>
50 #include <ctype.h>
51 #ifdef _MSC_VER
52 #  include <io.h>
53 #endif /* _MSC_VER */
54
55 #include "glib.h"
56
57 #ifdef G_WITH_CYGWIN
58 #include <sys/cygwin.h>
59 #endif
60
61 #ifndef G_WITH_CYGWIN
62
63 gint
64 g_win32_ftruncate (gint  fd,
65                    guint size)
66 {
67   HANDLE hfile;
68   guint curpos;
69
70   g_return_val_if_fail (fd >= 0, -1);
71   
72   hfile = (HANDLE) _get_osfhandle (fd);
73   curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
74   if (curpos == 0xFFFFFFFF
75       || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
76       || !SetEndOfFile (hfile))
77     {
78       gint error = GetLastError ();
79
80       switch (error)
81         {
82         case ERROR_INVALID_HANDLE:
83           errno = EBADF;
84           break;
85         default:
86           errno = EIO;
87           break;
88         }
89
90       return -1;
91     }
92
93   return 0;
94 }
95
96 DIR*
97 g_win32_opendir (const char *dirname)
98 {
99   DIR *result;
100   gchar *mask;
101   guint k;
102
103   g_return_val_if_fail (dirname != NULL, NULL);
104
105   result = g_new0 (DIR, 1);
106   result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
107   result->dir_name = g_strdup (dirname);
108   
109   k = strlen (result->dir_name);
110   if (k && result->dir_name[k - 1] == '\\')
111     {
112       result->dir_name[k - 1] = '\0';
113     }
114   mask = g_strdup_printf ("%s\\*", result->dir_name);
115
116   result->find_file_handle = (guint) FindFirstFile (mask,
117                                              (LPWIN32_FIND_DATA) result->find_file_data);
118   g_free (mask);
119
120   if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
121     {
122       int error = GetLastError ();
123
124       g_free (result->dir_name);
125       g_free (result->find_file_data);
126       g_free (result);
127       switch (error)
128         {
129         default:
130           errno = EIO;
131           return NULL;
132         }
133     }
134   result->just_opened = TRUE;
135
136   return result;
137 }
138
139 struct dirent*
140 g_win32_readdir (DIR *dir)
141 {
142   gchar *basename;
143
144   g_return_val_if_fail (dir != NULL, NULL);
145
146   if (dir->just_opened)
147     dir->just_opened = FALSE;
148   else
149     {
150       if (!FindNextFile ((HANDLE) dir->find_file_handle,
151                          (LPWIN32_FIND_DATA) dir->find_file_data))
152         {
153           int error = GetLastError ();
154
155           switch (error)
156             {
157             case ERROR_NO_MORE_FILES:
158               return NULL;
159             default:
160               errno = EIO;
161               return NULL;
162             }
163         }
164     }
165   
166   basename = g_path_get_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName);
167
168   strcpy (dir->readdir_result.d_name, basename);
169
170   g_free (basename);
171       
172   return &dir->readdir_result;
173 }
174
175 void
176 g_win32_rewinddir (DIR *dir)
177 {
178   gchar *mask;
179
180   g_return_if_fail (dir != NULL);
181
182   if (!FindClose ((HANDLE) dir->find_file_handle))
183     g_warning ("gwin_rewinddir(): FindClose() failed\n");
184
185   mask = g_strdup_printf ("%s\\*", dir->dir_name);
186   dir->find_file_handle = (guint) FindFirstFile (mask,
187                                           (LPWIN32_FIND_DATA) dir->find_file_data);
188   g_free (mask);
189
190   if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
191     {
192       int error = GetLastError ();
193
194       switch (error)
195         {
196         default:
197           errno = EIO;
198           return;
199         }
200     }
201   dir->just_opened = TRUE;
202 }  
203
204 gint
205 g_win32_closedir (DIR *dir)
206 {
207   g_return_val_if_fail (dir != NULL, -1);
208
209   if (!FindClose ((HANDLE) dir->find_file_handle))
210     {
211       int error = GetLastError ();
212
213       switch (error)
214         {
215         default:
216           errno = EIO; return -1;
217         }
218     }
219
220   g_free (dir->dir_name);
221   g_free (dir->find_file_data);
222   g_free (dir);
223
224   return 0;
225 }
226 #endif
227
228 /* MSVC 5.0 headers don't have latest language and sublanguage codes */
229 #ifndef LANG_ARMENIAN
230 #define LANG_ARMENIAN 0x2b
231 #endif
232 #ifndef LANG_ASSAMESE
233 #define LANG_ASSAMESE 0x4d
234 #endif
235 #ifndef LANG_AZERI
236 #define LANG_AZERI 0x2c
237 #endif
238 #ifndef LANG_BENGALI
239 #define LANG_BENGALI 0x45
240 #endif
241 #ifndef LANG_GEORGIAN
242 #define LANG_GEORGIAN 0x37
243 #endif
244 #ifndef LANG_GUJARATI
245 #define LANG_GUJARATI 0x47
246 #endif
247 #ifndef LANG_HINDI
248 #define LANG_HINDI 0x39
249 #endif
250 #ifndef LANG_KANNADA
251 #define LANG_KANNADA 0x4b
252 #endif
253 #ifndef LANG_KASHMIRI
254 #define LANG_KASHMIRI 0x60
255 #endif
256 #ifndef LANG_KAZAK
257 #define LANG_KAZAK 0x3f
258 #endif
259 #ifndef LANG_KONKANI
260 #define LANG_KONKANI 0x57
261 #endif
262 #ifndef LANG_MACEDONIAN
263 #define LANG_MACEDONIAN 0x2f
264 #endif
265 #ifndef LANG_MALAY
266 #define LANG_MALAY 0x3e
267 #endif
268 #ifndef LANG_MALAYALAM
269 #define LANG_MALAYALAM 0x4c
270 #endif
271 #ifndef LANG_MANIPURI
272 #define LANG_MANIPURI 0x58
273 #endif
274 #ifndef LANG_MARATHI
275 #define LANG_MARATHI 0x4e
276 #endif
277 #ifndef LANG_NEPALI
278 #define LANG_NEPALI 0x61
279 #endif
280 #ifndef LANG_ORIYA
281 #define LANG_ORIYA 0x48
282 #endif
283 #ifndef LANG_PUNJABI
284 #define LANG_PUNJABI 0x46
285 #endif
286 #ifndef LANG_SANSKRIT
287 #define LANG_SANSKRIT 0x4f
288 #endif
289 #ifndef LANG_SINDHI
290 #define LANG_SINDHI 0x59
291 #endif
292 #ifndef LANG_SWAHILI
293 #define LANG_SWAHILI 0x41
294 #endif
295 #ifndef LANG_TAMIL
296 #define LANG_TAMIL 0x49
297 #endif
298 #ifndef LANG_TATAR
299 #define LANG_TATAR 0x44
300 #endif
301 #ifndef LANG_TELUGU
302 #define LANG_TELUGU 0x4a
303 #endif
304 #ifndef LANG_URDU
305 #define LANG_URDU 0x20
306 #endif
307 #ifndef LANG_UZBEK
308 #define LANG_UZBEK 0x43
309 #endif
310
311 #ifndef SUBLANG_CHINESE_MACAU
312 #define SUBLANG_CHINESE_MACAU 0x05
313 #endif
314 #ifndef SUBLANG_ENGLISH_ZIMBABWE
315 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
316 #endif
317 #ifndef SUBLANG_ENGLISH_PHILIPPINES
318 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
319 #endif
320 #ifndef SUBLANG_FRENCH_MONACO
321 #define SUBLANG_FRENCH_MONACO 0x06
322 #endif
323 #ifndef SUBLANG_KASHMIRI_INDIA
324 #define SUBLANG_KASHMIRI_INDIA 0x02
325 #endif
326 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
327 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
328 #endif
329 #ifndef SUBLANG_NEPALI_INDIA
330 #define SUBLANG_NEPALI_INDIA 0x02
331 #endif
332 #ifndef SUBLANG_URDU_PAKISTAN
333 #define SUBLANG_URDU_PAKISTAN 0x01
334 #endif
335 #ifndef SUBLANG_URDU_INDIA
336 #define SUBLANG_URDU_INDIA 0x02
337 #endif
338 #ifndef SUBLANG_UZBEK_CYRILLIC
339 #define SUBLANG_UZBEK_CYRILLIC 0x02
340 #endif
341
342 /**
343  * g_win32_getlocale:
344  *
345  * The setlocale in the Microsoft C library uses locale names of the
346  * form "English_United States.1252" etc. We want the Unixish standard
347  * form "en_US", "zh_TW" etc. This function gets the current thread
348  * locale from Windows - without any encoding info - and returns it as
349  * a string of the above form for use in forming file names etc. The
350  * returned string should be deallocated with g_free().
351  *
352  * Returns: allocated locale name
353  */
354
355 gchar *
356 g_win32_getlocale (void)
357 {
358   LCID lcid;
359   LANGID langid;
360   gchar *ev;
361   gint primary, sub;
362   gchar *l = NULL, *sl = NULL;
363   gchar bfr[20];
364
365   /* Let the user override the system settings through environment
366      variables, as on POSIX systems.  */
367   if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
368       || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
369       || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
370     return g_strdup (ev);
371
372   /* Use native Win32 API locale ID.  */
373   lcid = GetThreadLocale ();
374
375   /* Strip off the sorting rules, keep only the language part.  */
376   langid = LANGIDFROMLCID (lcid);
377
378   /* Split into language and territory part.  */
379   primary = PRIMARYLANGID (langid);
380   sub = SUBLANGID (langid);
381   switch (primary)
382     {
383     case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
384     case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
385     case LANG_ARABIC:
386       l = "ar";
387       switch (sub)
388         {
389         case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
390         case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
391         case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
392         case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
393         case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
394         case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
395         case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
396         case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
397         case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
398         case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
399         case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
400         case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
401         case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
402         case SUBLANG_ARABIC_UAE: sl = "AE"; break;
403         case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
404         case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
405         }
406       break;
407     case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
408     case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
409     case LANG_AZERI:
410       l = "az";
411       switch (sub)
412         {
413         /* FIXME: Adjust this when Azerbaijani locales appear on Unix.  */
414         case SUBLANG_AZERI_LATIN: sl = "@latin"; break;
415         case SUBLANG_AZERI_CYRILLIC: sl = "@cyrillic"; break;
416         }
417       break;
418     case LANG_BASQUE:
419       l = "eu"; /* sl could be "ES" or "FR".  */
420       break;
421     case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
422     case LANG_BENGALI: l = "bn"; sl = "IN"; break;
423     case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
424     case LANG_CATALAN: l = "ca"; sl = "ES"; break;
425     case LANG_CHINESE:
426       l = "zh";
427       switch (sub)
428         {
429         case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
430         case SUBLANG_CHINESE_SIMPLIFIED: sl = "CN"; break;
431         case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
432         case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
433         case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
434         }
435       break;
436     case LANG_CROATIAN:         /* LANG_CROATIAN == LANG_SERBIAN
437                                  * What used to be called Serbo-Croatian
438                                  * should really now be two separate
439                                  * languages because of political reasons.
440                                  * (Says tml, who knows nothing about Serbian
441                                  * or Croatian.)
442                                  * (I can feel those flames coming already.)
443                                  */
444       switch (sub)
445         {
446         /* FIXME: How to distinguish Croatian and Latin Serbian locales?  */
447         case SUBLANG_SERBIAN_LATIN: l = "sr"; break;
448         case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; sl = "@cyrillic"; break;
449         default: l = "hr"; sl = "HR";
450         }
451       break;
452     case LANG_CZECH: l = "cs"; sl = "CZ"; break;
453     case LANG_DANISH: l = "da"; sl = "DK"; break;
454     case LANG_DUTCH:
455       l = "nl";
456       switch (sub)
457         {
458         case SUBLANG_DUTCH: sl = "NL"; break;
459         case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
460         }
461       break;
462     case LANG_ENGLISH:
463       l = "en";
464       switch (sub)
465         {
466         case SUBLANG_ENGLISH_US: sl = "US"; break;
467         case SUBLANG_ENGLISH_UK: sl = "GB"; break;
468         case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
469         case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
470         case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
471         case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
472         case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
473         case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
474         case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
475         case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
476         case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
477         case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
478         case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
479         }
480       break;
481     case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
482     case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
483     case LANG_FARSI: l = "fa"; sl = "IR"; break;
484     case LANG_FINNISH: l = "fi"; sl = "FI"; break;
485     case LANG_FRENCH:
486       l = "fr";
487       switch (sub)
488         {
489         case SUBLANG_FRENCH: sl = "FR"; break;
490         case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
491         case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
492         case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
493         case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
494         case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
495         }
496       break;
497     case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
498     case LANG_GERMAN:
499       l = "de";
500       switch (sub)
501         {
502         case SUBLANG_GERMAN: sl = "DE"; break;
503         case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
504         case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
505         case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
506         case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
507         }
508       break;
509     case LANG_GREEK: l = "el"; sl = "GR"; break;
510     case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
511     case LANG_HEBREW: l = "he"; sl = "IL"; break;
512     case LANG_HINDI: l = "hi"; sl = "IN"; break;
513     case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
514     case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
515     case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
516     case LANG_ITALIAN:
517       l = "it";
518       switch (sub)
519         {
520         case SUBLANG_ITALIAN: sl = "IT"; break;
521         case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
522         }
523       break;
524     case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
525     case LANG_KANNADA: l = "kn"; sl = "IN"; break;
526     case LANG_KASHMIRI:
527       l = "ks";
528       switch (sub)
529         {
530         case SUBLANG_DEFAULT: sl = "PK"; break;
531         case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
532         }
533       break;
534     case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
535     case LANG_KONKANI:
536       /* FIXME: Adjust this when such locales appear on Unix.  */
537       l = "kok"; sl = "IN";
538       break;
539     case LANG_KOREAN: l = "ko"; sl = "KR"; break;
540     case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
541     case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
542     case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
543     case LANG_MALAY:
544       l = "ms";
545       switch (sub)
546         {
547         case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
548         case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
549         }
550       break;
551     case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
552     case LANG_MANIPURI:
553       /* FIXME: Adjust this when such locales appear on Unix.  */
554       l = "mni"; sl = "IN";
555       break;
556     case LANG_MARATHI: l = "mr"; sl = "IN"; break;
557     case LANG_NEPALI:
558       l = "ne";
559       switch (sub)
560         {
561         case SUBLANG_DEFAULT: sl = "NP"; break;
562         case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
563         }
564       break;
565     case LANG_NORWEGIAN:
566       l = "no";
567       switch (sub)
568         {
569         case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
570         case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
571         }
572       break;
573     case LANG_ORIYA: l = "or"; sl = "IN"; break;
574     case LANG_POLISH: l = "pl"; sl = "PL"; break;
575     case LANG_PORTUGUESE:
576       l = "pt";
577       switch (sub)
578         {
579         case SUBLANG_PORTUGUESE: sl = "PT"; break;
580         case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
581         }
582       break;
583     case LANG_PUNJABI: l = "pa"; sl = "IN"; break;
584     case LANG_ROMANIAN: l = "ro"; sl = "RO"; break;
585     case LANG_RUSSIAN:
586       l = "ru"; /* sl could be "RU" or "UA".  */
587       break;
588     case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
589     case LANG_SINDHI: l = "sd"; break;
590     case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
591     case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
592 #ifdef LANG_SORBIAN
593     case LANG_SORBIAN:
594       /* FIXME: Adjust this when such locales appear on Unix.  */
595       l = "wen"; sl = "DE";
596       break;
597 #endif
598     case LANG_SPANISH:
599       l = "es";
600       switch (sub)
601         {
602         case SUBLANG_SPANISH: sl = "ES"; break;
603         case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
604         case SUBLANG_SPANISH_MODERN:
605           sl = "ES@modern"; break;      /* not seen on Unix */
606         case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
607         case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
608         case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
609         case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
610         case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
611         case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
612         case SUBLANG_SPANISH_PERU: sl = "PE"; break;
613         case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
614         case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
615         case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
616         case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
617         case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
618         case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
619         case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
620         case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
621         case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
622         case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
623         }
624       break;
625     case LANG_SWAHILI: l = "sw"; break;
626     case LANG_SWEDISH:
627       l = "sv";
628       switch (sub)
629         {
630         case SUBLANG_DEFAULT: sl = "SE"; break;
631         case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
632         }
633       break;
634     case LANG_TAMIL:
635       l = "ta"; /* sl could be "IN" or "LK".  */
636       break;
637     case LANG_TATAR: l = "tt"; break;
638     case LANG_TELUGU: l = "te"; sl = "IN"; break;
639     case LANG_THAI: l = "th"; sl = "TH"; break;
640     case LANG_TURKISH: l = "tr"; sl = "TR"; break;
641     case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
642     case LANG_URDU:
643       l = "ur";
644       switch (sub)
645         {
646         case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
647         case SUBLANG_URDU_INDIA: sl = "IN"; break;
648         }
649       break;
650     case LANG_UZBEK:
651       l = "uz";
652       switch (sub)
653         {
654         /* FIXME: Adjust this when Uzbek locales appear on Unix.  */
655         case SUBLANG_UZBEK_LATIN: sl = "UZ@latin"; break;
656         case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
657         }
658       break;
659     case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
660     default: l = "xx"; break;
661     }
662   strcpy (bfr, l);
663   if (sl != NULL)
664     {
665       if (sl[0] != '@')
666         strcat (bfr, "_");
667       strcat (bfr, sl);
668     }
669
670   return g_strdup (bfr);
671 }
672
673 /**
674  * g_win32_error_message:
675  * @error: error code
676  *
677  * Translate a Win32 error code (as returned by GetLastError()) into
678  * the corresponding message. The message is either language neutral,
679  * or in the thread's language, or the user's language, the system's
680  * langauge, or US English (see docs for FormatMessage). The returned
681  * string should be deallocated with g_free().
682  *
683  * Returns: allocated error message
684  */
685 gchar *
686 g_win32_error_message (gint error)
687 {
688   gchar *msg;
689   gchar *retval;
690   int nbytes;
691
692   FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
693                  |FORMAT_MESSAGE_IGNORE_INSERTS
694                  |FORMAT_MESSAGE_FROM_SYSTEM,
695                  NULL, error, 0,
696                  (LPTSTR) &msg, 0, NULL);
697   nbytes = strlen (msg);
698
699   if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
700     msg[nbytes-2] = '\0';
701   
702   retval = g_strdup (msg);
703
704   if (msg != NULL)
705     LocalFree (msg);
706
707   return retval;
708 }
709
710 static gchar *
711 get_package_directory_from_module (gchar *module_name)
712 {
713   static GHashTable *module_dirs = NULL;
714   HMODULE hmodule = NULL;
715   gchar *fn;
716   gchar *p;
717   gchar *result;
718
719   if (module_dirs == NULL)
720     module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
721   
722   result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
723       
724   if (result)
725     return g_strdup (result);
726
727   if (module_name)
728     {
729       hmodule = GetModuleHandle (module_name);
730       if (!hmodule)
731         return NULL;
732     }
733
734   fn = g_malloc (MAX_PATH);
735   if (!GetModuleFileName (hmodule, fn, MAX_PATH))
736     return NULL;
737
738 #ifdef G_WITH_CYGWIN
739   /* In Cygwin we need to have POSIX paths */
740   {
741     gchar tmp[MAX_PATH];
742
743     cygwin_conv_to_posix_path(fn, tmp);
744     g_free(fn);
745     fn = g_strdup(tmp);
746   }
747 #endif
748
749   if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
750     *p = '\0';
751
752   p = strrchr (fn, G_DIR_SEPARATOR);
753   if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
754             g_ascii_strcasecmp (p + 1, "lib") == 0))
755     *p = '\0';
756
757   g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
758
759   return g_strdup (fn);
760 }
761
762 /**
763  * g_win32_get_package_installation_directory:
764  * @package: An identifier for a software package, or NULL
765  * @dll_name: The name of a DLL that a package provides, or NULL
766  *
767  * Try to determine the installation directory for a software package.
768  * Typically used by GNU software packages.
769  *
770  * @package should be a short identifier for the package. Typically it
771  * is the same identifier as used for GETTEXT_PACKAGE in software
772  * configured accoring to GNU standards. The function first looks in
773  * the Windows Registry for the value #InstallationDirectory in the
774  * key #HKLM\Software\@package, and if that value exists and is a
775  * string, returns that.
776  *
777  * If @package is NULL, or the above value isn't found in the
778  * Registry, but @dll_name is non-NULL, it should name a DLL loaded
779  * into the current process. Typically that would be the name of the
780  * DLL calling this function, looking for its installation
781  * directory. The function then asks Windows what directory that DLL
782  * was loaded from. If that directory's last component is "bin" or
783  * "lib", the parent directory is returned, otherwise the directory
784  * itself. If that DLL isn't loaded, the function proceeds as if
785  * @dll_name was NULL.
786  *
787  * If both @package and @dll_name are NULL, the directory from where
788  * the main executable of the process was loaded is uses instead in
789  * the same way as above.
790  *
791  * The return value should be freed with g_free() when not needed any longer.  */
792
793 gchar *
794 g_win32_get_package_installation_directory (gchar *package,
795                                             gchar *dll_name)
796 {
797   static GHashTable *package_dirs = NULL;
798   gchar *result = NULL;
799   gchar *key;
800   HKEY reg_key = NULL;
801   DWORD type;
802   DWORD nbytes;
803
804   if (package != NULL)
805     {
806       if (package_dirs == NULL)
807         package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
808       
809       result = g_hash_table_lookup (package_dirs, package);
810       
811       if (result && result[0])
812         return g_strdup (result);
813       
814       key = g_strconcat ("Software\\", package, NULL);
815       
816       nbytes = 0;
817       if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
818                          KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
819            && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
820                                &type, NULL, &nbytes) == ERROR_SUCCESS)
821           ||
822           ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
823                          KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
824            && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
825                                &type, NULL, &nbytes) == ERROR_SUCCESS)
826            && type == REG_SZ))
827         {
828           result = g_malloc (nbytes + 1);
829           RegQueryValueEx (reg_key, "InstallationDirectory", 0,
830                            &type, result, &nbytes);
831           result[nbytes] = '\0';
832         }
833
834       if (reg_key != NULL)
835         RegCloseKey (reg_key);
836       
837       g_free (key);
838       
839     }
840   if (result)
841     {
842       g_hash_table_insert (package_dirs, package, result);
843       return g_strdup (result);
844     }
845
846   if (dll_name != NULL)
847     result = get_package_directory_from_module (dll_name);
848
849   if (result == NULL)
850     result = get_package_directory_from_module (NULL);
851
852   return result;
853 }
854
855 /**
856  * g_win32_get_package_installation_subdirectory:
857  * @package: An identifier for a software package, or NULL
858  * @dll_name: The name of a DLL that a package provides, or NULL
859  * @subdir: A subdirectory of the package installation directory.
860  *
861  * Returns a string containg the path of the subdirectory @subdir in
862  * the return value from calling
863  * g_win32_get_package_installation_directory() with the @package and
864  * @dll_name parameters. The return value should be freed with
865  * g_free() when no longer needed.
866  */
867
868 gchar *
869 g_win32_get_package_installation_subdirectory (gchar *package,
870                                                gchar *dll_name,
871                                                gchar *subdir)
872 {
873   gchar *prefix;
874   gchar *sep;
875
876   prefix = g_win32_get_package_installation_directory (package, dll_name);
877
878   sep = (prefix[strlen (prefix) - 1] == G_DIR_SEPARATOR ?
879          "" : G_DIR_SEPARATOR_S);
880
881   return g_strconcat (prefix, sep, subdir, NULL);
882 }