add a comment
[glib.git] / glib / gwin32.c
blobf7a295e4f10896e9c9e22edc6c6972bd2c95370a
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/.
28 /*
29 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
32 #include "config.h"
34 #include "glibconfig.h"
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <wchar.h>
40 #include <errno.h>
42 #define STRICT /* Strict typing, please */
43 #include <windows.h>
44 #undef STRICT
45 #ifndef G_WITH_CYGWIN
46 #include <direct.h>
47 #endif
48 #include <errno.h>
49 #include <ctype.h>
50 #ifdef _MSC_VER
51 # include <io.h>
52 #endif /* _MSC_VER */
54 #include "glib.h"
55 #include "galias.h"
57 #ifdef G_WITH_CYGWIN
58 #include <sys/cygwin.h>
59 #endif
61 #ifndef G_WITH_CYGWIN
63 gint
64 g_win32_ftruncate (gint fd,
65 guint size)
67 return _chsize (fd, size);
70 #endif
72 /**
73 * g_win32_getlocale:
75 * The setlocale() function in the Microsoft C library uses locale
76 * names of the form "English_United States.1252" etc. We want the
77 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
78 * current thread locale from Windows - without any encoding info -
79 * and returns it as a string of the above form for use in forming
80 * file names etc. The returned string should be deallocated with
81 * g_free().
83 * Returns: newly-allocated locale name.
84 **/
86 /* Borrowed from GNU gettext 0.13.1: */
87 /* Mingw headers don't have latest language and sublanguage codes. */
88 #ifndef LANG_AFRIKAANS
89 #define LANG_AFRIKAANS 0x36
90 #endif
91 #ifndef LANG_ALBANIAN
92 #define LANG_ALBANIAN 0x1c
93 #endif
94 #ifndef LANG_AMHARIC
95 #define LANG_AMHARIC 0x5e
96 #endif
97 #ifndef LANG_ARABIC
98 #define LANG_ARABIC 0x01
99 #endif
100 #ifndef LANG_ARMENIAN
101 #define LANG_ARMENIAN 0x2b
102 #endif
103 #ifndef LANG_ASSAMESE
104 #define LANG_ASSAMESE 0x4d
105 #endif
106 #ifndef LANG_AZERI
107 #define LANG_AZERI 0x2c
108 #endif
109 #ifndef LANG_BASQUE
110 #define LANG_BASQUE 0x2d
111 #endif
112 #ifndef LANG_BELARUSIAN
113 #define LANG_BELARUSIAN 0x23
114 #endif
115 #ifndef LANG_BENGALI
116 #define LANG_BENGALI 0x45
117 #endif
118 #ifndef LANG_BURMESE
119 #define LANG_BURMESE 0x55
120 #endif
121 #ifndef LANG_CAMBODIAN
122 #define LANG_CAMBODIAN 0x53
123 #endif
124 #ifndef LANG_CATALAN
125 #define LANG_CATALAN 0x03
126 #endif
127 #ifndef LANG_CHEROKEE
128 #define LANG_CHEROKEE 0x5c
129 #endif
130 #ifndef LANG_DIVEHI
131 #define LANG_DIVEHI 0x65
132 #endif
133 #ifndef LANG_EDO
134 #define LANG_EDO 0x66
135 #endif
136 #ifndef LANG_ESTONIAN
137 #define LANG_ESTONIAN 0x25
138 #endif
139 #ifndef LANG_FAEROESE
140 #define LANG_FAEROESE 0x38
141 #endif
142 #ifndef LANG_FARSI
143 #define LANG_FARSI 0x29
144 #endif
145 #ifndef LANG_FRISIAN
146 #define LANG_FRISIAN 0x62
147 #endif
148 #ifndef LANG_FULFULDE
149 #define LANG_FULFULDE 0x67
150 #endif
151 #ifndef LANG_GAELIC
152 #define LANG_GAELIC 0x3c
153 #endif
154 #ifndef LANG_GALICIAN
155 #define LANG_GALICIAN 0x56
156 #endif
157 #ifndef LANG_GEORGIAN
158 #define LANG_GEORGIAN 0x37
159 #endif
160 #ifndef LANG_GUARANI
161 #define LANG_GUARANI 0x74
162 #endif
163 #ifndef LANG_GUJARATI
164 #define LANG_GUJARATI 0x47
165 #endif
166 #ifndef LANG_HAUSA
167 #define LANG_HAUSA 0x68
168 #endif
169 #ifndef LANG_HAWAIIAN
170 #define LANG_HAWAIIAN 0x75
171 #endif
172 #ifndef LANG_HEBREW
173 #define LANG_HEBREW 0x0d
174 #endif
175 #ifndef LANG_HINDI
176 #define LANG_HINDI 0x39
177 #endif
178 #ifndef LANG_IBIBIO
179 #define LANG_IBIBIO 0x69
180 #endif
181 #ifndef LANG_IGBO
182 #define LANG_IGBO 0x70
183 #endif
184 #ifndef LANG_INDONESIAN
185 #define LANG_INDONESIAN 0x21
186 #endif
187 #ifndef LANG_INUKTITUT
188 #define LANG_INUKTITUT 0x5d
189 #endif
190 #ifndef LANG_KANNADA
191 #define LANG_KANNADA 0x4b
192 #endif
193 #ifndef LANG_KANURI
194 #define LANG_KANURI 0x71
195 #endif
196 #ifndef LANG_KASHMIRI
197 #define LANG_KASHMIRI 0x60
198 #endif
199 #ifndef LANG_KAZAK
200 #define LANG_KAZAK 0x3f
201 #endif
202 #ifndef LANG_KONKANI
203 #define LANG_KONKANI 0x57
204 #endif
205 #ifndef LANG_KYRGYZ
206 #define LANG_KYRGYZ 0x40
207 #endif
208 #ifndef LANG_LAO
209 #define LANG_LAO 0x54
210 #endif
211 #ifndef LANG_LATIN
212 #define LANG_LATIN 0x76
213 #endif
214 #ifndef LANG_LATVIAN
215 #define LANG_LATVIAN 0x26
216 #endif
217 #ifndef LANG_LITHUANIAN
218 #define LANG_LITHUANIAN 0x27
219 #endif
220 #ifndef LANG_MACEDONIAN
221 #define LANG_MACEDONIAN 0x2f
222 #endif
223 #ifndef LANG_MALAY
224 #define LANG_MALAY 0x3e
225 #endif
226 #ifndef LANG_MALAYALAM
227 #define LANG_MALAYALAM 0x4c
228 #endif
229 #ifndef LANG_MALTESE
230 #define LANG_MALTESE 0x3a
231 #endif
232 #ifndef LANG_MANIPURI
233 #define LANG_MANIPURI 0x58
234 #endif
235 #ifndef LANG_MARATHI
236 #define LANG_MARATHI 0x4e
237 #endif
238 #ifndef LANG_MONGOLIAN
239 #define LANG_MONGOLIAN 0x50
240 #endif
241 #ifndef LANG_NEPALI
242 #define LANG_NEPALI 0x61
243 #endif
244 #ifndef LANG_ORIYA
245 #define LANG_ORIYA 0x48
246 #endif
247 #ifndef LANG_OROMO
248 #define LANG_OROMO 0x72
249 #endif
250 #ifndef LANG_PAPIAMENTU
251 #define LANG_PAPIAMENTU 0x79
252 #endif
253 #ifndef LANG_PASHTO
254 #define LANG_PASHTO 0x63
255 #endif
256 #ifndef LANG_PUNJABI
257 #define LANG_PUNJABI 0x46
258 #endif
259 #ifndef LANG_RHAETO_ROMANCE
260 #define LANG_RHAETO_ROMANCE 0x17
261 #endif
262 #ifndef LANG_SAAMI
263 #define LANG_SAAMI 0x3b
264 #endif
265 #ifndef LANG_SANSKRIT
266 #define LANG_SANSKRIT 0x4f
267 #endif
268 #ifndef LANG_SERBIAN
269 #define LANG_SERBIAN 0x1a
270 #endif
271 #ifndef LANG_SINDHI
272 #define LANG_SINDHI 0x59
273 #endif
274 #ifndef LANG_SINHALESE
275 #define LANG_SINHALESE 0x5b
276 #endif
277 #ifndef LANG_SLOVAK
278 #define LANG_SLOVAK 0x1b
279 #endif
280 #ifndef LANG_SOMALI
281 #define LANG_SOMALI 0x77
282 #endif
283 #ifndef LANG_SORBIAN
284 #define LANG_SORBIAN 0x2e
285 #endif
286 #ifndef LANG_SUTU
287 #define LANG_SUTU 0x30
288 #endif
289 #ifndef LANG_SWAHILI
290 #define LANG_SWAHILI 0x41
291 #endif
292 #ifndef LANG_SYRIAC
293 #define LANG_SYRIAC 0x5a
294 #endif
295 #ifndef LANG_TAGALOG
296 #define LANG_TAGALOG 0x64
297 #endif
298 #ifndef LANG_TAJIK
299 #define LANG_TAJIK 0x28
300 #endif
301 #ifndef LANG_TAMAZIGHT
302 #define LANG_TAMAZIGHT 0x5f
303 #endif
304 #ifndef LANG_TAMIL
305 #define LANG_TAMIL 0x49
306 #endif
307 #ifndef LANG_TATAR
308 #define LANG_TATAR 0x44
309 #endif
310 #ifndef LANG_TELUGU
311 #define LANG_TELUGU 0x4a
312 #endif
313 #ifndef LANG_THAI
314 #define LANG_THAI 0x1e
315 #endif
316 #ifndef LANG_TIBETAN
317 #define LANG_TIBETAN 0x51
318 #endif
319 #ifndef LANG_TIGRINYA
320 #define LANG_TIGRINYA 0x73
321 #endif
322 #ifndef LANG_TSONGA
323 #define LANG_TSONGA 0x31
324 #endif
325 #ifndef LANG_TSWANA
326 #define LANG_TSWANA 0x32
327 #endif
328 #ifndef LANG_TURKMEN
329 #define LANG_TURKMEN 0x42
330 #endif
331 #ifndef LANG_UKRAINIAN
332 #define LANG_UKRAINIAN 0x22
333 #endif
334 #ifndef LANG_URDU
335 #define LANG_URDU 0x20
336 #endif
337 #ifndef LANG_UZBEK
338 #define LANG_UZBEK 0x43
339 #endif
340 #ifndef LANG_VENDA
341 #define LANG_VENDA 0x33
342 #endif
343 #ifndef LANG_VIETNAMESE
344 #define LANG_VIETNAMESE 0x2a
345 #endif
346 #ifndef LANG_WELSH
347 #define LANG_WELSH 0x52
348 #endif
349 #ifndef LANG_XHOSA
350 #define LANG_XHOSA 0x34
351 #endif
352 #ifndef LANG_YI
353 #define LANG_YI 0x78
354 #endif
355 #ifndef LANG_YIDDISH
356 #define LANG_YIDDISH 0x3d
357 #endif
358 #ifndef LANG_YORUBA
359 #define LANG_YORUBA 0x6a
360 #endif
361 #ifndef LANG_ZULU
362 #define LANG_ZULU 0x35
363 #endif
364 #ifndef SUBLANG_ARABIC_SAUDI_ARABIA
365 #define SUBLANG_ARABIC_SAUDI_ARABIA 0x01
366 #endif
367 #ifndef SUBLANG_ARABIC_IRAQ
368 #define SUBLANG_ARABIC_IRAQ 0x02
369 #endif
370 #ifndef SUBLANG_ARABIC_EGYPT
371 #define SUBLANG_ARABIC_EGYPT 0x03
372 #endif
373 #ifndef SUBLANG_ARABIC_LIBYA
374 #define SUBLANG_ARABIC_LIBYA 0x04
375 #endif
376 #ifndef SUBLANG_ARABIC_ALGERIA
377 #define SUBLANG_ARABIC_ALGERIA 0x05
378 #endif
379 #ifndef SUBLANG_ARABIC_MOROCCO
380 #define SUBLANG_ARABIC_MOROCCO 0x06
381 #endif
382 #ifndef SUBLANG_ARABIC_TUNISIA
383 #define SUBLANG_ARABIC_TUNISIA 0x07
384 #endif
385 #ifndef SUBLANG_ARABIC_OMAN
386 #define SUBLANG_ARABIC_OMAN 0x08
387 #endif
388 #ifndef SUBLANG_ARABIC_YEMEN
389 #define SUBLANG_ARABIC_YEMEN 0x09
390 #endif
391 #ifndef SUBLANG_ARABIC_SYRIA
392 #define SUBLANG_ARABIC_SYRIA 0x0a
393 #endif
394 #ifndef SUBLANG_ARABIC_JORDAN
395 #define SUBLANG_ARABIC_JORDAN 0x0b
396 #endif
397 #ifndef SUBLANG_ARABIC_LEBANON
398 #define SUBLANG_ARABIC_LEBANON 0x0c
399 #endif
400 #ifndef SUBLANG_ARABIC_KUWAIT
401 #define SUBLANG_ARABIC_KUWAIT 0x0d
402 #endif
403 #ifndef SUBLANG_ARABIC_UAE
404 #define SUBLANG_ARABIC_UAE 0x0e
405 #endif
406 #ifndef SUBLANG_ARABIC_BAHRAIN
407 #define SUBLANG_ARABIC_BAHRAIN 0x0f
408 #endif
409 #ifndef SUBLANG_ARABIC_QATAR
410 #define SUBLANG_ARABIC_QATAR 0x10
411 #endif
412 #ifndef SUBLANG_AZERI_LATIN
413 #define SUBLANG_AZERI_LATIN 0x01
414 #endif
415 #ifndef SUBLANG_AZERI_CYRILLIC
416 #define SUBLANG_AZERI_CYRILLIC 0x02
417 #endif
418 #ifndef SUBLANG_BENGALI_INDIA
419 #define SUBLANG_BENGALI_INDIA 0x00
420 #endif
421 #ifndef SUBLANG_BENGALI_BANGLADESH
422 #define SUBLANG_BENGALI_BANGLADESH 0x01
423 #endif
424 #ifndef SUBLANG_CHINESE_MACAU
425 #define SUBLANG_CHINESE_MACAU 0x05
426 #endif
427 #ifndef SUBLANG_ENGLISH_SOUTH_AFRICA
428 #define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07
429 #endif
430 #ifndef SUBLANG_ENGLISH_JAMAICA
431 #define SUBLANG_ENGLISH_JAMAICA 0x08
432 #endif
433 #ifndef SUBLANG_ENGLISH_CARIBBEAN
434 #define SUBLANG_ENGLISH_CARIBBEAN 0x09
435 #endif
436 #ifndef SUBLANG_ENGLISH_BELIZE
437 #define SUBLANG_ENGLISH_BELIZE 0x0a
438 #endif
439 #ifndef SUBLANG_ENGLISH_TRINIDAD
440 #define SUBLANG_ENGLISH_TRINIDAD 0x0b
441 #endif
442 #ifndef SUBLANG_ENGLISH_ZIMBABWE
443 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
444 #endif
445 #ifndef SUBLANG_ENGLISH_PHILIPPINES
446 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
447 #endif
448 #ifndef SUBLANG_ENGLISH_INDONESIA
449 #define SUBLANG_ENGLISH_INDONESIA 0x0e
450 #endif
451 #ifndef SUBLANG_ENGLISH_HONGKONG
452 #define SUBLANG_ENGLISH_HONGKONG 0x0f
453 #endif
454 #ifndef SUBLANG_ENGLISH_INDIA
455 #define SUBLANG_ENGLISH_INDIA 0x10
456 #endif
457 #ifndef SUBLANG_ENGLISH_MALAYSIA
458 #define SUBLANG_ENGLISH_MALAYSIA 0x11
459 #endif
460 #ifndef SUBLANG_ENGLISH_SINGAPORE
461 #define SUBLANG_ENGLISH_SINGAPORE 0x12
462 #endif
463 #ifndef SUBLANG_FRENCH_LUXEMBOURG
464 #define SUBLANG_FRENCH_LUXEMBOURG 0x05
465 #endif
466 #ifndef SUBLANG_FRENCH_MONACO
467 #define SUBLANG_FRENCH_MONACO 0x06
468 #endif
469 #ifndef SUBLANG_FRENCH_WESTINDIES
470 #define SUBLANG_FRENCH_WESTINDIES 0x07
471 #endif
472 #ifndef SUBLANG_FRENCH_REUNION
473 #define SUBLANG_FRENCH_REUNION 0x08
474 #endif
475 #ifndef SUBLANG_FRENCH_CONGO
476 #define SUBLANG_FRENCH_CONGO 0x09
477 #endif
478 #ifndef SUBLANG_FRENCH_SENEGAL
479 #define SUBLANG_FRENCH_SENEGAL 0x0a
480 #endif
481 #ifndef SUBLANG_FRENCH_CAMEROON
482 #define SUBLANG_FRENCH_CAMEROON 0x0b
483 #endif
484 #ifndef SUBLANG_FRENCH_COTEDIVOIRE
485 #define SUBLANG_FRENCH_COTEDIVOIRE 0x0c
486 #endif
487 #ifndef SUBLANG_FRENCH_MALI
488 #define SUBLANG_FRENCH_MALI 0x0d
489 #endif
490 #ifndef SUBLANG_FRENCH_MOROCCO
491 #define SUBLANG_FRENCH_MOROCCO 0x0e
492 #endif
493 #ifndef SUBLANG_FRENCH_HAITI
494 #define SUBLANG_FRENCH_HAITI 0x0f
495 #endif
496 #ifndef SUBLANG_GERMAN_LUXEMBOURG
497 #define SUBLANG_GERMAN_LUXEMBOURG 0x04
498 #endif
499 #ifndef SUBLANG_GERMAN_LIECHTENSTEIN
500 #define SUBLANG_GERMAN_LIECHTENSTEIN 0x05
501 #endif
502 #ifndef SUBLANG_KASHMIRI_INDIA
503 #define SUBLANG_KASHMIRI_INDIA 0x02
504 #endif
505 #ifndef SUBLANG_MALAY_MALAYSIA
506 #define SUBLANG_MALAY_MALAYSIA 0x01
507 #endif
508 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
509 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
510 #endif
511 #ifndef SUBLANG_NEPALI_INDIA
512 #define SUBLANG_NEPALI_INDIA 0x02
513 #endif
514 #ifndef SUBLANG_PUNJABI_INDIA
515 #define SUBLANG_PUNJABI_INDIA 0x00
516 #endif
517 #ifndef SUBLANG_PUNJABI_PAKISTAN
518 #define SUBLANG_PUNJABI_PAKISTAN 0x01
519 #endif
520 #ifndef SUBLANG_ROMANIAN_ROMANIA
521 #define SUBLANG_ROMANIAN_ROMANIA 0x00
522 #endif
523 #ifndef SUBLANG_ROMANIAN_MOLDOVA
524 #define SUBLANG_ROMANIAN_MOLDOVA 0x01
525 #endif
526 #ifndef SUBLANG_SERBIAN_LATIN
527 #define SUBLANG_SERBIAN_LATIN 0x02
528 #endif
529 #ifndef SUBLANG_SERBIAN_CYRILLIC
530 #define SUBLANG_SERBIAN_CYRILLIC 0x03
531 #endif
532 #ifndef SUBLANG_SINDHI_INDIA
533 #define SUBLANG_SINDHI_INDIA 0x00
534 #endif
535 #ifndef SUBLANG_SINDHI_PAKISTAN
536 #define SUBLANG_SINDHI_PAKISTAN 0x01
537 #endif
538 #ifndef SUBLANG_SPANISH_GUATEMALA
539 #define SUBLANG_SPANISH_GUATEMALA 0x04
540 #endif
541 #ifndef SUBLANG_SPANISH_COSTA_RICA
542 #define SUBLANG_SPANISH_COSTA_RICA 0x05
543 #endif
544 #ifndef SUBLANG_SPANISH_PANAMA
545 #define SUBLANG_SPANISH_PANAMA 0x06
546 #endif
547 #ifndef SUBLANG_SPANISH_DOMINICAN_REPUBLIC
548 #define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07
549 #endif
550 #ifndef SUBLANG_SPANISH_VENEZUELA
551 #define SUBLANG_SPANISH_VENEZUELA 0x08
552 #endif
553 #ifndef SUBLANG_SPANISH_COLOMBIA
554 #define SUBLANG_SPANISH_COLOMBIA 0x09
555 #endif
556 #ifndef SUBLANG_SPANISH_PERU
557 #define SUBLANG_SPANISH_PERU 0x0a
558 #endif
559 #ifndef SUBLANG_SPANISH_ARGENTINA
560 #define SUBLANG_SPANISH_ARGENTINA 0x0b
561 #endif
562 #ifndef SUBLANG_SPANISH_ECUADOR
563 #define SUBLANG_SPANISH_ECUADOR 0x0c
564 #endif
565 #ifndef SUBLANG_SPANISH_CHILE
566 #define SUBLANG_SPANISH_CHILE 0x0d
567 #endif
568 #ifndef SUBLANG_SPANISH_URUGUAY
569 #define SUBLANG_SPANISH_URUGUAY 0x0e
570 #endif
571 #ifndef SUBLANG_SPANISH_PARAGUAY
572 #define SUBLANG_SPANISH_PARAGUAY 0x0f
573 #endif
574 #ifndef SUBLANG_SPANISH_BOLIVIA
575 #define SUBLANG_SPANISH_BOLIVIA 0x10
576 #endif
577 #ifndef SUBLANG_SPANISH_EL_SALVADOR
578 #define SUBLANG_SPANISH_EL_SALVADOR 0x11
579 #endif
580 #ifndef SUBLANG_SPANISH_HONDURAS
581 #define SUBLANG_SPANISH_HONDURAS 0x12
582 #endif
583 #ifndef SUBLANG_SPANISH_NICARAGUA
584 #define SUBLANG_SPANISH_NICARAGUA 0x13
585 #endif
586 #ifndef SUBLANG_SPANISH_PUERTO_RICO
587 #define SUBLANG_SPANISH_PUERTO_RICO 0x14
588 #endif
589 #ifndef SUBLANG_SWEDISH_FINLAND
590 #define SUBLANG_SWEDISH_FINLAND 0x02
591 #endif
592 #ifndef SUBLANG_TAMAZIGHT_ARABIC
593 #define SUBLANG_TAMAZIGHT_ARABIC 0x01
594 #endif
595 #ifndef SUBLANG_TAMAZIGHT_LATIN
596 #define SUBLANG_TAMAZIGHT_LATIN 0x02
597 #endif
598 #ifndef SUBLANG_TIGRINYA_ETHIOPIA
599 #define SUBLANG_TIGRINYA_ETHIOPIA 0x00
600 #endif
601 #ifndef SUBLANG_TIGRINYA_ERITREA
602 #define SUBLANG_TIGRINYA_ERITREA 0x01
603 #endif
604 #ifndef SUBLANG_URDU_PAKISTAN
605 #define SUBLANG_URDU_PAKISTAN 0x01
606 #endif
607 #ifndef SUBLANG_URDU_INDIA
608 #define SUBLANG_URDU_INDIA 0x02
609 #endif
610 #ifndef SUBLANG_UZBEK_LATIN
611 #define SUBLANG_UZBEK_LATIN 0x01
612 #endif
613 #ifndef SUBLANG_UZBEK_CYRILLIC
614 #define SUBLANG_UZBEK_CYRILLIC 0x02
615 #endif
617 gchar *
618 g_win32_getlocale (void)
620 LCID lcid;
621 LANGID langid;
622 gchar *ev;
623 gint primary, sub;
624 gchar *l = "C", *sl = NULL;
625 gchar bfr[20];
627 /* Let the user override the system settings through environment
628 variables, as on POSIX systems. */
629 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
630 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
631 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
632 return g_strdup (ev);
634 /* Use native Win32 API locale ID. */
635 lcid = GetThreadLocale ();
637 /* Strip off the sorting rules, keep only the language part. */
638 langid = LANGIDFROMLCID (lcid);
640 /* Split into language and territory part. */
641 primary = PRIMARYLANGID (langid);
642 sub = SUBLANGID (langid);
643 switch (primary)
645 case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
646 case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
647 case LANG_ARABIC:
648 l = "ar";
649 switch (sub)
651 case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
652 case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
653 case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
654 case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
655 case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
656 case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
657 case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
658 case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
659 case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
660 case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
661 case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
662 case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
663 case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
664 case SUBLANG_ARABIC_UAE: sl = "AE"; break;
665 case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
666 case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
668 break;
669 case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
670 case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
671 case LANG_AZERI:
672 l = "az";
673 switch (sub)
675 /* FIXME: Adjust this when Azerbaijani locales appear on Unix. */
676 case SUBLANG_AZERI_LATIN: sl = "AZ@latin"; break;
677 case SUBLANG_AZERI_CYRILLIC: sl = "AZ@cyrillic"; break;
679 break;
680 case LANG_BASQUE:
681 l = "eu"; /* sl could be "ES" or "FR". */
682 break;
683 case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
684 case LANG_BENGALI:
685 l = "bn";
686 switch (sub)
688 case SUBLANG_BENGALI_INDIA: sl = "IN"; break;
689 case SUBLANG_BENGALI_BANGLADESH: sl = "BD"; break;
691 break;
692 case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
693 case LANG_BURMESE: l = "my"; sl = "MM"; break;
694 case LANG_CAMBODIAN: l = "km"; sl = "KH"; break;
695 case LANG_CATALAN: l = "ca"; sl = "ES"; break;
696 case LANG_CHINESE:
697 l = "zh";
698 switch (sub)
700 case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
701 case SUBLANG_CHINESE_SIMPLIFIED: sl = "CN"; break;
702 case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
703 case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
704 case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
706 break;
707 case LANG_CROATIAN: /* LANG_CROATIAN == LANG_SERBIAN */
708 switch (sub)
710 /* FIXME: How to distinguish Croatian and Latin Serbian locales? */
711 case SUBLANG_SERBIAN_LATIN: l = "sr"; sl = "@Latn"; break;
712 case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
713 default: l = "hr"; sl = "HR";
715 break;
716 case LANG_CZECH: l = "cs"; sl = "CZ"; break;
717 case LANG_DANISH: l = "da"; sl = "DK"; break;
718 case LANG_DIVEHI: l = "div"; sl = "MV"; break;
719 case LANG_DUTCH:
720 l = "nl";
721 switch (sub)
723 case SUBLANG_DUTCH: sl = "NL"; break;
724 case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
726 break;
727 case LANG_ENGLISH:
728 l = "en";
729 switch (sub)
731 case SUBLANG_ENGLISH_US: sl = "US"; break;
732 case SUBLANG_ENGLISH_UK: sl = "GB"; break;
733 case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
734 case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
735 case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
736 case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
737 case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
738 case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
739 case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
740 case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
741 case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
742 case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
743 case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
744 case SUBLANG_ENGLISH_INDONESIA: sl = "ID"; break;
745 case SUBLANG_ENGLISH_HONGKONG: sl = "HK"; break;
746 case SUBLANG_ENGLISH_INDIA: sl = "IN"; break;
747 case SUBLANG_ENGLISH_MALAYSIA: sl = "MY"; break;
748 case SUBLANG_ENGLISH_SINGAPORE: sl = "SG"; break;
750 break;
751 case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
752 case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
753 case LANG_FARSI: l = "fa"; sl = "IR"; break;
754 case LANG_FINNISH: l = "fi"; sl = "FI"; break;
755 case LANG_FRENCH:
756 l = "fr";
757 switch (sub)
759 case SUBLANG_FRENCH: sl = "FR"; break;
760 case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
761 case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
762 case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
763 case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
764 case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
765 case SUBLANG_FRENCH_WESTINDIES: break;
766 case SUBLANG_FRENCH_REUNION: sl = "RE"; break;
767 case SUBLANG_FRENCH_CONGO: sl = "CG"; break;
768 case SUBLANG_FRENCH_SENEGAL: sl = "SN"; break;
769 case SUBLANG_FRENCH_CAMEROON: sl = "CM"; break;
770 case SUBLANG_FRENCH_COTEDIVOIRE: sl = "CI"; break;
771 case SUBLANG_FRENCH_MALI: sl = "ML"; break;
772 case SUBLANG_FRENCH_MOROCCO: sl = "MA"; break;
773 case SUBLANG_FRENCH_HAITI: sl = "HT"; break;
775 break;
776 case LANG_FRISIAN: l = "fy"; sl ="NL"; break;
777 case LANG_FULFULDE: l = "ful"; sl = "NG"; break;
778 case LANG_GAELIC:
779 switch (sub)
781 case 0x01: /* SCOTTISH */ l = "gd"; sl = "GB"; break;
782 case 0x02: /* IRISH */ l = "ga"; sl = "IE"; break;
784 break;
785 case LANG_GALICIAN: l = "gl"; sl = "ES"; break;
786 case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
787 case LANG_GERMAN:
788 l = "de";
789 switch (sub)
791 case SUBLANG_GERMAN: sl = "DE"; break;
792 case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
793 case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
794 case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
795 case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
797 break;
798 case LANG_GREEK: l = "el"; sl = "GR"; break;
799 case LANG_GUARANI: l = "gn"; sl = "PY"; break;
800 case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
801 case LANG_HAUSA: l = "ha"; sl = "NG"; break;
802 case LANG_HAWAIIAN:
803 /* FIXME: Do they mean Hawaiian ("haw_US", 1000 speakers)
804 * or Hawaii Creole English ("cpe_US", 600000 speakers)?
806 l = "cpe";
807 sl = "US";
808 break;
809 case LANG_HEBREW: l = "he"; sl = "IL"; break;
810 case LANG_HINDI: l = "hi"; sl = "IN"; break;
811 case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
812 case LANG_IBIBIO: l = "nic"; sl = "NG"; break;
813 case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
814 case LANG_IGBO: l = "ibo"; sl = "NG"; break;
815 case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
816 case LANG_INUKTITUT: l = "iu"; sl = "CA"; break;
817 case LANG_ITALIAN:
818 l = "it";
819 switch (sub)
821 case SUBLANG_ITALIAN: sl = "IT"; break;
822 case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
824 break;
825 case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
826 case LANG_KANNADA: l = "kn"; sl = "IN"; break;
827 case LANG_KANURI: l = "kau"; sl = "NG"; break;
828 case LANG_KASHMIRI:
829 l = "ks";
830 switch (sub)
832 case SUBLANG_DEFAULT: sl = "PK"; break;
833 case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
835 break;
836 case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
837 case LANG_KONKANI:
838 /* FIXME: Adjust this when such locales appear on Unix. */
839 l = "kok";
840 sl = "IN";
841 break;
842 case LANG_KOREAN: l = "ko"; sl = "KR"; break;
843 case LANG_KYRGYZ: l = "ky"; sl = "KG"; break;
844 case LANG_LAO: l = "lo"; sl = "LA"; break;
845 case LANG_LATIN: l = "la"; sl = "VA"; break;
846 case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
847 case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
848 case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
849 case LANG_MALAY:
850 l = "ms";
851 switch (sub)
853 case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
854 case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
856 break;
857 case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
858 case LANG_MANIPURI:
859 /* FIXME: Adjust this when such locales appear on Unix. */
860 l = "mni";
861 sl = "IN";
862 break;
863 case LANG_MARATHI: l = "mr"; sl = "IN"; break;
864 case LANG_MONGOLIAN:
865 /* Ambiguous: could be "mn_CN" or "mn_MN". */
866 l = "mn";
867 break;
868 case LANG_NEPALI:
869 l = "ne";
870 switch (sub)
872 case SUBLANG_DEFAULT: sl = "NP"; break;
873 case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
875 break;
876 case LANG_NORWEGIAN:
877 l = "no";
878 switch (sub)
880 case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
881 case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
883 break;
884 case LANG_ORIYA: l = "or"; sl = "IN"; break;
885 case LANG_OROMO: l = "om"; sl = "ET"; break;
886 case LANG_PAPIAMENTU: l = "pap"; sl = "AN"; break;
887 case LANG_PASHTO:
888 /* Ambiguous: could be "ps_PK" or "ps_AF". */
889 l = "ps";
890 break;
891 case LANG_POLISH: l = "pl"; sl = "PL"; break;
892 case LANG_PORTUGUESE:
893 l = "pt";
894 switch (sub)
896 case SUBLANG_PORTUGUESE: sl = "PT"; break;
897 case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
899 break;
900 case LANG_PUNJABI:
901 l = "pa";
902 switch (sub)
904 case SUBLANG_PUNJABI_INDIA: sl = "IN"; break; /* Gurmukhi script */
905 case SUBLANG_PUNJABI_PAKISTAN: sl = "PK"; break; /* Arabic script */
907 break;
908 case LANG_RHAETO_ROMANCE: l = "rm"; sl = "CH"; break;
909 case LANG_ROMANIAN:
910 l = "ro";
911 switch (sub)
913 case SUBLANG_ROMANIAN_ROMANIA: sl = "RO"; break;
914 case SUBLANG_ROMANIAN_MOLDOVA: sl = "MD"; break;
916 break;
917 case LANG_RUSSIAN:
918 l = "ru";/* Ambiguous: could be "ru_RU" or "ru_UA" or "ru_MD". */
919 break;
920 case LANG_SAAMI: /* actually Northern Sami */ l = "se"; sl = "NO"; break;
921 case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
922 case LANG_SINDHI: l = "sd";
923 switch (sub)
925 case SUBLANG_SINDHI_INDIA: sl = "IN"; break;
926 case SUBLANG_SINDHI_PAKISTAN: sl = "PK"; break;
928 break;
929 case LANG_SINHALESE: l = "si"; sl = "LK"; break;
930 case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
931 case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
932 case LANG_SOMALI: l = "so"; sl = "SO"; break;
933 case LANG_SORBIAN:
934 /* FIXME: Adjust this when such locales appear on Unix. */
935 l = "wen";
936 sl = "DE";
937 break;
938 case LANG_SPANISH:
939 l = "es";
940 switch (sub)
942 case SUBLANG_SPANISH: sl = "ES"; break;
943 case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
944 case SUBLANG_SPANISH_MODERN:
945 sl = "ES@modern"; break; /* not seen on Unix */
946 case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
947 case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
948 case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
949 case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
950 case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
951 case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
952 case SUBLANG_SPANISH_PERU: sl = "PE"; break;
953 case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
954 case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
955 case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
956 case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
957 case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
958 case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
959 case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
960 case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
961 case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
962 case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
964 break;
965 case LANG_SUTU: l = "bnt"; sl = "TZ"; break; /* or "st_LS" or "nso_ZA"? */
966 case LANG_SWAHILI: l = "sw"; sl = "KE"; break;
967 case LANG_SWEDISH:
968 l = "sv";
969 switch (sub)
971 case SUBLANG_DEFAULT: sl = "SE"; break;
972 case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
974 break;
975 case LANG_SYRIAC: l = "syr"; sl = "TR"; break; /* An extinct language. */
976 case LANG_TAGALOG: l = "tl"; sl = "PH"; break;
977 case LANG_TAJIK: l = "tg"; sl = "TJ"; break;
978 case LANG_TAMIL:
979 l = "ta"; /* Ambiguous: could be "ta_IN" or "ta_LK" or "ta_SG". */
980 break;
981 case LANG_TATAR: l = "tt"; sl = "RU"; break;
982 case LANG_TELUGU: l = "te"; sl = "IN"; break;
983 case LANG_THAI: l = "th"; sl = "TH"; break;
984 case LANG_TIBETAN: l = "bo"; sl = "CN"; break;
985 case LANG_TIGRINYA:
986 l = "ti";
987 switch (sub)
989 case SUBLANG_TIGRINYA_ETHIOPIA: sl = "ET"; break;
990 case SUBLANG_TIGRINYA_ERITREA: sl = "ER"; break;
992 break;
993 case LANG_TSONGA: l = "ts"; sl = "ZA"; break;
994 case LANG_TSWANA: l = "tn"; sl = "BW"; break;
995 case LANG_TURKISH: l = "tr"; sl = "TR"; break;
996 case LANG_TURKMEN: l = "tk"; sl = "TM"; break;
997 case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
998 case LANG_URDU:
999 l = "ur";
1000 switch (sub)
1002 case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
1003 case SUBLANG_URDU_INDIA: sl = "IN"; break;
1005 break;
1006 case LANG_UZBEK:
1007 l = "uz";
1008 switch (sub)
1010 case SUBLANG_UZBEK_LATIN: sl = "UZ"; break;
1011 case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
1013 break;
1014 case LANG_VENDA:
1015 /* FIXME: It's not clear whether Venda has the ISO 639-2 two-letter code
1016 "ve" or not.
1017 http://www.loc.gov/standards/iso639-2/englangn.html has it, but
1018 http://lcweb.loc.gov/standards/iso639-2/codechanges.html doesn't, */
1019 l = "ven"; /* or "ve"? */
1020 sl = "ZA";
1021 break;
1022 case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
1023 case LANG_WELSH: l = "cy"; sl = "GB"; break;
1024 case LANG_XHOSA: l = "xh"; sl = "ZA"; break;
1025 case LANG_YI: l = "sit"; sl = "CN"; break;
1026 case LANG_YIDDISH: l = "yi"; sl = "IL"; break;
1027 case LANG_YORUBA: l = "yo"; sl = "NG"; break;
1028 case LANG_ZULU: l = "zu"; sl = "ZA"; break;
1030 strcpy (bfr, l);
1031 if (sl != NULL)
1033 if (sl[0] != '@')
1034 strcat (bfr, "_");
1035 strcat (bfr, sl);
1038 return g_strdup (bfr);
1042 * g_win32_error_message:
1043 * @error: error code.
1045 * Translate a Win32 error code (as returned by GetLastError()) into
1046 * the corresponding message. The message is either language neutral,
1047 * or in the thread's language, or the user's language, the system's
1048 * language, or US English (see docs for FormatMessage()). The
1049 * returned string is in UTF-8. It should be deallocated with
1050 * g_free().
1052 * Returns: newly-allocated error message
1054 gchar *
1055 g_win32_error_message (gint error)
1057 gchar *retval;
1058 wchar_t *msg = NULL;
1059 int nchars;
1061 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
1062 |FORMAT_MESSAGE_IGNORE_INSERTS
1063 |FORMAT_MESSAGE_FROM_SYSTEM,
1064 NULL, error, 0,
1065 (LPWSTR) &msg, 0, NULL);
1066 if (msg != NULL)
1068 nchars = wcslen (msg);
1070 if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
1071 msg[nchars-2] = '\0';
1073 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
1075 LocalFree (msg);
1077 else
1078 retval = g_strdup ("");
1080 return retval;
1083 static gchar *
1084 get_package_directory_from_module (gchar *module_name)
1086 static GHashTable *module_dirs = NULL;
1087 G_LOCK_DEFINE_STATIC (module_dirs);
1088 HMODULE hmodule = NULL;
1089 gchar *fn;
1090 gchar *p;
1091 gchar *result;
1092 wchar_t wc_fn[MAX_PATH];
1094 G_LOCK (module_dirs);
1096 if (module_dirs == NULL)
1097 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
1099 result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
1101 if (result)
1103 G_UNLOCK (module_dirs);
1104 return g_strdup (result);
1107 if (module_name)
1109 wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
1110 hmodule = GetModuleHandleW (wc_module_name);
1111 g_free (wc_module_name);
1113 if (!hmodule)
1114 return NULL;
1117 if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
1119 G_UNLOCK (module_dirs);
1120 return NULL;
1122 fn = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
1124 if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
1125 *p = '\0';
1127 p = strrchr (fn, G_DIR_SEPARATOR);
1128 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
1129 g_ascii_strcasecmp (p + 1, "lib") == 0))
1130 *p = '\0';
1132 #ifdef G_WITH_CYGWIN
1133 /* In Cygwin we need to have POSIX paths */
1135 gchar tmp[MAX_PATH];
1137 cygwin_conv_to_posix_path(fn, tmp);
1138 g_free(fn);
1139 fn = g_strdup(tmp);
1141 #endif
1143 g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
1145 G_UNLOCK (module_dirs);
1147 return g_strdup (fn);
1151 * g_win32_get_package_installation_directory:
1152 * @package: An identifier for a software package, or %NULL, in UTF-8
1153 * @dll_name: The name of a DLL that a package provides, or %NULL, in UTF-8
1155 * Try to determine the installation directory for a software package.
1156 * Typically used by GNU software packages.
1158 * @package should be a short identifier for the package. Typically it
1159 * is the same identifier as used for
1160 * <literal>GETTEXT_PACKAGE</literal> in software configured according
1161 * to GNU standards. The function first looks in the Windows Registry
1162 * for the value <literal>&num;InstallationDirectory</literal> in the key
1163 * <literal>&num;HKLM\Software\@package</literal>, and if that value
1164 * exists and is a string, returns that.
1166 * If @package is %NULL, or the above value isn't found in the
1167 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
1168 * into the current process. Typically that would be the name of the
1169 * DLL calling this function, looking for its installation
1170 * directory. The function then asks Windows what directory that DLL
1171 * was loaded from. If that directory's last component is "bin" or
1172 * "lib", the parent directory is returned, otherwise the directory
1173 * itself. If that DLL isn't loaded, the function proceeds as if
1174 * @dll_name was %NULL.
1176 * If both @package and @dll_name are %NULL, the directory from where
1177 * the main executable of the process was loaded is used instead in
1178 * the same way as above.
1180 * Returns: a string containing the installation directory for
1181 * @package. The string is in the GLib file name encoding, i.e. UTF-8
1182 * on Windows. The return value should be freed with g_free() when not
1183 * needed any longer.
1186 gchar *
1187 g_win32_get_package_installation_directory (gchar *package,
1188 gchar *dll_name)
1190 static GHashTable *package_dirs = NULL;
1191 G_LOCK_DEFINE_STATIC (package_dirs);
1192 gchar *result = NULL;
1193 gchar *key;
1194 wchar_t *wc_key;
1195 HKEY reg_key = NULL;
1196 DWORD type;
1197 DWORD nbytes;
1199 if (package != NULL)
1201 G_LOCK (package_dirs);
1203 if (package_dirs == NULL)
1204 package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
1206 result = g_hash_table_lookup (package_dirs, package);
1208 if (result && result[0])
1210 G_UNLOCK (package_dirs);
1211 return g_strdup (result);
1214 key = g_strconcat ("Software\\", package, NULL);
1216 nbytes = 0;
1218 wc_key = g_utf8_to_utf16 (key, -1, NULL, NULL, NULL);
1219 if (((RegOpenKeyExW (HKEY_CURRENT_USER, wc_key, 0,
1220 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
1221 && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
1222 &type, NULL, &nbytes) == ERROR_SUCCESS)
1224 (RegOpenKeyExW (HKEY_LOCAL_MACHINE, wc_key, 0,
1225 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
1226 && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
1227 &type, NULL, &nbytes) == ERROR_SUCCESS))
1228 && type == REG_SZ)
1230 wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
1231 RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
1232 &type, (LPBYTE) wc_temp, &nbytes);
1233 wc_temp[nbytes/2] = '\0';
1234 result = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
1235 g_free (wc_temp);
1237 g_free (wc_key);
1239 if (reg_key != NULL)
1240 RegCloseKey (reg_key);
1242 g_free (key);
1244 if (result)
1246 g_hash_table_insert (package_dirs, g_strdup (package), result);
1247 G_UNLOCK (package_dirs);
1248 return g_strdup (result);
1250 G_UNLOCK (package_dirs);
1253 if (dll_name != NULL)
1254 result = get_package_directory_from_module (dll_name);
1256 if (result == NULL)
1257 result = get_package_directory_from_module (NULL);
1259 return result;
1262 #undef g_win32_get_package_installation_directory
1264 /* DLL ABI binary compatibility version that uses system codepage file names */
1266 gchar *
1267 g_win32_get_package_installation_directory (gchar *package,
1268 gchar *dll_name)
1270 gchar *utf8_package = NULL, *utf8_dll_name = NULL;
1271 gchar *utf8_retval, *retval;
1273 if (package != NULL)
1274 utf8_package = g_locale_to_utf8 (package, -1, NULL, NULL, NULL);
1276 if (dll_name != NULL)
1277 utf8_dll_name = g_locale_to_utf8 (dll_name, -1, NULL, NULL, NULL);
1279 utf8_retval =
1280 g_win32_get_package_installation_directory_utf8 (utf8_package,
1281 utf8_dll_name);
1283 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
1285 g_free (utf8_package);
1286 g_free (utf8_dll_name);
1287 g_free (utf8_retval);
1289 return retval;
1293 * g_win32_get_package_installation_subdirectory:
1294 * @package: An identifier for a software package, in UTF-8, or %NULL
1295 * @dll_name: The name of a DLL that a package provides, in UTF-8, or %NULL
1296 * @subdir: A subdirectory of the package installation directory, also in UTF-8
1298 * Returns a newly-allocated string containing the path of the
1299 * subdirectory @subdir in the return value from calling
1300 * g_win32_get_package_installation_directory() with the @package and
1301 * @dll_name parameters.
1303 * Returns: a string containing the complete path to @subdir inside
1304 * the installation directory of @package. The returned string is in
1305 * the GLib file name encoding, i.e. UTF-8 on Windows. The return
1306 * value should be freed with g_free() when no longer needed.
1309 gchar *
1310 g_win32_get_package_installation_subdirectory (gchar *package,
1311 gchar *dll_name,
1312 gchar *subdir)
1314 gchar *prefix;
1315 gchar *dirname;
1317 prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
1319 dirname = g_build_filename (prefix, subdir, NULL);
1320 g_free (prefix);
1322 return dirname;
1325 #undef g_win32_get_package_installation_subdirectory
1327 /* DLL ABI binary compatibility version that uses system codepage file names */
1329 gchar *
1330 g_win32_get_package_installation_subdirectory (gchar *package,
1331 gchar *dll_name,
1332 gchar *subdir)
1334 gchar *prefix;
1335 gchar *dirname;
1337 prefix = g_win32_get_package_installation_directory (package, dll_name);
1339 dirname = g_build_filename (prefix, subdir, NULL);
1340 g_free (prefix);
1342 return dirname;
1345 static guint windows_version;
1347 static void
1348 g_win32_windows_version_init (void)
1350 static gboolean beenhere = FALSE;
1352 if (!beenhere)
1354 beenhere = TRUE;
1355 windows_version = GetVersion ();
1357 if (windows_version & 0x80000000)
1358 g_error ("This version of GLib requires NT-based Windows.");
1362 void
1363 _g_win32_thread_init (void)
1365 g_win32_windows_version_init ();
1369 * g_win32_get_windows_version:
1371 * Returns version information for the Windows operating system the
1372 * code is running on. See MSDN documentation for the GetVersion()
1373 * function. To summarize, the most significant bit is one on Win9x,
1374 * and zero on NT-based systems. Since version 2.14, GLib works only
1375 * on NT-based systems, so checking whether your are running on Win9x
1376 * in your own software is moot. The least significant byte is 4 on
1377 * Windows NT 4, and 5 on Windows XP. Software that needs really
1378 * detailled version and feature information should use Win32 API like
1379 * GetVersionEx() and VerifyVersionInfo().
1381 * Returns: The version information.
1383 * Since: 2.6
1385 guint
1386 g_win32_get_windows_version (void)
1388 g_win32_windows_version_init ();
1390 return windows_version;
1394 * g_win32_locale_filename_from_utf8:
1395 * @utf8filename: a UTF-8 encoded filename.
1397 * Converts a filename from UTF-8 to the system codepage.
1399 * On NT-based Windows, on NTFS file systems, file names are in
1400 * Unicode. It is quite possible that Unicode file names contain
1401 * characters not representable in the system codepage. (For instance,
1402 * Greek or Cyrillic characters on Western European or US Windows
1403 * installations, or various less common CJK characters on CJK Windows
1404 * installations.)
1406 * In such a case, and if the filename refers to an existing file, and
1407 * the file system stores alternate short (8.3) names for directory
1408 * entries, the short form of the filename is returned. Note that the
1409 * "short" name might in fact be longer than the Unicode name if the
1410 * Unicode name has very short pathname components containing
1411 * non-ASCII characters. If no system codepage name for the file is
1412 * possible, %NULL is returned.
1414 * The return value is dynamically allocated and should be freed with
1415 * g_free() when no longer needed.
1417 * Return value: The converted filename, or %NULL on conversion
1418 * failure and lack of short names.
1420 * Since: 2.8
1422 gchar *
1423 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
1425 gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);
1427 if (retval == NULL)
1429 /* Conversion failed, so convert to wide chars, check if there
1430 * is a 8.3 version, and use that.
1432 wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
1433 if (wname != NULL)
1435 wchar_t wshortname[MAX_PATH + 1];
1436 if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
1438 gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
1439 retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
1440 g_free (tem);
1442 g_free (wname);
1445 return retval;
1448 #define __G_WIN32_C__
1449 #include "galiasdef.c"