From 5553e9110eb3f3f9961cb30de40d1e8010b35b85 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 14 Jun 2021 15:40:26 +0200 Subject: [PATCH] add top-level read/write functions that accept arrays of strings directly --- src/templates_front/templator_front.org | 98 +++++++++++++++++++++++-- tests/test.c | 35 ++++++--- 2 files changed, 115 insertions(+), 18 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 37fee4b..b151535 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -599,7 +599,7 @@ interface end function trexio_open_c end interface #+end_src - + ** File closing ~trexio_close~ closes an existing ~trexio_t~ file. @@ -1520,13 +1520,15 @@ trexio_read_chunk_ao_2e_int_eri_value_64(trexio_t* const file, #+begin_src c :tangle hrw_dset_str_front.h :exports none trexio_exit_code trexio_has_$group_dset$(trexio_t* const file); -trexio_exit_code trexio_read_$group_dset$(trexio_t* const file, char* dset, const uint32_t max_str_len); -trexio_exit_code trexio_write_$group_dset$(trexio_t* const file, const char* dset, const uint32_t max_str_len); +trexio_exit_code trexio_read_$group_dset$_low(trexio_t* const file, char* dset, const uint32_t max_str_len); +trexio_exit_code trexio_write_$group_dset$_low(trexio_t* const file, const char* dset, const uint32_t max_str_len); +trexio_exit_code trexio_read_$group_dset$(trexio_t* const file, char** dset, const uint32_t max_str_len); +trexio_exit_code trexio_write_$group_dset$(trexio_t* const file, const char** dset, const uint32_t max_str_len); #+end_src #+begin_src c :tangle read_dset_str_front.c trexio_exit_code -trexio_read_$group_dset$ (trexio_t* const file, char* dset, const uint32_t max_str_len) +trexio_read_$group_dset$_low (trexio_t* const file, char* dset, const uint32_t max_str_len) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -1545,7 +1547,6 @@ trexio_read_$group_dset$ (trexio_t* const file, char* dset, const uint32_t max_s uint64_t dims[$group_dset_rank$] = {$group_dset_dim_list$}; assert(file->back_end < TREXIO_INVALID_BACK_END); - switch (file->back_end) { case TREXIO_TEXT: @@ -1562,12 +1563,57 @@ trexio_read_$group_dset$ (trexio_t* const file, char* dset, const uint32_t max_s ,*/ } +} + +trexio_exit_code +trexio_read_$group_dset$ (trexio_t* const file, char** dset, const uint32_t max_str_len) +{ + + if (file == NULL) return TREXIO_INVALID_ARG_1; + if (dset == NULL) return TREXIO_INVALID_ARG_2; + if (max_str_len <= 0) return TREXIO_INVALID_ARG_3; + + assert(file->back_end < TREXIO_INVALID_BACK_END); + + trexio_exit_code rc; + int64_t dset_dim = 0; + + /* Error handling for this call is added by the generator */ + rc = trexio_read_$group_dset_dim$_64(file, &(dset_dim)); + + if (dset_dim == 0L) return TREXIO_INVALID_NUM; + + char* str_compiled = CALLOC(dset_dim*max_str_len + 1, char); + if (str_compiled == NULL) return TREXIO_ALLOCATION_FAILED; + + rc = trexio_read_$group_dset$_low(file, str_compiled, max_str_len); + if (rc != TREXIO_SUCCESS) { + FREE(str_compiled); + return rc; + } + + char * pch; + for (uint64_t i=0; i < dset_dim; i++) { + + pch = i == 0 ? strtok(str_compiled, TREXIO_DELIM) : strtok(NULL, TREXIO_DELIM) ; + if (pch == NULL) { + FREE(str_compiled); + return TREXIO_FAILURE; + } + + strncpy(dset[i], pch, max_str_len+1); + + } + + FREE(str_compiled); + return TREXIO_SUCCESS; + } #+end_src #+begin_src c :tangle write_dset_str_front.c trexio_exit_code -trexio_write_$group_dset$ (trexio_t* const file, const char* dset, const uint32_t max_str_len) +trexio_write_$group_dset$_low (trexio_t* const file, const char* dset, const uint32_t max_str_len) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -1641,6 +1687,42 @@ trexio_write_$group_dset$ (trexio_t* const file, const char* dset, const uint32_ return rc; +} + +trexio_exit_code +trexio_write_$group_dset$ (trexio_t* const file, const char** dset, const uint32_t max_str_len) +{ + + if (file == NULL) return TREXIO_INVALID_ARG_1; + if (dset == NULL) return TREXIO_INVALID_ARG_2; + if (max_str_len <= 0) return TREXIO_INVALID_ARG_3; + if (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) return TREXIO_DSET_ALREADY_EXISTS; + + assert(file->back_end < TREXIO_INVALID_BACK_END); + + trexio_exit_code rc; + int64_t dset_dim = 0; + + /* Error handling for this call is added by the generator */ + rc = trexio_read_$group_dset_dim$_64(file, &(dset_dim)); + + if (dset_dim == 0L) return TREXIO_INVALID_NUM; + + char* str_compiled = CALLOC(dset_dim*max_str_len + 1, char); + if (str_compiled == NULL) return TREXIO_ALLOCATION_FAILED; + + strcpy(str_compiled, ""); + for (uint64_t i=0; i < dset_dim; i++) { + strcat(str_compiled, dset[i]); + strcat(str_compiled, TREXIO_DELIM); + } + + rc = trexio_write_$group_dset$_low(file, str_compiled, max_str_len); + + FREE(str_compiled); + + return rc; + } #+end_src @@ -1679,7 +1761,7 @@ trexio_has_$group_dset$ (trexio_t* const file) #+begin_src f90 :tangle write_dset_str_front_fortran.f90 interface - integer function trexio_write_$group_dset$ (trex_file, dset, max_str_len) bind(C) + integer function trexio_write_$group_dset$ (trex_file, dset, max_str_len) bind(C, name="trexio_write_$group_dset$_low") use, intrinsic :: iso_c_binding integer(8), intent(in), value :: trex_file character, intent(in) :: dset(*) @@ -1690,7 +1772,7 @@ end interface #+begin_src f90 :tangle read_dset_str_front_fortran.f90 interface - integer function trexio_read_$group_dset$ (trex_file, dset, max_str_len) bind(C) + integer function trexio_read_$group_dset$ (trex_file, dset, max_str_len) bind(C, name="trexio_read_$group_dset$_low") use, intrinsic :: iso_c_binding integer(8), intent(in), value :: trex_file character, intent(out) :: dset(*) diff --git a/tests/test.c b/tests/test.c index 1e83a16..9f9c0f0 100644 --- a/tests/test.c +++ b/tests/test.c @@ -16,14 +16,14 @@ int main() { assert (rc == 0); test_write("test_write.h5", TREXIO_HDF5); test_read ("test_write.h5", TREXIO_HDF5); - rc = system("rm -rf test_write.h5"); +// rc = system("rm -rf test_write.h5"); assert (rc == 0); rc = system("rm -rf test_write.dir"); 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; @@ -69,11 +69,11 @@ int test_write(const char* file_name, const back_end_t backend) { "H" }; //char labelxxx[] = "C C C Na C C H H H Ru H H"; - char labelxxx[128] = ""; + /*char labelxxx[128] = ""; for (int i=0; i