]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/loclibrary.c
mDNSResponder-108.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 = 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 * ext;
122 char * app;
123
124 GetModuleFileNameA( module, folder, MAX_PATH );
125
126 // Get folder string
127
128 app = strrchr( folder, '\\' );
129 require_action( app, exit, ret = 0 );
130 *app++ = '\0';
131
132 // Strip the extension
133
134 if ( ( ( ext = strstr( app, ".exe" ) ) != NULL ) || ( ( ext = strstr( app, ".dll" ) ) != NULL ) )
135 {
136 *ext = '\0';
137 }
138
139 snprintf( appPathNameA, MAX_PATH, "%s\\%s", folder, app );
140 }
141
142 ret = PathForResourceWithPathA (appPathNameA, name, locFile, locFileLen);
143
144 exit:
145
146 return ret;
147 }
148
149 static wchar_t appPathNameW[MAX_PATH] = L"";
150
151 int PathForResourceW ( HMODULE module, const wchar_t *name, wchar_t *locFile, int locFileLen)
152 {
153 int ret = 0;
154
155 if ( !wcscmp( appPathNameW, L"" ) )
156 {
157 wchar_t folder[MAX_PATH];
158 wchar_t * app;
159 wchar_t * ext;
160
161 GetModuleFileNameW( module, folder, MAX_PATH);
162
163 // Get folder string
164
165 app = wcsrchr( folder, '\\' );
166 require_action( app, exit, ret = 0 );
167 *app++ = '\0';
168
169 // Strip the extension
170
171 if ( ( ( ext = wcsstr( app, L".exe" ) ) != NULL ) || ( ( ext = wcsstr( app, L".dll" ) ) != NULL ) )
172 {
173 *ext = '\0';
174 }
175
176 swprintf( appPathNameW, MAX_PATH, L"%ls\\%ls", folder, app );
177 }
178
179 ret = PathForResourceWithPathW (appPathNameW, name, locFile, locFileLen);
180
181 exit:
182
183 return ret;
184 }
185
186
187 //// PathForResourceWithPath
188
189 #define TMP_BUF_SIZE MAX_PATH
190
191 int PathForResourceWithPathA (const char *path, const char *nm,
192 char *locFile, int locFileLen) {
193 char tmpBuffer[TMP_BUF_SIZE];
194
195 // build the path to the executable in the generic
196 // resources folder, check there first
197 snprintf(tmpBuffer, MAX_PATH, "%s.Resources\\%s", path, nm);
198
199 if (!PathFileExistsA(tmpBuffer)) {
200
201 // didn't hit generic resource folder, so need to get language codes
202 _setLanguageIfNeeded();
203
204 // test to see if localized directory exists,
205 // if so, we don't fall back if we don't find the file.
206 snprintf(tmpBuffer, TMP_BUF_SIZE,
207 "%s.Resources\\%s.lproj", path, isoLangCode);
208
209 if (PathFileExistsA(tmpBuffer)) {
210 snprintf(tmpBuffer, TMP_BUF_SIZE, "%s\\%s", tmpBuffer, nm);
211
212 if (!PathFileExistsA(tmpBuffer)) return 0;
213
214 strncpy(locFile, tmpBuffer, locFileLen);
215 return (int) strlen(locFile);
216 }
217
218 // fall back on DEFAULT_LANG_CODE if still no good
219 snprintf(tmpBuffer, TMP_BUF_SIZE, "%s.Resources\\%s.lproj\\%s",
220 path, DEFAULT_LANG_CODE, nm);
221
222 // we can't find the resource, so return 0
223 if (!PathFileExistsA(tmpBuffer)) return 0;
224 }
225
226 strncpy(locFile, tmpBuffer, locFileLen);
227 return (int) strlen(locFile);
228
229 }
230
231
232 int PathForResourceWithPathW (const wchar_t *path, const wchar_t *nm,
233 wchar_t *locFile, int locFileLen) {
234
235 wchar_t tmpBuffer[TMP_BUF_SIZE];
236
237 // build the path to the executable in the generic
238 // resources folder, check there first
239 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls.Resources\\%ls", path, nm);
240
241 if (!PathFileExistsW(tmpBuffer)) {
242 // didn't hit generic resource folder, so need to get language codes
243 _setLanguageIfNeeded();
244
245 // test to see if localized directory exists,
246 // if so, we don't fall back if we don't find the file.
247 swprintf(tmpBuffer, TMP_BUF_SIZE,
248 L"%ls.Resources\\%S.lproj", path, isoLangCode);
249
250 if (PathFileExistsW(tmpBuffer)) {
251 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls\\%ls", tmpBuffer, nm);
252
253 if (!PathFileExistsW(tmpBuffer)) return 0;
254
255 wcsncpy(locFile, tmpBuffer, locFileLen);
256 return (int) wcslen(locFile);
257 }
258
259 // fall back on DEFAULT_LANG_CODE if still no good
260 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls.Resources\\%S.lproj\\%ls",
261 path, DEFAULT_LANG_CODE, nm);
262
263 // we can't find the resource, so return 0
264 if (!PathFileExistsW(tmpBuffer)) return 0;
265 }
266
267 wcsncpy(locFile, tmpBuffer, locFileLen);
268 return (int) wcslen(locFile);
269
270
271 }
272
273
274
275 #ifdef __cplusplus
276 }
277 #endif