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

[WIP] working write dset of strings, but with memory leak [text]

This commit is contained in:
q-posev 2021-06-09 18:10:59 +02:00
parent e8732c3611
commit 5abac3fec2
6 changed files with 94 additions and 19 deletions

View File

@ -1549,7 +1549,7 @@ trexio_read_$group_dset$ (trexio_t* const file, char* dset, const uint32_t max_s
switch (file->back_end) {
case TREXIO_TEXT:
//return trexio_text_read_$group_dset$(file, dset_str, rank, dims);
//return trexio_text_read_$group_dset$(file, dset, rank, dims);
break;
case TREXIO_HDF5:
@ -1623,7 +1623,7 @@ trexio_write_$group_dset$ (trexio_t* const file, const char* dset, const uint32_
switch (file->back_end) {
case TREXIO_TEXT:
//rc = trexio_text_write_$group_dset$(file, dset, rank, dims);
rc = trexio_text_write_$group_dset$(file, (const char**) dset_str, rank, dims);
break;
case TREXIO_HDF5:
@ -1656,7 +1656,7 @@ trexio_has_$group_dset$ (trexio_t* const file)
switch (file->back_end) {
case TREXIO_TEXT:
//return trexio_text_has_$group_dset$(file);
return trexio_text_has_$group_dset$(file);
break;
case TREXIO_HDF5:

View File

@ -18,17 +18,17 @@ cat populated/pop_read_group_text.h >> trexio_text.h
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_dset_str_text.c >> trexio_text.c
cat populated/pop_has_num_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_dset_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_dset_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_dset_str_text.h >> trexio_text.h
cat rdm_text.c >> trexio_text.c
cat rdm_text.h >> trexio_text.h

View File

@ -669,16 +669,35 @@ trexio_text_has_$group_dset$ (trexio_t* const file)
#+begin_src c :tangle hrw_dset_str_text.h :exports none
trexio_exit_code trexio_text_has_$group_dset$ (trexio_t* const file);
trexio_exit_code trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const uint32_t rank, const uint64_t* dims);
#+end_src
#+begin_src c :tangle read_dset_str_text.c
trexio_exit_code
trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$,
const uint32_t rank, const uint64_t* dims)
trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t rank, const uint64_t* dims)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset == NULL) return TREXIO_INVALID_ARG_2;
$group$_t* const $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
if (rank != $group$->rank_$group_dset$) return TREXIO_INVALID_ARG_3;
for (unsigned int i=0 ; i<rank ; ++i) {
if (dims[i] != $group$->dims_$group_dset$[i]) return TREXIO_INVALID_ARG_4;
}
/* REDO ! */
strcpy(dset, "");
for (uint64_t i=0 ; i<dims[0] ; ++i) {
strcat(dset, $group$->$group_dset$[i]);
strcat(dset, TREXIO_DELIM);
}
/* REDO ! */
return TREXIO_SUCCESS;
}
@ -686,10 +705,46 @@ trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $
#+begin_src c :tangle write_dset_str_text.c
trexio_exit_code
trexio_text_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group_dset$,
const uint32_t rank, const uint64_t* dims)
trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const uint32_t rank, const uint64_t* dims)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (dset == 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_dset$ != NULL) {
FREE($group$->$group_dset$[0]);
FREE($group$->$group_dset$);
}
$group$->rank_$group_dset$ = rank;
uint64_t dim_size = 1;
for (unsigned int i=0; i<$group$->rank_$group_dset$; ++i){
$group$->dims_$group_dset$[i] = dims[i];
dim_size *= dims[i];
}
/* REDO ! */
$group$->$group_dset$ = CALLOC(dims[0], char*);
char* tmp_str = CALLOC(dims[0]*32 + 1, char);
size_t tmp_len = 0;
for (uint64_t i=0 ; i<dims[0] ; ++i) {
tmp_len = strlen(dset[i]);
$group$->$group_dset$[i] = tmp_str;
strncpy(tmp_str, dset[i], tmp_len);
tmp_str += tmp_len + 1;
printf("%s\n", $group$->$group_dset$[i]);
}
/* REDO ! */
$group$->to_flush = 1;
return TREXIO_SUCCESS;
}
@ -700,7 +755,16 @@ trexio_exit_code
trexio_text_has_$group_dset$ (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$->rank_$group_dset$ > 0){
return TREXIO_SUCCESS;
} else {
return TREXIO_HAS_NOT;
}
}
#+end_src

View File

@ -23,7 +23,7 @@ int main() {
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;
@ -93,11 +93,21 @@ 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);
if (backend == TREXIO_HDF5) rc = trexio_write_nucleus_label(file,labelxxx, 32);
rc = trexio_write_nucleus_label(file,labelxxx, 32);
assert (rc == TREXIO_SUCCESS);
if (backend == TREXIO_HDF5) rc = trexio_write_nucleus_point_group(file, sym, 32);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
assert (file != NULL);
// check if the written data exists in the file
rc = trexio_has_nucleus_num(file);
assert (rc == TREXIO_SUCCESS);
@ -133,6 +143,7 @@ int test_write(const char* file_name, const back_end_t backend) {
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
*/
/*================= END OF TEST ==================*/

View File

@ -63,6 +63,6 @@ for fname in files_todo['dset_str']:
# populate group-related functions with mixed (iterative+recursive) scheme [text backend]
for fname in files_todo['group']:
special_populate_text_group(fname, template_paths, group_dict, detailed_dsets_nostr, detailed_nums)
special_populate_text_group(fname, template_paths, group_dict, detailed_dsets, detailed_nums)
# --------------------------------------------------------------------------- #

View File

@ -43,7 +43,7 @@ def get_files_todo(source_files: dict) -> dict:
files_todo[key] = list(filter(lambda x: key in x, files_todo['all']))
files_todo['group'].append('struct_text_group_dset.h')
# files that correspond to todo1 group (e.g. only iterative population within the function body)
# files that correspond to iterative population (e.g. the code is repeated within the function body but the function itself is unique)
files_todo['auxiliary'] = ['def_hdf5.c', 'basic_hdf5.c', 'basic_text_group.c', 'struct_hdf5.h', 'struct_text_group.h']
return files_todo