Merge remote-tracking branch 'origin/master' into YaSp
[scilab.git] / scilab / modules / localization / src / c / getLocaleInfo_Windows.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2008 - INRIA - Allan CORNET
4  * Copyright (C) 2008 - Yung-Jang Lee
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13
14 /*--------------------------------------------------------------------------*/
15 #include <windows.h>
16 #include <stdio.h>
17 #include "getLocaleInfo_Windows.h"
18 #include "MALLOC.h"
19 #include "os_swprintf.h"
20 /*--------------------------------------------------------------------------*/
21 char* getLocaleSystemInfo(void)
22 {
23         #define LENGTH_BUFFER 1024
24         char buffer_LOCALE_SISO639LANGNAME[LENGTH_BUFFER];
25         char buffer_LOCALE_SISO3166CTRYNAME[LENGTH_BUFFER];
26         char *localeStr = NULL;
27         int ret = 0;
28         ret = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,
29                 LOCALE_SISO639LANGNAME,
30                 &buffer_LOCALE_SISO639LANGNAME[0],
31                 LENGTH_BUFFER);
32         if (ret > 0)
33         {
34                 ret = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,
35                         LOCALE_SISO3166CTRYNAME,
36                         &buffer_LOCALE_SISO3166CTRYNAME[0],
37                         LENGTH_BUFFER);
38                 if (ret >0)
39                 {
40                         int length_localeStr = (int)(strlen(buffer_LOCALE_SISO639LANGNAME)+
41                                 strlen(buffer_LOCALE_SISO3166CTRYNAME)+
42                                 strlen("_"));
43                         localeStr = (char*)MALLOC(sizeof(char)*(length_localeStr)+1);
44                         if (localeStr)
45                         {
46                                 #ifdef FORMAT_LOCALE
47                                         #undef FORMAT_LOCALE
48                                 #endif
49                                 #define FORMAT_LOCALE "%s_%s"
50                                 sprintf(localeStr,FORMAT_LOCALE,buffer_LOCALE_SISO639LANGNAME,buffer_LOCALE_SISO3166CTRYNAME);
51                         }
52                 }
53         }
54         return localeStr;
55 }
56 /*--------------------------------------------------------------------------*/
57 wchar_t* getLocaleUserInfo(void)
58 {
59         #define LENGTH_BUFFER 1024
60         wchar_t buffer_LOCALE_SISO639LANGNAME[LENGTH_BUFFER];
61         wchar_t buffer_LOCALE_SISO3166CTRYNAME[LENGTH_BUFFER];
62         wchar_t buffer_LOCALE_IDEFAULTANSICODEPAGE[LENGTH_BUFFER]; 
63         wchar_t *localeStr = NULL;
64         int ret = 0;
65         ret = GetLocaleInfoW(LOCALE_USER_DEFAULT,
66                                                 LOCALE_SISO639LANGNAME,
67                                                 buffer_LOCALE_SISO639LANGNAME,
68                                                 LENGTH_BUFFER);
69         if (ret > 0)
70         {
71
72                 ret = GetLocaleInfoW(LOCALE_USER_DEFAULT,
73                                                         LOCALE_SISO3166CTRYNAME,
74                                                         buffer_LOCALE_SISO3166CTRYNAME,
75                                                         LENGTH_BUFFER);
76                 if (ret >0)
77                 {
78             // BY YJLee get Windows CODE Page Information
79                         int ret = GetLocaleInfoW(LOCALE_USER_DEFAULT,
80                                                 LOCALE_IDEFAULTANSICODEPAGE,
81                                                 buffer_LOCALE_IDEFAULTANSICODEPAGE,
82                                                 LENGTH_BUFFER);
83
84                         int length_localeStr = (int)(wcslen(buffer_LOCALE_SISO639LANGNAME)+
85                                                                                  wcslen(buffer_LOCALE_SISO3166CTRYNAME)+
86                                                                                  wcslen(buffer_LOCALE_IDEFAULTANSICODEPAGE)+
87                                                                                  wcslen(L"_.CP") );
88                         localeStr = (wchar_t*)MALLOC(sizeof(wchar_t) * (length_localeStr + 1));
89                         if (localeStr)
90                         {
91                                 #ifdef FORMAT_LOCALE
92                                         #undef FORMAT_LOCALE
93                                 #endif
94                                 #define FORMAT_LOCALE L"%s_%s.CP%s"
95                                 // in lang_contry.CPxxx format
96                 os_swprintf(localeStr, length_localeStr + 1, FORMAT_LOCALE,
97                                                       buffer_LOCALE_SISO639LANGNAME,
98                                                                   buffer_LOCALE_SISO3166CTRYNAME,
99                                                                   buffer_LOCALE_IDEFAULTANSICODEPAGE);
100                         }
101                 }
102         }
103         return localeStr;
104 }
105 /*--------------------------------------------------------------------------*/