]> Devoid-pointer.net GitWeb - libHPCS.git/commitdiff
- Add a new function to read just the header of the file
authorMichal Malý <madcatxster@devoid-pointer.net>
Sun, 8 Nov 2015 16:38:14 +0000 (17:38 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Sun, 8 Nov 2015 16:38:14 +0000 (17:38 +0100)
- Add include guards to the headers

include/libhpcs.h
libhpcs.c
libhpcs_p.h
test_tool.c

index 3142c76a155d1af02e9a0a57dc50d1baf12c5cf5..d3dcfa9cd2831dca524eaab55b91450bba840e53 100644 (file)
@@ -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 */
index 237c7a19bbd5616e7ab9026c58530f13d5e0f46c..8b3f38c432ca62f8d03c46de32481b9e140c06dc 100644 (file)
--- 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];
index 02816c398e788d0a0997ba1b7707fc6a95b12c5e..918e850a5edc23803bd33a39970f67a8b0680ab2 100644 (file)
@@ -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 */
index 4c7c172aed95adb8bb81152a3006444921935921..a4e8c78a6df2efe095b74787a3128e76e381fe72 100644 (file)
@@ -4,7 +4,7 @@
 #include <string.h>
 #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 {