From 43514d6e27a9cd5e927f7a4e754469fb5936683c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Tue, 8 Dec 2015 21:38:24 +0100 Subject: [PATCH] Expect file path to measurement data to be UTF-8 encoded. Convert the path to wchar on Win32 --- libhpcs.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- libhpcs_p.h | 2 ++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/libhpcs.c b/libhpcs.c index eb03c72..a48e118 100644 --- a/libhpcs.c +++ b/libhpcs.c @@ -117,7 +117,7 @@ enum HPCS_RetCode hpcs_read_mdata(const char* filename, struct HPCS_MeasuredData if (mdata == NULL) return HPCS_E_NULLPTR; - datafile = fopen(filename, "rb"); + datafile = open_measurement_file(filename); if (datafile == NULL) return HPCS_E_CANT_OPEN; @@ -201,7 +201,7 @@ enum HPCS_RetCode hpcs_read_mheader(const char* filename, struct HPCS_MeasuredDa if (mdata == NULL) return HPCS_E_NULLPTR; - datafile = fopen(filename, "rb"); + datafile = open_measurement_file(filename); if (datafile == NULL) return HPCS_E_CANT_OPEN; @@ -307,7 +307,7 @@ static enum HPCS_DataCheckCode check_for_marker(const char* segment, size_t* con static enum HPCS_ChemStationVer detect_chemstation_version(const char*const version_string) { - PR_DEBUGF("ChemStation ersion string: %s\n", version_string); + PR_DEBUGF("ChemStation version string: %s\n", version_string); if (!strcmp(version_string, CHEMSTAT_B0625_STR)) { PR_DEBUG("ChemStation B.06.25\n"); @@ -448,6 +448,23 @@ static HPCS_UFH open_data_file(const char* filename) #endif } +static FILE* open_measurement_file(const char* filename) +{ +#ifdef _WIN32 + FILE* f; + wchar_t *win_filename; + + if (!__win32_utf8_to_wchar(&win_filename, filename)) + return NULL; + + f = _wfopen(win_filename, L"rb"); + free(win_filename); + return f; +#else + return fopen(filename, "rb"); +#endif +} + static bool p_means_pressure(const enum HPCS_ChemStationVer version) { if (version == CHEMSTAT_B0625) @@ -1129,6 +1146,27 @@ static enum HPCS_ParseCode __win32_parse_native_method_info_line(char** name, ch return PARSE_OK; } +static bool __win32_utf8_to_wchar(wchar_t** target, const char *s) +{ + size_t w_size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, -1, NULL, 0); + if (w_size == 0) { + PR_DEBUGF("Count MultiByteToWideChar() error 0x%x\n", GetLastError()); + return false; + } + PR_DEBUGF("w_size: %d\n", w_size); + *target = malloc(sizeof(wchar_t) * w_size); + if (*target == NULL) + return false; + + if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, -1, *target, w_size) == 0) { + free(*target); + PR_DEBUGF("Convert MultiByteToWideChar() error 0x%x\n", GetLastError()); + return false; + } + + return true; +} + static enum HPCS_ParseCode __win32_wchar_to_utf8(char** target, const WCHAR* s) { int mb_size; @@ -1151,6 +1189,7 @@ static enum HPCS_ParseCode __win32_wchar_to_utf8(char** target, const WCHAR* s) return PARSE_OK; } + #else static void __unix_hpcs_initialize() { diff --git a/libhpcs_p.h b/libhpcs_p.h index d2c1577..bd5e826 100644 --- a/libhpcs_p.h +++ b/libhpcs_p.h @@ -169,6 +169,7 @@ static void guess_sampling_rate(const enum HPCS_ChemStationVer version, struct H static bool file_type_description_is_readable(const char*const description); static enum HPCS_ParseCode next_native_line(HPCS_UFH fh, HPCS_NChar* line, int32_t length); static HPCS_UFH open_data_file(const char* filename); +static FILE* open_measurement_file(const char* filename); static enum HPCS_ParseCode parse_native_method_info_line(char** name, char** value, HPCS_NChar* line); static enum HPCS_ParseCode read_dad_wavelength(FILE* datafile, struct HPCS_Wavelength* const measured, struct HPCS_Wavelength* const reference); static uint8_t month_to_number(const char* month); @@ -189,6 +190,7 @@ static void remove_trailing_newline(HPCS_NChar* s); static enum HPCS_ParseCode __win32_next_native_line(FILE* fh, WCHAR* line, int32_t length); static HPCS_UFH __win32_open_data_file(const char* filename); static enum HPCS_ParseCode __win32_parse_native_method_info_line(char** name, char** value, WCHAR* line); +static bool __win32_utf8_to_wchar(wchar_t** target, const char* s); static enum HPCS_ParseCode __win32_wchar_to_utf8(char** target, const WCHAR* s); #else static void __attribute((constructor)) __unix_hpcs_initialize(); -- 2.43.5