From f2c5a3a51b1e12a70d08b6311242a075b5eba749 Mon Sep 17 00:00:00 2001 From: q-posev Date: Tue, 8 Jun 2021 10:45:43 +0200 Subject: [PATCH] add max_str_len argument to read/write a single string --- src/templates_front/templator_front.org | 47 ++++++++++++++++--------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index eb33b1f..9ea77cb 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -1546,25 +1546,22 @@ trexio_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t assert(file->back_end < TREXIO_INVALID_BACK_END); - rc = TREXIO_FAILURE; switch (file->back_end) { case TREXIO_TEXT: - //rc = return trexio_text_read_$group_dset$(file, dset_str, rank, dims); + //return trexio_text_read_$group_dset$(file, dset_str, rank, dims); break; case TREXIO_HDF5: - rc = trexio_hdf5_read_$group_dset$(file, dset, rank, dims, max_str_len); + return trexio_hdf5_read_$group_dset$(file, dset, rank, dims, max_str_len); break; /* case TREXIO_JSON: - rc = trexio_json_read_$group_dset$(file, dset, rank, dims); + return trexio_json_read_$group_dset$(file, dset, rank, dims); break; ,*/ } - return rc; - } #+end_src @@ -1730,26 +1727,29 @@ end interface #+begin_src c :tangle hrw_attr_str_front.h :exports none trexio_exit_code trexio_has_$group_str$(trexio_t* const file); -trexio_exit_code trexio_read_$group_str$(trexio_t* const file, char* const str); -trexio_exit_code trexio_write_$group_str$(trexio_t* const file, const char* str); +trexio_exit_code trexio_read_$group_str$(trexio_t* const file, char* const str, const uint32_t max_str_len); +trexio_exit_code trexio_write_$group_str$(trexio_t* const file, const char* str, const uint32_t max_str_len); #+end_src #+begin_src c :tangle read_attr_str_front.c trexio_exit_code -trexio_read_$group_str$ (trexio_t* const file, char* const str) +trexio_read_$group_str$ (trexio_t* const file, char* const str, const uint32_t max_str_len) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (str == NULL) return TREXIO_INVALID_ARG_2; + if (max_str_len <= 0) return TREXIO_INVALID_ARG_3; + + trexio_exit_code rc = TREXIO_FAILURE; switch (file->back_end) { case TREXIO_TEXT: - //return trexio_text_read_$group_str$(file, str); + //rc = trexio_text_read_$group_str$(file, str); break; case TREXIO_HDF5: - return trexio_hdf5_read_$group_str$(file, str); + rc = trexio_hdf5_read_$group_str$(file, str); break; /* case TREXIO_JSON: @@ -1758,19 +1758,32 @@ trexio_read_$group_str$ (trexio_t* const file, char* const str) ,*/ } - return TREXIO_FAILURE; + if (rc != TREXIO_SUCCESS) return rc; + + size_t len_read = strlen(str); + if (max_str_len < len_read) { + for(size_t i=max_str_len; iback_end) { case TREXIO_TEXT: @@ -1815,8 +1828,8 @@ trexio_has_$group_str$ (trexio_t* const file) break; ,*/ } - return TREXIO_FAILURE; + return TREXIO_FAILURE; } #+end_src @@ -1828,20 +1841,22 @@ trexio_has_$group_str$ (trexio_t* const file) #+begin_src f90 :tangle write_attr_str_front_fortran.f90 interface - integer function trexio_write_$group_str$ (trex_file, str) bind(C) + integer function trexio_write_$group_str$ (trex_file, str, max_str_len) bind(C) use, intrinsic :: iso_c_binding integer(8), intent(in), value :: trex_file character, intent(in) :: str(*) + integer(4), intent(in) :: max_str_len end function trexio_write_$group_str$ end interface #+end_src #+begin_src f90 :tangle read_attr_str_front_fortran.f90 interface - integer function trexio_read_$group_str$ (trex_file, str) bind(C) + integer function trexio_read_$group_str$ (trex_file, str, max_str_len) bind(C) use, intrinsic :: iso_c_binding integer(8), intent(in), value :: trex_file character, intent(out) :: str(*) + integer(4), intent(in) :: max_str_len end function trexio_read_$group_str$ end interface #+end_src