1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-03 20:54:07 +01:00

enable operation on string attributes [text]

This commit is contained in:
q-posev 2021-06-11 18:15:30 +02:00
parent a40804be2e
commit 535d7ed019
3 changed files with 153 additions and 8 deletions

View File

@ -1741,7 +1741,7 @@ trexio_read_$group_str$ (trexio_t* const file, char* const str, const uint32_t m
switch (file->back_end) {
case TREXIO_TEXT:
//rc = trexio_text_read_$group_str$(file, str);
rc = trexio_text_read_$group_str$(file, str);
break;
case TREXIO_HDF5:
@ -1783,7 +1783,7 @@ trexio_write_$group_str$ (trexio_t* const file, const char* str, const uint32_t
switch (file->back_end) {
case TREXIO_TEXT:
//return trexio_text_write_$group_str$(file, str);
return trexio_text_write_$group_str$(file, str);
break;
case TREXIO_HDF5:
@ -1812,7 +1812,7 @@ trexio_has_$group_str$ (trexio_t* const file)
switch (file->back_end) {
case TREXIO_TEXT:
//return trexio_text_has_$group_str$(file);
return trexio_text_has_$group_str$(file);
break;
case TREXIO_HDF5:

View File

@ -20,15 +20,19 @@ cat populated/pop_flush_group_text.h >> trexio_text.h
cat populated/pop_has_dset_data_text.c >> trexio_text.c
cat populated/pop_has_dset_str_text.c >> trexio_text.c
cat populated/pop_has_num_text.c >> trexio_text.c
cat populated/pop_has_attr_str_text.c >> trexio_text.c
cat populated/pop_read_dset_data_text.c >> trexio_text.c
cat populated/pop_read_dset_str_text.c >> trexio_text.c
cat populated/pop_read_attr_str_text.c >> trexio_text.c
cat populated/pop_read_num_text.c >> trexio_text.c
cat populated/pop_write_dset_data_text.c >> trexio_text.c
cat populated/pop_write_dset_str_text.c >> trexio_text.c
cat populated/pop_write_attr_str_text.c >> trexio_text.c
cat populated/pop_write_num_text.c >> trexio_text.c
cat populated/pop_hrw_num_text.h >> trexio_text.h
cat populated/pop_hrw_dset_data_text.h >> trexio_text.h
cat populated/pop_hrw_dset_str_text.h >> trexio_text.h
cat populated/pop_hrw_attr_str_text.h >> trexio_text.h
cat rdm_text.c >> trexio_text.c
cat rdm_text.h >> trexio_text.h

View File

@ -83,6 +83,8 @@ typedef struct $group$_s {
uint32_t rank_$group_dset$;
uint32_t to_flush;
uint64_t dims_$group_dset$[16];
uint64_t len_$group_str$;
char* $group_str$;
char file_name[TREXIO_MAX_FILENAME_LENGTH];
} $group$_t;
#+end_src
@ -365,6 +367,60 @@ trexio_text_read_$group$ (trexio_text_t* const file)
}
// END REPEAT GROUP_NUM
// START REPEAT GROUP_ATTR_STR
rc = fscanf(f, "%1023s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "len_$group_str$") != 0)));
if ((rc != 1) || (strcmp(buffer, "len_$group_str$") != 0)) {
FREE(buffer);
fclose(f);
FREE($group$);
return NULL;
}
rc = fscanf(f, "%" SCNu64 "", &($group$->len_$group_str$));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
FREE($group$);
return NULL;
}
rc = fscanf(f, "%1023s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "$group_str$") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_str$") != 0)) {
FREE(buffer);
fclose(f);
FREE($group$->$group_str$);
FREE($group$);
return NULL;
}
if ($group$->len_$group_str$ != 0) {
$group$->$group_str$ = CALLOC($group$->len_$group_str$, char);
assert (!($group$->$group_str$ == NULL));
if ($group$->$group_str$ == NULL) {
FREE(buffer);
fclose(f);
FREE($group$->$group_str$);
FREE($group$);
return NULL;
}
rc = fscanf(f, " %1023[^\n]", $group$->$group_str$);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
FREE($group$->$group_str$);
FREE($group$);
return NULL;
}
}
// END REPEAT GROUP_ATTR_STR
// START REPEAT GROUP_DSET_NUM
/* Allocate arrays */
$group$->$group_dset$ = CALLOC(size_$group_dset$, $group_dset_dtype$);
@ -498,13 +554,18 @@ trexio_text_flush_$group$ (trexio_text_t* const file)
fprintf(f, "dims_$group_dset$ %u %" PRIu64 "\n", i, $group$->dims_$group_dset$[i]);
size_$group_dset$ *= $group$->dims_$group_dset$[i];
}
// END REPEAT GROUP_DSET_ALL
// START REPEAT GROUP_NUM
fprintf(f, "$group_num$ %" PRIu64 "\n", $group$->$group_num$);
// END REPEAT GROUP_NUM
// START REPEAT GROUP_ATTR_STR
fprintf(f, "len_$group_str$ %" PRIu64 "\n", $group$->len_$group_str$);
fprintf(f, "$group_str$\n");
if ($group$->len_$group_str$ != 0) fprintf(f, "%s\n", $group$->$group_str$);
// END REPEAT GROUP_ATTR_STR
/* Write arrays */
// START REPEAT GROUP_DSET_ALL
@ -545,9 +606,7 @@ trexio_text_free_$group$ (trexio_text_t* const file)
if ($group$ == NULL) return TREXIO_SUCCESS;
// START REPEAT GROUP_DSET_NUM
if ($group$->$group_dset$ != NULL) {
FREE ($group$->$group_dset$);
}
if ($group$->$group_dset$ != NULL) FREE ($group$->$group_dset$);
// END REPEAT GROUP_DSET_NUM
// START REPEAT GROUP_DSET_STR
@ -557,6 +616,10 @@ trexio_text_free_$group$ (trexio_text_t* const file)
}
// END REPEAT GROUP_DSET_STR
// START REPEAT GROUP_ATTR_STR
if ($group$->$group_str$ != NULL) FREE ($group$->$group_str$);
// END REPEAT GROUP_ATTR_STR
FREE ($group$);
return TREXIO_SUCCESS;
@ -773,7 +836,7 @@ trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const u
if ($group$ == NULL) return TREXIO_FAILURE;
if ($group$->$group_dset$ != NULL) {
FREE($group$->$group_dset$[0]);
if ($group$->rank_$group_dset$ != 0) FREE($group$->$group_dset$[0]);
FREE($group$->$group_dset$);
}
@ -784,8 +847,11 @@ trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const u
}
$group$->$group_dset$ = CALLOC(dims[0], char*);
if ($group$->$group_dset$ == NULL) return TREXIO_ALLOCATION_FAILED;
char* tmp_str = CALLOC(dims[0]*32 + 1, char);
if (tmp_str == NULL) return TREXIO_ALLOCATION_FAILED;
size_t tmp_len = 0;
for (uint64_t i=0 ; i<dims[0] ; ++i) {
tmp_len = strlen(dset[i]);
@ -817,6 +883,81 @@ trexio_text_has_$group_dset$ (trexio_t* const file)
return TREXIO_HAS_NOT;
}
}
#+end_src
** Template for has/read/write the string attribute
#+begin_src c :tangle hrw_attr_str_text.h :exports none
trexio_exit_code trexio_text_has_$group_str$ (trexio_t* const file);
trexio_exit_code trexio_text_read_$group_str$ (trexio_t* const file, char* const str);
trexio_exit_code trexio_text_write_$group_str$ (trexio_t* const file, const char* str);
#+end_src
#+begin_src c :tangle read_attr_str_text.c
trexio_exit_code
trexio_text_read_$group_str$ (trexio_t* const file, char* const str)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (str == NULL) return TREXIO_INVALID_ARG_2;
$group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
strcpy(str, $group$->$group_str$);
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle write_attr_str_text.c
trexio_exit_code
trexio_text_write_$group_str$ (trexio_t* const file, const char *str)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (str == NULL) return TREXIO_INVALID_ARG_2;
if (file->mode == 'r') return TREXIO_READONLY;
$group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
if ($group$->$group_str$ != NULL) FREE($group$->$group_str$);
size_t tmp_len = strlen(str);
$group$->$group_str$ = CALLOC(tmp_len + 1, char);
if ($group$->$group_str$ == NULL) return TREXIO_ALLOCATION_FAILED;
$group$->len_$group_str$ = tmp_len + 1;
strncpy($group$->$group_str$, str, tmp_len + 1);
$group$->to_flush = 1;
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle has_attr_str_text.c
trexio_exit_code
trexio_text_has_$group_str$ (trexio_t* const file)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
$group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
if ($group$->len_$group_str$ > 0){
return TREXIO_SUCCESS;
} else {
return TREXIO_HAS_NOT;
}
}
#+end_src
** RDM struct (hard-coded)