commit 7a18a46e2af2668a2375bf7eb7ffa13955d99aa3
parent 3a2138a2defff7181e56b5b18c555b850e6bc55a
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 9 Jan 2026 15:11:21 +0100
The compression level of lines becomes a user parameter
This allows users to define their own balance between memory usage and
data compression/decompression speed.
Diffstat:
2 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/src/shtr.h b/src/shtr.h
@@ -42,6 +42,7 @@
#define SHTR_MAX_MOLECULES_COUNT 100
#define SHTR_MAX_ISOTOPES_COUNT 10
+#define SHTR_DEFAULT_COMPRESSION (~0)
struct shtr_isotope {
double abundance; /* in ]0, 1] */
@@ -160,8 +161,9 @@ static const struct shtr_create_args SHTR_CREATE_ARGS_DEFAULT =
struct shtr_line_list_load_args {
const char* filename; /* Name of the file to load or of the provided stream */
FILE* file; /* Stream from where data are loaded. NULL <=> load from file */
+ int compression_level;
};
-#define SHTR_LINE_LIST_LOAD_ARGS_NULL__ {NULL, NULL}
+#define SHTR_LINE_LIST_LOAD_ARGS_NULL__ {NULL, NULL, SHTR_DEFAULT_COMPRESSION}
static const struct shtr_line_list_load_args SHTR_LINE_LIST_LOAD_ARGS_NULL =
SHTR_LINE_LIST_LOAD_ARGS_NULL__;
diff --git a/src/shtr_line_list.c b/src/shtr_line_list.c
@@ -72,12 +72,14 @@ zctx_release(struct zctx* zctx)
}
static res_T
-zctx_init(struct zctx* zctx, struct shtr* shtr)
+zctx_init(struct zctx* zctx, struct shtr* shtr, const int level)
{
int ret = Z_OK;
+ int z_level = 0;
res_T res = RES_OK;
ASSERT(zctx && shtr);
+
*zctx = ZCTX_NULL;
SHTR(ref_get(shtr));
@@ -93,11 +95,18 @@ zctx_init(struct zctx* zctx, struct shtr* shtr)
zctx->zlines = MEM_ALLOC(zctx->shtr->allocator, ZCHUNK_MAX_SIZE);
if(!zctx->zlines) { res = RES_MEM_ERR; goto error; }
+ /* Define the zlib compression level */
+ if(level == SHTR_DEFAULT_COMPRESSION) {
+ z_level = Z_DEFAULT_COMPRESSION;
+ } else {
+ z_level = CLAMP(level, 0, 9); /* zlib compression level in [0,9] */
+ }
+
/* Initialize zlib */
zctx->stream.zalloc = zalloc_func;
zctx->stream.zfree = zfree_func;
zctx->stream.opaque = zctx->shtr->allocator;
- ret = deflateInit(&zctx->stream, Z_DEFAULT_COMPRESSION);
+ ret = deflateInit(&zctx->stream, z_level);
if(ret != Z_OK) { res = RES_UNKNOWN_ERR; goto error; }
zctx->zlib_is_init = 1;
@@ -490,19 +499,26 @@ static res_T
load_stream
(struct shtr* shtr,
FILE* stream,
- const char* name,
+ const struct shtr_line_list_load_args* args,
struct shtr_line_list** out_lines)
{
struct zctx zctx = ZCTX_NULL;
struct shtr_line_list* list = NULL;
struct txtrdr* txtrdr = NULL;
+ const char* name = NULL;
res_T res = RES_OK;
ASSERT(shtr && stream && name && out_lines);
+ if(args->file) { /* Load from stream */
+ name = args->filename ? args->filename : "<stream>";
+ } else {
+ name = args->filename;
+ }
+
res = create_line_list(shtr, &list);
if(res != RES_OK) goto error;
- res = zctx_init(&zctx, shtr);
+ res = zctx_init(&zctx, shtr, args->compression_level);
if(res != RES_OK) goto error;
res = txtrdr_stream(list->shtr->allocator, stream, name,
@@ -627,7 +643,6 @@ shtr_line_list_load
struct shtr_line_list** list)
{
FILE* file = NULL;
- const char* name = NULL;
res_T res = RES_OK;
if(!shtr || !list) { res = RES_BAD_ARG; goto error; }
@@ -635,11 +650,9 @@ shtr_line_list_load
if(res != RES_OK) goto error;
if(args->file) { /* Load from stream */
- name = args->filename ? args->filename : "<stream>";
file = args->file;
} else { /* Load from file */
- name = args->filename;
file = fopen(args->filename, "r");
if(!file) {
ERROR(shtr, "%s: error opening file `%s'.\n", FUNC_NAME, args->filename);
@@ -648,7 +661,7 @@ shtr_line_list_load
}
}
- res = load_stream(shtr, file, name, list);
+ res = load_stream(shtr, file, args, list);
if(res != RES_OK) goto error;
exit: