1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-08-25 06:31:43 +02:00

add top-level read/write functions that accept arrays of strings directly

This commit is contained in:
q-posev 2021-06-14 15:40:26 +02:00
parent ae32a02652
commit 5553e9110e
2 changed files with 115 additions and 18 deletions

View File

@ -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(*)

View File

@ -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<num; i++){
strcat(labelxxx,label[i]);
strcat(labelxxx,TREXIO_DELIM);
}
}*/
const char* sym = "B3U and some stuff";
/*================= START OF TEST ==================*/
@ -93,7 +93,8 @@ 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);
rc = trexio_write_nucleus_label(file,labelxxx, 32);
//rc = trexio_write_nucleus_label(file,labelxxx, 32);
rc = trexio_write_nucleus_label(file, label, 32);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_point_group(file, sym, 32);
assert (rc == TREXIO_SUCCESS);
@ -180,17 +181,27 @@ int test_read(const char* file_name, const back_end_t backend) {
assert( x*x < 1.e-14);
// read nucleus_label
labelxxx = (char*) malloc(num*32*sizeof(char));
rc = trexio_read_nucleus_label(file,labelxxx, 2);
/*labelxxx = (char*) malloc(num*32*sizeof(char));
rc = trexio_read_nucleus_label(file,labelxxx, 2);*/
label = (char**) malloc(num*sizeof(char*));
for (int i=0; i<num; i++){
label[i] = (char*) malloc(32*sizeof(char));
}
rc = trexio_read_nucleus_label(file, label, 2);
//printf("%s\n", trexio_string_of_error(rc));
//printf("%s\n", labelxxx);
for (int i=0; i<num; i++){
printf("%s\n", label[i]);
}
assert (rc == TREXIO_SUCCESS);
assert( strcmp(label[0], "C") == 0 );
assert( strcmp(label[1], "Na") == 0 );
char * pch;
pch = strtok(labelxxx, TREXIO_DELIM);
/*pch = strtok(labelxxx, TREXIO_DELIM);
assert( strcmp(pch, "C") == 0 );
pch = strtok(NULL, TREXIO_DELIM);
assert( strcmp(pch, "Na") == 0 );
assert( strcmp(pch, "Na") == 0 );*/
point_group = (char*) malloc(32*sizeof(char));
@ -206,7 +217,11 @@ int test_read(const char* file_name, const back_end_t backend) {
free(point_group);
free(labelxxx);
for (int i=0; i<num; i++){
free(label[i]);
}
free(label);
//free(labelxxx);
// close current session
rc = trexio_close(file);