1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-10-02 14:31:05 +02:00

add max_str_len argument to read/write a single string

This commit is contained in:
q-posev 2021-06-08 10:45:43 +02:00
parent 625cbc3d20
commit f2c5a3a51b

View File

@ -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); assert(file->back_end < TREXIO_INVALID_BACK_END);
rc = TREXIO_FAILURE;
switch (file->back_end) { switch (file->back_end) {
case TREXIO_TEXT: 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; break;
case TREXIO_HDF5: 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; break;
/* /*
case TREXIO_JSON: case TREXIO_JSON:
rc = trexio_json_read_$group_dset$(file, dset, rank, dims); return trexio_json_read_$group_dset$(file, dset, rank, dims);
break; break;
,*/ ,*/
} }
return rc;
} }
#+end_src #+end_src
@ -1730,26 +1727,29 @@ end interface
#+begin_src c :tangle hrw_attr_str_front.h :exports none #+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_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_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); trexio_exit_code trexio_write_$group_str$(trexio_t* const file, const char* str, const uint32_t max_str_len);
#+end_src #+end_src
#+begin_src c :tangle read_attr_str_front.c #+begin_src c :tangle read_attr_str_front.c
trexio_exit_code 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 (file == NULL) return TREXIO_INVALID_ARG_1;
if (str == NULL) return TREXIO_INVALID_ARG_2; 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) { switch (file->back_end) {
case TREXIO_TEXT: case TREXIO_TEXT:
//return trexio_text_read_$group_str$(file, str); //rc = trexio_text_read_$group_str$(file, str);
break; break;
case TREXIO_HDF5: case TREXIO_HDF5:
return trexio_hdf5_read_$group_str$(file, str); rc = trexio_hdf5_read_$group_str$(file, str);
break; break;
/* /*
case TREXIO_JSON: 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; i<len_read; i++) {
str[i] = '\0';
}
}
return TREXIO_SUCCESS;
} }
#+end_src #+end_src
#+begin_src c :tangle write_attr_str_front.c #+begin_src c :tangle write_attr_str_front.c
trexio_exit_code trexio_exit_code
trexio_write_$group_str$ (trexio_t* const file, const char* str) trexio_write_$group_str$ (trexio_t* const file, const char* str, const uint32_t max_str_len)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
if (str == NULL) return TREXIO_INVALID_ARG_2; if (str == NULL) return TREXIO_INVALID_ARG_2;
if (max_str_len <= 0) return TREXIO_INVALID_ARG_3;
if (trexio_has_$group_str$(file) == TREXIO_SUCCESS) return TREXIO_NUM_ALREADY_EXISTS; if (trexio_has_$group_str$(file) == TREXIO_SUCCESS) return TREXIO_NUM_ALREADY_EXISTS;
size_t len_write = strlen(str);
if (max_str_len < len_write) return TREXIO_INVALID_ARG_3;
switch (file->back_end) { switch (file->back_end) {
case TREXIO_TEXT: case TREXIO_TEXT:
@ -1815,8 +1828,8 @@ trexio_has_$group_str$ (trexio_t* const file)
break; break;
,*/ ,*/
} }
return TREXIO_FAILURE;
return TREXIO_FAILURE;
} }
#+end_src #+end_src
@ -1828,20 +1841,22 @@ trexio_has_$group_str$ (trexio_t* const file)
#+begin_src f90 :tangle write_attr_str_front_fortran.f90 #+begin_src f90 :tangle write_attr_str_front_fortran.f90
interface 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 use, intrinsic :: iso_c_binding
integer(8), intent(in), value :: trex_file integer(8), intent(in), value :: trex_file
character, intent(in) :: str(*) character, intent(in) :: str(*)
integer(4), intent(in) :: max_str_len
end function trexio_write_$group_str$ end function trexio_write_$group_str$
end interface end interface
#+end_src #+end_src
#+begin_src f90 :tangle read_attr_str_front_fortran.f90 #+begin_src f90 :tangle read_attr_str_front_fortran.f90
interface 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 use, intrinsic :: iso_c_binding
integer(8), intent(in), value :: trex_file integer(8), intent(in), value :: trex_file
character, intent(out) :: str(*) character, intent(out) :: str(*)
integer(4), intent(in) :: max_str_len
end function trexio_read_$group_str$ end function trexio_read_$group_str$
end interface end interface
#+end_src #+end_src