commit ae4e7fcebfca51049896bb2f7bbc341d6c208f2d
parent 7d57dc6c29978eb6458b775d7ee23fd707e8f94c
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 23 May 2022 12:19:17 +0200
[De]serialize the line list
Add the shtr_lines_list_write and shtr_lines_list_create_from_stream
functions.
Diffstat:
3 files changed, 114 insertions(+), 0 deletions(-)
diff --git a/src/shtr.h b/src/shtr.h
@@ -248,6 +248,13 @@ shtr_lines_list_load_stream
const char* stream_name, /* NULL <=> use default stream name */
struct shtr_lines_list** list);
+/* Load the line list serialized with the "shtr_lines_list_write" function */
+SHTR_API res_T
+shtr_lines_list_create_from_stream
+ (struct shtr* shtr,
+ FILE* stream,
+ struct shtr_lines_list** list);
+
SHTR_API res_T
shtr_lines_list_ref_get
(struct shtr_lines_list* list);
@@ -266,6 +273,11 @@ shtr_lines_list_get
(const struct shtr_lines_list* list,
const struct shtr_line* lines[]);
+SHTR_API res_T
+shtr_lines_list_write
+ (const struct shtr_lines_list* list,
+ FILE* stream);
+
/*******************************************************************************
* Lines view API
******************************************************************************/
diff --git a/src/shtr_lines_list.c b/src/shtr_lines_list.c
@@ -284,6 +284,71 @@ shtr_lines_list_load_stream
}
res_T
+shtr_lines_list_create_from_stream
+ (struct shtr* shtr,
+ FILE* stream,
+ struct shtr_lines_list** out_lnlst)
+{
+ struct shtr_lines_list* lnlst = NULL;
+ size_t nlines;
+ int version = 0;
+ res_T res = RES_OK;
+
+ if(!shtr || !out_lnlst || !stream) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ res = create_lines_list(shtr, &lnlst);
+ if(res != RES_OK) goto error;
+
+ #define READ(Var, Nb) { \
+ if(fread((Var), sizeof(*(Var)), (Nb), stream) != (Nb)) { \
+ if(feof(stream)) { \
+ res = RES_BAD_ARG; \
+ } else if(ferror(stream)) { \
+ res = RES_IO_ERR; \
+ } else { \
+ res = RES_UNKNOWN_ERR; \
+ } \
+ log_err(shtr, "%s: error reading isotope metadata -- %s.\n", \
+ FUNC_NAME, res_to_cstr(res)); \
+ goto error; \
+ } \
+ } (void)0
+ READ(&version, 1);
+ if(version != SHTR_LINES_LIST_VERSION) {
+ log_err(shtr,
+ "%s: unexpected line list version %d. "
+ "Experting a line list in version %d.\n",
+ FUNC_NAME, version, SHTR_LINES_LIST_VERSION);
+ res = RES_BAD_ARG;
+ goto error;
+ }
+ READ(&nlines, 1);
+
+ res = darray_line_resize(&lnlst->lines, nlines);
+ if(res != RES_OK) {
+ log_err(shtr, "%s: error allocating the line list -- %s.\n",
+ FUNC_NAME, res_to_cstr(res));
+ goto error;
+ }
+
+ READ(darray_line_data_get(&lnlst->lines), nlines);
+ #undef READ
+
+exit:
+ if(out_lnlst) *out_lnlst = lnlst;
+ return res;
+error:
+ if(lnlst) {
+ SHTR(lines_list_ref_put(lnlst));
+ lnlst = NULL;
+ }
+ goto exit;
+}
+
+res_T
shtr_lines_list_ref_get(struct shtr_lines_list* lnlst)
{
if(!lnlst) return RES_BAD_ARG;
@@ -318,3 +383,36 @@ shtr_lines_list_get
*lines_list = darray_line_cdata_get(&lnlst->lines);
return RES_OK;
}
+
+res_T
+shtr_lines_list_write
+ (const struct shtr_lines_list* lnlst,
+ FILE* stream)
+{
+ size_t nlines = 0;
+ res_T res = RES_OK;
+
+ if(!lnlst || !stream) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ nlines = darray_line_size_get(&lnlst->lines);
+
+ #define WRITE(Var, Nb) { \
+ if(fwrite((Var), sizeof(*(Var)), (Nb), stream) != (Nb)) { \
+ log_err(lnlst->shtr, "%s: error writing line list.\n", FUNC_NAME); \
+ res = RES_IO_ERR; \
+ goto error; \
+ } \
+ } (void)0
+ WRITE(&SHTR_LINES_LIST_VERSION, 1);
+ WRITE(&nlines, 1);
+ WRITE(darray_line_cdata_get(&lnlst->lines), nlines);
+ #undef WRITE
+
+exit:
+ return res;
+error:
+ goto exit;
+}
diff --git a/src/shtr_lines_list_c.h b/src/shtr_lines_list_c.h
@@ -29,6 +29,10 @@
struct shtr;
+/* Version of the lines list. One should increment it and perform a version
+ * management onto serialized data when the lines list structure are updated. */
+static const int SHTR_LINES_LIST_VERSION = 0;
+
struct shtr_lines_list {
/* Lines sorted in ascending order wrt their wavenumber */
struct darray_line lines;