From: Michal MalĂ˝ Date: Tue, 23 Aug 2016 09:18:03 +0000 (+0200) Subject: Read start of the scans block from file header X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=47f8ddc1641f973fadcbc29d156789fe28f78bd0;p=libHPCS.git Read start of the scans block from file header --- diff --git a/libhpcs.c b/libhpcs.c index 8677e87..9b1ab0a 100644 --- a/libhpcs.c +++ b/libhpcs.c @@ -115,6 +115,7 @@ enum HPCS_RetCode hpcs_read_mdata(const char* filename, struct HPCS_MeasuredData enum HPCS_ChemStationVer cs_ver; double signal_step; double signal_shift; + size_t scans_start; if (mdata == NULL) return HPCS_E_NULLPTR; @@ -176,7 +177,14 @@ enum HPCS_RetCode hpcs_read_mdata(const char* filename, struct HPCS_MeasuredData goto out; } - pret = read_signal(datafile, &mdata->data, &mdata->data_count, signal_step, signal_shift, gentype); + pret = read_scans_start(datafile, &scans_start); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read scans start offset\n"); + ret = HPCS_E_PARSE_ERROR; + goto out; + } + + pret = read_signal(datafile, &mdata->data, &mdata->data_count, scans_start, signal_step, signal_shift); if (pret != PARSE_OK) { PR_DEBUG("Cannot parse data in the file\n"); ret = HPCS_E_PARSE_ERROR; @@ -906,8 +914,30 @@ out: return ret; } +static enum HPCS_ParseCode read_scans_start(FILE* datafile, size_t *scans_start) +{ + size_t r; + int32_t _start; + + fseek(datafile, DATA_SCANS_START, SEEK_SET); + if (feof(datafile)) + return PARSE_E_OUT_OF_RANGE; + if (ferror(datafile)) + return PARSE_E_CANT_READ; + + r = fread(&_start, LARGE_SEGMENT_SIZE, 1, datafile); + if (r != 1) + return PARSE_E_CANT_READ; + + be_to_cpu_val(_start); + + *scans_start = (_start - 1) * 512; + + return PARSE_OK; +} + static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pairs, size_t* pairs_count, - const double signal_step, const double signal_shift, const enum HPCS_GenType gentype) + const size_t scans_start, const double signal_step, const double signal_shift) { size_t alloc_size = (size_t)((60 * 25)); bool read_file = true; @@ -915,12 +945,11 @@ static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pair size_t segments_read = 0; size_t data_segments_read = 0; size_t next_marker_idx = 0; - const HPCS_offset data_start_offset = OLD_FORMAT(gentype) ? DATA_OFFSET_DATA_START_OLD : DATA_OFFSET_DATA_START; char raw[2]; size_t r; enum HPCS_DataCheckCode dret; - fseek(datafile, data_start_offset, SEEK_SET); + fseek(datafile, scans_start, SEEK_SET); if (feof(datafile)) return PARSE_E_OUT_OF_RANGE; if (ferror(datafile)) diff --git a/libhpcs_p.h b/libhpcs_p.h index 8483215..7b02556 100644 --- a/libhpcs_p.h +++ b/libhpcs_p.h @@ -83,6 +83,7 @@ const double CE_WORK_PARAM_SAMPRATE = 1.67; /* Offsets containing data of interest in .ch files */ const HPCS_offset DATA_OFFSET_GENTYPE = 0x000; +const HPCS_offset DATA_SCANS_START = 0x108; const HPCS_offset DATA_OFFSET_XMIN = 0x11A; const HPCS_offset DATA_OFFSEt_XMAX = 0x11E; const HPCS_offset DATA_OFFSET_FILE_DESC = 0x15B; @@ -190,8 +191,9 @@ static enum HPCS_ParseCode read_file_header(FILE* datafile, enum HPCS_ChemStatio static enum HPCS_ParseCode read_file_type_description(FILE* datafile, char** const description, const enum HPCS_GenType gentype); static enum HPCS_ParseCode read_generic_type(FILE* datafile, enum HPCS_GenType* gentype); static enum HPCS_ParseCode read_method_info_file(HPCS_UFH fh, struct HPCS_MethodInfo* minfo); +static enum HPCS_ParseCode read_scans_start(FILE* datafile, size_t *scans_start); static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pairs, size_t* pairs_count, - const double sigal_step, const double signal_shift, const enum HPCS_GenType gentype); + const size_t scans_start, const double sigal_step, const double signal_shift); static enum HPCS_ParseCode read_string_at_offset(FILE* datafile, const HPCS_offset, char** const result, const bool read_as_wchar); static enum HPCS_ParseCode read_timing(FILE* datafile, struct HPCS_TVPair*const pairs, double *sampling_rate, const size_t data_count); static void remove_trailing_newline(HPCS_NChar* s);