Don't define LANG_AZERI etc in case those aren't defined in the headers
[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 #endif
97
98 /**
99  * g_win32_getlocale:
100  *
101  * The setlocale in the Microsoft C library uses locale names of the
102  * form "English_United States.1252" etc. We want the Unixish standard
103  * form "en_US", "zh_TW" etc. This function gets the current thread
104  * locale from Windows - without any encoding info - and returns it as
105  * a string of the above form for use in forming file names etc. The
106  * returned string should be deallocated with g_free().
107  *
108  * Returns: newly-allocated locale name.
109  **/
110
111 gchar *
112 g_win32_getlocale (void)
113 {
114   LCID lcid;
115   LANGID langid;
116   gchar *ev;
117   gint primary, sub;
118   gchar *l = NULL, *sl = NULL;
119   gchar bfr[20];
120
121   /* Let the user override the system settings through environment
122      variables, as on POSIX systems.  */
123   if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
124       || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
125       || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
126     return g_strdup (ev);
127
128   /* Use native Win32 API locale ID.  */
129   lcid = GetThreadLocale ();
130
131   /* Strip off the sorting rules, keep only the language part.  */
132   langid = LANGIDFROMLCID (lcid);
133
134   /* Split into language and territory part.  */
135   primary = PRIMARYLANGID (langid);
136   sub = SUBLANGID (langid);
137   switch (primary)
138     {
139     case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
140     case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
141     case LANG_ARABIC:
142       l = "ar";
143       switch (sub)
144         {
145         case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
146         case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
147         case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
148         case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
149         case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
150         case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
151         case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
152         case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
153         case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
154         case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
155         case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
156         case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
157         case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
158         case SUBLANG_ARABIC_UAE: sl = "AE"; break;
159         case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
160         case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
161         }
162       break;
163 #ifdef LANG_ARMENIAN
164     case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
165 #endif
166 #ifdef LANG_ASSAMESE
167     case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
168 #endif
169 #ifdef LANG_AZERI
170     case LANG_AZERI:
171       l = "az";
172 #if defined (SUBLANG_AZERI_LATIN) && defined (SUBLANG_AZERI_CYRILLIC)
173       switch (sub)
174         {
175         /* FIXME: Adjust this when Azerbaijani locales appear on Unix.  */
176         case SUBLANG_AZERI_LATIN: sl = "@latin"; break;
177         case SUBLANG_AZERI_CYRILLIC: sl = "@cyrillic"; break;
178         }
179 #endif
180       break;
181 #endif
182     case LANG_BASQUE:
183       l = "eu"; /* sl could be "ES" or "FR".  */
184       break;
185     case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
186 #ifdef LANG_BENGALI
187     case LANG_BENGALI: l = "bn"; sl = "IN"; break;
188 #endif
189     case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
190     case LANG_CATALAN: l = "ca"; sl = "ES"; break;
191     case LANG_CHINESE:
192       l = "zh";
193       switch (sub)
194         {
195         case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
196         case SUBLANG_CHINESE_SIMPLIFIED: sl = "CN"; break;
197         case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
198         case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
199 #ifdef SUBLANG_CHINESE_MACAU
200         case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
201 #endif
202         }
203       break;
204     case LANG_CROATIAN:         /* LANG_CROATIAN == LANG_SERBIAN
205                                  * What used to be called Serbo-Croatian
206                                  * should really now be two separate
207                                  * languages because of political reasons.
208                                  * (Says tml, who knows nothing about Serbian
209                                  * or Croatian.)
210                                  * (I can feel those flames coming already.)
211                                  */
212       switch (sub)
213         {
214         /* FIXME: How to distinguish Croatian and Latin Serbian locales?  */
215         case SUBLANG_SERBIAN_LATIN: l = "sr"; break;
216         case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; sl = "@cyrillic"; break;
217         default: l = "hr"; sl = "HR";
218         }
219       break;
220     case LANG_CZECH: l = "cs"; sl = "CZ"; break;
221     case LANG_DANISH: l = "da"; sl = "DK"; break;
222     case LANG_DUTCH:
223       l = "nl";
224       switch (sub)
225         {
226         case SUBLANG_DUTCH: sl = "NL"; break;
227         case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
228         }
229       break;
230     case LANG_ENGLISH:
231       l = "en";
232       switch (sub)
233         {
234         case SUBLANG_ENGLISH_US: sl = "US"; break;
235         case SUBLANG_ENGLISH_UK: sl = "GB"; break;
236         case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
237         case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
238         case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
239         case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
240         case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
241         case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
242         case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
243         case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
244         case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
245 #ifdef SUBLANG_ENGLISH_ZIMBABWE
246         case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
247 #endif
248 #ifdef SUBLANG_ENGLISH_PHILIPPINES
249         case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
250 #endif
251         }
252       break;
253     case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
254     case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
255     case LANG_FARSI: l = "fa"; sl = "IR"; break;
256     case LANG_FINNISH: l = "fi"; sl = "FI"; break;
257     case LANG_FRENCH:
258       l = "fr";
259       switch (sub)
260         {
261         case SUBLANG_FRENCH: sl = "FR"; break;
262         case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
263         case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
264         case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
265         case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
266 #ifdef SUBLANG_FRENCH_MONACO
267         case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
268 #endif
269         }
270       break;
271 #ifdef LANG_GEORGIAN
272     case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
273 #endif
274     case LANG_GERMAN:
275       l = "de";
276       switch (sub)
277         {
278         case SUBLANG_GERMAN: sl = "DE"; break;
279         case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
280         case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
281         case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
282         case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
283         }
284       break;
285     case LANG_GREEK: l = "el"; sl = "GR"; break;
286 #ifdef LANG_GUJARATI
287     case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
288 #endif
289     case LANG_HEBREW: l = "he"; sl = "IL"; break;
290 #ifdef LANG_HINDI
291     case LANG_HINDI: l = "hi"; sl = "IN"; break;
292 #endif
293     case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
294     case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
295     case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
296     case LANG_ITALIAN:
297       l = "it";
298       switch (sub)
299         {
300         case SUBLANG_ITALIAN: sl = "IT"; break;
301         case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
302         }
303       break;
304     case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
305 #ifdef LANG_KANNADA
306     case LANG_KANNADA: l = "kn"; sl = "IN"; break;
307 #endif
308 #ifdef LANG_KASHMIRI
309     case LANG_KASHMIRI:
310       l = "ks";
311       switch (sub)
312         {
313         case SUBLANG_DEFAULT: sl = "PK"; break;
314 #ifdef SUBLANG_KASHMIRI_INDIA
315         case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
316 #endif
317         }
318       break;
319 #endif
320 #ifdef LANG_KAZAK
321     case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
322 #endif
323 #ifdef LANG_KONKANI
324     case LANG_KONKANI:
325       /* FIXME: Adjust this when such locales appear on Unix.  */
326       l = "kok"; sl = "IN";
327       break;
328 #endif
329     case LANG_KOREAN: l = "ko"; sl = "KR"; break;
330     case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
331     case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
332 #ifdef LANG_MACEDONIAN
333     case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
334 #endif
335 #ifdef LANG_MALAY
336     case LANG_MALAY:
337       l = "ms";
338       switch (sub)
339         {
340 #ifdef SUBLANG_MALAY_MALAYSIA
341         case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
342 #endif
343 #ifdef SUBLANG_MALAY_BRUNEI_DARUSSALAM
344         case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
345 #endif
346         }
347       break;
348 #endif
349 #ifdef LANG_MALAYALAM
350     case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
351 #endif
352 #ifdef LANG_MANIPURI
353     case LANG_MANIPURI:
354       /* FIXME: Adjust this when such locales appear on Unix.  */
355       l = "mni"; sl = "IN";
356       break;
357 #endif
358 #ifdef LANG_MARATHI
359     case LANG_MARATHI: l = "mr"; sl = "IN"; break;
360 #endif
361 #ifdef LANG_NEPALI
362     case LANG_NEPALI:
363       l = "ne";
364       switch (sub)
365         {
366         case SUBLANG_DEFAULT: sl = "NP"; break;
367 #ifdef SUBLANG_NEPALI_INDIA
368         case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
369 #endif
370         }
371       break;
372 #endif
373     case LANG_NORWEGIAN:
374       l = "no";
375       switch (sub)
376         {
377         case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
378         case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
379         }
380       break;
381 #ifdef LANG_ORIYA
382     case LANG_ORIYA: l = "or"; sl = "IN"; break;
383 #endif
384     case LANG_POLISH: l = "pl"; sl = "PL"; break;
385     case LANG_PORTUGUESE:
386       l = "pt";
387       switch (sub)
388         {
389         case SUBLANG_PORTUGUESE: sl = "PT"; break;
390         case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
391         }
392       break;
393 #ifdef LANG_PUNJABI
394     case LANG_PUNJABI: l = "pa"; sl = "IN"; break;
395 #endif
396     case LANG_ROMANIAN: l = "ro"; sl = "RO"; break;
397     case LANG_RUSSIAN:
398       l = "ru"; /* sl could be "RU" or "UA".  */
399       break;
400 #ifdef LANG_SANSKRIT
401     case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
402 #endif
403 #ifdef LANG_SINDHI
404     case LANG_SINDHI: l = "sd"; break;
405 #endif
406     case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
407     case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
408 #ifdef LANG_SORBIAN
409     case LANG_SORBIAN:
410       /* FIXME: Adjust this when such locales appear on Unix.  */
411       l = "wen"; sl = "DE";
412       break;
413 #endif
414     case LANG_SPANISH:
415       l = "es";
416       switch (sub)
417         {
418         case SUBLANG_SPANISH: sl = "ES"; break;
419         case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
420         case SUBLANG_SPANISH_MODERN:
421           sl = "ES@modern"; break;      /* not seen on Unix */
422         case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
423         case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
424         case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
425         case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
426         case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
427         case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
428         case SUBLANG_SPANISH_PERU: sl = "PE"; break;
429         case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
430         case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
431         case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
432         case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
433         case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
434         case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
435         case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
436         case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
437         case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
438         case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
439         }
440       break;
441 #ifdef LANG_SWAHILI
442     case LANG_SWAHILI: l = "sw"; break;
443 #endif
444     case LANG_SWEDISH:
445       l = "sv";
446       switch (sub)
447         {
448         case SUBLANG_DEFAULT: sl = "SE"; break;
449         case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
450         }
451       break;
452 #ifdef LANG_TAMIL
453     case LANG_TAMIL:
454       l = "ta"; /* sl could be "IN" or "LK".  */
455       break;
456 #endif
457 #ifdef LANG_TATAR
458     case LANG_TATAR: l = "tt"; break;
459 #endif
460 #ifdef LANG_TELUGU
461     case LANG_TELUGU: l = "te"; sl = "IN"; break;
462 #endif
463     case LANG_THAI: l = "th"; sl = "TH"; break;
464     case LANG_TURKISH: l = "tr"; sl = "TR"; break;
465     case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
466 #ifdef LANG_URDU
467     case LANG_URDU:
468       l = "ur";
469       switch (sub)
470         {
471 #ifdef SUBLANG_URDU_PAKISTAN
472         case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
473 #endif
474 #ifdef SUBLANG_URDU_INDIA
475         case SUBLANG_URDU_INDIA: sl = "IN"; break;
476 #endif
477         }
478       break;
479 #endif
480 #ifdef LANG_UZBEK
481     case LANG_UZBEK:
482       l = "uz";
483       switch (sub)
484         {
485         /* FIXME: Adjust this when Uzbek locales appear on Unix.  */
486 #ifdef SUBLANG_UZBEK_LATIN
487         case SUBLANG_UZBEK_LATIN: sl = "UZ@latin"; break;
488 #endif
489 #ifdef SUBLANG_UZBEK_CYRILLIC
490         case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
491 #endif
492         }
493       break;
494 #endif
495     case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
496     default: l = "xx"; break;
497     }
498   strcpy (bfr, l);
499   if (sl != NULL)
500     {
501       if (sl[0] != '@')
502         strcat (bfr, "_");
503       strcat (bfr, sl);
504     }
505
506   return g_strdup (bfr);
507 }
508
509 /**
510  * g_win32_error_message:
511  * @error: error code.
512  *
513  * Translate a Win32 error code (as returned by GetLastError()) into
514  * the corresponding message. The message is either language neutral,
515  * or in the thread's language, or the user's language, the system's
516  * language, or US English (see docs for <function>FormatMessage()</function>). *
517  * The returned string should be deallocated with g_free().
518  *
519  * Returns: newly-allocated error message
520  **/
521 gchar *
522 g_win32_error_message (gint error)
523 {
524   gchar *msg;
525   gchar *retval;
526   int nbytes;
527
528   FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
529                  |FORMAT_MESSAGE_IGNORE_INSERTS
530                  |FORMAT_MESSAGE_FROM_SYSTEM,
531                  NULL, error, 0,
532                  (LPTSTR) &msg, 0, NULL);
533   nbytes = strlen (msg);
534
535   if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
536     msg[nbytes-2] = '\0';
537   
538   retval = g_strdup (msg);
539
540   if (msg != NULL)
541     LocalFree (msg);
542
543   return retval;
544 }
545
546 static gchar *
547 get_package_directory_from_module (gchar *module_name)
548 {
549   static GHashTable *module_dirs = NULL;
550   G_LOCK_DEFINE_STATIC (module_dirs);
551   HMODULE hmodule = NULL;
552   gchar *fn;
553   gchar *p;
554   gchar *result;
555
556   G_LOCK (module_dirs);
557
558   if (module_dirs == NULL)
559     module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
560   
561   result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
562       
563   if (result)
564     {
565       G_UNLOCK (module_dirs);
566       return g_strdup (result);
567     }
568
569   if (module_name)
570     {
571       hmodule = GetModuleHandle (module_name);
572       if (!hmodule)
573         return NULL;
574     }
575
576   fn = g_malloc (MAX_PATH);
577   if (!GetModuleFileName (hmodule, fn, MAX_PATH))
578     {
579       G_UNLOCK (module_dirs);
580       g_free (fn);
581       return NULL;
582     }
583
584   if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
585     *p = '\0';
586
587   p = strrchr (fn, G_DIR_SEPARATOR);
588   if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
589             g_ascii_strcasecmp (p + 1, "lib") == 0))
590     *p = '\0';
591
592 #ifdef G_WITH_CYGWIN
593   /* In Cygwin we need to have POSIX paths */
594   {
595     gchar tmp[MAX_PATH];
596
597     cygwin_conv_to_posix_path(fn, tmp);
598     g_free(fn);
599     fn = g_strdup(tmp);
600   }
601 #endif
602
603   g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
604
605   G_UNLOCK (module_dirs);
606
607   return g_strdup (fn);
608 }
609
610 /**
611  * g_win32_get_package_installation_directory:
612  * @package: An identifier for a software package, or %NULL
613  * @dll_name: The name of a DLL that a package provides, or %NULL.
614  *
615  * Try to determine the installation directory for a software package.
616  * Typically used by GNU software packages.
617  *
618  * @package should be a short identifier for the package. Typically it
619  * is the same identifier as used for
620  * <literal>GETTEXT_PACKAGE</literal> in software configured according
621  * to GNU standards. The function first looks in the Windows Registry
622  * for the value <literal>#InstallationDirectory</literal> in the key
623  * <literal>#HKLM\Software\@package</literal>, and if that value
624  * exists and is a string, returns that.
625  *
626  * If @package is %NULL, or the above value isn't found in the
627  * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
628  * into the current process. Typically that would be the name of the
629  * DLL calling this function, looking for its installation
630  * directory. The function then asks Windows what directory that DLL
631  * was loaded from. If that directory's last component is "bin" or
632  * "lib", the parent directory is returned, otherwise the directory
633  * itself. If that DLL isn't loaded, the function proceeds as if
634  * @dll_name was %NULL.
635  *
636  * If both @package and @dll_name are %NULL, the directory from where
637  * the main executable of the process was loaded is uses instead in
638  * the same way as above.
639  *
640  * Returns: a string containing the installation directory for @package.
641  * The return value should be freed with g_free() when not needed any longer.
642  **/
643
644 gchar *
645 g_win32_get_package_installation_directory (gchar *package,
646                                             gchar *dll_name)
647 {
648   static GHashTable *package_dirs = NULL;
649   G_LOCK_DEFINE_STATIC (package_dirs);
650   gchar *result = NULL;
651   gchar *key;
652   HKEY reg_key = NULL;
653   DWORD type;
654   DWORD nbytes;
655
656   if (package != NULL)
657     {
658       G_LOCK (package_dirs);
659       
660       if (package_dirs == NULL)
661         package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
662       
663       result = g_hash_table_lookup (package_dirs, package);
664       
665       if (result && result[0])
666         {
667           G_UNLOCK (package_dirs);
668           return g_strdup (result);
669         }
670       
671       key = g_strconcat ("Software\\", package, NULL);
672       
673       nbytes = 0;
674       if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
675                          KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
676            && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
677                                &type, NULL, &nbytes) == ERROR_SUCCESS)
678           ||
679           ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
680                          KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
681            && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
682                                &type, NULL, &nbytes) == ERROR_SUCCESS)
683            && type == REG_SZ))
684         {
685           result = g_malloc (nbytes + 1);
686           RegQueryValueEx (reg_key, "InstallationDirectory", 0,
687                            &type, result, &nbytes);
688           result[nbytes] = '\0';
689         }
690
691       if (reg_key != NULL)
692         RegCloseKey (reg_key);
693       
694       g_free (key);
695
696       if (result)
697         {
698           g_hash_table_insert (package_dirs, package, result);
699           G_UNLOCK (package_dirs);
700           return g_strdup (result);
701         }
702       G_UNLOCK (package_dirs);
703     }
704
705   if (dll_name != NULL)
706     result = get_package_directory_from_module (dll_name);
707
708   if (result == NULL)
709     result = get_package_directory_from_module (NULL);
710
711   return result;
712 }
713
714 /**
715  * g_win32_get_package_installation_subdirectory:
716  * @package: An identifier for a software package, or %NULL.
717  * @dll_name: The name of a DLL that a package provides, or %NULL.
718  * @subdir: A subdirectory of the package installation directory.
719  *
720  * Returns a newly-allocated string containing the path of the
721  * subdirectory @subdir in the return value from calling
722  * g_win32_get_package_installation_directory() with the @package and
723  * @dll_name parameters. 
724  *
725  * Returns: a string containing the complete path to @subdir inside the 
726  * installation directory of @package. The return value should be freed with
727  * g_free() when no longer needed.
728  **/
729
730 gchar *
731 g_win32_get_package_installation_subdirectory (gchar *package,
732                                                gchar *dll_name,
733                                                gchar *subdir)
734 {
735   gchar *prefix;
736
737   prefix = g_win32_get_package_installation_directory (package, dll_name);
738
739   return g_build_filename (prefix, subdir, NULL);
740 }