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.
34 #include "glibconfig.h"
41 #define STRICT /* Strict typing, please */
56 #include <sys/cygwin.h>
62 g_win32_ftruncate (gint fd,
68 g_return_val_if_fail (fd >= 0, -1);
70 hfile = (HANDLE) _get_osfhandle (fd);
71 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
72 if (curpos == 0xFFFFFFFF
73 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
74 || !SetEndOfFile (hfile))
76 gint error = GetLastError ();
80 case ERROR_INVALID_HANDLE:
99 * The setlocale in the Microsoft C library uses locale names of the
100 * form "English_United States.1252" etc. We want the UNIXish standard
101 * form "en_US", "zh_TW" etc. This function gets the current thread
102 * locale from Windows - without any encoding info - and returns it as
103 * a string of the above form for use in forming file names etc. The
104 * returned string should be deallocated with g_free().
106 * Returns: newly-allocated locale name.
109 /* Borrowed from GNU gettext 0.13.1: */
110 /* Mingw headers don't have latest language and sublanguage codes. */
111 #ifndef LANG_AFRIKAANS
112 #define LANG_AFRIKAANS 0x36
114 #ifndef LANG_ALBANIAN
115 #define LANG_ALBANIAN 0x1c
118 #define LANG_AMHARIC 0x5e
121 #define LANG_ARABIC 0x01
123 #ifndef LANG_ARMENIAN
124 #define LANG_ARMENIAN 0x2b
126 #ifndef LANG_ASSAMESE
127 #define LANG_ASSAMESE 0x4d
130 #define LANG_AZERI 0x2c
133 #define LANG_BASQUE 0x2d
135 #ifndef LANG_BELARUSIAN
136 #define LANG_BELARUSIAN 0x23
139 #define LANG_BENGALI 0x45
142 #define LANG_BURMESE 0x55
144 #ifndef LANG_CAMBODIAN
145 #define LANG_CAMBODIAN 0x53
148 #define LANG_CATALAN 0x03
150 #ifndef LANG_CHEROKEE
151 #define LANG_CHEROKEE 0x5c
154 #define LANG_DIVEHI 0x65
157 #define LANG_EDO 0x66
159 #ifndef LANG_ESTONIAN
160 #define LANG_ESTONIAN 0x25
162 #ifndef LANG_FAEROESE
163 #define LANG_FAEROESE 0x38
166 #define LANG_FARSI 0x29
169 #define LANG_FRISIAN 0x62
171 #ifndef LANG_FULFULDE
172 #define LANG_FULFULDE 0x67
175 #define LANG_GAELIC 0x3c
177 #ifndef LANG_GALICIAN
178 #define LANG_GALICIAN 0x56
180 #ifndef LANG_GEORGIAN
181 #define LANG_GEORGIAN 0x37
184 #define LANG_GUARANI 0x74
186 #ifndef LANG_GUJARATI
187 #define LANG_GUJARATI 0x47
190 #define LANG_HAUSA 0x68
192 #ifndef LANG_HAWAIIAN
193 #define LANG_HAWAIIAN 0x75
196 #define LANG_HEBREW 0x0d
199 #define LANG_HINDI 0x39
202 #define LANG_IBIBIO 0x69
205 #define LANG_IGBO 0x70
207 #ifndef LANG_INDONESIAN
208 #define LANG_INDONESIAN 0x21
210 #ifndef LANG_INUKTITUT
211 #define LANG_INUKTITUT 0x5d
214 #define LANG_KANNADA 0x4b
217 #define LANG_KANURI 0x71
219 #ifndef LANG_KASHMIRI
220 #define LANG_KASHMIRI 0x60
223 #define LANG_KAZAK 0x3f
226 #define LANG_KONKANI 0x57
229 #define LANG_KYRGYZ 0x40
232 #define LANG_LAO 0x54
235 #define LANG_LATIN 0x76
238 #define LANG_LATVIAN 0x26
240 #ifndef LANG_LITHUANIAN
241 #define LANG_LITHUANIAN 0x27
243 #ifndef LANG_MACEDONIAN
244 #define LANG_MACEDONIAN 0x2f
247 #define LANG_MALAY 0x3e
249 #ifndef LANG_MALAYALAM
250 #define LANG_MALAYALAM 0x4c
253 #define LANG_MALTESE 0x3a
255 #ifndef LANG_MANIPURI
256 #define LANG_MANIPURI 0x58
259 #define LANG_MARATHI 0x4e
261 #ifndef LANG_MONGOLIAN
262 #define LANG_MONGOLIAN 0x50
265 #define LANG_NEPALI 0x61
268 #define LANG_ORIYA 0x48
271 #define LANG_OROMO 0x72
273 #ifndef LANG_PAPIAMENTU
274 #define LANG_PAPIAMENTU 0x79
277 #define LANG_PASHTO 0x63
280 #define LANG_PUNJABI 0x46
282 #ifndef LANG_RHAETO_ROMANCE
283 #define LANG_RHAETO_ROMANCE 0x17
286 #define LANG_SAAMI 0x3b
288 #ifndef LANG_SANSKRIT
289 #define LANG_SANSKRIT 0x4f
292 #define LANG_SERBIAN 0x1a
295 #define LANG_SINDHI 0x59
297 #ifndef LANG_SINHALESE
298 #define LANG_SINHALESE 0x5b
301 #define LANG_SLOVAK 0x1b
304 #define LANG_SOMALI 0x77
307 #define LANG_SORBIAN 0x2e
310 #define LANG_SUTU 0x30
313 #define LANG_SWAHILI 0x41
316 #define LANG_SYRIAC 0x5a
319 #define LANG_TAGALOG 0x64
322 #define LANG_TAJIK 0x28
324 #ifndef LANG_TAMAZIGHT
325 #define LANG_TAMAZIGHT 0x5f
328 #define LANG_TAMIL 0x49
331 #define LANG_TATAR 0x44
334 #define LANG_TELUGU 0x4a
337 #define LANG_THAI 0x1e
340 #define LANG_TIBETAN 0x51
342 #ifndef LANG_TIGRINYA
343 #define LANG_TIGRINYA 0x73
346 #define LANG_TSONGA 0x31
349 #define LANG_TSWANA 0x32
352 #define LANG_TURKMEN 0x42
354 #ifndef LANG_UKRAINIAN
355 #define LANG_UKRAINIAN 0x22
358 #define LANG_URDU 0x20
361 #define LANG_UZBEK 0x43
364 #define LANG_VENDA 0x33
366 #ifndef LANG_VIETNAMESE
367 #define LANG_VIETNAMESE 0x2a
370 #define LANG_WELSH 0x52
373 #define LANG_XHOSA 0x34
379 #define LANG_YIDDISH 0x3d
382 #define LANG_YORUBA 0x6a
385 #define LANG_ZULU 0x35
387 #ifndef SUBLANG_ARABIC_SAUDI_ARABIA
388 #define SUBLANG_ARABIC_SAUDI_ARABIA 0x01
390 #ifndef SUBLANG_ARABIC_IRAQ
391 #define SUBLANG_ARABIC_IRAQ 0x02
393 #ifndef SUBLANG_ARABIC_EGYPT
394 #define SUBLANG_ARABIC_EGYPT 0x03
396 #ifndef SUBLANG_ARABIC_LIBYA
397 #define SUBLANG_ARABIC_LIBYA 0x04
399 #ifndef SUBLANG_ARABIC_ALGERIA
400 #define SUBLANG_ARABIC_ALGERIA 0x05
402 #ifndef SUBLANG_ARABIC_MOROCCO
403 #define SUBLANG_ARABIC_MOROCCO 0x06
405 #ifndef SUBLANG_ARABIC_TUNISIA
406 #define SUBLANG_ARABIC_TUNISIA 0x07
408 #ifndef SUBLANG_ARABIC_OMAN
409 #define SUBLANG_ARABIC_OMAN 0x08
411 #ifndef SUBLANG_ARABIC_YEMEN
412 #define SUBLANG_ARABIC_YEMEN 0x09
414 #ifndef SUBLANG_ARABIC_SYRIA
415 #define SUBLANG_ARABIC_SYRIA 0x0a
417 #ifndef SUBLANG_ARABIC_JORDAN
418 #define SUBLANG_ARABIC_JORDAN 0x0b
420 #ifndef SUBLANG_ARABIC_LEBANON
421 #define SUBLANG_ARABIC_LEBANON 0x0c
423 #ifndef SUBLANG_ARABIC_KUWAIT
424 #define SUBLANG_ARABIC_KUWAIT 0x0d
426 #ifndef SUBLANG_ARABIC_UAE
427 #define SUBLANG_ARABIC_UAE 0x0e
429 #ifndef SUBLANG_ARABIC_BAHRAIN
430 #define SUBLANG_ARABIC_BAHRAIN 0x0f
432 #ifndef SUBLANG_ARABIC_QATAR
433 #define SUBLANG_ARABIC_QATAR 0x10
435 #ifndef SUBLANG_AZERI_LATIN
436 #define SUBLANG_AZERI_LATIN 0x01
438 #ifndef SUBLANG_AZERI_CYRILLIC
439 #define SUBLANG_AZERI_CYRILLIC 0x02
441 #ifndef SUBLANG_BENGALI_INDIA
442 #define SUBLANG_BENGALI_INDIA 0x00
444 #ifndef SUBLANG_BENGALI_BANGLADESH
445 #define SUBLANG_BENGALI_BANGLADESH 0x01
447 #ifndef SUBLANG_CHINESE_MACAU
448 #define SUBLANG_CHINESE_MACAU 0x05
450 #ifndef SUBLANG_ENGLISH_SOUTH_AFRICA
451 #define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07
453 #ifndef SUBLANG_ENGLISH_JAMAICA
454 #define SUBLANG_ENGLISH_JAMAICA 0x08
456 #ifndef SUBLANG_ENGLISH_CARIBBEAN
457 #define SUBLANG_ENGLISH_CARIBBEAN 0x09
459 #ifndef SUBLANG_ENGLISH_BELIZE
460 #define SUBLANG_ENGLISH_BELIZE 0x0a
462 #ifndef SUBLANG_ENGLISH_TRINIDAD
463 #define SUBLANG_ENGLISH_TRINIDAD 0x0b
465 #ifndef SUBLANG_ENGLISH_ZIMBABWE
466 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
468 #ifndef SUBLANG_ENGLISH_PHILIPPINES
469 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
471 #ifndef SUBLANG_ENGLISH_INDONESIA
472 #define SUBLANG_ENGLISH_INDONESIA 0x0e
474 #ifndef SUBLANG_ENGLISH_HONGKONG
475 #define SUBLANG_ENGLISH_HONGKONG 0x0f
477 #ifndef SUBLANG_ENGLISH_INDIA
478 #define SUBLANG_ENGLISH_INDIA 0x10
480 #ifndef SUBLANG_ENGLISH_MALAYSIA
481 #define SUBLANG_ENGLISH_MALAYSIA 0x11
483 #ifndef SUBLANG_ENGLISH_SINGAPORE
484 #define SUBLANG_ENGLISH_SINGAPORE 0x12
486 #ifndef SUBLANG_FRENCH_LUXEMBOURG
487 #define SUBLANG_FRENCH_LUXEMBOURG 0x05
489 #ifndef SUBLANG_FRENCH_MONACO
490 #define SUBLANG_FRENCH_MONACO 0x06
492 #ifndef SUBLANG_FRENCH_WESTINDIES
493 #define SUBLANG_FRENCH_WESTINDIES 0x07
495 #ifndef SUBLANG_FRENCH_REUNION
496 #define SUBLANG_FRENCH_REUNION 0x08
498 #ifndef SUBLANG_FRENCH_CONGO
499 #define SUBLANG_FRENCH_CONGO 0x09
501 #ifndef SUBLANG_FRENCH_SENEGAL
502 #define SUBLANG_FRENCH_SENEGAL 0x0a
504 #ifndef SUBLANG_FRENCH_CAMEROON
505 #define SUBLANG_FRENCH_CAMEROON 0x0b
507 #ifndef SUBLANG_FRENCH_COTEDIVOIRE
508 #define SUBLANG_FRENCH_COTEDIVOIRE 0x0c
510 #ifndef SUBLANG_FRENCH_MALI
511 #define SUBLANG_FRENCH_MALI 0x0d
513 #ifndef SUBLANG_FRENCH_MOROCCO
514 #define SUBLANG_FRENCH_MOROCCO 0x0e
516 #ifndef SUBLANG_FRENCH_HAITI
517 #define SUBLANG_FRENCH_HAITI 0x0f
519 #ifndef SUBLANG_GERMAN_LUXEMBOURG
520 #define SUBLANG_GERMAN_LUXEMBOURG 0x04
522 #ifndef SUBLANG_GERMAN_LIECHTENSTEIN
523 #define SUBLANG_GERMAN_LIECHTENSTEIN 0x05
525 #ifndef SUBLANG_KASHMIRI_INDIA
526 #define SUBLANG_KASHMIRI_INDIA 0x02
528 #ifndef SUBLANG_MALAY_MALAYSIA
529 #define SUBLANG_MALAY_MALAYSIA 0x01
531 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
532 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
534 #ifndef SUBLANG_NEPALI_INDIA
535 #define SUBLANG_NEPALI_INDIA 0x02
537 #ifndef SUBLANG_PUNJABI_INDIA
538 #define SUBLANG_PUNJABI_INDIA 0x00
540 #ifndef SUBLANG_PUNJABI_PAKISTAN
541 #define SUBLANG_PUNJABI_PAKISTAN 0x01
543 #ifndef SUBLANG_ROMANIAN_ROMANIA
544 #define SUBLANG_ROMANIAN_ROMANIA 0x00
546 #ifndef SUBLANG_ROMANIAN_MOLDOVA
547 #define SUBLANG_ROMANIAN_MOLDOVA 0x01
549 #ifndef SUBLANG_SERBIAN_LATIN
550 #define SUBLANG_SERBIAN_LATIN 0x02
552 #ifndef SUBLANG_SERBIAN_CYRILLIC
553 #define SUBLANG_SERBIAN_CYRILLIC 0x03
555 #ifndef SUBLANG_SINDHI_INDIA
556 #define SUBLANG_SINDHI_INDIA 0x00
558 #ifndef SUBLANG_SINDHI_PAKISTAN
559 #define SUBLANG_SINDHI_PAKISTAN 0x01
561 #ifndef SUBLANG_SPANISH_GUATEMALA
562 #define SUBLANG_SPANISH_GUATEMALA 0x04
564 #ifndef SUBLANG_SPANISH_COSTA_RICA
565 #define SUBLANG_SPANISH_COSTA_RICA 0x05
567 #ifndef SUBLANG_SPANISH_PANAMA
568 #define SUBLANG_SPANISH_PANAMA 0x06
570 #ifndef SUBLANG_SPANISH_DOMINICAN_REPUBLIC
571 #define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07
573 #ifndef SUBLANG_SPANISH_VENEZUELA
574 #define SUBLANG_SPANISH_VENEZUELA 0x08
576 #ifndef SUBLANG_SPANISH_COLOMBIA
577 #define SUBLANG_SPANISH_COLOMBIA 0x09
579 #ifndef SUBLANG_SPANISH_PERU
580 #define SUBLANG_SPANISH_PERU 0x0a
582 #ifndef SUBLANG_SPANISH_ARGENTINA
583 #define SUBLANG_SPANISH_ARGENTINA 0x0b
585 #ifndef SUBLANG_SPANISH_ECUADOR
586 #define SUBLANG_SPANISH_ECUADOR 0x0c
588 #ifndef SUBLANG_SPANISH_CHILE
589 #define SUBLANG_SPANISH_CHILE 0x0d
591 #ifndef SUBLANG_SPANISH_URUGUAY
592 #define SUBLANG_SPANISH_URUGUAY 0x0e
594 #ifndef SUBLANG_SPANISH_PARAGUAY
595 #define SUBLANG_SPANISH_PARAGUAY 0x0f
597 #ifndef SUBLANG_SPANISH_BOLIVIA
598 #define SUBLANG_SPANISH_BOLIVIA 0x10
600 #ifndef SUBLANG_SPANISH_EL_SALVADOR
601 #define SUBLANG_SPANISH_EL_SALVADOR 0x11
603 #ifndef SUBLANG_SPANISH_HONDURAS
604 #define SUBLANG_SPANISH_HONDURAS 0x12
606 #ifndef SUBLANG_SPANISH_NICARAGUA
607 #define SUBLANG_SPANISH_NICARAGUA 0x13
609 #ifndef SUBLANG_SPANISH_PUERTO_RICO
610 #define SUBLANG_SPANISH_PUERTO_RICO 0x14
612 #ifndef SUBLANG_SWEDISH_FINLAND
613 #define SUBLANG_SWEDISH_FINLAND 0x02
615 #ifndef SUBLANG_TAMAZIGHT_ARABIC
616 #define SUBLANG_TAMAZIGHT_ARABIC 0x01
618 #ifndef SUBLANG_TAMAZIGHT_LATIN
619 #define SUBLANG_TAMAZIGHT_LATIN 0x02
621 #ifndef SUBLANG_TIGRINYA_ETHIOPIA
622 #define SUBLANG_TIGRINYA_ETHIOPIA 0x00
624 #ifndef SUBLANG_TIGRINYA_ERITREA
625 #define SUBLANG_TIGRINYA_ERITREA 0x01
627 #ifndef SUBLANG_URDU_PAKISTAN
628 #define SUBLANG_URDU_PAKISTAN 0x01
630 #ifndef SUBLANG_URDU_INDIA
631 #define SUBLANG_URDU_INDIA 0x02
633 #ifndef SUBLANG_UZBEK_LATIN
634 #define SUBLANG_UZBEK_LATIN 0x01
636 #ifndef SUBLANG_UZBEK_CYRILLIC
637 #define SUBLANG_UZBEK_CYRILLIC 0x02
641 g_win32_getlocale (void)
647 gchar *l = "C", *sl = NULL;
650 /* Let the user override the system settings through environment
651 variables, as on POSIX systems. */
652 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
653 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
654 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
655 return g_strdup (ev);
657 /* Use native Win32 API locale ID. */
658 lcid = GetThreadLocale ();
660 /* Strip off the sorting rules, keep only the language part. */
661 langid = LANGIDFROMLCID (lcid);
663 /* Split into language and territory part. */
664 primary = PRIMARYLANGID (langid);
665 sub = SUBLANGID (langid);
668 case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
669 case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
674 case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
675 case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
676 case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
677 case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
678 case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
679 case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
680 case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
681 case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
682 case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
683 case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
684 case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
685 case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
686 case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
687 case SUBLANG_ARABIC_UAE: sl = "AE"; break;
688 case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
689 case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
692 case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
693 case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
698 /* FIXME: Adjust this when Azerbaijani locales appear on Unix. */
699 case SUBLANG_AZERI_LATIN: sl = "AZ@latin"; break;
700 case SUBLANG_AZERI_CYRILLIC: sl = "AZ@cyrillic"; break;
704 l = "eu"; /* sl could be "ES" or "FR". */
706 case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
711 case SUBLANG_BENGALI_INDIA: sl = "IN"; break;
712 case SUBLANG_BENGALI_BANGLADESH: sl = "BD"; break;
715 case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
716 case LANG_BURMESE: l = "my"; sl = "MM"; break;
717 case LANG_CAMBODIAN: l = "km"; sl = "KH"; break;
718 case LANG_CATALAN: l = "ca"; sl = "ES"; break;
723 case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
724 case SUBLANG_CHINESE_SIMPLIFIED: sl = "CN"; break;
725 case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
726 case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
727 case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
730 case LANG_CROATIAN: /* LANG_CROATIAN == LANG_SERBIAN */
733 /* FIXME: How to distinguish Croatian and Latin Serbian locales? */
734 case SUBLANG_SERBIAN_LATIN: l = "sr"; sl = "@Latn"; break;
735 case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
736 default: l = "hr"; sl = "HR";
739 case LANG_CZECH: l = "cs"; sl = "CZ"; break;
740 case LANG_DANISH: l = "da"; sl = "DK"; break;
741 case LANG_DIVEHI: l = "div"; sl = "MV"; break;
746 case SUBLANG_DUTCH: sl = "NL"; break;
747 case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
754 case SUBLANG_ENGLISH_US: sl = "US"; break;
755 case SUBLANG_ENGLISH_UK: sl = "GB"; break;
756 case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
757 case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
758 case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
759 case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
760 case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
761 case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
762 case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
763 case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
764 case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
765 case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
766 case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
767 case SUBLANG_ENGLISH_INDONESIA: sl = "ID"; break;
768 case SUBLANG_ENGLISH_HONGKONG: sl = "HK"; break;
769 case SUBLANG_ENGLISH_INDIA: sl = "IN"; break;
770 case SUBLANG_ENGLISH_MALAYSIA: sl = "MY"; break;
771 case SUBLANG_ENGLISH_SINGAPORE: sl = "SG"; break;
774 case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
775 case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
776 case LANG_FARSI: l = "fa"; sl = "IR"; break;
777 case LANG_FINNISH: l = "fi"; sl = "FI"; break;
782 case SUBLANG_FRENCH: sl = "FR"; break;
783 case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
784 case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
785 case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
786 case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
787 case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
788 case SUBLANG_FRENCH_WESTINDIES: break;
789 case SUBLANG_FRENCH_REUNION: sl = "RE"; break;
790 case SUBLANG_FRENCH_CONGO: sl = "CG"; break;
791 case SUBLANG_FRENCH_SENEGAL: sl = "SN"; break;
792 case SUBLANG_FRENCH_CAMEROON: sl = "CM"; break;
793 case SUBLANG_FRENCH_COTEDIVOIRE: sl = "CI"; break;
794 case SUBLANG_FRENCH_MALI: sl = "ML"; break;
795 case SUBLANG_FRENCH_MOROCCO: sl = "MA"; break;
796 case SUBLANG_FRENCH_HAITI: sl = "HT"; break;
799 case LANG_FRISIAN: l = "fy"; sl ="NL"; break;
800 case LANG_FULFULDE: l = "ful"; sl = "NG"; break;
804 case 0x01: /* SCOTTISH */ l = "gd"; sl = "GB"; break;
805 case 0x02: /* IRISH */ l = "ga"; sl = "IE"; break;
808 case LANG_GALICIAN: l = "gl"; sl = "ES"; break;
809 case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
814 case SUBLANG_GERMAN: sl = "DE"; break;
815 case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
816 case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
817 case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
818 case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
821 case LANG_GREEK: l = "el"; sl = "GR"; break;
822 case LANG_GUARANI: l = "gn"; sl = "PY"; break;
823 case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
824 case LANG_HAUSA: l = "ha"; sl = "NG"; break;
826 /* FIXME: Do they mean Hawaiian ("haw_US", 1000 speakers)
827 * or Hawaii Creole English ("cpe_US", 600000 speakers)?
832 case LANG_HEBREW: l = "he"; sl = "IL"; break;
833 case LANG_HINDI: l = "hi"; sl = "IN"; break;
834 case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
835 case LANG_IBIBIO: l = "nic"; sl = "NG"; break;
836 case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
837 case LANG_IGBO: l = "ibo"; sl = "NG"; break;
838 case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
839 case LANG_INUKTITUT: l = "iu"; sl = "CA"; break;
844 case SUBLANG_ITALIAN: sl = "IT"; break;
845 case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
848 case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
849 case LANG_KANNADA: l = "kn"; sl = "IN"; break;
850 case LANG_KANURI: l = "kau"; sl = "NG"; break;
855 case SUBLANG_DEFAULT: sl = "PK"; break;
856 case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
859 case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
861 /* FIXME: Adjust this when such locales appear on Unix. */
865 case LANG_KOREAN: l = "ko"; sl = "KR"; break;
866 case LANG_KYRGYZ: l = "ky"; sl = "KG"; break;
867 case LANG_LAO: l = "lo"; sl = "LA"; break;
868 case LANG_LATIN: l = "la"; sl = "VA"; break;
869 case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
870 case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
871 case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
876 case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
877 case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
880 case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
882 /* FIXME: Adjust this when such locales appear on Unix. */
886 case LANG_MARATHI: l = "mr"; sl = "IN"; break;
888 /* Ambiguous: could be "mn_CN" or "mn_MN". */
895 case SUBLANG_DEFAULT: sl = "NP"; break;
896 case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
903 case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
904 case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
907 case LANG_ORIYA: l = "or"; sl = "IN"; break;
908 case LANG_OROMO: l = "om"; sl = "ET"; break;
909 case LANG_PAPIAMENTU: l = "pap"; sl = "AN"; break;
911 /* Ambiguous: could be "ps_PK" or "ps_AF". */
914 case LANG_POLISH: l = "pl"; sl = "PL"; break;
915 case LANG_PORTUGUESE:
919 case SUBLANG_PORTUGUESE: sl = "PT"; break;
920 case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
927 case SUBLANG_PUNJABI_INDIA: sl = "IN"; break; /* Gurmukhi script */
928 case SUBLANG_PUNJABI_PAKISTAN: sl = "PK"; break; /* Arabic script */
931 case LANG_RHAETO_ROMANCE: l = "rm"; sl = "CH"; break;
936 case SUBLANG_ROMANIAN_ROMANIA: sl = "RO"; break;
937 case SUBLANG_ROMANIAN_MOLDOVA: sl = "MD"; break;
941 l = "ru";/* Ambiguous: could be "ru_RU" or "ru_UA" or "ru_MD". */
943 case LANG_SAAMI: /* actually Northern Sami */ l = "se"; sl = "NO"; break;
944 case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
945 case LANG_SINDHI: l = "sd";
948 case SUBLANG_SINDHI_INDIA: sl = "IN"; break;
949 case SUBLANG_SINDHI_PAKISTAN: sl = "PK"; break;
952 case LANG_SINHALESE: l = "si"; sl = "LK"; break;
953 case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
954 case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
955 case LANG_SOMALI: l = "so"; sl = "SO"; break;
957 /* FIXME: Adjust this when such locales appear on Unix. */
965 case SUBLANG_SPANISH: sl = "ES"; break;
966 case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
967 case SUBLANG_SPANISH_MODERN:
968 sl = "ES@modern"; break; /* not seen on Unix */
969 case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
970 case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
971 case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
972 case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
973 case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
974 case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
975 case SUBLANG_SPANISH_PERU: sl = "PE"; break;
976 case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
977 case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
978 case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
979 case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
980 case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
981 case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
982 case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
983 case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
984 case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
985 case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
988 case LANG_SUTU: l = "bnt"; sl = "TZ"; break; /* or "st_LS" or "nso_ZA"? */
989 case LANG_SWAHILI: l = "sw"; sl = "KE"; break;
994 case SUBLANG_DEFAULT: sl = "SE"; break;
995 case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
998 case LANG_SYRIAC: l = "syr"; sl = "TR"; break; /* An extinct language. */
999 case LANG_TAGALOG: l = "tl"; sl = "PH"; break;
1000 case LANG_TAJIK: l = "tg"; sl = "TJ"; break;
1002 l = "ta"; /* Ambiguous: could be "ta_IN" or "ta_LK" or "ta_SG". */
1004 case LANG_TATAR: l = "tt"; sl = "RU"; break;
1005 case LANG_TELUGU: l = "te"; sl = "IN"; break;
1006 case LANG_THAI: l = "th"; sl = "TH"; break;
1007 case LANG_TIBETAN: l = "bo"; sl = "CN"; break;
1012 case SUBLANG_TIGRINYA_ETHIOPIA: sl = "ET"; break;
1013 case SUBLANG_TIGRINYA_ERITREA: sl = "ER"; break;
1016 case LANG_TSONGA: l = "ts"; sl = "ZA"; break;
1017 case LANG_TSWANA: l = "tn"; sl = "BW"; break;
1018 case LANG_TURKISH: l = "tr"; sl = "TR"; break;
1019 case LANG_TURKMEN: l = "tk"; sl = "TM"; break;
1020 case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
1025 case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
1026 case SUBLANG_URDU_INDIA: sl = "IN"; break;
1033 case SUBLANG_UZBEK_LATIN: sl = "UZ"; break;
1034 case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
1038 /* FIXME: It's not clear whether Venda has the ISO 639-2 two-letter code
1040 http://www.loc.gov/standards/iso639-2/englangn.html has it, but
1041 http://lcweb.loc.gov/standards/iso639-2/codechanges.html doesn't, */
1042 l = "ven"; /* or "ve"? */
1045 case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
1046 case LANG_WELSH: l = "cy"; sl = "GB"; break;
1047 case LANG_XHOSA: l = "xh"; sl = "ZA"; break;
1048 case LANG_YI: l = "sit"; sl = "CN"; break;
1049 case LANG_YIDDISH: l = "yi"; sl = "IL"; break;
1050 case LANG_YORUBA: l = "yo"; sl = "NG"; break;
1051 case LANG_ZULU: l = "zu"; sl = "ZA"; break;
1061 return g_strdup (bfr);
1065 * g_win32_error_message:
1066 * @error: error code.
1068 * Translate a Win32 error code (as returned by GetLastError()) into
1069 * the corresponding message. The message is either language neutral,
1070 * or in the thread's language, or the user's language, the system's
1071 * language, or US English (see docs for FormatMessage()). *
1072 * The returned string should be deallocated with g_free().
1074 * Returns: newly-allocated error message
1077 g_win32_error_message (gint error)
1083 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
1084 |FORMAT_MESSAGE_IGNORE_INSERTS
1085 |FORMAT_MESSAGE_FROM_SYSTEM,
1087 (LPTSTR) &msg, 0, NULL);
1088 nbytes = strlen (msg);
1090 if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
1091 msg[nbytes-2] = '\0';
1093 retval = g_strdup (msg);
1102 get_package_directory_from_module (gchar *module_name)
1104 static GHashTable *module_dirs = NULL;
1105 G_LOCK_DEFINE_STATIC (module_dirs);
1106 HMODULE hmodule = NULL;
1111 G_LOCK (module_dirs);
1113 if (module_dirs == NULL)
1114 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
1116 result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
1120 G_UNLOCK (module_dirs);
1121 return g_strdup (result);
1126 hmodule = GetModuleHandle (module_name);
1131 fn = g_malloc (MAX_PATH);
1132 if (!GetModuleFileName (hmodule, fn, MAX_PATH))
1134 G_UNLOCK (module_dirs);
1139 if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
1142 p = strrchr (fn, G_DIR_SEPARATOR);
1143 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
1144 g_ascii_strcasecmp (p + 1, "lib") == 0))
1147 #ifdef G_WITH_CYGWIN
1148 /* In Cygwin we need to have POSIX paths */
1150 gchar tmp[MAX_PATH];
1152 cygwin_conv_to_posix_path(fn, tmp);
1158 g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
1160 G_UNLOCK (module_dirs);
1162 return g_strdup (fn);
1166 * g_win32_get_package_installation_directory:
1167 * @package: An identifier for a software package, or %NULL
1168 * @dll_name: The name of a DLL that a package provides, or %NULL.
1170 * Try to determine the installation directory for a software package.
1171 * Typically used by GNU software packages.
1173 * @package should be a short identifier for the package. Typically it
1174 * is the same identifier as used for
1175 * <literal>GETTEXT_PACKAGE</literal> in software configured according
1176 * to GNU standards. The function first looks in the Windows Registry
1177 * for the value <literal>#InstallationDirectory</literal> in the key
1178 * <literal>#HKLM\Software\@package</literal>, and if that value
1179 * exists and is a string, returns that.
1181 * If @package is %NULL, or the above value isn't found in the
1182 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
1183 * into the current process. Typically that would be the name of the
1184 * DLL calling this function, looking for its installation
1185 * directory. The function then asks Windows what directory that DLL
1186 * was loaded from. If that directory's last component is "bin" or
1187 * "lib", the parent directory is returned, otherwise the directory
1188 * itself. If that DLL isn't loaded, the function proceeds as if
1189 * @dll_name was %NULL.
1191 * If both @package and @dll_name are %NULL, the directory from where
1192 * the main executable of the process was loaded is uses instead in
1193 * the same way as above.
1195 * Returns: a string containing the installation directory for @package.
1196 * The return value should be freed with g_free() when not needed any longer.
1200 g_win32_get_package_installation_directory (gchar *package,
1203 static GHashTable *package_dirs = NULL;
1204 G_LOCK_DEFINE_STATIC (package_dirs);
1205 gchar *result = NULL;
1207 HKEY reg_key = NULL;
1211 if (package != NULL)
1213 G_LOCK (package_dirs);
1215 if (package_dirs == NULL)
1216 package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
1218 result = g_hash_table_lookup (package_dirs, package);
1220 if (result && result[0])
1222 G_UNLOCK (package_dirs);
1223 return g_strdup (result);
1226 key = g_strconcat ("Software\\", package, NULL);
1229 if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
1230 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
1231 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
1232 &type, NULL, &nbytes) == ERROR_SUCCESS)
1234 ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
1235 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
1236 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
1237 &type, NULL, &nbytes) == ERROR_SUCCESS)
1240 result = g_malloc (nbytes + 1);
1241 RegQueryValueEx (reg_key, "InstallationDirectory", 0,
1242 &type, result, &nbytes);
1243 result[nbytes] = '\0';
1246 if (reg_key != NULL)
1247 RegCloseKey (reg_key);
1253 g_hash_table_insert (package_dirs, package, result);
1254 G_UNLOCK (package_dirs);
1255 return g_strdup (result);
1257 G_UNLOCK (package_dirs);
1260 if (dll_name != NULL)
1261 result = get_package_directory_from_module (dll_name);
1264 result = get_package_directory_from_module (NULL);
1270 * g_win32_get_package_installation_subdirectory:
1271 * @package: An identifier for a software package, or %NULL.
1272 * @dll_name: The name of a DLL that a package provides, or %NULL.
1273 * @subdir: A subdirectory of the package installation directory.
1275 * Returns a newly-allocated string containing the path of the
1276 * subdirectory @subdir in the return value from calling
1277 * g_win32_get_package_installation_directory() with the @package and
1278 * @dll_name parameters.
1280 * Returns: a string containing the complete path to @subdir inside the
1281 * installation directory of @package. The return value should be freed with
1282 * g_free() when no longer needed.
1286 g_win32_get_package_installation_subdirectory (gchar *package,
1292 prefix = g_win32_get_package_installation_directory (package, dll_name);
1294 return g_build_filename (prefix, subdir, NULL);