From 5abac3fec2c15ece7e6acb6aba49188797974c63 Mon Sep 17 00:00:00 2001 From: q-posev Date: Wed, 9 Jun 2021 18:10:59 +0200 Subject: [PATCH] [WIP] working write dset of strings, but with memory leak [text] --- src/templates_front/templator_front.org | 6 +- src/templates_text/build.sh | 8 +-- src/templates_text/templator_text.org | 78 ++++++++++++++++++++++--- tests/test.c | 17 +++++- tools/generator.py | 2 +- tools/generator_tools.py | 2 +- 6 files changed, 94 insertions(+), 19 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index ffdf6d1..e964e4d 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -1549,7 +1549,7 @@ trexio_read_$group_dset$ (trexio_t* const file, char* dset, const uint32_t max_s switch (file->back_end) { case TREXIO_TEXT: - //return trexio_text_read_$group_dset$(file, dset_str, rank, dims); + //return trexio_text_read_$group_dset$(file, dset, rank, dims); break; case TREXIO_HDF5: @@ -1623,7 +1623,7 @@ trexio_write_$group_dset$ (trexio_t* const file, const char* dset, const uint32_ switch (file->back_end) { case TREXIO_TEXT: - //rc = trexio_text_write_$group_dset$(file, dset, rank, dims); + rc = trexio_text_write_$group_dset$(file, (const char**) dset_str, rank, dims); break; case TREXIO_HDF5: @@ -1656,7 +1656,7 @@ trexio_has_$group_dset$ (trexio_t* const file) switch (file->back_end) { case TREXIO_TEXT: - //return trexio_text_has_$group_dset$(file); + return trexio_text_has_$group_dset$(file); break; case TREXIO_HDF5: diff --git a/src/templates_text/build.sh b/src/templates_text/build.sh index da0520c..0c0aecc 100644 --- a/src/templates_text/build.sh +++ b/src/templates_text/build.sh @@ -18,17 +18,17 @@ cat populated/pop_read_group_text.h >> trexio_text.h cat populated/pop_flush_group_text.h >> trexio_text.h cat populated/pop_has_dset_data_text.c >> trexio_text.c -#cat populated/pop_has_dset_str_text.c >> trexio_text.c +cat populated/pop_has_dset_str_text.c >> trexio_text.c cat populated/pop_has_num_text.c >> trexio_text.c cat populated/pop_read_dset_data_text.c >> trexio_text.c -#cat populated/pop_read_dset_str_text.c >> trexio_text.c +cat populated/pop_read_dset_str_text.c >> trexio_text.c cat populated/pop_read_num_text.c >> trexio_text.c cat populated/pop_write_dset_data_text.c >> trexio_text.c -#cat populated/pop_write_dset_str_text.c >> trexio_text.c +cat populated/pop_write_dset_str_text.c >> trexio_text.c cat populated/pop_write_num_text.c >> trexio_text.c cat populated/pop_hrw_num_text.h >> trexio_text.h cat populated/pop_hrw_dset_data_text.h >> trexio_text.h -#cat populated/pop_hrw_dset_str_text.h >> trexio_text.h +cat populated/pop_hrw_dset_str_text.h >> trexio_text.h cat rdm_text.c >> trexio_text.c cat rdm_text.h >> trexio_text.h diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index 5b5e2c5..4ac4817 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -669,16 +669,35 @@ trexio_text_has_$group_dset$ (trexio_t* const file) #+begin_src c :tangle hrw_dset_str_text.h :exports none trexio_exit_code trexio_text_has_$group_dset$ (trexio_t* const file); -trexio_exit_code trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, const uint32_t rank, const uint64_t* dims); -trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims); +trexio_exit_code trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t rank, const uint64_t* dims); +trexio_exit_code trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const uint32_t rank, const uint64_t* dims); #+end_src #+begin_src c :tangle read_dset_str_text.c trexio_exit_code -trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, - const uint32_t rank, const uint64_t* dims) +trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t rank, const uint64_t* dims) { + if (file == NULL) return TREXIO_INVALID_ARG_1; + if (dset == NULL) return TREXIO_INVALID_ARG_2; + + $group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file); + if ($group$ == NULL) return TREXIO_FAILURE; + + if (rank != $group$->rank_$group_dset$) return TREXIO_INVALID_ARG_3; + + for (unsigned int i=0 ; idims_$group_dset$[i]) return TREXIO_INVALID_ARG_4; + } + + /* REDO ! */ + strcpy(dset, ""); + for (uint64_t i=0 ; i$group_dset$[i]); + strcat(dset, TREXIO_DELIM); + } + /* REDO ! */ + return TREXIO_SUCCESS; } @@ -686,10 +705,46 @@ trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $ #+begin_src c :tangle write_dset_str_text.c trexio_exit_code -trexio_text_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group_dset$, - const uint32_t rank, const uint64_t* dims) +trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const uint32_t rank, const uint64_t* dims) { + if (file == NULL) return TREXIO_INVALID_ARG_1; + if (dset == NULL) return TREXIO_INVALID_ARG_2; + + if (file->mode == 'r') return TREXIO_READONLY; + + $group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file); + if ($group$ == NULL) return TREXIO_FAILURE; + + if ($group$->$group_dset$ != NULL) { + FREE($group$->$group_dset$[0]); + FREE($group$->$group_dset$); + } + + $group$->rank_$group_dset$ = rank; + + uint64_t dim_size = 1; + for (unsigned int i=0; i<$group$->rank_$group_dset$; ++i){ + $group$->dims_$group_dset$[i] = dims[i]; + dim_size *= dims[i]; + } + + /* REDO ! */ + $group$->$group_dset$ = CALLOC(dims[0], char*); + + char* tmp_str = CALLOC(dims[0]*32 + 1, char); + size_t tmp_len = 0; + for (uint64_t i=0 ; i$group_dset$[i] = tmp_str; + strncpy(tmp_str, dset[i], tmp_len); + tmp_str += tmp_len + 1; + printf("%s\n", $group$->$group_dset$[i]); + } + /* REDO ! */ + + $group$->to_flush = 1; + return TREXIO_SUCCESS; } @@ -700,7 +755,16 @@ trexio_exit_code trexio_text_has_$group_dset$ (trexio_t* const file) { - return TREXIO_SUCCESS; + if (file == NULL) return TREXIO_INVALID_ARG_1; + + $group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file); + if ($group$ == NULL) return TREXIO_FAILURE; + + if ($group$->rank_$group_dset$ > 0){ + return TREXIO_SUCCESS; + } else { + return TREXIO_HAS_NOT; + } } #+end_src diff --git a/tests/test.c b/tests/test.c index 7c24fc9..da1a42c 100644 --- a/tests/test.c +++ b/tests/test.c @@ -23,7 +23,7 @@ int main() { assert (rc == 0); test_write("test_write.dir", TREXIO_TEXT); test_read ("test_write.dir", TREXIO_TEXT); - rc = system("rm -rf test_write.dir"); +// rc = system("rm -rf test_write.dir"); assert (rc == 0); return 0; @@ -93,11 +93,21 @@ int test_write(const char* file_name, const back_end_t backend) { assert (rc == TREXIO_SUCCESS); rc = trexio_write_nucleus_coord(file,coord); assert (rc == TREXIO_SUCCESS); - if (backend == TREXIO_HDF5) rc = trexio_write_nucleus_label(file,labelxxx, 32); + rc = trexio_write_nucleus_label(file,labelxxx, 32); assert (rc == TREXIO_SUCCESS); - if (backend == TREXIO_HDF5) rc = trexio_write_nucleus_point_group(file, sym, 32); assert (rc == TREXIO_SUCCESS); + + // close current session + rc = trexio_close(file); + assert (rc == TREXIO_SUCCESS); + + /* + // open file in 'write' mode + file = trexio_open(file_name, 'w', backend); + assert (file != NULL); + + // check if the written data exists in the file rc = trexio_has_nucleus_num(file); assert (rc == TREXIO_SUCCESS); @@ -133,6 +143,7 @@ int test_write(const char* file_name, const back_end_t backend) { // close current session rc = trexio_close(file); assert (rc == TREXIO_SUCCESS); + */ /*================= END OF TEST ==================*/ diff --git a/tools/generator.py b/tools/generator.py index a9286df..e85d47c 100644 --- a/tools/generator.py +++ b/tools/generator.py @@ -63,6 +63,6 @@ for fname in files_todo['dset_str']: # populate group-related functions with mixed (iterative+recursive) scheme [text backend] for fname in files_todo['group']: - special_populate_text_group(fname, template_paths, group_dict, detailed_dsets_nostr, detailed_nums) + special_populate_text_group(fname, template_paths, group_dict, detailed_dsets, detailed_nums) # --------------------------------------------------------------------------- # diff --git a/tools/generator_tools.py b/tools/generator_tools.py index cf26bc2..6597470 100644 --- a/tools/generator_tools.py +++ b/tools/generator_tools.py @@ -43,7 +43,7 @@ def get_files_todo(source_files: dict) -> dict: files_todo[key] = list(filter(lambda x: key in x, files_todo['all'])) files_todo['group'].append('struct_text_group_dset.h') - # files that correspond to todo1 group (e.g. only iterative population within the function body) + # files that correspond to iterative population (e.g. the code is repeated within the function body but the function itself is unique) files_todo['auxiliary'] = ['def_hdf5.c', 'basic_hdf5.c', 'basic_text_group.c', 'struct_hdf5.h', 'struct_text_group.h'] return files_todo