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