From: Michal MalĂ˝ Date: Thu, 10 Jul 2014 21:45:15 +0000 (+0200) Subject: API CHANGE: Rework wavelength parsing logic X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=3f10799612659025ac887e89021349be28e1ea7a;p=libHPCS.git API CHANGE: Rework wavelength parsing logic --- diff --git a/libhpcs.c b/libhpcs.c index 072c0a5..a43f183 100644 --- a/libhpcs.c +++ b/libhpcs.c @@ -131,18 +131,12 @@ enum HPCS_RetCode hpcs_read_file(const char* filename, struct HPCS_MeasuredData* } if (mdata->file_type == HPCS_TYPE_CE_DAD) { - pret = read_dad_wavelength(datafile, WAVELENGTH_MEASURED, &mdata->dad_wavelength_msr); + pret = read_dad_wavelength(datafile, &mdata->dad_wavelength_msr, &mdata->dad_wavelength_ref); if (pret != PARSE_OK && pret != PARSE_W_NO_DATA) { PR_DEBUG("Cannot read measured wavelength"); ret = HPCS_E_PARSE_ERROR; goto out; } - pret = read_dad_wavelength(datafile, WAVELENGTH_REFERENCE, &mdata->dad_wavelength_ref); - if (pret != PARSE_OK && pret != PARSE_W_NO_DATA) { - PR_DEBUG("Cannot read reference wavelength"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } } switch (mdata->file_type) { @@ -227,45 +221,105 @@ static enum HPCS_DataCheckCode check_for_marker(const char* const segment, size_ return DCHECK_E_NO_MARKER; } -static enum HPCS_ParseCode read_dad_wavelength(FILE* datafile, const enum HPCS_Wavelength_Type wl_type, uint16_t* const wavelength) +static enum HPCS_ParseCode read_dad_wavelength(FILE* datafile, struct HPCS_Wavelength* const measured, struct HPCS_Wavelength* const reference) { - char* start_idx, *end_idx, *temp, *type_str; + char* start_idx, *interv_idx, *end_idx, *temp, *str; size_t len; - enum HPCS_ParseCode pret; + enum HPCS_ParseCode pret, ret; - *wavelength = 0; - pret = read_string_at_offset(datafile, DATA_OFFSET_DEVSIG_INFO, &type_str); + measured->wavelength = 0; + measured->interval = 0; + reference->wavelength = 0; + reference->interval = 0; + pret = read_string_at_offset(datafile, DATA_OFFSET_DEVSIG_INFO, &str); if (pret != PARSE_OK) return pret; - switch (wl_type) { - case WAVELENGTH_MEASURED: - start_idx = strstr(type_str, WAVELENGTH_MEASURED_TEXT); - break; - case WAVELENGTH_REFERENCE: - start_idx = strstr(type_str, WAVELENGTH_REFERENCE_TEXT); - break; - default: - return PARSE_E_INV_PARAM; + /* Read MEASURED wavelength */ + start_idx = strstr(str, WAVELENGTH_MEASURED_TEXT) + strlen(WAVELENGTH_MEASURED_TEXT); + if (start_idx == NULL) { + ret = PARSE_W_NO_DATA; + goto out; } + interv_idx = strchr(start_idx, WAVELENGTH_DELIMITER_TEXT); + if (interv_idx == NULL) { + PR_DEBUG("No spectral interval value"); + ret = PARSE_E_NOT_FOUND; + goto out; + } + end_idx = strchr(interv_idx, WAVELENGTH_END_TEXT); + if (end_idx == NULL) { + PR_DEBUG("No measured/reference wavelength delimiter found"); + ret = PARSE_E_NOT_FOUND; + goto out; + } + len = interv_idx - start_idx; + temp = malloc(len + 1); + if (temp == NULL) { + PR_DEBUG("No memory for temporary string"); + ret = PARSE_E_NO_MEM; + goto out; + } + memcpy(temp, start_idx, len); + temp[len] = 0; + measured->wavelength = strtoul(temp, NULL, 10); + + /* Read MEASURED spectral interval */ + if (end_idx - interv_idx - 1 > len) { + free(temp); + temp = malloc(end_idx - interv_idx - 1); + if (temp == NULL) { + PR_DEBUG("No memory for temporary string"); + ret = PARSE_E_NO_MEM; + goto out; + } + } + len = end_idx - interv_idx - 2; + memcpy(temp, interv_idx + 1, len); + temp[len] = 0; + measured->interval = strtoul(temp, NULL, 10); - if (start_idx == NULL) - return PARSE_W_NO_DATA; - - end_idx = strchr(start_idx, WAVELENGTH_DELIMITER_TEXT); - if (end_idx == NULL) - return PARSE_E_NOT_FOUND; - len = end_idx - (start_idx + 4); - temp = malloc(len + 1); - if (temp == NULL) - return PARSE_E_NO_MEM; - memcpy(temp, start_idx + 4, len); + /* Read REFERENCE wavelength */ + start_idx = strstr(end_idx, WAVELENGTH_REFERENCE_TEXT) + strlen(WAVELENGTH_REFERENCE_TEXT); + if (start_idx == NULL) { + PR_DEBUG("No reference wavelength data"); + ret = PARSE_W_NO_DATA; + goto out2; + } + interv_idx = strchr(start_idx, WAVELENGTH_DELIMITER_TEXT); + if (interv_idx == NULL) { + /* Is the reference wavelength disabled? */ + if (strcmp(start_idx, WAVELENGTH_REFERENCE_OFF_TEXT) == 0) { + ret = PARSE_OK; + goto out2; + } + PR_DEBUG("No reference spectral interval but reference wavelength is not 'off'"); + ret = PARSE_E_NOT_FOUND; + goto out2; + } + if (interv_idx - start_idx > len + 1) { + free(temp); + temp = malloc(interv_idx - start_idx + 1); + if (temp == NULL) { + PR_DEBUG("No memory for temporary string"); + ret = PARSE_E_NO_MEM; + goto out; + } + } + len = interv_idx - start_idx; + memcpy(temp, start_idx, len); temp[len] = 0; + reference->wavelength = strtoul(temp, NULL, 10); + reference->interval = strtoul(interv_idx + 1, NULL, 10); + ret = PARSE_OK; - *wavelength = strtoul(temp, NULL, 10); +out2: + printf("%u %u %u %u\n", measured->wavelength, measured->interval, reference->wavelength, reference->interval); free(temp); - return PARSE_OK; +out: + free(str); + return ret; } static enum HPCS_ParseCode read_date(FILE* datafile, struct HPCS_Date* date) diff --git a/libhpcs.h b/libhpcs.h index 9b79451..93352e4 100644 --- a/libhpcs.h +++ b/libhpcs.h @@ -36,6 +36,11 @@ struct HPCS_TVPair { double value; }; +struct HPCS_Wavelength { + uint16_t wavelength; + uint16_t interval; +}; + struct HPCS_MeasuredData { char* file_description; char* sample_info; @@ -46,8 +51,8 @@ struct HPCS_MeasuredData { char* cs_rev; char* y_units; double sampling_rate; - uint16_t dad_wavelength_msr; - uint16_t dad_wavelength_ref; + struct HPCS_Wavelength dad_wavelength_msr; + struct HPCS_Wavelength dad_wavelength_ref; enum HPCS_File_Type file_type; struct HPCS_TVPair* data; size_t data_count; diff --git a/libhpcs_p.h b/libhpcs_p.h index 05529ae..b8f790f 100644 --- a/libhpcs_p.h +++ b/libhpcs_p.h @@ -36,7 +36,9 @@ const char* FILE_TYPE_CE_POWER = "HPCE1P"; /* Char and text values */ const char* WAVELENGTH_MEASURED_TEXT = "Sig="; const char* WAVELENGTH_REFERENCE_TEXT = "Ref="; -const char WAVELENGTH_DELIMITER_TEXT = ','; +const char* WAVELENGTH_REFERENCE_OFF_TEXT = "off"; +const char WAVELENGTH_DELIMITER_TEXT = (const char)(0x2C); +const char WAVELENGTH_END_TEXT = (const char)(0x20); const char DATA_FILE_COMMA = (const char)(0x2C); const char DATA_FILE_DASH = (const char)(0x2D); const char DATA_FILE_COLON = (const char)(0x3A); @@ -90,7 +92,7 @@ const char* HPCS_E__UNKNOWN_EC_STR = "Unknown error code."; static enum HPCS_ParseCode autodetect_file_type(FILE* datafile, enum HPCS_File_Type* file_type); static enum HPCS_DataCheckCode check_for_marker(const char* const segment, size_t* const next_marker_idx); -static enum HPCS_ParseCode read_dad_wavelength(FILE* datafile, const enum HPCS_Wavelength_Type wl_type, uint16_t* const wavelength); +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* const month); static enum HPCS_ParseCode read_date(FILE* datafile, struct HPCS_Date* const date); static enum HPCS_ParseCode read_fixed_signal(FILE* datafile, struct HPCS_TVPair** pairs, size_t* pairs_count,