comparison libgaim/win32/win32dep.c @ 14272:7635195195c0

[gaim-migrate @ 16957] -Updated the win32dep debug function to correctly match the core function (the core was apparently changed a while ago and this was never updated). -Updated wgaim_read_reg_string() to deal with non-ascii strings correctly, created corresponding wgaim_write_reg_string(). Use these in various places. -Update some win32dep arguments and return values to be correctly marked as having constant values. committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Mon, 21 Aug 2006 20:58:20 +0000
parents d1cb45dec12f
children 51685370de57
comparison
equal deleted inserted replaced
14271:9e5c28c3b163 14272:7635195195c0
61 /* 61 /*
62 * STATIC CODE 62 * STATIC CODE
63 */ 63 */
64 64
65 static void wgaim_debug_print(GaimDebugLevel level, const char *category, 65 static void wgaim_debug_print(GaimDebugLevel level, const char *category,
66 const char *format, va_list args) { 66 const char *arg_s) {
67 char *str = NULL; 67 if(category)
68 if (args != NULL) 68 printf("%s: %s", category, arg_s);
69 str = g_strdup_vprintf(format, args); 69 else
70 printf("%s%s%s", 70 printf(arg_s);
71 category ? category : "",
72 category ? ": " : "",
73 str ? str : format);
74 g_free(str);
75 } 71 }
76 72
77 static GaimDebugUiOps ops = { 73 static GaimDebugUiOps ops = {
78 wgaim_debug_print 74 wgaim_debug_print
79 }; 75 };
84 80
85 /* Escape windows dir separators. This is needed when paths are saved, 81 /* Escape windows dir separators. This is needed when paths are saved,
86 and on being read back have their '\' chars used as an escape char. 82 and on being read back have their '\' chars used as an escape char.
87 Returns an allocated string which needs to be freed. 83 Returns an allocated string which needs to be freed.
88 */ 84 */
89 char* wgaim_escape_dirsep(char* filename) { 85 char *wgaim_escape_dirsep(const char *filename) {
90 int sepcount = 0; 86 int sepcount = 0;
91 char* ret = NULL; 87 const char *tmp = filename;
88 char *ret;
92 int cnt = 0; 89 int cnt = 0;
93 90
94 ret = filename; 91 g_return_val_if_fail(filename != NULL, NULL);
95 while(*ret) { 92
96 if(*ret == '\\') 93 while(*tmp) {
94 if(*tmp == '\\')
97 sepcount++; 95 sepcount++;
98 ret++; 96 tmp++;
99 } 97 }
100 ret = g_malloc0(strlen(filename) + sepcount + 1); 98 ret = g_malloc0(strlen(filename) + sepcount + 1);
101 while(*filename) { 99 while(*filename) {
102 ret[cnt] = *filename; 100 ret[cnt] = *filename;
103 if(*filename == '\\') 101 if(*filename == '\\')
109 return ret; 107 return ret;
110 } 108 }
111 109
112 /* Determine whether the specified dll contains the specified procedure. 110 /* Determine whether the specified dll contains the specified procedure.
113 If so, load it (if not already loaded). */ 111 If so, load it (if not already loaded). */
114 FARPROC wgaim_find_and_loadproc(char* dllname, char* procedure) { 112 FARPROC wgaim_find_and_loadproc(const char *dllname, const char *procedure) {
115 HMODULE hmod; 113 HMODULE hmod;
116 BOOL did_load = FALSE; 114 BOOL did_load = FALSE;
117 FARPROC proc = 0; 115 FARPROC proc = 0;
118 116
119 if(!(hmod = GetModuleHandle(dllname))) { 117 if(!(hmod = GetModuleHandle(dllname))) {
180 } 178 }
181 179
182 return retval; 180 return retval;
183 } 181 }
184 182
185 char* wgaim_install_dir(void) { 183 const char *wgaim_install_dir(void) {
186 static gboolean initialized = FALSE; 184 static gboolean initialized = FALSE;
187 185
188 if (!initialized) { 186 if (!initialized) {
189 char *tmp = NULL; 187 char *tmp = NULL;
190 if (G_WIN32_HAVE_WIDECHAR_API()) { 188 if (G_WIN32_HAVE_WIDECHAR_API()) {
217 } 215 }
218 216
219 return install_dir; 217 return install_dir;
220 } 218 }
221 219
222 char* wgaim_lib_dir(void) { 220 const char *wgaim_lib_dir(void) {
223 static gboolean initialized = FALSE; 221 static gboolean initialized = FALSE;
224 222
225 if (!initialized) { 223 if (!initialized) {
226 char *inst_dir = wgaim_install_dir(); 224 const char *inst_dir = wgaim_install_dir();
227 if (inst_dir != NULL) { 225 if (inst_dir != NULL) {
228 lib_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "plugins", inst_dir); 226 lib_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "plugins", inst_dir);
229 initialized = TRUE; 227 initialized = TRUE;
230 } else { 228 } else {
231 return NULL; 229 return NULL;
233 } 231 }
234 232
235 return lib_dir; 233 return lib_dir;
236 } 234 }
237 235
238 char* wgaim_locale_dir(void) { 236 const char *wgaim_locale_dir(void) {
239 static gboolean initialized = FALSE; 237 static gboolean initialized = FALSE;
240 238
241 if (!initialized) { 239 if (!initialized) {
242 char *inst_dir = wgaim_install_dir(); 240 const char *inst_dir = wgaim_install_dir();
243 if (inst_dir != NULL) { 241 if (inst_dir != NULL) {
244 locale_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "locale", inst_dir); 242 locale_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "locale", inst_dir);
245 initialized = TRUE; 243 initialized = TRUE;
246 } else { 244 } else {
247 return NULL; 245 return NULL;
249 } 247 }
250 248
251 return locale_dir; 249 return locale_dir;
252 } 250 }
253 251
254 char* wgaim_data_dir(void) { 252 const char *wgaim_data_dir(void) {
255 253
256 if (!app_data_dir) { 254 if (!app_data_dir) {
257 /* Set app data dir, used by gaim_home_dir */ 255 /* Set app data dir, used by gaim_home_dir */
258 const char *newenv = g_getenv("GAIMHOME"); 256 const char *newenv = g_getenv("GAIMHOME");
259 if (newenv) 257 if (newenv)
270 return app_data_dir; 268 return app_data_dir;
271 } 269 }
272 270
273 /* Miscellaneous */ 271 /* Miscellaneous */
274 272
275 gboolean wgaim_read_reg_string(HKEY key, char* sub_key, char* val_name, LPBYTE data, LPDWORD data_len) { 273 gboolean wgaim_write_reg_string(HKEY rootkey, const char *subkey, const char *valname,
276 HKEY hkey; 274 const char *value) {
277 gboolean ret = FALSE; 275 HKEY reg_key;
278 276 gboolean success = FALSE;
279 if(ERROR_SUCCESS == RegOpenKeyEx(key, sub_key, 0, KEY_QUERY_VALUE, 277
280 &hkey)) { 278 if(G_WIN32_HAVE_WIDECHAR_API()) {
281 if(ERROR_SUCCESS == RegQueryValueEx(hkey, val_name, 0, NULL, 279 wchar_t *wc_subkey = g_utf8_to_utf16(subkey, -1, NULL,
282 data, data_len)) 280 NULL, NULL);
283 ret = TRUE; 281
284 RegCloseKey(key); 282 if(RegOpenKeyExW(rootkey, wc_subkey, 0,
285 } 283 KEY_SET_VALUE, &reg_key) == ERROR_SUCCESS) {
286 return ret; 284 wchar_t *wc_valname = NULL;
285
286 if (valname)
287 wc_valname = g_utf8_to_utf16(valname, -1,
288 NULL, NULL, NULL);
289
290 if(value) {
291 wchar_t *wc_value = g_utf8_to_utf16(value, -1,
292 NULL, NULL, NULL);
293 int len = (wcslen(wc_value) * sizeof(wchar_t)) + 1;
294 if(RegSetValueExW(reg_key, wc_valname, 0, REG_SZ,
295 (LPBYTE)wc_value, len
296 ) == ERROR_SUCCESS)
297 success = TRUE;
298 g_free(wc_value);
299 } else
300 if(RegDeleteValueW(reg_key, wc_valname) == ERROR_SUCCESS)
301 success = TRUE;
302
303 g_free(wc_valname);
304 }
305 g_free(wc_subkey);
306 } else {
307 char *cp_subkey = g_locale_from_utf8(subkey, -1, NULL,
308 NULL, NULL);
309 if(RegOpenKeyExA(rootkey, cp_subkey, 0,
310 KEY_SET_VALUE, &reg_key) == ERROR_SUCCESS) {
311 char *cp_valname = NULL;
312 if(valname)
313 cp_valname = g_locale_from_utf8(valname, -1,
314 NULL, NULL, NULL);
315
316 if (value) {
317 char *cp_value = g_locale_from_utf8(value, -1,
318 NULL, NULL, NULL);
319 int len = strlen(cp_value) + 1;
320 if(RegSetValueExA(reg_key, cp_valname, 0, REG_SZ,
321 cp_value, len
322 ) == ERROR_SUCCESS)
323 success = TRUE;
324 g_free(cp_value);
325 } else
326 if(RegDeleteValueA(reg_key, cp_valname) == ERROR_SUCCESS)
327 success = TRUE;
328
329 g_free(cp_valname);
330 }
331 g_free(cp_subkey);
332 }
333
334 if(reg_key != NULL)
335 RegCloseKey(reg_key);
336
337 return success;
338 }
339
340 char *wgaim_read_reg_string(HKEY rootkey, const char *subkey, const char *valname) {
341
342 DWORD type;
343 DWORD nbytes;
344 HKEY reg_key;
345 char *result = NULL;
346
347 if(G_WIN32_HAVE_WIDECHAR_API()) {
348 wchar_t *wc_subkey = g_utf8_to_utf16(subkey, -1, NULL,
349 NULL, NULL);
350
351 if(RegOpenKeyExW(rootkey, wc_subkey, 0,
352 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS) {
353 wchar_t *wc_valname = NULL;
354 if (valname)
355 wc_valname = g_utf8_to_utf16(valname, -1,
356 NULL, NULL, NULL);
357
358 if(RegQueryValueExW(reg_key, wc_valname, 0, &type,
359 NULL, &nbytes) == ERROR_SUCCESS
360 && type == REG_SZ) {
361 wchar_t *wc_temp =
362 g_new(wchar_t, ((nbytes + 1) / sizeof(wchar_t)) + 1);
363
364 if(RegQueryValueExW(reg_key, wc_valname, 0,
365 &type, (LPBYTE) wc_temp,
366 &nbytes) == ERROR_SUCCESS) {
367 wc_temp[nbytes / sizeof(wchar_t)] = '\0';
368 result = g_utf16_to_utf8(wc_temp, -1,
369 NULL, NULL, NULL);
370 }
371 g_free(wc_temp);
372 }
373 g_free(wc_valname);
374 }
375 g_free(wc_subkey);
376 } else {
377 char *cp_subkey = g_locale_from_utf8(subkey, -1, NULL,
378 NULL, NULL);
379 if(RegOpenKeyExA(rootkey, cp_subkey, 0,
380 KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS) {
381 char *cp_valname = NULL;
382 if(valname)
383 cp_valname = g_locale_from_utf8(valname, -1,
384 NULL, NULL, NULL);
385
386 if(RegQueryValueExA(reg_key, cp_valname, 0, &type,
387 NULL, &nbytes) == ERROR_SUCCESS
388 && type == REG_SZ) {
389 char *cp_temp = g_malloc(nbytes + 1);
390
391 if(RegQueryValueExA(reg_key, cp_valname, 0,
392 &type, cp_temp,
393 &nbytes) == ERROR_SUCCESS) {
394 cp_temp[nbytes] = '\0';
395 result = g_locale_to_utf8(cp_temp, -1,
396 NULL, NULL, NULL);
397 }
398 g_free (cp_temp);
399 }
400 g_free(cp_valname);
401 }
402 g_free(cp_subkey);
403 }
404
405 if(reg_key != NULL)
406 RegCloseKey(reg_key);
407
408 return result;
287 } 409 }
288 410
289 void wgaim_init(void) { 411 void wgaim_init(void) {
290 WORD wVersionRequested; 412 WORD wVersionRequested;
291 WSADATA wsaData; 413 WSADATA wsaData;
312 } 434 }
313 435
314 /* Set Environmental Variables */ 436 /* Set Environmental Variables */
315 /* Tell perl where to find Gaim's perl modules */ 437 /* Tell perl where to find Gaim's perl modules */
316 perlenv = g_getenv("PERL5LIB"); 438 perlenv = g_getenv("PERL5LIB");
317 newenv = g_strdup_printf("PERL5LIB=%s%s%s" G_DIR_SEPARATOR_S "perlmod;", 439 newenv = g_strdup_printf("%s%s%s" G_DIR_SEPARATOR_S "perlmod;",
318 perlenv ? perlenv : "", 440 perlenv ? perlenv : "",
319 perlenv ? ";" : "", 441 perlenv ? ";" : "",
320 wgaim_install_dir()); 442 wgaim_install_dir());
321 if (putenv(newenv) < 0) 443 if (!g_setenv("PERL5LIB", newenv, TRUE))
322 gaim_debug(GAIM_DEBUG_WARNING, "wgaim", "putenv failed\n"); 444 gaim_debug(GAIM_DEBUG_WARNING, "wgaim", "putenv failed\n");
323 g_free(newenv); 445 g_free(newenv);
324 446
325 gaim_debug(GAIM_DEBUG_INFO, "wgaim", "wgaim_init end\n"); 447 gaim_debug(GAIM_DEBUG_INFO, "wgaim", "wgaim_init end\n");
326 } 448 }