]> Devoid-pointer.net GitWeb - libHPCS.git/commitdiff
Read strings from data files as UTF-16LE encoded strings
authorMichal Malý <madcatxster@devoid-pointer.net>
Thu, 26 Mar 2015 22:26:39 +0000 (23:26 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Thu, 26 Mar 2015 22:26:39 +0000 (23:26 +0100)
VS2013/libHPCS/libHPCS.vcxproj
libhpcs.c
test_tool.c

index 02d71377094ec2d80133c9e26727de7a59f4159d..39289b803a5a0b4e77b383df747d97c3f95aa305 100644 (file)
@@ -91,7 +91,7 @@
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <AdditionalDependencies>Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <Version>2.0</Version>
+      <Version>3.0</Version>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug with test_tool|Win32'">
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <AdditionalDependencies>Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <Version>2.0</Version>
+      <Version>3.0</Version>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <AdditionalDependencies>Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <Version>2.0</Version>
+      <Version>3.0</Version>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release with test_tool|Win32'">
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <AdditionalDependencies>Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
-      <Version>2.0</Version>
+      <Version>3.0</Version>
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
index 33d69ff3b755157dd93b66983b07aba350bb53d4..0ab677523eaf593910b6946d5fbe074bf0c8728e 100644 (file)
--- a/libhpcs.c
+++ b/libhpcs.c
@@ -476,6 +476,10 @@ out:
        return ret;
 }
 
+/* This function assumes that the date information are composed only
+   of characters from ISO-8859-1 charset. Under such assumption it is
+   possible to treat UTF-8 strings as single-byte strings with ISO-8859-1
+   encoding */
 static enum HPCS_ParseCode read_date(FILE* datafile, struct HPCS_Date* date)
 {
        char* date_str;
@@ -768,7 +772,7 @@ static enum HPCS_ParseCode read_sampling_rate(FILE* datafile, double* sampling_r
 static enum HPCS_ParseCode read_string_at_offset(FILE* datafile, const HPCS_offset offset, char** const result)
 {
        char* string;
-       uint8_t str_length, idx;
+       uint8_t str_length;
        size_t r;
 
        fseek(datafile, offset, SEEK_SET);
@@ -779,18 +783,29 @@ static enum HPCS_ParseCode read_string_at_offset(FILE* datafile, const HPCS_offs
        if (r != 1)
                return PARSE_E_CANT_READ;
 
-       string = malloc(str_length + 1);
+       if (str_length == 0) {
+               *result = malloc(sizeof(char));
+               *result[0] = 0;
+               return PARSE_OK;
+       }
+
+       string = calloc(str_length + 1, SEGMENT_SIZE);
        if (string == NULL)
                return PARSE_E_NO_MEM;
+       memset(string, 0, (str_length + 1) * SEGMENT_SIZE);
 
-       for (idx = 0; idx < str_length; idx++) {
-               fread(string+idx, SMALL_SEGMENT_SIZE, 1, datafile);
-               fseek(datafile, SMALL_SEGMENT_SIZE, SEEK_CUR);
+       r = fread(string, SEGMENT_SIZE, str_length, datafile);
+       if (r < str_length) {
+               free(string);
+               return PARSE_E_CANT_READ;
        }
-       string[str_length] = 0;
 
-       *result = string;
-       return PARSE_OK;
+#ifdef _WIN32
+       /* String is stored as native Windows WCHAR */
+       return __win32_wchar_to_utf8(result, string);
+#else
+       #error "Not implemented"
+#endif
 }
 
 /** Platform-specific functions */
@@ -869,6 +884,7 @@ static enum HPCS_ParseCode __win32_wchar_to_utf8(char** target, const WCHAR* s)
                PR_DEBUGF("Count WideCharToMultiByte() error: 0x%x\n", GetLastError());
                return PARSE_E_INTERNAL;
        }
+       PR_DEBUGF("mb_size: %d\n", mb_size);
        *target = malloc(mb_size);
        if (*target == NULL)
                return PARSE_E_NO_MEM;
index 144796126149229d2b91ccc1985ec91c081746e7..4c7c172aed95adb8bb81152a3006444921935921 100644 (file)
@@ -22,6 +22,15 @@ int read_data(const char* path)
                return EXIT_FAILURE;
        }
 
+       printf("Sample info: %s\n"
+                 "Operator name: %s\n"
+                 "Method name: %s\n"
+                 "Y units: %s\n",
+                 mdata->sample_info,
+                 mdata->operator_name,
+                 mdata->method_name,
+                 mdata->y_units);
+
        for (di = 0; di < mdata->data_count; di++)
                printf("Time: %.17lg, Value: %.17lg\n", mdata->data[di].time, mdata->data[di].value);