From: Michal MalĂ˝ Date: Fri, 11 Jul 2014 08:34:09 +0000 (+0200) Subject: Fix file type autodetection X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=f00386458f179cf4a41a52c26d486fe4ab0ea348;p=libHPCS.git Fix file type autodetection --- diff --git a/libhpcs.c b/libhpcs.c index 3b3583b..3b8d9aa 100644 --- a/libhpcs.c +++ b/libhpcs.c @@ -184,42 +184,31 @@ out: static enum HPCS_ParseCode autodetect_file_type(FILE* datafile, enum HPCS_File_Type* file_type) { - char* type_str; char* type_id; - char* delim; - size_t len; enum HPCS_ParseCode pret; - pret = read_string_at_offset(datafile, DATA_OFFSET_DEVSIG_INFO, &type_str); + pret = read_string_at_offset(datafile, DATA_OFFSET_DEVSIG_INFO, &type_id); if (pret != PARSE_OK) return pret; - delim = strchr(type_str, DATA_FILE_COMMA); - if (delim == NULL) { - free(type_str); - return PARSE_E_NOT_FOUND; - } - - len = delim - type_str; - type_id = malloc(len + 1); - memcpy(type_id, type_str, len); - type_id[len] = 0; - - if (strcmp(FILE_TYPE_CE_CCD, type_id) == 0) - *file_type = HPCS_TYPE_CE_CCD; - else if (strcmp(FILE_TYPE_CE_CURRENT, type_id) == 0) - *file_type = HPCS_TYPE_CE_CURRENT; - else if (strstr(type_id, FILE_TYPE_CE_DAD) != NULL) + if (strstr(type_id, FILE_TYPE_ID_DAD) == type_id) *file_type = HPCS_TYPE_CE_DAD; - else if (strcmp(FILE_TYPE_CE_POWER, type_id) == 0) - *file_type = HPCS_TYPE_CE_POWER; - else if (strcmp(FILE_TYPE_CE_VOLTAGE, type_id) == 0) - *file_type = HPCS_TYPE_CE_VOLTAGE; - else - *file_type = HPCS_TYPE_UNKNOWN; + else if (strstr(type_id, FILE_TYPE_ID_HPCE) == type_id) { + const char hpce_id = type_id[strlen(FILE_TYPE_ID_HPCE) + 1]; + + if (hpce_id == FILE_TYPE_HPCE_CCD) + *file_type = HPCS_TYPE_CE_CCD; + else if (hpce_id == FILE_TYPE_HPCE_CURRENT) + *file_type = HPCS_TYPE_CE_CURRENT; + else if (hpce_id == FILE_TYPE_HPCE_POWER) + *file_type = HPCS_TYPE_CE_POWER; + else if (hpce_id == FILE_TYPE_HPCE_VOLTAGE) + *file_type = HPCS_TYPE_CE_VOLTAGE; + else + *file_type = HPCS_TYPE_UNKNOWN; + } else + *file_type = HPCS_TYPE_UNKNOWN; - free(type_str); - free(type_id); return PARSE_OK; } diff --git a/libhpcs_p.h b/libhpcs_p.h index a1fde65..86ad51d 100644 --- a/libhpcs_p.h +++ b/libhpcs_p.h @@ -26,12 +26,13 @@ typedef double HPCS_step; typedef size_t HPCS_segsize; -/* Identifiers of file types found at offset 0x1075 */ -const char* FILE_TYPE_CE_CCD = "HPCE1L"; -const char* FILE_TYPE_CE_CURRENT = "HPCE1C"; -const char* FILE_TYPE_CE_DAD = "DAD"; -const char* FILE_TYPE_CE_VOLTAGE = "HPCE1V"; -const char* FILE_TYPE_CE_POWER = "HPCE1P"; +/* Identifiers of file types found at offset 0x1075 onward */ +const char* FILE_TYPE_ID_DAD = "DAD"; +const char* FILE_TYPE_ID_HPCE = "HPCE"; +const char FILE_TYPE_HPCE_CCD = 'L'; +const char FILE_TYPE_HPCE_CURRENT = 'C'; +const char FILE_TYPE_HPCE_VOLTAGE = 'V'; +const char FILE_TYPE_HPCE_POWER = 'P'; /* Char and text values */ const char* WAVELENGTH_MEASURED_TEXT = "Sig=";