]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/loclibrary.c
mDNSResponder-107.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / loclibrary.c
1 /*
2 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22
23 Change History (most recent first):
24
25 */
26
27 /* loclibrary.c
28 * ----------------------------------------------------------------------
29 * Source for localization library
30 * Originally created by jsantamaria: 3 may 2004
31 * ----------------------------------------------------------------------
32 */
33
34 #include "DebugServices.h"
35 #include <windows.h>
36 #include <stdio.h>
37 #include "isocode.h"
38 #include "loclibrary.h"
39 #include "Shlwapi.h"
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <wchar.h>
43
44
45 #ifdef __cplusplus
46 extern "c" {
47 #endif
48
49 #ifdef _MSC_VER
50 #define swprintf _snwprintf
51 #define snprintf _snprintf
52 #endif
53
54
55
56 #define DEFAULT_LANG_CODE "en"
57
58 // gets the user language
59 static LANGID _getUserLanguage( void ) {
60
61 return GetUserDefaultUILanguage();
62
63 }
64
65
66 // gets the ISO mapping
67 static int _getISOCode(LANGID wLangID, char *isoLangCode, int codeLen) {
68 int i;
69 unsigned short langCode;
70
71 for (i = 0; i < NUM_ISOCODES; i++) {
72 int startIndex = i * MODULO_ISOCODES;
73
74 langCode = (ISOCODES[startIndex] << 8);
75 langCode += ( (unsigned short) (ISOCODES[startIndex + 1]) );
76
77 if (langCode == wLangID) {
78 char *langStr = (char *)&(ISOCODES[startIndex+2]);
79 strncpy(isoLangCode, langStr, codeLen);
80 return 0;
81 }
82 }
83 return 1;
84 }
85
86 static char isoLangCode[LANG_CODE_LEN + 1] = "";
87 static LANGID wLangID = (LANGID) -1;
88
89 static void _setLanguageIfNeeded(void) {
90
91 // get the language code if we don't have it cached
92 if (!strncmp(isoLangCode,"",LANG_CODE_LEN + 1)) {
93
94 // if we haven't cached the language id, do the lookup
95 if (wLangID == (LANGID) -1) {
96 wLangID = _getUserLanguage();
97 }
98
99 // if no ISOCode, set it to DEFAULT_LANG_CODE
100 if (_getISOCode(wLangID, isoLangCode, LANG_CODE_LEN + 1)) {
101 strncpy(isoLangCode, DEFAULT_LANG_CODE, LANG_CODE_LEN+1);
102 }
103 }
104
105 }
106
107 //// PathForResource
108
109 // Gets the PathForResource for handle 0 for the current process
110
111
112 static char appPathNameA[MAX_PATH] = "";
113
114 int PathForResourceA ( HMODULE module, const char *name, char *locFile, int locFileLen)
115 {
116 int ret = 0;
117
118 if ( !strcmp( appPathNameA, "" ) )
119 {
120 char folder[MAX_PATH];
121 char * app;
122
123 GetModuleFileNameA( module, folder, MAX_PATH );
124
125 // Get folder string
126
127 app = strrchr( folder, '\\' );
128 require_action( app, exit, ret = 0 );
129
130 *app++ = '\0';
131
132 snprintf( appPathNameA, MAX_PATH, "%s\\Resources\\%s", folder, app );
133 }
134
135 ret = PathForResourceWithPathA (appPathNameA, name, locFile, locFileLen);
136
137 exit:
138
139 return ret;
140 }
141
142 static wchar_t appPathNameW[MAX_PATH] = L"";
143
144 int PathForResourceW ( HMODULE module, const wchar_t *name, wchar_t *locFile, int locFileLen)
145 {
146 int ret = 0;
147
148 if ( !wcscmp( appPathNameW, L"" ) )
149 {
150 wchar_t folder[MAX_PATH];
151 wchar_t * app;
152
153 GetModuleFileNameW( module, folder, MAX_PATH);
154
155 // Get folder string
156
157 app = wcsrchr( folder, '\\' );
158 require_action( app, exit, ret = 0 );
159
160 *app++ = '\0';
161
162 swprintf( appPathNameW, MAX_PATH, L"%ls\\Resources\\%ls", folder, app );
163 }
164
165 ret = PathForResourceWithPathW (appPathNameW, name, locFile, locFileLen);
166
167 exit:
168
169 return ret;
170 }
171
172
173 //// PathForResourceWithPath
174
175 #define TMP_BUF_SIZE MAX_PATH
176
177 int PathForResourceWithPathA (const char *path, const char *nm,
178 char *locFile, int locFileLen) {
179 char tmpBuffer[TMP_BUF_SIZE];
180
181 // build the path to the executable in the generic
182 // resources folder, check there first
183 snprintf(tmpBuffer, MAX_PATH, "%s.Resources\\%s", path, nm);
184
185 if (!PathFileExistsA(tmpBuffer)) {
186
187 // didn't hit generic resource folder, so need to get language codes
188 _setLanguageIfNeeded();
189
190 // test to see if localized directory exists,
191 // if so, we don't fall back if we don't find the file.
192 snprintf(tmpBuffer, TMP_BUF_SIZE,
193 "%s.Resources\\%s.lproj", path, isoLangCode);
194
195 if (PathFileExistsA(tmpBuffer)) {
196 snprintf(tmpBuffer, TMP_BUF_SIZE, "%s\\%s", tmpBuffer, nm);
197
198 if (!PathFileExistsA(tmpBuffer)) return 0;
199
200 strncpy(locFile, tmpBuffer, locFileLen);
201 return (int) strlen(locFile);
202 }
203
204 // fall back on DEFAULT_LANG_CODE if still no good
205 snprintf(tmpBuffer, TMP_BUF_SIZE, "%s.Resources\\%s.lproj\\%s",
206 path, DEFAULT_LANG_CODE, nm);
207
208 // we can't find the resource, so return 0
209 if (!PathFileExistsA(tmpBuffer)) return 0;
210 }
211
212 strncpy(locFile, tmpBuffer, locFileLen);
213 return (int) strlen(locFile);
214
215 }
216
217
218 int PathForResourceWithPathW (const wchar_t *path, const wchar_t *nm,
219 wchar_t *locFile, int locFileLen) {
220
221 wchar_t tmpBuffer[TMP_BUF_SIZE];
222
223 // build the path to the executable in the generic
224 // resources folder, check there first
225 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls.Resources\\%ls", path, nm);
226
227 if (!PathFileExistsW(tmpBuffer)) {
228 // didn't hit generic resource folder, so need to get language codes
229 _setLanguageIfNeeded();
230
231 // test to see if localized directory exists,
232 // if so, we don't fall back if we don't find the file.
233 swprintf(tmpBuffer, TMP_BUF_SIZE,
234 L"%ls.Resources\\%S.lproj", path, isoLangCode);
235
236 if (PathFileExistsW(tmpBuffer)) {
237 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls\\%ls", tmpBuffer, nm);
238
239 if (!PathFileExistsW(tmpBuffer)) return 0;
240
241 wcsncpy(locFile, tmpBuffer, locFileLen);
242 return (int) wcslen(locFile);
243 }
244
245 // fall back on DEFAULT_LANG_CODE if still no good
246 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls.Resources\\%S.lproj\\%ls",
247 path, DEFAULT_LANG_CODE, nm);
248
249 // we can't find the resource, so return 0
250 if (!PathFileExistsW(tmpBuffer)) return 0;
251 }
252
253 wcsncpy(locFile, tmpBuffer, locFileLen);
254 return (int) wcslen(locFile);
255
256
257 }
258
259
260
261 #ifdef __cplusplus
262 }
263 #endif