]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/loclibrary.c
78f16a48eb7a568f8f91e03993f063974e08cf71
[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 <windows.h>
35 #include <stdio.h>
36 #include "isocode.h"
37 #include "loclibrary.h"
38 #include "Shlwapi.h"
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <wchar.h>
42
43 #ifdef __cplusplus
44 extern "c" {
45 #endif
46
47 #ifdef _MSC_VER
48 #define swprintf _snwprintf
49 #define snprintf _snprintf
50 #endif
51
52
53
54 #define DEFAULT_LANG_CODE "en"
55
56 // gets the user language
57 static LANGID _getUserLanguage( void ) {
58
59 return GetUserDefaultUILanguage();
60
61 }
62
63
64 // gets the ISO mapping
65 static int _getISOCode(LANGID wLangID, char *isoLangCode, int codeLen) {
66 int i;
67 unsigned short langCode;
68
69 for (i = 0; i < NUM_ISOCODES; i++) {
70 int startIndex = i * MODULO_ISOCODES;
71
72 langCode = (ISOCODES[startIndex] << 8);
73 langCode += ( (unsigned short) (ISOCODES[startIndex + 1]) );
74
75 if (langCode == wLangID) {
76 char *langStr = (char *)&(ISOCODES[startIndex+2]);
77 strncpy(isoLangCode, langStr, codeLen);
78 return 0;
79 }
80 }
81 return 1;
82 }
83
84 static char isoLangCode[LANG_CODE_LEN + 1] = "";
85 static LANGID wLangID = (LANGID) -1;
86
87 static void _setLanguageIfNeeded(void) {
88
89 // get the language code if we don't have it cached
90 if (!strncmp(isoLangCode,"",LANG_CODE_LEN + 1)) {
91
92 // if we haven't cached the language id, do the lookup
93 if (wLangID == (LANGID) -1) {
94 wLangID = _getUserLanguage();
95 }
96
97 // if no ISOCode, set it to DEFAULT_LANG_CODE
98 if (_getISOCode(wLangID, isoLangCode, LANG_CODE_LEN + 1)) {
99 strncpy(isoLangCode, DEFAULT_LANG_CODE, LANG_CODE_LEN+1);
100 }
101 }
102
103 }
104
105 //// PathForResource
106
107 // Gets the PathForResource for handle 0 for the current process
108
109
110 static char appPathNameA[MAX_PATH] = "";
111
112 int PathForResourceA ( HMODULE module, const char *name, char *locFile, int locFileLen) {
113 if (!strcmp(appPathNameA,"")) {
114 GetModuleFileNameA(module, appPathNameA, MAX_PATH);
115 }
116
117 return PathForResourceWithPathA (appPathNameA, name, locFile, locFileLen);
118
119 }
120
121 static wchar_t appPathNameW[MAX_PATH] = L"";
122
123 int PathForResourceW ( HMODULE module, const wchar_t *name, wchar_t *locFile, int locFileLen) {
124 if (!wcscmp(appPathNameW,L"")) {
125 GetModuleFileNameW( module, appPathNameW, MAX_PATH);
126 }
127
128 OutputDebugString( appPathNameW );
129
130 return PathForResourceWithPathW (appPathNameW, name, locFile, locFileLen);
131 }
132
133
134 //// PathForResourceWithPath
135
136 #define TMP_BUF_SIZE MAX_PATH
137
138 int PathForResourceWithPathA (const char *path, const char *nm,
139 char *locFile, int locFileLen) {
140 char tmpBuffer[TMP_BUF_SIZE];
141
142 // build the path to the executable in the generic
143 // resources folder, check there first
144 snprintf(tmpBuffer, MAX_PATH, "%s.Resources\\%s", path, nm);
145
146 if (!PathFileExistsA(tmpBuffer)) {
147
148 // didn't hit generic resource folder, so need to get language codes
149 _setLanguageIfNeeded();
150
151 // test to see if localized directory exists,
152 // if so, we don't fall back if we don't find the file.
153 snprintf(tmpBuffer, TMP_BUF_SIZE,
154 "%s.Resources\\%s.lproj", path, isoLangCode);
155
156 if (PathFileExistsA(tmpBuffer)) {
157 snprintf(tmpBuffer, TMP_BUF_SIZE, "%s\\%s", tmpBuffer, nm);
158
159 if (!PathFileExistsA(tmpBuffer)) return 0;
160
161 strncpy(locFile, tmpBuffer, locFileLen);
162 return (int) strlen(locFile);
163 }
164
165 // fall back on DEFAULT_LANG_CODE if still no good
166 snprintf(tmpBuffer, TMP_BUF_SIZE, "%s.Resources\\%s.lproj\\%s",
167 path, DEFAULT_LANG_CODE, nm);
168
169 // we can't find the resource, so return 0
170 if (!PathFileExistsA(tmpBuffer)) return 0;
171 }
172
173 strncpy(locFile, tmpBuffer, locFileLen);
174 return (int) strlen(locFile);
175
176 }
177
178
179 int PathForResourceWithPathW (const wchar_t *path, const wchar_t *nm,
180 wchar_t *locFile, int locFileLen) {
181
182 wchar_t tmpBuffer[TMP_BUF_SIZE];
183
184 // build the path to the executable in the generic
185 // resources folder, check there first
186 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls.Resources\\%ls", path, nm);
187
188 if (!PathFileExistsW(tmpBuffer)) {
189 // didn't hit generic resource folder, so need to get language codes
190 _setLanguageIfNeeded();
191
192 // test to see if localized directory exists,
193 // if so, we don't fall back if we don't find the file.
194 swprintf(tmpBuffer, TMP_BUF_SIZE,
195 L"%ls.Resources\\%S.lproj", path, isoLangCode);
196
197 if (PathFileExistsW(tmpBuffer)) {
198 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls\\%ls", tmpBuffer, nm);
199
200 if (!PathFileExistsW(tmpBuffer)) return 0;
201
202 wcsncpy(locFile, tmpBuffer, locFileLen);
203 return (int) wcslen(locFile);
204 }
205
206 // fall back on DEFAULT_LANG_CODE if still no good
207 swprintf(tmpBuffer, TMP_BUF_SIZE, L"%ls.Resources\\%s.lproj\\%ls",
208 path, DEFAULT_LANG_CODE, nm);
209
210 // we can't find the resource, so return 0
211 if (!PathFileExistsW(tmpBuffer)) return 0;
212 }
213
214 wcsncpy(locFile, tmpBuffer, locFileLen);
215 return (int) wcslen(locFile);
216
217
218 }
219
220
221
222 #ifdef __cplusplus
223 }
224 #endif