]> Devoid-pointer.net GitWeb - libHPCS.git/commitdiff
Expect file path to measurement data to be UTF-8 encoded. Convert the
authorMichal Malý <madcatxster@devoid-pointer.net>
Tue, 8 Dec 2015 20:38:24 +0000 (21:38 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Tue, 8 Dec 2015 20:38:24 +0000 (21:38 +0100)
path to wchar on Win32

libhpcs.c
libhpcs_p.h

index eb03c723823cbb77ec5e85bbc85db83b35599c84..a48e11859697dea87e69ea760c0ec487ece168a3 100644 (file)
--- 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()
 {
index d2c1577038ecd1fad26e5b55b192c4be3d3d73ec..bd5e826245e79c2c4a176d1c252d42f0d98ae5b1 100644 (file)
@@ -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();