From: Michal MalĂ˝ Date: Sun, 8 Nov 2015 16:38:14 +0000 (+0100) Subject: - Add a new function to read just the header of the file X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=2e1051f6e22d81b3554ef5a526f1ae4f1822ea2a;p=libHPCS.git - Add a new function to read just the header of the file - Add include guards to the headers --- diff --git a/include/libhpcs.h b/include/libhpcs.h index 3142c76..d3dcfa9 100644 --- a/include/libhpcs.h +++ b/include/libhpcs.h @@ -1,3 +1,6 @@ +#ifndef LIBHPCS_H +#define LIBHPCS_H + #ifdef __cplusplus extern "C" { #endif @@ -85,8 +88,11 @@ LIBHPCS_API void LIBHPCS_CC hpcs_free_mdata(struct HPCS_MeasuredData* const mdat LIBHPCS_API void LIBHPCS_CC hpcs_free_minfo(struct HPCS_MethodInfo* const minfo); LIBHPCS_API const char* LIBHPCS_CC hpcs_error_to_string(const enum HPCS_RetCode); LIBHPCS_API enum HPCS_RetCode LIBHPCS_CC hpcs_read_mdata(const char* filename, struct HPCS_MeasuredData* mdata); +LIBHPCS_API enum HPCS_RetCode LIBHPCS_CC hpcs_read_mheader(const char* filename, struct HPCS_MeasuredData* mdata); LIBHPCS_API enum HPCS_RetCode LIBHPCS_CC hpcs_read_minfo(const char* filename, struct HPCS_MethodInfo* minfo); #ifdef __cplusplus } #endif + +#endif /* LIBHPCS_H */ diff --git a/libhpcs.c b/libhpcs.c index 237c7a1..8b3f38c 100644 --- a/libhpcs.c +++ b/libhpcs.c @@ -104,121 +104,79 @@ void hpcs_free_minfo(struct HPCS_MethodInfo* const minfo) enum HPCS_RetCode hpcs_read_mdata(const char* filename, struct HPCS_MeasuredData* mdata) { - FILE* datafile; - enum HPCS_ParseCode pret; - enum HPCS_RetCode ret; + FILE* datafile; + enum HPCS_ParseCode pret; + enum HPCS_RetCode ret; - if (mdata == NULL) + if (mdata == NULL) return HPCS_E_NULLPTR; - datafile = fopen(filename, "rb"); - if (datafile == NULL) + datafile = fopen(filename, "rb"); + if (datafile == NULL) return HPCS_E_CANT_OPEN; - pret = read_string_at_offset(datafile, DATA_OFFSET_FILE_DESC, &mdata->file_description); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read file description\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - pret = read_string_at_offset(datafile, DATA_OFFSET_SAMPLE_INFO, &mdata->sample_info); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read sample info\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - pret = read_string_at_offset(datafile, DATA_OFFSET_OPERATOR_NAME, &mdata->operator_name); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read operator name\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - pret = read_string_at_offset(datafile, DATA_OFFSET_METHOD_NAME, &mdata->method_name); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read method name\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - pret = read_date(datafile, &mdata->date); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read date of measurement\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - pret = read_string_at_offset(datafile, DATA_OFFSET_CS_VER, &mdata->cs_ver); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read ChemStation software version\n"); + pret = read_file_header(datafile, mdata); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read the header\n"); ret = HPCS_E_PARSE_ERROR; goto out; - } - pret = read_string_at_offset(datafile, DATA_OFFSET_Y_UNITS, &mdata->y_units); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read values of Y axis\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - pret = read_string_at_offset(datafile, DATA_OFFSET_CS_REV, &mdata->cs_rev); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read ChemStation software revision\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - pret = read_sampling_rate(datafile, &mdata->sampling_rate); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read sampling rate of the file\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - pret = autodetect_file_type(datafile, &mdata->file_type, guess_p_meaning(mdata)); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot determine the type of file\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - - if (mdata->file_type == HPCS_TYPE_CE_DAD) { - 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 wavelength\n"); - ret = HPCS_E_PARSE_ERROR; - goto out; - } - } - - guess_sampling_rate(mdata); + } - switch (mdata->file_type) { + switch (mdata->file_type) { case HPCS_TYPE_CE_CCD: - pret = read_signal(datafile, &mdata->data, &mdata->data_count, CE_CCD_STEP, mdata->sampling_rate); - break; + pret = read_signal(datafile, &mdata->data, &mdata->data_count, CE_CCD_STEP, mdata->sampling_rate); + break; case HPCS_TYPE_CE_CURRENT: - pret = read_signal(datafile, &mdata->data, &mdata->data_count, guess_current_step(mdata), mdata->sampling_rate); - break; + pret = read_signal(datafile, &mdata->data, &mdata->data_count, guess_current_step(mdata), mdata->sampling_rate); + break; case HPCS_TYPE_CE_DAD: - pret = read_signal(datafile, &mdata->data, &mdata->data_count, CE_DAD_STEP, mdata->sampling_rate); - break; + pret = read_signal(datafile, &mdata->data, &mdata->data_count, CE_DAD_STEP, mdata->sampling_rate); + break; case HPCS_TYPE_CE_POWER: case HPCS_TYPE_CE_VOLTAGE: - pret = read_signal(datafile, &mdata->data, &mdata->data_count, guess_elec_sigstep(mdata), mdata->sampling_rate); - break; + pret = read_signal(datafile, &mdata->data, &mdata->data_count, guess_elec_sigstep(mdata), mdata->sampling_rate); + break; case HPCS_TYPE_CE_PRESSURE: - pret = read_signal(datafile, &mdata->data, &mdata->data_count, CE_WORK_PARAM_STEP, mdata->sampling_rate); - break; + pret = read_signal(datafile, &mdata->data, &mdata->data_count, CE_WORK_PARAM_STEP, mdata->sampling_rate); + break; case HPCS_TYPE_CE_TEMPERATURE: - pret = read_signal(datafile, &mdata->data, &mdata->data_count, CE_WORK_PARAM_OLD_STEP * 10.0, mdata->sampling_rate); - break; + pret = read_signal(datafile, &mdata->data, &mdata->data_count, CE_WORK_PARAM_OLD_STEP * 10.0, mdata->sampling_rate); + break; case HPCS_TYPE_UNKNOWN: - ret = HPCS_E_UNKNOWN_TYPE; - goto out; - } + ret = HPCS_E_UNKNOWN_TYPE; + goto out; + } + + if (pret != PARSE_OK) { + PR_DEBUG("Cannot parse data in the file\n"); + ret = HPCS_E_PARSE_ERROR; + } + else + ret = HPCS_OK; +out: + fclose(datafile); + return ret; +} - if (pret != PARSE_OK) { - PR_DEBUG("Cannot parse data in the file\n"); +enum HPCS_RetCode hpcs_read_mheader(const char* filename, struct HPCS_MeasuredData* mdata) +{ + FILE* datafile; + enum HPCS_ParseCode pret; + enum HPCS_RetCode ret; + + if (mdata == NULL) + return HPCS_E_NULLPTR; + + datafile = fopen(filename, "rb"); + if (datafile == NULL) + return HPCS_E_CANT_OPEN; + + pret = read_file_header(datafile, mdata); + if (pret != PARSE_OK) ret = HPCS_E_PARSE_ERROR; - } else ret = HPCS_OK; -out: + fclose(datafile); return ret; } @@ -575,6 +533,73 @@ static enum HPCS_ParseCode read_date(FILE* datafile, struct HPCS_Date* date) return PARSE_OK; } +static enum HPCS_ParseCode read_file_header(FILE* datafile, struct HPCS_MeasuredData* mdata) +{ + enum HPCS_ParseCode pret; + + pret = read_string_at_offset(datafile, DATA_OFFSET_FILE_DESC, &mdata->file_description); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read file description\n"); + return pret; + } + pret = read_string_at_offset(datafile, DATA_OFFSET_SAMPLE_INFO, &mdata->sample_info); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read sample info\n"); + return pret; + } + pret = read_string_at_offset(datafile, DATA_OFFSET_OPERATOR_NAME, &mdata->operator_name); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read operator name\n"); + return pret; + } + pret = read_string_at_offset(datafile, DATA_OFFSET_METHOD_NAME, &mdata->method_name); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read method name\n"); + return pret; + } + pret = read_date(datafile, &mdata->date); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read date of measurement\n"); + return pret; + } + pret = read_string_at_offset(datafile, DATA_OFFSET_CS_VER, &mdata->cs_ver); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read ChemStation software version\n"); + return pret; + } + pret = read_string_at_offset(datafile, DATA_OFFSET_Y_UNITS, &mdata->y_units); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read values of Y axis\n"); + return pret; + } + pret = read_string_at_offset(datafile, DATA_OFFSET_CS_REV, &mdata->cs_rev); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read ChemStation software revision\n"); + return pret; + } + pret = read_sampling_rate(datafile, &mdata->sampling_rate); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot read sampling rate of the file\n"); + return pret; + } + pret = autodetect_file_type(datafile, &mdata->file_type, guess_p_meaning(mdata)); + if (pret != PARSE_OK) { + PR_DEBUG("Cannot determine the type of file\n"); + return pret; + } + + if (mdata->file_type == HPCS_TYPE_CE_DAD) { + 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 wavelength\n"); + return pret; + } + } + + guess_sampling_rate(mdata); + return PARSE_OK; +} + static enum HPCS_ParseCode read_method_info_file(HPCS_UFH fh, struct HPCS_MethodInfo* minfo) { HPCS_NChar line[64]; diff --git a/libhpcs_p.h b/libhpcs_p.h index 02816c3..918e850 100644 --- a/libhpcs_p.h +++ b/libhpcs_p.h @@ -1,3 +1,6 @@ +#ifndef LIBHPCS_P_H +#define LIBHCPS_P_H + #ifdef __cplusplus extern "C" { #endif @@ -139,6 +142,7 @@ static enum HPCS_ParseCode parse_native_method_info_line(char** name, char** val 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); static enum HPCS_ParseCode read_date(FILE* datafile, struct HPCS_Date* date); +static enum HPCS_ParseCode read_file_header(FILE* datafile, struct HPCS_MeasuredData* mdata); static enum HPCS_ParseCode read_method_info_file(HPCS_UFH fh, struct HPCS_MethodInfo* minfo); static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pairs, size_t* pairs_count, const HPCS_step step, const double sampling_rate); @@ -202,3 +206,5 @@ void reverse_endianness(char* bytes, size_t sz) { #ifdef __cplusplus } #endif + +#endif /* LIBHCPS_P_H */ diff --git a/test_tool.c b/test_tool.c index 4c7c172..a4e8c78 100644 --- a/test_tool.c +++ b/test_tool.c @@ -4,7 +4,7 @@ #include #include "include/libhpcs.h" -int read_data(const char* path) +static int read_data(const char* path) { struct HPCS_MeasuredData* mdata; enum HPCS_RetCode hret; @@ -39,7 +39,42 @@ int read_data(const char* path) return EXIT_SUCCESS; } -int read_info(const char* path) +static int read_header(const char* path) +{ + struct HPCS_MeasuredData* mdata; + enum HPCS_RetCode hret; + + mdata = hpcs_alloc_mdata(); + if (mdata == NULL) { + printf("Out of memory\n"); + return EXIT_FAILURE; + } + + hret = hpcs_read_mheader(path, mdata); + if (hret != HPCS_OK) { + printf("Cannot parse file: %s\n", hpcs_error_to_string(hret)); + return EXIT_FAILURE; + } + + printf("Sample info: %s\n" + "Operator name: %s\n" + "Method name: %s\n" + "Y units: %s\n" + "Sampling rate: %f\n" + "File description: %s\n", + mdata->sample_info, + mdata->operator_name, + mdata->method_name, + mdata->y_units, + mdata->sampling_rate, + mdata->file_description); + + hpcs_free_mdata(mdata); + + return EXIT_SUCCESS; +} + +static int read_info(const char* path) { struct HPCS_MethodInfo* minfo; enum HPCS_RetCode hret; @@ -74,12 +109,15 @@ int main(int argc, char** argv) printf("Usage: test_tool MODE FILE\n"); printf("MODE: d - read data file\n" " i - method info\n" + " h - read header only\n" "FILE: path\n"); return EXIT_FAILURE; } if (strcmp(argv[1], "d") == 0) return read_data(argv[2]); + else if (strcmp(argv[1], "h") == 0) + return read_header(argv[2]); else if (strcmp(argv[1], "i") == 0) return read_info(argv[2]); else {