commit fad88711a3c3573b9a79cfa61f23f3dd7d7a577d
parent 81407cedf36c48fffc78494f58e78a8f96709818
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 23 May 2022 14:45:46 +0200
[De]serialize the line view
Add the shtr_lines_view_write and shtr_lines_view_create_from_stream
functions.
Diffstat:
2 files changed, 148 insertions(+), 8 deletions(-)
diff --git a/src/shtr.h b/src/shtr.h
@@ -287,6 +287,13 @@ shtr_lines_view_create
const struct shtr_lines_view_create_args* args,
struct shtr_lines_view** view);
+/* Load the line list serialized with the "shtr_lines_view_write" function */
+SHTR_API res_T
+shtr_lines_view_create_from_stream
+ (struct shtr* shtr,
+ FILE* stream,
+ struct shtr_lines_view** view);
+
SHTR_API res_T
shtr_lines_view_ref_get
(struct shtr_lines_view* view);
@@ -306,6 +313,11 @@ shtr_lines_view_get_line
const size_t iline,
const struct shtr_line** line);
+SHTR_API res_T
+shtr_lines_view_write
+ (const struct shtr_lines_view* view,
+ FILE* stream);
+
END_DECLS
#endif /* SHTR_H */
diff --git a/src/shtr_lines_view.c b/src/shtr_lines_view.c
@@ -28,6 +28,10 @@
#include <math.h>
+/* Version of the line view. One should increment it and perform a version
+ * management onto serialized data when the line view structure are updated. */
+static const int SHTR_LINES_VIEW_VERSION = 0;
+
struct shtr_lines_view {
struct shtr_lines_list* list;
struct darray_size_t line_ids; /* Indices of the selected lines */
@@ -115,6 +119,33 @@ check_shtr_lines_view_create_args
}
static res_T
+create_lines_view(struct shtr* shtr, struct shtr_lines_view** out_view)
+{
+ struct shtr_lines_view* view = NULL;
+ res_T res = RES_OK;
+ ASSERT(shtr && out_view);
+
+ view = MEM_CALLOC(shtr->allocator, 1, sizeof(*view));
+ if(!view) {
+ log_err(shtr, "Could not allocate the line view.\n");
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ ref_init(&view->ref);
+ darray_size_t_init(shtr->allocator, &view->line_ids);
+
+exit:
+ *out_view = view;
+ return res;
+error:
+ if(view) {
+ SHTR(lines_view_ref_put(view));
+ view = NULL;
+ }
+ goto exit;
+}
+
+static res_T
select_lines
(struct shtr_lines_view* view,
const char* caller,
@@ -217,18 +248,14 @@ shtr_lines_view_create
res_T res = RES_OK;
if(!list || !out_view) { res = RES_BAD_ARG; goto error; }
+
res = check_shtr_lines_view_create_args(list->shtr, FUNC_NAME, args);
if(res != RES_OK) goto error;
- view = MEM_CALLOC(list->shtr->allocator, 1, sizeof(*view));
- if(!view) {
- log_err(list->shtr, "Could not allocate the lines view.\n");
- res = RES_MEM_ERR;
- goto error;
- }
- ref_init(&view->ref);
+
+ res = create_lines_view(list->shtr, &view);
+ if(res != RES_OK) goto error;
SHTR(lines_list_ref_get(list));
view->list = list;
- darray_size_t_init(list->shtr->allocator, &view->line_ids);
res = select_lines(view, FUNC_NAME, args);
if(res != RES_OK) goto error;
@@ -242,6 +269,71 @@ error:
}
res_T
+shtr_lines_view_create_from_stream
+ (struct shtr* shtr,
+ FILE* stream,
+ struct shtr_lines_view** out_view)
+{
+ struct shtr_lines_view* view = NULL;
+ size_t nids;
+ int version;
+ res_T res = RES_OK;
+
+ if(!shtr || !stream || !out_view) { res = RES_BAD_ARG; goto error; }
+
+ res = create_lines_view(shtr, &view);
+ 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_VIEW_VERSION) {
+ log_err(shtr,
+ "%s: unexpected line view version %d. "
+ "Expecting a line view in version %d.\n",
+ FUNC_NAME, version, SHTR_LINES_VIEW_VERSION);
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ res = shtr_lines_list_create_from_stream(shtr, stream, &view->list);
+ if(res != RES_OK) goto error;
+
+ READ(&nids, 1);
+ res = darray_size_t_resize(&view->line_ids, nids);
+ if(res != RES_OK) {
+ log_err(shtr, "%s: error allocating the line view -- %s.\n",
+ FUNC_NAME, res_to_cstr(res));
+ goto error;
+ }
+
+ READ(darray_size_t_data_get(&view->line_ids), nids);
+ #undef READ
+
+exit:
+ if(out_view) *out_view = view;
+ return res;
+error:
+ if(view) {
+ SHTR(lines_view_ref_put(view));
+ view = NULL;
+ }
+ goto exit;
+}
+
+res_T
shtr_lines_view_ref_get(struct shtr_lines_view* view)
{
if(!view) return RES_BAD_ARG;
@@ -279,3 +371,39 @@ shtr_lines_view_get_line
*line = darray_line_cdata_get(&view->list->lines) + i;
return RES_OK;
}
+
+res_T
+shtr_lines_view_write
+ (const struct shtr_lines_view* view,
+ FILE* stream)
+{
+ size_t nids = 0;
+ res_T res = RES_OK;
+
+ if(!view || !stream) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ #define WRITE(Var, Nb) { \
+ if(fwrite((Var), sizeof(*(Var)), (Nb), stream) != (Nb)) { \
+ log_err(view->list->shtr, "%s: error writing line view.\n", FUNC_NAME); \
+ res = RES_IO_ERR; \
+ goto error; \
+ } \
+ } (void)0
+ WRITE(&SHTR_LINES_VIEW_VERSION, 1);
+
+ res = shtr_lines_list_write(view->list, stream);
+ if(res != RES_OK) goto error;
+
+ nids = darray_size_t_size_get(&view->line_ids);
+ WRITE(&nids, 1);
+ WRITE(darray_size_t_cdata_get(&view->line_ids), nids);
+ #undef WRITE
+
+exit:
+ return res;
+error:
+ goto exit;
+}