1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2025-01-03 18:16:22 +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) { switch (file->back_end) {
case TREXIO_TEXT: case TREXIO_TEXT:
rc = trexio_text_finalize(file); rc = trexio_text_deinit(file);
break; break;
case TREXIO_HDF5: case TREXIO_HDF5:
rc = trexio_hdf5_finalize(file); rc = trexio_hdf5_deinit(file);
break; break;
/* /*
case TREXIO_JSON: case TREXIO_JSON:
rc = trexio_json_finalize(file); rc = trexio_json_deinit(file);
break; break;
,*/ ,*/
default: default:
assert (1 == 0); /* Impossible case */ rc = TREXIO_FAILURE; /* Impossible case */
} }
if (rc != TREXIO_SUCCESS) { if (rc != TREXIO_SUCCESS) {

View File

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

View File

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