1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-31 16:45:59 +01:00

consistent naming init/deinit instead init/finalize in back ends

This commit is contained in:
q-posev 2021-04-08 11:31:51 +02:00
parent b8c9bfa2df
commit b656917323
3 changed files with 210 additions and 210 deletions

View File

@ -549,19 +549,19 @@ trexio_exit_code trexio_close(trexio_t* file) {
switch (file->back_end) {
case TREXIO_TEXT:
rc = trexio_text_finalize(file);
rc = trexio_text_deinit(file);
break;
case TREXIO_HDF5:
rc = trexio_hdf5_finalize(file);
rc = trexio_hdf5_deinit(file);
break;
/*
case TREXIO_JSON:
rc = trexio_json_finalize(file);
rc = trexio_json_deinit(file);
break;
,*/
default:
assert (1 == 0); /* Impossible case */
rc = TREXIO_FAILURE; /* Impossible case */
}
if (rc != TREXIO_SUCCESS) {

View File

@ -1,10 +1,10 @@
#+Title: Templator for HDF5 backend
** Constant file prefixes (not used by generator) for HDF5 :noxport:
** Constant file prefixes (not used by generator) for HDF5 :noxport:
#+NAME:header
#+begin_src c
#+begin_src c
/* This file was generated from the org-mode file.
To generate it, open templator_hdf5.org file in Emacs and execute
M-x org-babel-tangle
@ -31,21 +31,21 @@
#include "hdf5_hl.h" // needed for high-level APIs like H5LT, requires additional linking in Makefile
#+end_src
#+begin_src c :tangle prefix_hdf5.c :noweb yes
<<header>>
#include "trexio_hdf5.h"
#+end_src
** Template for HDF5 definitions
** Template for HDF5 definitions
#+begin_src c :tangle def_hdf5.c
#define $GROUP$_GROUP_NAME "$group$"
#define $GROUP_NUM$_NAME "$group_num$"
#define $GROUP$_$GROUP_DSET$_NAME "$group_dset$"
#+end_src
@ -61,12 +61,12 @@ typedef struct trexio_hdf5_s {
} trexio_hdf5_t;
trexio_exit_code trexio_hdf5_init(trexio_t* const file);
trexio_exit_code trexio_hdf5_finalize(trexio_t* const file);
trexio_exit_code trexio_hdf5_deinit(trexio_t* const file);
#+end_src
** Template for HDF5 init/deinit
** Template for HDF5 init/deinit
#+begin_src c :tangle basic_hdf5.c
@ -77,46 +77,46 @@ trexio_exit_code trexio_hdf5_init(trexio_t* const file) {
/* If file doesn't exist, create it */
int f_exists = 0;
struct stat st;
if (stat(file->file_name, &st) == 0) f_exists = 1;
if (f_exists == 1) {
switch (file->mode) {
case 'r':
case 'r':
// reading the existing file -> open as RDONLY
f->file_id = H5Fopen(file->file_name, H5F_ACC_RDONLY, H5P_DEFAULT);
break;
case 'a':
case 'a':
// appending the existing file -> open as RDWR
f->file_id = H5Fopen(file->file_name, H5F_ACC_RDWR, H5P_DEFAULT);
break;
case 'w':
case 'w':
// writing the existing file -> overwrite it (_TRUNC) [_EXCL | H5F_ACC_DEBUG as an alternative]
f->file_id = H5Fcreate(file->file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
break;
}
}
} else {
switch (file->mode) {
case 'r':
case 'a':
case 'r':
case 'a':
// reading or appending non-existing file -> error
return TREXIO_FAILURE;
case 'w':
case 'w':
// writing non-existing file -> create it
f->file_id = H5Fcreate(file->file_name, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
break;
}
}
}
}
/* Create or open groups in the hdf5 file assuming that they exist if file exists */
/* Create or open groups in the hdf5 file assuming that they exist if file exists */
switch (file->mode) {
// the switch for 'r'/'a' is reached only if file exists
case 'r':
case 'a':
case 'a':
f->$group$_group = H5Gopen(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT);
break;
case 'w':
@ -128,7 +128,7 @@ trexio_exit_code trexio_hdf5_init(trexio_t* const file) {
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_hdf5_finalize(trexio_t* const file) {
trexio_exit_code trexio_hdf5_deinit(trexio_t* const file) {
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
@ -152,12 +152,12 @@ trexio_exit_code trexio_hdf5_read_$group_num$ (trexio_t* const file, uint64_t* c
trexio_exit_code trexio_hdf5_write_$group_num$(trexio_t* const file, const uint64_t num);
#+end_src
#+begin_src c :tangle read_num_hdf5.c
trexio_exit_code trexio_hdf5_read_$group_num$ (trexio_t* const file, uint64_t* const num) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num == NULL) return TREXIO_INVALID_ARG_2;
const trexio_hdf5_t* f = (const trexio_hdf5_t*) file;
/* Quit if the dimensioning attribute is missing in the file */
if (H5Aexists(f->$group$_group, $GROUP_NUM$_NAME) == 0) return TREXIO_FAILURE;
@ -173,28 +173,28 @@ trexio_exit_code trexio_hdf5_read_$group_num$ (trexio_t* const file, uint64_t* c
}
#+end_src
#+begin_src c :tangle write_num_hdf5.c
trexio_exit_code trexio_hdf5_write_$group_num$ (trexio_t* const file, const uint64_t num) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num == 0L ) return TREXIO_INVALID_ARG_2;
trexio_hdf5_t* const f = (trexio_hdf5_t*) file;
if (H5Aexists(f->$group$_group, $GROUP_NUM$_NAME) == 0) {
/* Write the dimensioning variables */
const hid_t dtype = H5Tcopy(H5T_NATIVE_ULLONG);
const hid_t dspace = H5Screate(H5S_SCALAR);
const hid_t num_id = H5Acreate(f->$group$_group, $GROUP_NUM$_NAME, dtype, dspace,
H5P_DEFAULT, H5P_DEFAULT);
H5P_DEFAULT, H5P_DEFAULT);
if (num_id <= 0) {
H5Sclose(dspace);
H5Tclose(dtype);
return TREXIO_INVALID_ID;
}
const herr_t status = H5Awrite(num_id, dtype, &(num));
if (status < 0) {
H5Aclose(num_id);
@ -207,7 +207,7 @@ trexio_exit_code trexio_hdf5_write_$group_num$ (trexio_t* const file, const uint
H5Aclose(num_id);
H5Tclose(dtype);
return TREXIO_SUCCESS;
} else {
uint64_t infile_num;
@ -217,27 +217,27 @@ trexio_exit_code trexio_hdf5_write_$group_num$ (trexio_t* const file, const uint
if (infile_num != num) {
if (infile_num != 0) {
printf("%lu -> %lu %s \n", num, infile_num,
printf("%lu -> %lu %s \n", num, infile_num,
"This variable already exists. Overwriting it is not supported");
return TREXIO_FAILURE;
return TREXIO_FAILURE;
} else {
const hid_t dtype = H5Tcopy(H5T_NATIVE_ULLONG);
const hid_t num_id = H5Aopen(f->$group$_group, $GROUP_NUM$_NAME, H5P_DEFAULT);
if (num_id <= 0) return TREXIO_INVALID_ID;
const herr_t status = H5Awrite(num_id, dtype, &(num));
if (status < 0) return TREXIO_FAILURE;
H5Aclose(num_id);
H5Tclose(dtype);
const hid_t dtype = H5Tcopy(H5T_NATIVE_ULLONG);
const hid_t num_id = H5Aopen(f->$group$_group, $GROUP_NUM$_NAME, H5P_DEFAULT);
if (num_id <= 0) return TREXIO_INVALID_ID;
const herr_t status = H5Awrite(num_id, dtype, &(num));
if (status < 0) return TREXIO_FAILURE;
H5Aclose(num_id);
H5Tclose(dtype);
}
}
return TREXIO_SUCCESS;
}
}
#+end_src
@ -260,7 +260,7 @@ trexio_exit_code trexio_hdf5_has_$group_num$ (trexio_t* const file) {
#+end_src
** Template for HDF5 has/read/write a dataset
#+begin_src c :tangle hrw_dset_hdf5.h
trexio_exit_code trexio_hdf5_has_$group$_$group_dset$(trexio_t* const file);
trexio_exit_code trexio_hdf5_read_$group$_$group_dset$(trexio_t* const file, $group_dset_dtype$* const $group_dset$, const uint32_t rank, const uint64_t* dims);
@ -271,22 +271,22 @@ trexio_exit_code trexio_hdf5_write_$group$_$group_dset$(trexio_t* const file, co
trexio_exit_code trexio_hdf5_read_$group$_$group_dset$(trexio_t* const file, $group_dset_dtype$* const $group_dset$, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2;
const trexio_hdf5_t* f = (const trexio_hdf5_t*) file;
herr_t status;
int rrank;
// get the rank of the dataset in a file
status = H5LTget_dataset_ndims (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME,
&rrank);
status = H5LTget_dataset_ndims (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME,
&rrank);
if (status < 0) return TREXIO_FAILURE;
if (rrank != (int) rank) return TREXIO_INVALID_ARG_3;
// open the dataset to get its dimensions
// open the dataset to get its dimensions
hid_t dset_id = H5Dopen(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, H5P_DEFAULT);
if (dset_id <= 0) return TREXIO_INVALID_ID;
if (dset_id <= 0) return TREXIO_INVALID_ID;
// allocate space for the dimensions to be read
hsize_t* ddims = CALLOC( (int) rank, hsize_t);
@ -311,8 +311,8 @@ trexio_exit_code trexio_hdf5_read_$group$_$group_dset$(trexio_t* const file, $gr
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_$group_dset_h5_dtype$(f->$group$_group,
$GROUP$_$GROUP_DSET$_NAME,
$group_dset$);
$GROUP$_$GROUP_DSET$_NAME,
$group_dset$);
if (status < 0) return TREXIO_FAILURE;
return TREXIO_SUCCESS;
@ -324,36 +324,36 @@ trexio_exit_code trexio_hdf5_read_$group$_$group_dset$(trexio_t* const file, $gr
trexio_exit_code trexio_hdf5_write_$group$_$group_dset$(trexio_t* const file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2;
trexio_exit_code rc;
uint64_t $group_dset_dim$;
// error handling for rc is added by the generator
rc = trexio_hdf5_read_$group_dset_dim$(file, &($group_dset_dim$));
rc = trexio_hdf5_read_$group_dset_dim$(file, &($group_dset_dim$));
if ($group_dset_dim$ == 0L) return TREXIO_INVALID_NUM;
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
if ( H5LTfind_dataset(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME) != 1) {
if ( H5LTfind_dataset(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME) != 1) {
const herr_t status =
H5LTmake_dataset_$group_dset_h5_dtype$ (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME,
(int) rank, (const hsize_t*) dims, $group_dset$);
H5LTmake_dataset_$group_dset_h5_dtype$ (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME,
(int) rank, (const hsize_t*) dims, $group_dset$);
if (status < 0) return TREXIO_FAILURE;
} else {
} else {
hid_t dset_id = H5Dopen(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, H5P_DEFAULT);
if (dset_id <= 0) return TREXIO_INVALID_ID;
const herr_t status =
H5Dwrite(dset_id, H5T_NATIVE_$GROUP_DSET_H5_DTYPE$, H5S_ALL, H5S_ALL,
H5P_DEFAULT, $group_dset$);
H5P_DEFAULT, $group_dset$);
H5Dclose(dset_id);
if (status < 0) return TREXIO_FAILURE;
}
return TREXIO_SUCCESS;
}
#+end_src
@ -376,7 +376,7 @@ trexio_exit_code trexio_hdf5_has_$group$_$group_dset$(trexio_t* const file) {
}
#+end_src
** Constant file suffixes (not used by generator) for HDF5 :noxport:
** Constant file suffixes (not used by generator) for HDF5 :noxport:
#+begin_src c :tangle suffix_hdf5.h

View File

@ -17,7 +17,7 @@
#+NAME:header
#+begin_src c
#+begin_src c
/* This file was generated from the trexio.org org-mode file.
To generate it, open trexio.org in Emacs and execute
M-x org-babel-tangle
@ -46,7 +46,7 @@
#include <sys/stat.h>
#+end_src
#+begin_src c :tangle prefix_text.c :noweb yes
/* This file was generated from the trexio.org org-mode file.
To generate it, open trexio.org in Emacs and execute
@ -63,16 +63,16 @@
The "file" produced by the text back end is a directory with one
file per group.
When the file is open, it is locked by the current process. No other
process can read/write the same file. This guarantees that the
representation in memory is consistent with the file and avoid
re-reading the file before writing.
To lock the file, we lock the =.lock= file which is present in the
directory.
directory.
The file is written when closed, or when the flush function is called.
** Template for group-related structures in text back end
#+begin_src c :tangle struct_text_group_dset.h
@ -89,7 +89,7 @@ typedef struct $group$_s {
#+end_src
** Template for general structure in text back end
#+begin_src c :tangle struct_text_group.h
typedef struct rdm_s {
@ -115,7 +115,7 @@ typedef struct trexio_text_s {
#+begin_src c :tangle basic_text.h
trexio_exit_code trexio_text_init(trexio_t* const file);
#+end_src
#+begin_src c :tangle basic_text.c
trexio_exit_code trexio_text_init(trexio_t* const file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -127,16 +127,16 @@ trexio_exit_code trexio_text_init(trexio_t* const file) {
/* If directory doesn't exist, create it in write mode */
struct stat st;
if (stat(file->file_name, &st) == 0 && S_ISDIR(st.st_mode)) {
/* Do nothing */
} else {
if (file->mode == 'r') return TREXIO_READONLY;
if (mkdir(file->file_name, 0777) != 0) {
return TREXIO_FAILURE;
}
}
}
/* Create the lock file in the directory */
const char* lock_file_name = "/.lock";
@ -147,7 +147,7 @@ trexio_exit_code trexio_text_init(trexio_t* const file) {
if (file_name == NULL) {
return TREXIO_ALLOCATION_FAILED;
}
strncpy (file_name, file->file_name, str_size);
strncat (file_name, lock_file_name, strlen(lock_file_name));
@ -165,7 +165,7 @@ trexio_exit_code trexio_text_init(trexio_t* const file) {
#+begin_src c :tangle basic_text.h
trexio_exit_code trexio_text_lock(trexio_t* const file);
#+end_src
#+begin_src c :tangle basic_text.c
trexio_exit_code trexio_text_lock(trexio_t* const file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -173,13 +173,13 @@ trexio_exit_code trexio_text_lock(trexio_t* const file) {
trexio_text_t* const f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
int rc = fcntl(f->lock_file, F_SETLKW, &fl);
if (rc == -1) return TREXIO_FAILURE;
@ -187,15 +187,15 @@ trexio_exit_code trexio_text_lock(trexio_t* const file) {
}
#+end_src
#+begin_src c :tangle basic_text.h
trexio_exit_code trexio_text_finalize(trexio_t* const file);
trexio_exit_code trexio_text_deinit(trexio_t* const file);
#+end_src
#+begin_src c :tangle basic_text.h
trexio_exit_code trexio_text_unlock(trexio_t* const file);
#+end_src
#+begin_src c :tangle basic_text.c
trexio_exit_code trexio_text_unlock(trexio_t* const file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -204,23 +204,23 @@ trexio_exit_code trexio_text_unlock(trexio_t* const file) {
struct flock fl;
fl.l_type = F_UNLCK;
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid();
fcntl(f->lock_file, F_SETLK, &fl);
close(f->lock_file);
return TREXIO_SUCCESS;
}
#+end_src
** Init/deinit functions (templated part)
#+begin_src c :tangle basic_text_group.c
trexio_exit_code trexio_text_finalize(trexio_t* const file) {
trexio_exit_code trexio_text_deinit(trexio_t* const file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_exit_code rc;
@ -229,7 +229,7 @@ trexio_exit_code trexio_text_finalize(trexio_t* const file) {
rc = trexio_text_free_rdm( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
return TREXIO_SUCCESS;
}
#+end_src
@ -248,7 +248,7 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
if (file->$group$ != NULL) {
return file->$group$;
}
/* Allocate the data structure */
$group$_t* $group$ = MALLOC($group$_t);
if ($group$ == NULL) return NULL;
@ -271,7 +271,7 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
/* If the file exists, read it */
FILE* f = fopen(file_name,"r");
if (f != NULL) {
/* Find size of file to allocate the max size of the string buffer */
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
@ -285,7 +285,7 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
FREE($group$);
return NULL;
}
/* Read the dimensioning variables */
int rc;
// START REPEAT GROUP_DSET
@ -297,7 +297,7 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
FREE($group$);
return NULL;
}
rc = fscanf(f, "%u", &($group$->rank_$group_dset$));
if (rc != 1) {
FREE(buffer);
@ -317,21 +317,21 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
rc = fscanf(f, "%1023s %u", buffer, &j);
if ((rc != 2) || (strcmp(buffer, "dims_$group_dset$") != 0) || (j!=i)) {
FREE(buffer);
FREE(file_name);
fclose(f);
FREE($group$);
return NULL;
FREE(buffer);
FREE(file_name);
fclose(f);
FREE($group$);
return NULL;
}
rc = fscanf(f, "%lu\n", &($group$->dims_$group_dset$[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
fclose(f);
FREE($group$);
return NULL;
FREE(buffer);
FREE(file_name);
fclose(f);
FREE($group$);
return NULL;
}
size_$group_dset$ *= $group$->dims_$group_dset$[i];
@ -349,7 +349,7 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
FREE($group$);
return NULL;
}
rc = fscanf(f, "%lu", &($group$->$group_num$));
assert(!(rc != 1));
if (rc != 1) {
@ -360,8 +360,8 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
return NULL;
}
// END REPEAT GROUP_NUM
// START REPEAT GROUP_DSET
// START REPEAT GROUP_DSET
/* Allocate arrays */
$group$->$group_dset$ = CALLOC(size_$group_dset$, $group_dset_dtype$);
assert (!($group$->$group_dset$ == NULL));
@ -372,28 +372,28 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
FREE($group$);
return NULL;
}
rc = fscanf(f, "%1023s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "$group_dset$") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_dset$") != 0)) {
FREE(buffer);
FREE(file_name);
fclose(f);
FREE($group$->$group_dset$);
FREE($group$);
return NULL;
FREE(buffer);
FREE(file_name);
fclose(f);
FREE($group$->$group_dset$);
FREE($group$);
return NULL;
}
for (uint64_t i=0 ; i<size_$group_dset$ ; ++i) {
rc = fscanf(f, "%$group_dset_std_dtype_in$", &($group$->$group_dset$[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
fclose(f);
FREE($group$->$group_dset$);
FREE($group$);
return NULL;
FREE(buffer);
FREE(file_name);
fclose(f);
FREE($group$->$group_dset$);
FREE($group$);
return NULL;
}
}
// END REPEAT GROUP_DSET
@ -404,9 +404,9 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
}
if (file->parent.mode == 'w') {
$group$->file = fopen(file_name,"a");
} else {
$group$->file = fopen(file_name,"r");
$group$->file = fopen(file_name,"a");
} else {
$group$->file = fopen(file_name,"r");
}
FREE(file_name);
assert (!($group$->file == NULL));
@ -421,7 +421,7 @@ $group$_t* trexio_text_read_$group$(trexio_text_t* const file) {
return $group$;
}
#+end_src
** Template for text flush struct
#+begin_src c :tangle flush_group_text.h
@ -435,7 +435,7 @@ trexio_exit_code trexio_text_flush_$group$(trexio_text_t* const file) {
if (file->parent.mode == 'r') return TREXIO_READONLY;
$group$_t* $group$ = file->$group$;
if ($group$ == NULL) return TREXIO_SUCCESS;
if ($group$->to_flush == 0) return TREXIO_SUCCESS;
@ -456,7 +456,7 @@ trexio_exit_code trexio_text_flush_$group$(trexio_text_t* const file) {
size_$group_dset$ *= $group$->dims_$group_dset$[i];
}
// END REPEAT GROUP_DSET
// END REPEAT GROUP_DSET
// START REPEAT GROUP_NUM
fprintf(f, "$group_num$ %lu\n", $group$->$group_num$);
@ -480,15 +480,15 @@ trexio_exit_code trexio_text_flush_$group$(trexio_text_t* const file) {
** Template for text free memory
Memory is allocated when reading. The following function frees memory.
#+begin_src c :tangle free_group_text.h
trexio_exit_code trexio_text_free_$group$(trexio_text_t* const file);
#+end_src
#+begin_src c :tangle free_group_text.c
trexio_exit_code trexio_text_free_$group$(trexio_text_t* const file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (file->parent.mode != 'r') {
trexio_exit_code rc = trexio_text_flush_$group$(file);
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
@ -501,7 +501,7 @@ trexio_exit_code trexio_text_free_$group$(trexio_text_t* const file) {
fclose($group$->file);
$group$->file = NULL;
}
// START REPEAT GROUP_DSET
if ($group$->$group_dset$ != NULL) {
FREE ($group$->$group_dset$);
@ -536,17 +536,17 @@ trexio_exit_code trexio_text_read_$group_num$(trexio_t* const file, uint64_t* co
#+end_src
#+begin_src c :tangle write_num_text.c
trexio_exit_code trexio_text_write_$group_num$(trexio_t* const file, const uint64_t num) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (file->mode == 'r') return TREXIO_READONLY;
$group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
$group$->$group_num$ = num;
$group$->to_flush = 1;
return TREXIO_SUCCESS;
}
#+end_src
@ -571,7 +571,7 @@ trexio_exit_code trexio_text_has_$group_num$(trexio_t* const file) {
** Template for has/read/write the $group_dset$ dataset
The ~dset~ array is assumed allocated with the appropriate size.
#+begin_src c :tangle hrw_dset_text.h
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);
@ -588,7 +588,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file, $group_dset
if ($group$ == NULL) return TREXIO_FAILURE;
if (rank != $group$->rank_$group_dset$) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = 1;
for (unsigned int i=0; i<rank; ++i){
if (dims[i] != $group$->dims_$group_dset$[i]) return TREXIO_INVALID_ARG_4;
@ -612,13 +612,13 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, const $gro
$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$);
}
$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];
@ -630,7 +630,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, const $gro
for (uint64_t i=0 ; i<dim_size ; ++i) {
$group$->$group_dset$[i] = $group_dset$[i];
}
$group$->to_flush = 1;
return TREXIO_SUCCESS;
}
@ -657,7 +657,7 @@ trexio_exit_code trexio_text_has_$group_dset$(trexio_t* const file) {
#+begin_src c :tangle rdm_text.h
rdm_t* trexio_text_read_rdm(trexio_text_t* const file);
#+end_src
#+begin_src c :tangle rdm_text.c
rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
if (file == NULL) return NULL;
@ -685,56 +685,56 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
/* If the file exists, read it */
FILE* f = fopen(file_name,"r");
if (f != NULL) {
/* Find size of file to allocate the max size of the string buffer */
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET);
sz = (sz < 1024) ? (1024) : (sz);
char* buffer = CALLOC(sz, char);
/* Read the dimensioning variables */
int rc;
rc = fscanf(f, "%1023s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "dim_one_e") == 0);
rc = fscanf(f, "%lu", &(rdm->dim_one_e));
assert (rc == 1);
/* Allocate arrays */
rdm->one_e = CALLOC(rdm->dim_one_e, double);
assert (rdm->one_e != NULL);
/* Read one_e */
rc = fscanf(f, "%1023s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "one_e") == 0);
for (uint64_t i=0 ; i<rdm->dim_one_e; ++i) {
rc = fscanf(f, "%lf", &(rdm->one_e[i]));
assert (rc == 1);
}
/* Read two_e */
rc = fscanf(f, "%1023s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "two_e_file_name") == 0);
rc = fscanf(f, "%1023s", buffer);
assert (rc == 1);
str_size = strlen(buffer);
rdm->two_e_file_name = CALLOC(str_size,char);
strncpy(rdm->two_e_file_name, buffer, str_size);
FREE(buffer);
fclose(f);
f = NULL;
}
if (file->parent.mode == 'w') {
rdm->file = fopen(file_name,"a");
rdm->file = fopen(file_name,"a");
} else {
rdm->file = fopen(file_name,"r");
rdm->file = fopen(file_name,"r");
}
FREE(file_name);
file->rdm = rdm ;
@ -784,15 +784,15 @@ trexio_exit_code trexio_text_flush_rdm(trexio_text_t* const file) {
*** Free memory
Memory is allocated when reading. The followig function frees memory.
#+begin_src c :tangle rdm_text.h
trexio_exit_code trexio_text_free_rdm(trexio_text_t* const file);
#+end_src
#+begin_src c :tangle rdm_text.c
trexio_exit_code trexio_text_free_rdm(trexio_text_t* const file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (file->parent.mode != 'r') {
trexio_exit_code rc = trexio_text_flush_rdm(file);
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
@ -805,15 +805,15 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* const file) {
fclose(rdm->file);
rdm->file = NULL;
}
if (rdm->one_e != NULL) {
FREE (rdm->one_e);
}
if (rdm->two_e_file_name != NULL) {
FREE (rdm->two_e_file_name);
}
free (rdm);
file->rdm = NULL;
return TREXIO_SUCCESS;
@ -823,24 +823,24 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* const file) {
*** Read/Write the one_e attribute
The ~one_e~ array is assumed allocated with the appropriate size.
#+begin_src c :tangle rdm_text.h
trexio_exit_code
trexio_text_read_rdm_one_e(trexio_t* const file,
double* const one_e,
const uint64_t dim_one_e);
double* const one_e,
const uint64_t dim_one_e);
trexio_exit_code
trexio_text_write_rdm_one_e(trexio_t* const file,
const double* one_e,
const uint64_t dim_one_e);
const double* one_e,
const uint64_t dim_one_e);
#+end_src
#+begin_src c :tangle rdm_text.c
trexio_exit_code
trexio_text_read_rdm_one_e(trexio_t* const file,
double* const one_e,
const uint64_t dim_one_e)
double* const one_e,
const uint64_t dim_one_e)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (one_e == NULL) return TREXIO_INVALID_ARG_2;
@ -857,11 +857,11 @@ trexio_text_read_rdm_one_e(trexio_t* const file,
return TREXIO_SUCCESS;
}
trexio_exit_code
trexio_text_write_rdm_one_e(trexio_t* const file,
const double* one_e,
const uint64_t dim_one_e)
const double* one_e,
const uint64_t dim_one_e)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (one_e == NULL) return TREXIO_INVALID_ARG_2;
@ -869,12 +869,12 @@ trexio_text_write_rdm_one_e(trexio_t* const file,
rdm_t* const rdm = trexio_text_read_rdm((trexio_text_t*) file);
if (rdm == NULL) return TREXIO_FAILURE;
rdm->dim_one_e = dim_one_e;
for (uint64_t i=0 ; i<dim_one_e ; ++i) {
rdm->one_e[i] = one_e[i];
}
rdm->to_flush = 1;
return TREXIO_SUCCESS;
}
@ -887,30 +887,30 @@ trexio_text_write_rdm_one_e(trexio_t* const file,
chunks.
In the text back end, the easiest way to do it is to create a
file for each sparse float structure.
#+begin_src c :tangle rdm_text.h
trexio_exit_code
trexio_text_buffered_read_rdm_two_e(trexio_t* const file,
const uint64_t offset,
const uint64_t size,
int64_t* const index,
double* const value);
const uint64_t offset,
const uint64_t size,
int64_t* const index,
double* const value);
trexio_exit_code
trexio_text_buffered_write_rdm_two_e(trexio_t* const file,
const uint64_t offset,
const uint64_t size,
const int64_t* index,
const double* value);
const uint64_t offset,
const uint64_t size,
const int64_t* index,
const double* value);
#+end_src
#+begin_src c :tangle rdm_text.c
trexio_exit_code
trexio_text_buffered_read_rdm_two_e(trexio_t* const file,
const uint64_t offset,
const uint64_t size,
int64_t* const index,
double* const value)
const uint64_t offset,
const uint64_t size,
int64_t* const index,
double* const value)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (index == NULL) return TREXIO_INVALID_ARG_4;
@ -924,14 +924,14 @@ trexio_text_buffered_read_rdm_two_e(trexio_t* const file,
const uint64_t line_length = 64L;
fseek(f, (long) offset * line_length, SEEK_SET);
for (uint64_t i=0 ; i<size ; ++i) {
int rc = fscanf(f, "%9ld %9ld %9ld %9ld %24le\n",
&index[4*i],
&index[4*i+1],
&index[4*i+2],
&index[4*i+3],
&value[i]);
&index[4*i],
&index[4*i+1],
&index[4*i+2],
&index[4*i+3],
&value[i]);
if (rc == 5) {
/* Do nothing */
} else if (rc == EOF) {
@ -942,13 +942,13 @@ trexio_text_buffered_read_rdm_two_e(trexio_t* const file,
return TREXIO_SUCCESS;
}
trexio_exit_code
trexio_text_buffered_write_rdm_two_e(trexio_t* const file,
const uint64_t offset,
const uint64_t size,
const int64_t* index,
const double* value)
const uint64_t offset,
const uint64_t size,
const int64_t* index,
const double* value)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (index == NULL) return TREXIO_INVALID_ARG_4;
@ -963,14 +963,14 @@ trexio_text_buffered_write_rdm_two_e(trexio_t* const file,
const uint64_t line_length = 64L;
fseek(f, (long) offset * line_length, SEEK_SET);
for (uint64_t i=0 ; i<size ; ++i) {
int rc = fprintf(f, "%9ld %9ld %9ld %9ld %24le\n",
index[4*i],
index[4*i+1],
index[4*i+2],
index[4*i+3],
value[i]);
index[4*i],
index[4*i+1],
index[4*i+2],
index[4*i+3],
value[i]);
if (rc != 5) return TREXIO_FAILURE;
}
@ -978,7 +978,7 @@ trexio_text_buffered_write_rdm_two_e(trexio_t* const file,
}
#+end_src
:noexport:
:noexport:
* Constant file suffixes :noxport: