diff options
author | Chris Robinson <[email protected]> | 2015-02-07 04:19:50 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2015-02-07 04:19:50 -0800 |
commit | 50cdc0ac1e3e69142989c9aa2f27cbeb44ec6c3d (patch) | |
tree | 8a166a5724be1f77818189c37abe6f4597e548ce /Alc/helpers.c | |
parent | 61743a3a0017c567ee62ab50c3da28a5ed2fed8d (diff) |
Avoid tracing wide-char strings
Because on Windows, traced strings are written to a char string, which causes
UTF-16 strings to be converted to a narrow (non-UTF-8) encoding, potentially
losing characters.
Diffstat (limited to 'Alc/helpers.c')
-rw-r--r-- | Alc/helpers.c | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/Alc/helpers.c b/Alc/helpers.c index aa8feebc..674c7eb6 100644 --- a/Alc/helpers.c +++ b/Alc/helpers.c @@ -457,34 +457,42 @@ FILE *OpenDataFile(const char *fname, const char *subdir) static const int ids[2] = { CSIDL_APPDATA, CSIDL_COMMON_APPDATA }; WCHAR *wname=NULL, *wsubdir=NULL; FILE *f; - int i; + size_t i; - /* If the path is absolute, open it directly. */ - if(fname[0] != '\0' && fname[1] == ':' && is_slash(fname[2])) + wname = FromUTF8(fname); + if(!wname) { - if((f=al_fopen(fname, "rb")) != NULL) - { - TRACE("Opened %s\n", fname); - return f; - } - WARN("Could not open %s\n", fname); + ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname); return NULL; } - /* If it's relative, try the current directory first before the data directories. */ - if((f=al_fopen(fname, "rb")) != NULL) + /* If the path is absolute, open it directly. */ + if(wname[0] != '\0' && wname[1] == ':' && is_slash(wname[2])) + { + f = _wfopen(wname, L"rb"); + if(f) TRACE("Opened %s\n", fname); + else WARN("Could not open %s\n", fname); + free(wname); + return f; + } + + /* Try the current directory first before the data directories. */ + if((f=_wfopen(wname, L"rb")) != NULL) { TRACE("Opened %s\n", fname); + free(wname); return f; } - wname = FromUTF8(fname); wsubdir = FromUTF8(subdir); - if(!wname) - ERR("Failed to convert UTF-8 filename: \"%s\"\n", fname); - else if(!wsubdir) + if(!wsubdir) + { ERR("Failed to convert UTF-8 subdir: \"%s\"\n", subdir); - else for(i = 0;i < 2;i++) + free(wname); + return NULL; + } + + for(i = 0;i < COUNTOF(ids);i++) { WCHAR buffer[PATH_MAX]; size_t len; @@ -506,16 +514,18 @@ FILE *OpenDataFile(const char *fname, const char *subdir) if((f=_wfopen(buffer, L"rb")) != NULL) { - TRACE("Opened %ls\n", buffer); - free(wname); - free(wsubdir); - return f; + al_string filepath = AL_STRING_INIT_STATIC(); + al_string_copy_wcstr(&filepath, buffer); + TRACE("Opened %s\n", al_string_get_cstr(filepath)); + al_string_deinit(&filepath); + break; } } - WARN("Could not open %ls\\%ls\n", wsubdir, wname); free(wname); free(wsubdir); + if(f == NULL) + WARN("Could not open %s\\%s\n", subdir, fname); return NULL; } |