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.
This commit is contained in:
Chris Robinson
2015-02-07 04:19:50 -08:00
parent 61743a3a00
commit 50cdc0ac1e
2 changed files with 44 additions and 28 deletions
+12 -6
View File
@@ -305,27 +305,33 @@ void ReadALConfig(void)
if(SHGetSpecialFolderPathW(NULL, buffer, CSIDL_APPDATA, FALSE) != FALSE)
{
size_t p = lstrlenW(buffer);
_snwprintf(buffer+p, PATH_MAX-p, L"\\alsoft.ini");
al_string filepath = AL_STRING_INIT_STATIC();
al_string_copy_wcstr(&filepath, buffer);
al_string_append_cstr(&filepath, "\\alsoft.ini");
TRACE("Loading config %ls...\n", buffer);
f = _wfopen(buffer, L"rt");
TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
f = al_fopen(al_string_get_cstr(filepath), "rt");
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
al_string_deinit(&filepath);
}
if((str=_wgetenv(L"ALSOFT_CONF")) != NULL && *str)
{
TRACE("Loading config %ls...\n", str);
f = _wfopen(str, L"rt");
al_string filepath = AL_STRING_INIT_STATIC();
al_string_copy_wcstr(&filepath, str);
TRACE("Loading config %s...\n", al_string_get_cstr(filepath));
f = al_fopen(al_string_get_cstr(filepath), "rt");
if(f)
{
LoadConfigFromFile(f);
fclose(f);
}
al_string_deinit(&filepath);
}
}
#else
+32 -22
View File
@@ -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]))
{
TRACE("Opened %s\n", fname);
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;
}