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
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.
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.
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.
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/.
29 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
35 #include "glibconfig.h"
42 #define STRICT /* Strict typing, please */
57 #include <sys/cygwin.h>
63 g_win32_ftruncate (gint fd,
66 return _chsize (fd, size);
74 * The setlocale() function in the Microsoft C library uses locale
75 * names of the form "English_United States.1252" etc. We want the
76 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
77 * current thread locale from Windows - without any encoding info -
78 * and returns it as a string of the above form for use in forming
79 * file names etc. The returned string should be deallocated with
82 * Returns: newly-allocated locale name.
85 /* Borrowed from GNU gettext 0.13.1: */
86 /* Mingw headers don't have latest language and sublanguage codes. */
87 #ifndef LANG_AFRIKAANS
88 #define LANG_AFRIKAANS 0x36
91 #define LANG_ALBANIAN 0x1c
94 #define LANG_AMHARIC 0x5e
97 #define LANG_ARABIC 0x01
100 #define LANG_ARMENIAN 0x2b
102 #ifndef LANG_ASSAMESE
103 #define LANG_ASSAMESE 0x4d
106 #define LANG_AZERI 0x2c
109 #define LANG_BASQUE 0x2d
111 #ifndef LANG_BELARUSIAN
112 #define LANG_BELARUSIAN 0x23
115 #define LANG_BENGALI 0x45
118 #define LANG_BURMESE 0x55
120 #ifndef LANG_CAMBODIAN
121 #define LANG_CAMBODIAN 0x53
124 #define LANG_CATALAN 0x03
126 #ifndef LANG_CHEROKEE
127 #define LANG_CHEROKEE 0x5c
130 #define LANG_DIVEHI 0x65
133 #define LANG_EDO 0x66
135 #ifndef LANG_ESTONIAN
136 #define LANG_ESTONIAN 0x25
138 #ifndef LANG_FAEROESE
139 #define LANG_FAEROESE 0x38
142 #define LANG_FARSI 0x29
145 #define LANG_FRISIAN 0x62
147 #ifndef LANG_FULFULDE
148 #define LANG_FULFULDE 0x67
151 #define LANG_GAELIC 0x3c
153 #ifndef LANG_GALICIAN
154 #define LANG_GALICIAN 0x56
156 #ifndef LANG_GEORGIAN
157 #define LANG_GEORGIAN 0x37
160 #define LANG_GUARANI 0x74
162 #ifndef LANG_GUJARATI
163 #define LANG_GUJARATI 0x47
166 #define LANG_HAUSA 0x68
168 #ifndef LANG_HAWAIIAN
169 #define LANG_HAWAIIAN 0x75
172 #define LANG_HEBREW 0x0d
175 #define LANG_HINDI 0x39
178 #define LANG_IBIBIO 0x69
181 #define LANG_IGBO 0x70
183 #ifndef LANG_INDONESIAN
184 #define LANG_INDONESIAN 0x21
186 #ifndef LANG_INUKTITUT
187 #define LANG_INUKTITUT 0x5d
190 #define LANG_KANNADA 0x4b
193 #define LANG_KANURI 0x71
195 #ifndef LANG_KASHMIRI
196 #define LANG_KASHMIRI 0x60
199 #define LANG_KAZAK 0x3f
202 #define LANG_KONKANI 0x57
205 #define LANG_KYRGYZ 0x40
208 #define LANG_LAO 0x54
211 #define LANG_LATIN 0x76
214 #define LANG_LATVIAN 0x26
216 #ifndef LANG_LITHUANIAN
217 #define LANG_LITHUANIAN 0x27
219 #ifndef LANG_MACEDONIAN
220 #define LANG_MACEDONIAN 0x2f
223 #define LANG_MALAY 0x3e
225 #ifndef LANG_MALAYALAM
226 #define LANG_MALAYALAM 0x4c
229 #define LANG_MALTESE 0x3a
231 #ifndef LANG_MANIPURI
232 #define LANG_MANIPURI 0x58
235 #define LANG_MARATHI 0x4e
237 #ifndef LANG_MONGOLIAN
238 #define LANG_MONGOLIAN 0x50
241 #define LANG_NEPALI 0x61
244 #define LANG_ORIYA 0x48
247 #define LANG_OROMO 0x72
249 #ifndef LANG_PAPIAMENTU
250 #define LANG_PAPIAMENTU 0x79
253 #define LANG_PASHTO 0x63
256 #define LANG_PUNJABI 0x46
258 #ifndef LANG_RHAETO_ROMANCE
259 #define LANG_RHAETO_ROMANCE 0x17
262 #define LANG_SAAMI 0x3b
264 #ifndef LANG_SANSKRIT
265 #define LANG_SANSKRIT 0x4f
268 #define LANG_SERBIAN 0x1a
271 #define LANG_SINDHI 0x59
273 #ifndef LANG_SINHALESE
274 #define LANG_SINHALESE 0x5b
277 #define LANG_SLOVAK 0x1b
280 #define LANG_SOMALI 0x77
283 #define LANG_SORBIAN 0x2e
286 #define LANG_SUTU 0x30
289 #define LANG_SWAHILI 0x41
292 #define LANG_SYRIAC 0x5a
295 #define LANG_TAGALOG 0x64
298 #define LANG_TAJIK 0x28
300 #ifndef LANG_TAMAZIGHT
301 #define LANG_TAMAZIGHT 0x5f
304 #define LANG_TAMIL 0x49
307 #define LANG_TATAR 0x44
310 #define LANG_TELUGU 0x4a
313 #define LANG_THAI 0x1e
316 #define LANG_TIBETAN 0x51
318 #ifndef LANG_TIGRINYA
319 #define LANG_TIGRINYA 0x73
322 #define LANG_TSONGA 0x31
325 #define LANG_TSWANA 0x32
328 #define LANG_TURKMEN 0x42
330 #ifndef LANG_UKRAINIAN
331 #define LANG_UKRAINIAN 0x22
334 #define LANG_URDU 0x20
337 #define LANG_UZBEK 0x43
340 #define LANG_VENDA 0x33
342 #ifndef LANG_VIETNAMESE
343 #define LANG_VIETNAMESE 0x2a
346 #define LANG_WELSH 0x52
349 #define LANG_XHOSA 0x34
355 #define LANG_YIDDISH 0x3d
358 #define LANG_YORUBA 0x6a
361 #define LANG_ZULU 0x35
363 #ifndef SUBLANG_ARABIC_SAUDI_ARABIA
364 #define SUBLANG_ARABIC_SAUDI_ARABIA 0x01
366 #ifndef SUBLANG_ARABIC_IRAQ
367 #define SUBLANG_ARABIC_IRAQ 0x02
369 #ifndef SUBLANG_ARABIC_EGYPT
370 #define SUBLANG_ARABIC_EGYPT 0x03
372 #ifndef SUBLANG_ARABIC_LIBYA
373 #define SUBLANG_ARABIC_LIBYA 0x04
375 #ifndef SUBLANG_ARABIC_ALGERIA
376 #define SUBLANG_ARABIC_ALGERIA 0x05
378 #ifndef SUBLANG_ARABIC_MOROCCO
379 #define SUBLANG_ARABIC_MOROCCO 0x06
381 #ifndef SUBLANG_ARABIC_TUNISIA
382 #define SUBLANG_ARABIC_TUNISIA 0x07
384 #ifndef SUBLANG_ARABIC_OMAN
385 #define SUBLANG_ARABIC_OMAN 0x08
387 #ifndef SUBLANG_ARABIC_YEMEN
388 #define SUBLANG_ARABIC_YEMEN 0x09
390 #ifndef SUBLANG_ARABIC_SYRIA
391 #define SUBLANG_ARABIC_SYRIA 0x0a
393 #ifndef SUBLANG_ARABIC_JORDAN
394 #define SUBLANG_ARABIC_JORDAN 0x0b
396 #ifndef SUBLANG_ARABIC_LEBANON
397 #define SUBLANG_ARABIC_LEBANON 0x0c
399 #ifndef SUBLANG_ARABIC_KUWAIT
400 #define SUBLANG_ARABIC_KUWAIT 0x0d
402 #ifndef SUBLANG_ARABIC_UAE
403 #define SUBLANG_ARABIC_UAE 0x0e
405 #ifndef SUBLANG_ARABIC_BAHRAIN
406 #define SUBLANG_ARABIC_BAHRAIN 0x0f
408 #ifndef SUBLANG_ARABIC_QATAR
409 #define SUBLANG_ARABIC_QATAR 0x10
411 #ifndef SUBLANG_AZERI_LATIN
412 #define SUBLANG_AZERI_LATIN 0x01
414 #ifndef SUBLANG_AZERI_CYRILLIC
415 #define SUBLANG_AZERI_CYRILLIC 0x02
417 #ifndef SUBLANG_BENGALI_INDIA
418 #define SUBLANG_BENGALI_INDIA 0x00
420 #ifndef SUBLANG_BENGALI_BANGLADESH
421 #define SUBLANG_BENGALI_BANGLADESH 0x01
423 #ifndef SUBLANG_CHINESE_MACAU
424 #define SUBLANG_CHINESE_MACAU 0x05
426 #ifndef SUBLANG_ENGLISH_SOUTH_AFRICA
427 #define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07
429 #ifndef SUBLANG_ENGLISH_JAMAICA
430 #define SUBLANG_ENGLISH_JAMAICA 0x08
432 #ifndef SUBLANG_ENGLISH_CARIBBEAN
433 #define SUBLANG_ENGLISH_CARIBBEAN 0x09
435 #ifndef SUBLANG_ENGLISH_BELIZE
436 #define SUBLANG_ENGLISH_BELIZE 0x0a
438 #ifndef SUBLANG_ENGLISH_TRINIDAD
439 #define SUBLANG_ENGLISH_TRINIDAD 0x0b
441 #ifndef SUBLANG_ENGLISH_ZIMBABWE
442 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
444 #ifndef SUBLANG_ENGLISH_PHILIPPINES
445 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
447 #ifndef SUBLANG_ENGLISH_INDONESIA
448 #define SUBLANG_ENGLISH_INDONESIA 0x0e
450 #ifndef SUBLANG_ENGLISH_HONGKONG
451 #define SUBLANG_ENGLISH_HONGKONG 0x0f
453 #ifndef SUBLANG_ENGLISH_INDIA
454 #define SUBLANG_ENGLISH_INDIA 0x10
456 #ifndef SUBLANG_ENGLISH_MALAYSIA
457 #define SUBLANG_ENGLISH_MALAYSIA 0x11
459 #ifndef SUBLANG_ENGLISH_SINGAPORE
460 #define SUBLANG_ENGLISH_SINGAPORE 0x12
462 #ifndef SUBLANG_FRENCH_LUXEMBOURG
463 #define SUBLANG_FRENCH_LUXEMBOURG 0x05
465 #ifndef SUBLANG_FRENCH_MONACO
466 #define SUBLANG_FRENCH_MONACO 0x06
468 #ifndef SUBLANG_FRENCH_WESTINDIES
469 #define SUBLANG_FRENCH_WESTINDIES 0x07
471 #ifndef SUBLANG_FRENCH_REUNION
472 #define SUBLANG_FRENCH_REUNION 0x08
474 #ifndef SUBLANG_FRENCH_CONGO
475 #define SUBLANG_FRENCH_CONGO 0x09
477 #ifndef SUBLANG_FRENCH_SENEGAL
478 #define SUBLANG_FRENCH_SENEGAL 0x0a
480 #ifndef SUBLANG_FRENCH_CAMEROON
481 #define SUBLANG_FRENCH_CAMEROON 0x0b
483 #ifndef SUBLANG_FRENCH_COTEDIVOIRE
484 #define SUBLANG_FRENCH_COTEDIVOIRE 0x0c
486 #ifndef SUBLANG_FRENCH_MALI
487 #define SUBLANG_FRENCH_MALI 0x0d
489 #ifndef SUBLANG_FRENCH_MOROCCO
490 #define SUBLANG_FRENCH_MOROCCO 0x0e
492 #ifndef SUBLANG_FRENCH_HAITI
493 #define SUBLANG_FRENCH_HAITI 0x0f
495 #ifndef SUBLANG_GERMAN_LUXEMBOURG
496 #define SUBLANG_GERMAN_LUXEMBOURG 0x04
498 #ifndef SUBLANG_GERMAN_LIECHTENSTEIN
499 #define SUBLANG_GERMAN_LIECHTENSTEIN 0x05
501 #ifndef SUBLANG_KASHMIRI_INDIA
502 #define SUBLANG_KASHMIRI_INDIA 0x02
504 #ifndef SUBLANG_MALAY_MALAYSIA
505 #define SUBLANG_MALAY_MALAYSIA 0x01
507 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
508 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
510 #ifndef SUBLANG_NEPALI_INDIA
511 #define SUBLANG_NEPALI_INDIA 0x02
513 #ifndef SUBLANG_PUNJABI_INDIA
514 #define SUBLANG_PUNJABI_INDIA 0x00
516 #ifndef SUBLANG_PUNJABI_PAKISTAN
517 #define SUBLANG_PUNJABI_PAKISTAN 0x01
519 #ifndef SUBLANG_ROMANIAN_ROMANIA
520 #define SUBLANG_ROMANIAN_ROMANIA 0x00
522 #ifndef SUBLANG_ROMANIAN_MOLDOVA
523 #define SUBLANG_ROMANIAN_MOLDOVA 0x01
525 #ifndef SUBLANG_SERBIAN_LATIN
526 #define SUBLANG_SERBIAN_LATIN 0x02
528 #ifndef SUBLANG_SERBIAN_CYRILLIC
529 #define SUBLANG_SERBIAN_CYRILLIC 0x03
531 #ifndef SUBLANG_SINDHI_INDIA
532 #define SUBLANG_SINDHI_INDIA 0x00
534 #ifndef SUBLANG_SINDHI_PAKISTAN
535 #define SUBLANG_SINDHI_PAKISTAN 0x01
537 #ifndef SUBLANG_SPANISH_GUATEMALA
538 #define SUBLANG_SPANISH_GUATEMALA 0x04
540 #ifndef SUBLANG_SPANISH_COSTA_RICA
541 #define SUBLANG_SPANISH_COSTA_RICA 0x05
543 #ifndef SUBLANG_SPANISH_PANAMA
544 #define SUBLANG_SPANISH_PANAMA 0x06
546 #ifndef SUBLANG_SPANISH_DOMINICAN_REPUBLIC
547 #define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07
549 #ifndef SUBLANG_SPANISH_VENEZUELA
550 #define SUBLANG_SPANISH_VENEZUELA 0x08
552 #ifndef SUBLANG_SPANISH_COLOMBIA
553 #define SUBLANG_SPANISH_COLOMBIA 0x09
555 #ifndef SUBLANG_SPANISH_PERU
556 #define SUBLANG_SPANISH_PERU 0x0a
558 #ifndef SUBLANG_SPANISH_ARGENTINA
559 #define SUBLANG_SPANISH_ARGENTINA 0x0b
561 #ifndef SUBLANG_SPANISH_ECUADOR
562 #define SUBLANG_SPANISH_ECUADOR 0x0c
564 #ifndef SUBLANG_SPANISH_CHILE
565 #define SUBLANG_SPANISH_CHILE 0x0d
567 #ifndef SUBLANG_SPANISH_URUGUAY
568 #define SUBLANG_SPANISH_URUGUAY 0x0e
570 #ifndef SUBLANG_SPANISH_PARAGUAY
571 #define SUBLANG_SPANISH_PARAGUAY 0x0f
573 #ifndef SUBLANG_SPANISH_BOLIVIA
574 #define SUBLANG_SPANISH_BOLIVIA 0x10
576 #ifndef SUBLANG_SPANISH_EL_SALVADOR
577 #define SUBLANG_SPANISH_EL_SALVADOR 0x11
579 #ifndef SUBLANG_SPANISH_HONDURAS
580 #define SUBLANG_SPANISH_HONDURAS 0x12
582 #ifndef SUBLANG_SPANISH_NICARAGUA
583 #define SUBLANG_SPANISH_NICARAGUA 0x13
585 #ifndef SUBLANG_SPANISH_PUERTO_RICO
586 #define SUBLANG_SPANISH_PUERTO_RICO 0x14
588 #ifndef SUBLANG_SWEDISH_FINLAND
589 #define SUBLANG_SWEDISH_FINLAND 0x02
591 #ifndef SUBLANG_TAMAZIGHT_ARABIC
592 #define SUBLANG_TAMAZIGHT_ARABIC 0x01
594 #ifndef SUBLANG_TAMAZIGHT_LATIN
595 #define SUBLANG_TAMAZIGHT_LATIN 0x02
597 #ifndef SUBLANG_TIGRINYA_ETHIOPIA
598 #define SUBLANG_TIGRINYA_ETHIOPIA 0x00
600 #ifndef SUBLANG_TIGRINYA_ERITREA
601 #define SUBLANG_TIGRINYA_ERITREA 0x01
603 #ifndef SUBLANG_URDU_PAKISTAN
604 #define SUBLANG_URDU_PAKISTAN 0x01
606 #ifndef SUBLANG_URDU_INDIA
607 #define SUBLANG_URDU_INDIA 0x02
609 #ifndef SUBLANG_UZBEK_LATIN
610 #define SUBLANG_UZBEK_LATIN 0x01
612 #ifndef SUBLANG_UZBEK_CYRILLIC
613 #define SUBLANG_UZBEK_CYRILLIC 0x02
617 g_win32_getlocale (void)
623 gchar *l = "C", *sl = NULL;
626 /* Let the user override the system settings through environment
627 variables, as on POSIX systems. */
628 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
629 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
630 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
631 return g_strdup (ev);
633 /* Use native Win32 API locale ID. */
634 lcid = GetThreadLocale ();
636 /* Strip off the sorting rules, keep only the language part. */
637 langid = LANGIDFROMLCID (lcid);
639 /* Split into language and territory part. */
640 primary = PRIMARYLANGID (langid);
641 sub = SUBLANGID (langid);
644 case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
645 case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
650 case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
651 case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
652 case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
653 case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
654 case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
655 case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
656 case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
657 case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
658 case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
659 case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
660 case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
661 case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
662 case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
663 case SUBLANG_ARABIC_UAE: sl = "AE"; break;
664 case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
665 case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
668 case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
669 case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
674 /* FIXME: Adjust this when Azerbaijani locales appear on Unix. */
675 case SUBLANG_AZERI_LATIN: sl = "AZ@latin"; break;
676 case SUBLANG_AZERI_CYRILLIC: sl = "AZ@cyrillic"; break;
680 l = "eu"; /* sl could be "ES" or "FR". */
682 case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
687 case SUBLANG_BENGALI_INDIA: sl = "IN"; break;
688 case SUBLANG_BENGALI_BANGLADESH: sl = "BD"; break;
691 case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
692 case LANG_BURMESE: l = "my"; sl = "MM"; break;
693 case LANG_CAMBODIAN: l = "km"; sl = "KH"; break;
694 case LANG_CATALAN: l = "ca"; sl = "ES"; break;
699 case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
700 case SUBLANG_CHINESE_SIMPLIFIED: sl = "CN"; break;
701 case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
702 case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
703 case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
706 case LANG_CROATIAN: /* LANG_CROATIAN == LANG_SERBIAN */
709 /* FIXME: How to distinguish Croatian and Latin Serbian locales? */
710 case SUBLANG_SERBIAN_LATIN: l = "sr"; sl = "@Latn"; break;
711 case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
712 default: l = "hr"; sl = "HR";
715 case LANG_CZECH: l = "cs"; sl = "CZ"; break;
716 case LANG_DANISH: l = "da"; sl = "DK"; break;
717 case LANG_DIVEHI: l = "div"; sl = "MV"; break;
722 case SUBLANG_DUTCH: sl = "NL"; break;
723 case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
730 case SUBLANG_ENGLISH_US: sl = "US"; break;
731 case SUBLANG_ENGLISH_UK: sl = "GB"; break;
732 case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
733 case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
734 case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
735 case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
736 case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
737 case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
738 case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
739 case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
740 case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
741 case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
742 case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
743 case SUBLANG_ENGLISH_INDONESIA: sl = "ID"; break;
744 case SUBLANG_ENGLISH_HONGKONG: sl = "HK"; break;
745 case SUBLANG_ENGLISH_INDIA: sl = "IN"; break;
746 case SUBLANG_ENGLISH_MALAYSIA: sl = "MY"; break;
747 case SUBLANG_ENGLISH_SINGAPORE: sl = "SG"; break;
750 case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
751 case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
752 case LANG_FARSI: l = "fa"; sl = "IR"; break;
753 case LANG_FINNISH: l = "fi"; sl = "FI"; break;
758 case SUBLANG_FRENCH: sl = "FR"; break;
759 case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
760 case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
761 case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
762 case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
763 case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
764 case SUBLANG_FRENCH_WESTINDIES: break;
765 case SUBLANG_FRENCH_REUNION: sl = "RE"; break;
766 case SUBLANG_FRENCH_CONGO: sl = "CG"; break;
767 case SUBLANG_FRENCH_SENEGAL: sl = "SN"; break;
768 case SUBLANG_FRENCH_CAMEROON: sl = "CM"; break;
769 case SUBLANG_FRENCH_COTEDIVOIRE: sl = "CI"; break;
770 case SUBLANG_FRENCH_MALI: sl = "ML"; break;
771 case SUBLANG_FRENCH_MOROCCO: sl = "MA"; break;
772 case SUBLANG_FRENCH_HAITI: sl = "HT"; break;
775 case LANG_FRISIAN: l = "fy"; sl ="NL"; break;
776 case LANG_FULFULDE: l = "ful"; sl = "NG"; break;
780 case 0x01: /* SCOTTISH */ l = "gd"; sl = "GB"; break;
781 case 0x02: /* IRISH */ l = "ga"; sl = "IE"; break;
784 case LANG_GALICIAN: l = "gl"; sl = "ES"; break;
785 case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
790 case SUBLANG_GERMAN: sl = "DE"; break;
791 case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
792 case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
793 case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
794 case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
797 case LANG_GREEK: l = "el"; sl = "GR"; break;
798 case LANG_GUARANI: l = "gn"; sl = "PY"; break;
799 case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
800 case LANG_HAUSA: l = "ha"; sl = "NG"; break;
802 /* FIXME: Do they mean Hawaiian ("haw_US", 1000 speakers)
803 * or Hawaii Creole English ("cpe_US", 600000 speakers)?
808 case LANG_HEBREW: l = "he"; sl = "IL"; break;
809 case LANG_HINDI: l = "hi"; sl = "IN"; break;
810 case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
811 case LANG_IBIBIO: l = "nic"; sl = "NG"; break;
812 case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
813 case LANG_IGBO: l = "ibo"; sl = "NG"; break;
814 case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
815 case LANG_INUKTITUT: l = "iu"; sl = "CA"; break;
820 case SUBLANG_ITALIAN: sl = "IT"; break;
821 case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
824 case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
825 case LANG_KANNADA: l = "kn"; sl = "IN"; break;
826 case LANG_KANURI: l = "kau"; sl = "NG"; break;
831 case SUBLANG_DEFAULT: sl = "PK"; break;
832 case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
835 case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
837 /* FIXME: Adjust this when such locales appear on Unix. */
841 case LANG_KOREAN: l = "ko"; sl = "KR"; break;
842 case LANG_KYRGYZ: l = "ky"; sl = "KG"; break;
843 case LANG_LAO: l = "lo"; sl = "LA"; break;
844 case LANG_LATIN: l = "la"; sl = "VA"; break;
845 case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
846 case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
847 case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
852 case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
853 case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
856 case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
858 /* FIXME: Adjust this when such locales appear on Unix. */
862 case LANG_MARATHI: l = "mr"; sl = "IN"; break;
864 /* Ambiguous: could be "mn_CN" or "mn_MN". */
871 case SUBLANG_DEFAULT: sl = "NP"; break;
872 case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
879 case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
880 case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
883 case LANG_ORIYA: l = "or"; sl = "IN"; break;
884 case LANG_OROMO: l = "om"; sl = "ET"; break;
885 case LANG_PAPIAMENTU: l = "pap"; sl = "AN"; break;
887 /* Ambiguous: could be "ps_PK" or "ps_AF". */
890 case LANG_POLISH: l = "pl"; sl = "PL"; break;
891 case LANG_PORTUGUESE:
895 case SUBLANG_PORTUGUESE: sl = "PT"; break;
896 case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
903 case SUBLANG_PUNJABI_INDIA: sl = "IN"; break; /* Gurmukhi script */
904 case SUBLANG_PUNJABI_PAKISTAN: sl = "PK"; break; /* Arabic script */
907 case LANG_RHAETO_ROMANCE: l = "rm"; sl = "CH"; break;
912 case SUBLANG_ROMANIAN_ROMANIA: sl = "RO"; break;
913 case SUBLANG_ROMANIAN_MOLDOVA: sl = "MD"; break;
917 l = "ru";/* Ambiguous: could be "ru_RU" or "ru_UA" or "ru_MD". */
919 case LANG_SAAMI: /* actually Northern Sami */ l = "se"; sl = "NO"; break;
920 case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
921 case LANG_SINDHI: l = "sd";
924 case SUBLANG_SINDHI_INDIA: sl = "IN"; break;
925 case SUBLANG_SINDHI_PAKISTAN: sl = "PK"; break;
928 case LANG_SINHALESE: l = "si"; sl = "LK"; break;
929 case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
930 case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
931 case LANG_SOMALI: l = "so"; sl = "SO"; break;
933 /* FIXME: Adjust this when such locales appear on Unix. */
941 case SUBLANG_SPANISH: sl = "ES"; break;
942 case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
943 case SUBLANG_SPANISH_MODERN:
944 sl = "ES@modern"; break; /* not seen on Unix */
945 case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
946 case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
947 case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
948 case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
949 case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
950 case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
951 case SUBLANG_SPANISH_PERU: sl = "PE"; break;
952 case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
953 case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
954 case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
955 case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
956 case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
957 case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
958 case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
959 case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
960 case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
961 case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
964 case LANG_SUTU: l = "bnt"; sl = "TZ"; break; /* or "st_LS" or "nso_ZA"? */
965 case LANG_SWAHILI: l = "sw"; sl = "KE"; break;
970 case SUBLANG_DEFAULT: sl = "SE"; break;
971 case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
974 case LANG_SYRIAC: l = "syr"; sl = "TR"; break; /* An extinct language. */
975 case LANG_TAGALOG: l = "tl"; sl = "PH"; break;
976 case LANG_TAJIK: l = "tg"; sl = "TJ"; break;
978 l = "ta"; /* Ambiguous: could be "ta_IN" or "ta_LK" or "ta_SG". */
980 case LANG_TATAR: l = "tt"; sl = "RU"; break;
981 case LANG_TELUGU: l = "te"; sl = "IN"; break;
982 case LANG_THAI: l = "th"; sl = "TH"; break;
983 case LANG_TIBETAN: l = "bo"; sl = "CN"; break;
988 case SUBLANG_TIGRINYA_ETHIOPIA: sl = "ET"; break;
989 case SUBLANG_TIGRINYA_ERITREA: sl = "ER"; break;
992 case LANG_TSONGA: l = "ts"; sl = "ZA"; break;
993 case LANG_TSWANA: l = "tn"; sl = "BW"; break;
994 case LANG_TURKISH: l = "tr"; sl = "TR"; break;
995 case LANG_TURKMEN: l = "tk"; sl = "TM"; break;
996 case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
1001 case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
1002 case SUBLANG_URDU_INDIA: sl = "IN"; break;
1009 case SUBLANG_UZBEK_LATIN: sl = "UZ"; break;
1010 case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
1014 /* FIXME: It's not clear whether Venda has the ISO 639-2 two-letter code
1016 http://www.loc.gov/standards/iso639-2/englangn.html has it, but
1017 http://lcweb.loc.gov/standards/iso639-2/codechanges.html doesn't, */
1018 l = "ven"; /* or "ve"? */
1021 case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
1022 case LANG_WELSH: l = "cy"; sl = "GB"; break;
1023 case LANG_XHOSA: l = "xh"; sl = "ZA"; break;
1024 case LANG_YI: l = "sit"; sl = "CN"; break;
1025 case LANG_YIDDISH: l = "yi"; sl = "IL"; break;
1026 case LANG_YORUBA: l = "yo"; sl = "NG"; break;
1027 case LANG_ZULU: l = "zu"; sl = "ZA"; break;
1037 return g_strdup (bfr);
1041 * g_win32_error_message:
1042 * @error: error code.
1044 * Translate a Win32 error code (as returned by GetLastError()) into
1045 * the corresponding message. The message is either language neutral,
1046 * or in the thread's language, or the user's language, the system's
1047 * language, or US English (see docs for FormatMessage()). The
1048 * returned string is in UTF-8. It should be deallocated with
1051 * Returns: newly-allocated error message
1054 g_win32_error_message (gint error)
1060 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
1061 |FORMAT_MESSAGE_IGNORE_INSERTS
1062 |FORMAT_MESSAGE_FROM_SYSTEM,
1064 (LPTSTR) &msg, 0, NULL);
1067 nbytes = strlen (msg);
1069 if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
1070 msg[nbytes-2] = '\0';
1072 retval = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1077 retval = g_strdup ("");
1083 get_package_directory_from_module (gchar *module_name)
1085 static GHashTable *module_dirs = NULL;
1086 G_LOCK_DEFINE_STATIC (module_dirs);
1087 HMODULE hmodule = NULL;
1092 G_LOCK (module_dirs);
1094 if (module_dirs == NULL)
1095 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
1097 result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
1101 G_UNLOCK (module_dirs);
1102 return g_strdup (result);
1107 hmodule = GetModuleHandle (module_name);
1112 fn = g_malloc (MAX_PATH);
1113 if (!GetModuleFileName (hmodule, fn, MAX_PATH))
1115 G_UNLOCK (module_dirs);
1120 if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
1123 p = strrchr (fn, G_DIR_SEPARATOR);
1124 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
1125 g_ascii_strcasecmp (p + 1, "lib") == 0))
1128 #ifdef G_WITH_CYGWIN
1129 /* In Cygwin we need to have POSIX paths */
1131 gchar tmp[MAX_PATH];
1133 cygwin_conv_to_posix_path(fn, tmp);
1139 g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
1141 G_UNLOCK (module_dirs);
1143 return g_strdup (fn);
1147 * g_win32_get_package_installation_directory:
1148 * @package: An identifier for a software package, or %NULL
1149 * @dll_name: The name of a DLL that a package provides, or %NULL.
1151 * Try to determine the installation directory for a software package.
1152 * Typically used by GNU software packages.
1154 * @package should be a short identifier for the package. Typically it
1155 * is the same identifier as used for
1156 * <literal>GETTEXT_PACKAGE</literal> in software configured according
1157 * to GNU standards. The function first looks in the Windows Registry
1158 * for the value <literal>#InstallationDirectory</literal> in the key
1159 * <literal>#HKLM\Software\@package</literal>, and if that value
1160 * exists and is a string, returns that.
1162 * If @package is %NULL, or the above value isn't found in the
1163 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
1164 * into the current process. Typically that would be the name of the
1165 * DLL calling this function, looking for its installation
1166 * directory. The function then asks Windows what directory that DLL
1167 * was loaded from. If that directory's last component is "bin" or
1168 * "lib", the parent directory is returned, otherwise the directory
1169 * itself. If that DLL isn't loaded, the function proceeds as if
1170 * @dll_name was %NULL.
1172 * If both @package and @dll_name are %NULL, the directory from where
1173 * the main executable of the process was loaded is uses instead in
1174 * the same way as above.
1176 * Returns: a string containing the installation directory for @package.
1177 * The return value should be freed with g_free() when not needed any longer.
1181 g_win32_get_package_installation_directory (gchar *package,
1184 static GHashTable *package_dirs = NULL;
1185 G_LOCK_DEFINE_STATIC (package_dirs);
1186 gchar *result = NULL;
1188 HKEY reg_key = NULL;
1192 if (package != NULL)
1194 G_LOCK (package_dirs);
1196 if (package_dirs == NULL)
1197 package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
1199 result = g_hash_table_lookup (package_dirs, package);
1201 if (result && result[0])
1203 G_UNLOCK (package_dirs);
1204 return g_strdup (result);
1207 key = g_strconcat ("Software\\", package, NULL);
1210 if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
1211 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
1212 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
1213 &type, NULL, &nbytes) == ERROR_SUCCESS)
1215 ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
1216 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
1217 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
1218 &type, NULL, &nbytes) == ERROR_SUCCESS)
1221 result = g_malloc (nbytes + 1);
1222 RegQueryValueEx (reg_key, "InstallationDirectory", 0,
1223 &type, result, &nbytes);
1224 result[nbytes] = '\0';
1227 if (reg_key != NULL)
1228 RegCloseKey (reg_key);
1234 g_hash_table_insert (package_dirs, package, result);
1235 G_UNLOCK (package_dirs);
1236 return g_strdup (result);
1238 G_UNLOCK (package_dirs);
1241 if (dll_name != NULL)
1242 result = get_package_directory_from_module (dll_name);
1245 result = get_package_directory_from_module (NULL);
1251 * g_win32_get_package_installation_subdirectory:
1252 * @package: An identifier for a software package, or %NULL.
1253 * @dll_name: The name of a DLL that a package provides, or %NULL.
1254 * @subdir: A subdirectory of the package installation directory.
1256 * Returns a newly-allocated string containing the path of the
1257 * subdirectory @subdir in the return value from calling
1258 * g_win32_get_package_installation_directory() with the @package and
1259 * @dll_name parameters.
1261 * Returns: a string containing the complete path to @subdir inside the
1262 * installation directory of @package. The return value should be freed with
1263 * g_free() when no longer needed.
1267 g_win32_get_package_installation_subdirectory (gchar *package,
1274 prefix = g_win32_get_package_installation_directory (package, dll_name);
1276 dirname = g_build_filename (prefix, subdir, NULL);
1282 static guint windows_version;
1285 g_win32_windows_version_init (void)
1287 static gboolean beenhere = FALSE;
1292 if (getenv ("G_WIN32_PRETEND_WIN9X"))
1293 windows_version = 0x80000004;
1295 windows_version = GetVersion ();
1300 _g_win32_thread_init (void)
1302 g_win32_windows_version_init ();
1306 * g_win32_get_windows_version:
1308 * Returns version information for the Windows operating system the
1309 * code is running on. See MSDN documentation for the GetVersion()
1310 * function. To summarize, the most significant bit is one on Win9x,
1311 * and zero on NT-based systems. The least significant byte is 4 on
1312 * Windows NT 4, 5 on Windows XP. Software that needs really detailled
1313 * version and feature information should use Win32 API like
1314 * GetVersionEx() and VerifyVersionInfo().
1316 * If there is an environment variable G_WIN32_PRETEND_WIN9X defined
1317 * (with any value), this function always returns a version code for
1318 * Windows 9x. This is mainly an internal debugging aid for GTK and
1319 * GLib developers, to be able to check the code paths for Win9x.
1321 * Returns: The version information.
1324 g_win32_get_windows_version (void)
1326 g_win32_windows_version_init ();
1328 return windows_version;