1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 20:35:44 +01:00

read single numerical attributes based on _isSet bool flag

This commit is contained in:
q-posev 2021-09-21 15:41:12 +02:00
parent d5405e700c
commit 788f7e666d
3 changed files with 69 additions and 6 deletions

View File

@ -46,6 +46,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdbool.h>
#+end_src #+end_src
@ -79,6 +80,7 @@
#+begin_src c :tangle struct_text_group_dset.h #+begin_src c :tangle struct_text_group_dset.h
typedef struct $group$_s { typedef struct $group$_s {
$group_num_dtype_double$ $group_num$; $group_num_dtype_double$ $group_num$;
bool $group_num$_isSet;
$group_dset_dtype$* $group_dset$; $group_dset_dtype$* $group_dset$;
uint32_t rank_$group_dset$; uint32_t rank_$group_dset$;
uint32_t to_flush; uint32_t to_flush;
@ -346,8 +348,30 @@ trexio_text_read_$group$ (trexio_text_t* const file)
} }
// END REPEAT GROUP_DSET_ALL // END REPEAT GROUP_DSET_ALL
unsigned int local_isSet;
// START REPEAT GROUP_NUM // START REPEAT GROUP_NUM
/* Read data */ /* Read data */
rc = fscanf(f, "%1023s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "$group_num$_isSet") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_num$_isSet") != 0)) {
FREE(buffer);
fclose(f);
FREE($group$);
return NULL;
}
/* additional parameter local_isSet is needed to suppress warning when fscanf into bool variable using %u or %d */
rc = fscanf(f, "%u", &(local_isSet));
$group$->$group_num$_isSet = (bool) local_isSet;
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
FREE($group$);
return NULL;
}
if ($group$->$group_num$_isSet == true) {
rc = fscanf(f, "%1023s", buffer); rc = fscanf(f, "%1023s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "$group_num$") != 0))); assert(!((rc != 1) || (strcmp(buffer, "$group_num$") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_num$") != 0)) { if ((rc != 1) || (strcmp(buffer, "$group_num$") != 0)) {
@ -365,6 +389,7 @@ trexio_text_read_$group$ (trexio_text_t* const file)
FREE($group$); FREE($group$);
return NULL; return NULL;
} }
}
// END REPEAT GROUP_NUM // END REPEAT GROUP_NUM
// START REPEAT GROUP_ATTR_STR // START REPEAT GROUP_ATTR_STR
@ -555,7 +580,8 @@ trexio_text_flush_$group$ (trexio_text_t* const file)
// END REPEAT GROUP_DSET_ALL // END REPEAT GROUP_DSET_ALL
// START REPEAT GROUP_NUM // START REPEAT GROUP_NUM
fprintf(f, "$group_num$ %$group_num_std_dtype_out$ \n", $group$->$group_num$); fprintf(f, "$group_num$_isSet %u \n", $group$->$group_num$_isSet);
if ($group$->$group_num$_isSet == true) fprintf(f, "$group_num$ %$group_num_std_dtype_out$ \n", $group$->$group_num$);
// END REPEAT GROUP_NUM // END REPEAT GROUP_NUM
// START REPEAT GROUP_ATTR_STR // START REPEAT GROUP_ATTR_STR
@ -662,6 +688,7 @@ trexio_text_write_$group_num$ (trexio_t* const file, const $group_num_dtype_doub
if ($group$ == NULL) return TREXIO_FAILURE; if ($group$ == NULL) return TREXIO_FAILURE;
$group$->$group_num$ = num; $group$->$group_num$ = num;
$group$->$group_num$_isSet = true;
$group$->to_flush = 1; $group$->to_flush = 1;
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
@ -678,7 +705,7 @@ trexio_text_has_$group_num$ (trexio_t* const file)
$group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file); $group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE; if ($group$ == NULL) return TREXIO_FAILURE;
if ($group$->$group_num$ > 0L){ if ($group$->$group_num$_isSet == true){
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} else { } else {
return TREXIO_HAS_NOT; return TREXIO_HAS_NOT;

View File

@ -27,6 +27,14 @@ static int test_write_num (const char* file_name, const back_end_t backend) {
rc = trexio_write_nucleus_num(file, num); rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS); assert (rc == TREXIO_SUCCESS);
// attempt to write 0 as dimensioning variable in an empty file; should FAIL and return TREXIO_INVALID_ARG_2
rc = trexio_write_mo_num(file, 0);
assert (rc == TREXIO_INVALID_ARG_2);
// write numerical attribute ao_cartesian as 0
rc = trexio_write_ao_cartesian(file, 0);
assert (rc == TREXIO_SUCCESS);
// close current session // close current session
rc = trexio_close(file); rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS); assert (rc == TREXIO_SUCCESS);
@ -77,6 +85,7 @@ static int test_read_num (const char* file_name, const back_end_t backend) {
// parameters to be read // parameters to be read
int num; int num;
int cartesian;
/*================= START OF TEST ==================*/ /*================= START OF TEST ==================*/
@ -89,6 +98,15 @@ static int test_read_num (const char* file_name, const back_end_t backend) {
assert (rc == TREXIO_SUCCESS); assert (rc == TREXIO_SUCCESS);
assert (num == 12); assert (num == 12);
// read non-existing numerical attribute from the file
rc = trexio_read_mo_num(file, &num);
assert (rc == TREXIO_ATTR_MISSING);
// read ao_cartesian (zero) value from the file
rc = trexio_read_ao_cartesian(file, &cartesian);
assert (rc == TREXIO_SUCCESS);
assert (cartesian == 0);
// close current session // close current session
rc = trexio_close(file); rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS); assert (rc == TREXIO_SUCCESS);

View File

@ -27,6 +27,14 @@ static int test_write_num (const char* file_name, const back_end_t backend) {
rc = trexio_write_nucleus_num(file, num); rc = trexio_write_nucleus_num(file, num);
assert (rc == TREXIO_SUCCESS); assert (rc == TREXIO_SUCCESS);
// attempt to write 0 as dimensioning variable in an empty file; should FAIL and return TREXIO_INVALID_ARG_2
rc = trexio_write_mo_num(file, 0);
assert (rc == TREXIO_INVALID_ARG_2);
// write numerical attribute ao_cartesian as 0
rc = trexio_write_ao_cartesian(file, 0);
assert (rc == TREXIO_SUCCESS);
// close current session // close current session
rc = trexio_close(file); rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS); assert (rc == TREXIO_SUCCESS);
@ -77,6 +85,7 @@ static int test_read_num (const char* file_name, const back_end_t backend) {
// parameters to be read // parameters to be read
int num; int num;
int cartesian;
/*================= START OF TEST ==================*/ /*================= START OF TEST ==================*/
@ -89,6 +98,15 @@ static int test_read_num (const char* file_name, const back_end_t backend) {
assert (rc == TREXIO_SUCCESS); assert (rc == TREXIO_SUCCESS);
assert (num == 12); assert (num == 12);
// read non-existing numerical attribute from the file
rc = trexio_read_mo_num(file, &num);
assert (rc == TREXIO_ATTR_MISSING);
// read ao_cartesian (zero) value from the file
rc = trexio_read_ao_cartesian(file, &cartesian);
assert (rc == TREXIO_SUCCESS);
assert (cartesian == 0);
// close current session // close current session
rc = trexio_close(file); rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS); assert (rc == TREXIO_SUCCESS);