made 'make distcheck' happy.
[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 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 /* Mingw headers don't have latest language and sublanguage codes */
229 #ifndef LANG_AFRIKAANS
230 #define LANG_AFRIKAANS 0x36
231 #endif
232 #ifndef LANG_ALBANIAN
233 #define LANG_ALBANIAN 0x1c
234 #endif
235 #ifndef LANG_ARABIC
236 #define LANG_ARABIC 0x01
237 #endif
238 #ifndef LANG_ARMENIAN
239 #define LANG_ARMENIAN 0x2b
240 #endif
241 #ifndef LANG_ASSAMESE
242 #define LANG_ASSAMESE 0x4d
243 #endif
244 #ifndef LANG_AZERI
245 #define LANG_AZERI 0x2c
246 #endif
247 #ifndef LANG_BASQUE
248 #define LANG_BASQUE 0x2d
249 #endif
250 #ifndef LANG_BELARUSIAN
251 #define LANG_BELARUSIAN 0x23
252 #endif
253 #ifndef LANG_BENGALI
254 #define LANG_BENGALI 0x45
255 #endif
256 #ifndef LANG_CATALAN
257 #define LANG_CATALAN 0x03
258 #endif
259 #ifndef LANG_ESTONIAN
260 #define LANG_ESTONIAN 0x25
261 #endif
262 #ifndef LANG_FAEROESE
263 #define LANG_FAEROESE 0x38
264 #endif
265 #ifndef LANG_FARSI
266 #define LANG_FARSI 0x29
267 #endif
268 #ifndef LANG_GEORGIAN
269 #define LANG_GEORGIAN 0x37
270 #endif
271 #ifndef LANG_GUJARATI
272 #define LANG_GUJARATI 0x47
273 #endif
274 #ifndef LANG_HEBREW
275 #define LANG_HEBREW 0x0d
276 #endif
277 #ifndef LANG_HINDI
278 #define LANG_HINDI 0x39
279 #endif
280 #ifndef LANG_INDONESIAN
281 #define LANG_INDONESIAN 0x21
282 #endif
283 #ifndef LANG_KANNADA
284 #define LANG_KANNADA 0x4b
285 #endif
286 #ifndef LANG_KASHMIRI
287 #define LANG_KASHMIRI 0x60
288 #endif
289 #ifndef LANG_KAZAK
290 #define LANG_KAZAK 0x3f
291 #endif
292 #ifndef LANG_KONKANI
293 #define LANG_KONKANI 0x57
294 #endif
295 #ifndef LANG_LATVIAN
296 #define LANG_LATVIAN 0x26
297 #endif
298 #ifndef LANG_LITHUANIAN
299 #define LANG_LITHUANIAN 0x27
300 #endif
301 #ifndef LANG_MACEDONIAN
302 #define LANG_MACEDONIAN 0x2f
303 #endif
304 #ifndef LANG_MALAY
305 #define LANG_MALAY 0x3e
306 #endif
307 #ifndef LANG_MALAYALAM
308 #define LANG_MALAYALAM 0x4c
309 #endif
310 #ifndef LANG_MANIPURI
311 #define LANG_MANIPURI 0x58
312 #endif
313 #ifndef LANG_MARATHI
314 #define LANG_MARATHI 0x4e
315 #endif
316 #ifndef LANG_NEPALI
317 #define LANG_NEPALI 0x61
318 #endif
319 #ifndef LANG_ORIYA
320 #define LANG_ORIYA 0x48
321 #endif
322 #ifndef LANG_PUNJABI
323 #define LANG_PUNJABI 0x46
324 #endif
325 #ifndef LANG_SANSKRIT
326 #define LANG_SANSKRIT 0x4f
327 #endif
328 #ifndef LANG_SERBIAN
329 #define LANG_SERBIAN 0x1a
330 #endif
331 #ifndef LANG_SINDHI
332 #define LANG_SINDHI 0x59
333 #endif
334 #ifndef LANG_SLOVAK
335 #define LANG_SLOVAK 0x1b
336 #endif
337 #ifndef LANG_SWAHILI
338 #define LANG_SWAHILI 0x41
339 #endif
340 #ifndef LANG_TAMIL
341 #define LANG_TAMIL 0x49
342 #endif
343 #ifndef LANG_TATAR
344 #define LANG_TATAR 0x44
345 #endif
346 #ifndef LANG_TELUGU
347 #define LANG_TELUGU 0x4a
348 #endif
349 #ifndef LANG_THAI
350 #define LANG_THAI 0x1e
351 #endif
352 #ifndef LANG_UKRAINIAN
353 #define LANG_UKRAINIAN 0x22
354 #endif
355 #ifndef LANG_URDU
356 #define LANG_URDU 0x20
357 #endif
358 #ifndef LANG_UZBEK
359 #define LANG_UZBEK 0x43
360 #endif
361 #ifndef LANG_VIETNAMESE
362 #define LANG_VIETNAMESE 0x2a
363 #endif
364 #ifndef SUBLANG_ARABIC_SAUDI_ARABIA
365 #define SUBLANG_ARABIC_SAUDI_ARABIA 0x01
366 #endif
367 #ifndef SUBLANG_ARABIC_IRAQ
368 #define SUBLANG_ARABIC_IRAQ 0x02
369 #endif
370 #ifndef SUBLANG_ARABIC_EGYPT
371 #define SUBLANG_ARABIC_EGYPT 0x03
372 #endif
373 #ifndef SUBLANG_ARABIC_LIBYA
374 #define SUBLANG_ARABIC_LIBYA 0x04
375 #endif
376 #ifndef SUBLANG_ARABIC_ALGERIA
377 #define SUBLANG_ARABIC_ALGERIA 0x05
378 #endif
379 #ifndef SUBLANG_ARABIC_MOROCCO
380 #define SUBLANG_ARABIC_MOROCCO 0x06
381 #endif
382 #ifndef SUBLANG_ARABIC_TUNISIA
383 #define SUBLANG_ARABIC_TUNISIA 0x07
384 #endif
385 #ifndef SUBLANG_ARABIC_OMAN
386 #define SUBLANG_ARABIC_OMAN 0x08
387 #endif
388 #ifndef SUBLANG_ARABIC_YEMEN
389 #define SUBLANG_ARABIC_YEMEN 0x09
390 #endif
391 #ifndef SUBLANG_ARABIC_SYRIA
392 #define SUBLANG_ARABIC_SYRIA 0x0a
393 #endif
394 #ifndef SUBLANG_ARABIC_JORDAN
395 #define SUBLANG_ARABIC_JORDAN 0x0b
396 #endif
397 #ifndef SUBLANG_ARABIC_LEBANON
398 #define SUBLANG_ARABIC_LEBANON 0x0c
399 #endif
400 #ifndef SUBLANG_ARABIC_KUWAIT
401 #define SUBLANG_ARABIC_KUWAIT 0x0d
402 #endif
403 #ifndef SUBLANG_ARABIC_UAE
404 #define SUBLANG_ARABIC_UAE 0x0e
405 #endif
406 #ifndef SUBLANG_ARABIC_BAHRAIN
407 #define SUBLANG_ARABIC_BAHRAIN 0x0f
408 #endif
409 #ifndef SUBLANG_ARABIC_QATAR
410 #define SUBLANG_ARABIC_QATAR 0x10
411 #endif
412 #ifndef SUBLANG_AZERI_LATIN
413 #define SUBLANG_AZERI_LATIN 0x01
414 #endif
415 #ifndef SUBLANG_AZERI_CYRILLIC
416 #define SUBLANG_AZERI_CYRILLIC 0x02
417 #endif
418 #ifndef SUBLANG_CHINESE_MACAU
419 #define SUBLANG_CHINESE_MACAU 0x05
420 #endif
421 #ifndef SUBLANG_ENGLISH_SOUTH_AFRICA
422 #define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07
423 #endif
424 #ifndef SUBLANG_ENGLISH_JAMAICA
425 #define SUBLANG_ENGLISH_JAMAICA 0x08
426 #endif
427 #ifndef SUBLANG_ENGLISH_CARIBBEAN
428 #define SUBLANG_ENGLISH_CARIBBEAN 0x09
429 #endif
430 #ifndef SUBLANG_ENGLISH_BELIZE
431 #define SUBLANG_ENGLISH_BELIZE 0x0a
432 #endif
433 #ifndef SUBLANG_ENGLISH_TRINIDAD
434 #define SUBLANG_ENGLISH_TRINIDAD 0x0b
435 #endif
436 #ifndef SUBLANG_ENGLISH_ZIMBABWE
437 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
438 #endif
439 #ifndef SUBLANG_ENGLISH_PHILIPPINES
440 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
441 #endif
442 #ifndef SUBLANG_FRENCH_LUXEMBOURG
443 #define SUBLANG_FRENCH_LUXEMBOURG 0x05
444 #endif
445 #ifndef SUBLANG_FRENCH_MONACO
446 #define SUBLANG_FRENCH_MONACO 0x06
447 #endif
448 #ifndef SUBLANG_GERMAN_LUXEMBOURG
449 #define SUBLANG_GERMAN_LUXEMBOURG 0x04
450 #endif
451 #ifndef SUBLANG_GERMAN_LIECHTENSTEIN
452 #define SUBLANG_GERMAN_LIECHTENSTEIN 0x05
453 #endif
454 #ifndef SUBLANG_KASHMIRI_INDIA
455 #define SUBLANG_KASHMIRI_INDIA 0x02
456 #endif
457 #ifndef SUBLANG_MALAY_MALAYSIA
458 #define SUBLANG_MALAY_MALAYSIA 0x01
459 #endif
460 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
461 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
462 #endif
463 #ifndef SUBLANG_NEPALI_INDIA
464 #define SUBLANG_NEPALI_INDIA 0x02
465 #endif
466 #ifndef SUBLANG_SERBIAN_LATIN
467 #define SUBLANG_SERBIAN_LATIN 0x02
468 #endif
469 #ifndef SUBLANG_SERBIAN_CYRILLIC
470 #define SUBLANG_SERBIAN_CYRILLIC 0x03
471 #endif
472 #ifndef SUBLANG_SPANISH_GUATEMALA
473 #define SUBLANG_SPANISH_GUATEMALA 0x04
474 #endif
475 #ifndef SUBLANG_SPANISH_COSTA_RICA
476 #define SUBLANG_SPANISH_COSTA_RICA 0x05
477 #endif
478 #ifndef SUBLANG_SPANISH_PANAMA
479 #define SUBLANG_SPANISH_PANAMA 0x06
480 #endif
481 #ifndef SUBLANG_SPANISH_DOMINICAN_REPUBLIC
482 #define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07
483 #endif
484 #ifndef SUBLANG_SPANISH_VENEZUELA
485 #define SUBLANG_SPANISH_VENEZUELA 0x08
486 #endif
487 #ifndef SUBLANG_SPANISH_COLOMBIA
488 #define SUBLANG_SPANISH_COLOMBIA 0x09
489 #endif
490 #ifndef SUBLANG_SPANISH_PERU
491 #define SUBLANG_SPANISH_PERU 0x0a
492 #endif
493 #ifndef SUBLANG_SPANISH_ARGENTINA
494 #define SUBLANG_SPANISH_ARGENTINA 0x0b
495 #endif
496 #ifndef SUBLANG_SPANISH_ECUADOR
497 #define SUBLANG_SPANISH_ECUADOR 0x0c
498 #endif
499 #ifndef SUBLANG_SPANISH_CHILE
500 #define SUBLANG_SPANISH_CHILE 0x0d
501 #endif
502 #ifndef SUBLANG_SPANISH_URUGUAY
503 #define SUBLANG_SPANISH_URUGUAY 0x0e
504 #endif
505 #ifndef SUBLANG_SPANISH_PARAGUAY
506 #define SUBLANG_SPANISH_PARAGUAY 0x0f
507 #endif
508 #ifndef SUBLANG_SPANISH_BOLIVIA
509 #define SUBLANG_SPANISH_BOLIVIA 0x10
510 #endif
511 #ifndef SUBLANG_SPANISH_EL_SALVADOR
512 #define SUBLANG_SPANISH_EL_SALVADOR 0x11
513 #endif
514 #ifndef SUBLANG_SPANISH_HONDURAS
515 #define SUBLANG_SPANISH_HONDURAS 0x12
516 #endif
517 #ifndef SUBLANG_SPANISH_NICARAGUA
518 #define SUBLANG_SPANISH_NICARAGUA 0x13
519 #endif
520 #ifndef SUBLANG_SPANISH_PUERTO_RICO
521 #define SUBLANG_SPANISH_PUERTO_RICO 0x14
522 #endif
523 #ifndef SUBLANG_SWEDISH_FINLAND
524 #define SUBLANG_SWEDISH_FINLAND 0x02
525 #endif
526 #ifndef SUBLANG_URDU_PAKISTAN
527 #define SUBLANG_URDU_PAKISTAN 0x01
528 #endif
529 #ifndef SUBLANG_URDU_INDIA
530 #define SUBLANG_URDU_INDIA 0x02
531 #endif
532 #ifndef SUBLANG_UZBEK_LATIN
533 #define SUBLANG_UZBEK_LATIN 0x01
534 #endif
535 #ifndef SUBLANG_UZBEK_CYRILLIC
536 #define SUBLANG_UZBEK_CYRILLIC 0x02
537 #endif
538
539 /**
540  * g_win32_getlocale:
541  *
542  * The setlocale in the Microsoft C library uses locale names of the
543  * form "English_United States.1252" etc. We want the Unixish standard
544  * form "en", "zh_TW" etc. This function gets the current thread
545  * locale from Windows and returns it as a string of the above form
546  * for use in forming file names etc. The returned string should be
547  * deallocated with g_free().
548  *
549  * Returns: allocated locale name
550  */
551
552 gchar *
553 g_win32_getlocale (void)
554 {
555   LCID lcid;
556   gchar *ev;
557   gint primary, sub;
558   gchar *l = NULL, *sl = NULL;
559   gchar bfr[20];
560
561   if ((ev = getenv ("LC_ALL")) != NULL
562       || (ev = getenv ("LC_CTYPE")) != NULL
563       || (ev = getenv ("LANG")) != NULL)
564     return g_strdup (ev);
565
566   lcid = GetThreadLocale ();
567   primary = PRIMARYLANGID (LANGIDFROMLCID (lcid));
568   sub = SUBLANGID (LANGIDFROMLCID (lcid));
569   switch (primary)
570     {
571     case LANG_AFRIKAANS: l = "af"; break;
572     case LANG_ALBANIAN: l = "sq"; break;
573     case LANG_ARABIC:
574       l = "ar";
575       switch (sub)
576         {
577         case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
578         case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
579         case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
580         case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
581         case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
582         case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
583         case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
584         case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
585         case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
586         case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
587         case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
588         case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
589         case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
590         case SUBLANG_ARABIC_UAE: sl = "AE"; break;
591         case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
592         case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
593         }
594       break;
595     case LANG_ARMENIAN: l = "hy"; break;
596     case LANG_ASSAMESE: l = "as"; break;
597     case LANG_AZERI: l = "az"; break;
598     case LANG_BASQUE: l = "eu"; break;
599     case LANG_BELARUSIAN: l = "be"; break;
600     case LANG_BENGALI: l = "bn"; break;
601     case LANG_BULGARIAN: l = "bg"; break;
602     case LANG_CATALAN: l = "ca"; break;
603     case LANG_CHINESE:
604       l = "zh";
605       switch (sub)
606         {
607         case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
608         case SUBLANG_CHINESE_SIMPLIFIED: sl = "CH"; break;
609         case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
610         case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
611         case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
612         }
613       break;
614     case LANG_CROATIAN:         /* LANG_CROATIAN == LANG_SERBIAN
615                                  * What used to be called Serbo-Croatian
616                                  * should really now be two separate
617                                  * languages because of political reasons.
618                                  * (Says tml, who knows nothing about Serbian
619                                  * or Croatian.)
620                                  * (I can feel those flames coming already.)
621                                  */
622       switch (sub)
623         {
624         case SUBLANG_SERBIAN_LATIN: l = "sp"; break;
625         case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
626         default: l = "hr";      /* ??? */
627         }
628       break;
629     case LANG_CZECH: l = "cs"; break;
630     case LANG_DANISH: l = "da"; break;
631     case LANG_DUTCH:
632       l = "nl";
633       switch (sub)
634         {
635         case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
636         }
637       break;
638     case LANG_ENGLISH:
639       l = "en";
640       switch (sub)
641         {
642         /* SUBLANG_ENGLISH_US == SUBLANG_DEFAULT. Heh. I thought
643          * English was the language spoken in England.
644          * Oh well.
645          */
646         case SUBLANG_ENGLISH_UK: sl = "GB"; break;
647         case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
648         case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
649         case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
650         case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
651         case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
652         case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
653         case SUBLANG_ENGLISH_CARIBBEAN: sl = "@caribbean"; break; /* ??? */
654         case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
655         case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
656         case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
657         case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
658         }
659       break;
660     case LANG_ESTONIAN: l = "et"; break;
661     case LANG_FAEROESE: l = "fo"; break;
662     case LANG_FARSI: l = "fa"; break;
663     case LANG_FINNISH: l = "fi"; break;
664     case LANG_FRENCH:
665       l = "fr";
666       switch (sub)
667         {
668         case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
669         case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
670         case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
671         case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
672         case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
673         }
674       break;
675     case LANG_GEORGIAN: l = "ka"; break;
676     case LANG_GERMAN:
677       l = "de";
678       switch (sub)
679         {
680         case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
681         case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
682         case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
683         case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
684         }
685       break;
686     case LANG_GREEK: l = "el"; break;
687     case LANG_GUJARATI: l = "gu"; break;
688     case LANG_HEBREW: l = "he"; break;
689     case LANG_HINDI: l = "hi"; break;
690     case LANG_HUNGARIAN: l = "hu"; break;
691     case LANG_ICELANDIC: l = "is"; break;
692     case LANG_INDONESIAN: l = "id"; break;
693     case LANG_ITALIAN:
694       l = "it";
695       switch (sub)
696         {
697         case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
698         }
699       break;
700     case LANG_JAPANESE: l = "ja"; break;
701     case LANG_KANNADA: l = "kn"; break;
702     case LANG_KASHMIRI:
703       l = "ks";
704       switch (sub)
705         {
706         case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
707         }
708       break;
709     case LANG_KAZAK: l = "kk"; break;
710     case LANG_KONKANI: l = "kok"; break; /* ??? */
711     case LANG_KOREAN: l = "ko"; break;
712     case LANG_LATVIAN: l = "lv"; break;
713     case LANG_LITHUANIAN: l = "lt"; break;
714     case LANG_MACEDONIAN: l = "mk"; break;
715     case LANG_MALAY:
716       l = "ms";
717       switch (sub)
718         {
719         case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
720         }
721       break;
722     case LANG_MALAYALAM: l = "ml"; break;
723     case LANG_MANIPURI: l = "mni"; break;
724     case LANG_MARATHI: l = "mr"; break;
725     case LANG_NEPALI:
726       l = "ne";
727       switch (sub)
728         {
729         case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
730         }
731       break;
732     case LANG_NORWEGIAN:
733       l = "no";
734       switch (sub)
735         {
736         /* SUBLANG_NORWEGIAN_BOKMAL == SUBLANG_DEFAULT */
737         case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; break;
738         }
739       break;
740     case LANG_ORIYA: l = "or"; break;
741     case LANG_POLISH: l = "pl"; break;
742     case LANG_PORTUGUESE:
743       l = "pt";
744       switch (sub)
745         {
746         /* Hmm. SUBLANG_PORTUGUESE_BRAZILIAN == SUBLANG_DEFAULT */
747         case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
748         }
749       break;
750     case LANG_PUNJABI: l = "pa"; break;
751     case LANG_ROMANIAN: l = "ro"; break;
752     case LANG_RUSSIAN: l = "ru"; break;
753     case LANG_SANSKRIT: l = "sa"; break;
754     case LANG_SINDHI: l = "sd"; break;
755     case LANG_SLOVAK: l = "sk"; break;
756     case LANG_SLOVENIAN: l = "sl"; break;
757     case LANG_SPANISH:
758       l = "es";
759       switch (sub)
760         {
761         case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
762         case SUBLANG_SPANISH_MODERN: sl = "@modern"; break;     /* ??? */
763         case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
764         case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
765         case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
766         case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
767         case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
768         case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
769         case SUBLANG_SPANISH_PERU: sl = "PE"; break;
770         case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
771         case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
772         case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
773         case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
774         case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
775         case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
776         case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
777         case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
778         case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
779         case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
780         }
781       break;
782     case LANG_SWAHILI: l = "sw"; break;
783     case LANG_SWEDISH:
784       l = "sv";
785       switch (sub)
786         {
787         case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
788         }
789       break;
790     case LANG_TAMIL: l = "ta"; break;
791     case LANG_TATAR: l = "tt"; break;
792     case LANG_TELUGU: l = "te"; break;
793     case LANG_THAI: l = "th"; break;
794     case LANG_TURKISH: l = "tr"; break;
795     case LANG_UKRAINIAN: l = "uk"; break;
796     case LANG_URDU:
797       l = "ur";
798       switch (sub)
799         {
800         case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
801         case SUBLANG_URDU_INDIA: sl = "IN"; break;
802         }
803       break;
804     case LANG_UZBEK:
805       l = "uz";
806       switch (sub)
807         {
808         case SUBLANG_UZBEK_CYRILLIC: sl = "@cyrillic"; break;
809         }
810       break;
811     case LANG_VIETNAMESE: l = "vi"; break;
812     default: l = "xx"; break;
813     }
814   strcpy (bfr, l);
815   if (sl != NULL)
816     {
817       if (sl[0] != '@')
818         strcat (bfr, "_");
819       strcat (bfr, sl);
820     }
821
822   return g_strdup (bfr);
823 }
824
825 /**
826  * g_win32_error_message:
827  * @error: error code
828  *
829  * Translate a Win32 error code (as returned by GetLastError()) into
830  * the corresponding message. The message is either language neutral,
831  * or in the thread's language, or the user's language, the system's
832  * langauge, or US English (see docs for FormatMessage). The returned
833  * string should be deallocated with g_free().
834  *
835  * Returns: allocated error message
836  */
837 gchar *
838 g_win32_error_message (gint error)
839 {
840   gchar *msg;
841   gchar *retval;
842   int nbytes;
843
844   FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
845                  |FORMAT_MESSAGE_IGNORE_INSERTS
846                  |FORMAT_MESSAGE_FROM_SYSTEM,
847                  NULL, error, 0,
848                  (LPTSTR) &msg, 0, NULL);
849   nbytes = strlen (msg);
850
851   if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
852     msg[nbytes-2] = '\0';
853   
854   retval = g_strdup (msg);
855
856   if (msg != NULL)
857     LocalFree (msg);
858
859   return retval;
860 }
861
862 static gchar *
863 get_package_directory_from_module (gchar *module_name)
864 {
865   static GHashTable *module_dirs = NULL;
866   HMODULE hmodule = NULL;
867   gchar *fn;
868   gchar *p;
869   gchar *result;
870
871   if (module_dirs == NULL)
872     module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
873   
874   result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
875       
876   if (result)
877     return g_strdup (result);
878
879   if (module_name)
880     {
881       hmodule = GetModuleHandle (module_name);
882       if (!hmodule)
883         return NULL;
884     }
885
886   fn = g_malloc (MAX_PATH);
887   if (!GetModuleFileName (hmodule, fn, MAX_PATH))
888     return NULL;
889
890 #ifdef G_WITH_CYGWIN
891   /* In Cygwin we need to have POSIX paths */
892   {
893     gchar tmp[MAX_PATH];
894
895     cygwin_conv_to_posix_path(fn, tmp);
896     g_free(fn);
897     fn = g_strdup(tmp);
898   }
899 #endif
900
901   if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
902     *p = '\0';
903
904   if (module_name)
905     {
906       p = strrchr (fn, G_DIR_SEPARATOR);
907       if (p && (g_strcasecmp (p + 1, "bin") == 0 ||
908                 g_strcasecmp (p + 1, "lib") == 0))
909         *p = '\0';
910     }
911
912   g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
913
914   return g_strdup (fn);
915 }
916
917 /**
918  * g_win32_get_package_installation_directory:
919  * @package: An identifier for a software package, or NULL
920  * @dll_name: The name of a DLL that a package provides, or NULL
921  *
922  * Try to determine the installation directory for a software package.
923  * Typically used by GNU software packages.
924  *
925  * @package should be a short identifier for the package. Typically it
926  * is the same identifier as used for GETTEXT_PACKAGE in software
927  * configured accoring to GNU standards. The function first looks in
928  * the Windows Registry for the value #InstallationDirectory in the
929  * key #HKLM\Software\@package, and if that value exists and is a
930  * string, returns that.
931  *
932  * If @package is NULL, or the above value isn't found in the
933  * Registry, but @dll_name is non-NULL, it should name a DLL loaded
934  * into the current process. Typically that would be the name of the
935  * DLL calling this function, looking for its installation
936  * directory. The function then asks Windows what directory that DLL
937  * was loaded from. If that directory's last component is "bin" or
938  * "lib", the parent directory is returned, otherwise the directory
939  * itself. If that DLL isn't loaded, the function proceeds as if
940  * @dll_name was NULL.
941  *
942  * If both @package and @dll_name are NULL, the directory from where
943  * the main executable of the process was loaded is uses instead in
944  * the same way as above.
945  *
946  * The return value should be freed with g_free() when not needed any longer.  */
947
948 gchar *
949 g_win32_get_package_installation_directory (gchar *package,
950                                             gchar *dll_name)
951 {
952   static GHashTable *package_dirs = NULL;
953   gchar *result = NULL;
954   gchar *key;
955   HKEY reg_key = NULL;
956   DWORD type;
957   DWORD nbytes;
958
959   if (package != NULL)
960     {
961       if (package_dirs == NULL)
962         package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
963       
964       result = g_hash_table_lookup (package_dirs, package);
965       
966       if (result && result[0])
967         return g_strdup (result);
968       
969       key = g_strconcat ("Software\\", package, NULL);
970       
971       nbytes = 0;
972       if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
973                         KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
974           && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
975                               &type, NULL, &nbytes) == ERROR_SUCCESS
976           && type == REG_SZ)
977         {
978           result = g_malloc (nbytes + 1);
979           RegQueryValueEx (reg_key, "InstallationDirectory", 0,
980                            &type, result, &nbytes);
981           result[nbytes] = '\0';
982         }
983
984       if (reg_key != NULL)
985         RegCloseKey (reg_key);
986       
987       g_free (key);
988       
989     }
990   if (result)
991     {
992       g_hash_table_insert (package_dirs, package, result);
993       return g_strdup (result);
994     }
995
996   if (dll_name != NULL)
997     result = get_package_directory_from_module (dll_name);
998
999   if (result == NULL)
1000     result = get_package_directory_from_module (NULL);
1001
1002   return result;
1003 }
1004
1005 /**
1006  * g_win32_get_package_installation_subdirectory:
1007  * @package: An identifier for a software package, or NULL
1008  * @dll_name: The name of a DLL that a package provides, or NULL
1009  * @subdir: A subdirectory of the package installation directory.
1010  *
1011  * Returns a string containg the path of the subdirectory @subdir in
1012  * the return value from calling
1013  * g_win32_get_package_installation_directory() with the @package and
1014  * @dll_name parameters. The return value should be freed with
1015  * g_free() when no longer needed.
1016  */
1017
1018 gchar *
1019 g_win32_get_package_installation_subdirectory (gchar *package,
1020                                                gchar *dll_name,
1021                                                gchar *subdir)
1022 {
1023   gchar *prefix;
1024   gchar *sep;
1025
1026   prefix = g_win32_get_package_installation_directory (package, dll_name);
1027
1028   sep = (prefix[strlen (prefix) - 1] == G_DIR_SEPARATOR ?
1029          "" : G_DIR_SEPARATOR_S);
1030
1031   return g_strconcat (prefix, sep, subdir, NULL);
1032 }