mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-11-03 20:54:07 +01:00
Implement trexio_has_group templates
This commit is contained in:
parent
a5632ab7a8
commit
4ed3e0c6d0
@ -1755,6 +1755,98 @@ trexio_pre_close (trexio_t* file)
|
||||
considered dimensioning variables and cannot be negative or 0. An attempt to write negative or 0
|
||||
value will result in ~TREXIO_INVALID_ARG_2~ exit code.
|
||||
|
||||
** Templates for front end has_group functions
|
||||
*** Introduction
|
||||
|
||||
This section concerns API calls related to TREXIO groups
|
||||
|
||||
| Function name | Description |
|
||||
|----------------------+-----------------------------------|
|
||||
| ~trexio_has_$group$~ | Check if a group exists in a file |
|
||||
|
||||
*** C templates for front end
|
||||
|
||||
The ~C~ templates that correspond to each of the abovementioned
|
||||
functions can be found below. First parameter is the ~TREXIO~ file
|
||||
handle.
|
||||
|
||||
**** Function declarations
|
||||
|
||||
#+begin_src c :tangle hrw_group_front.h :exports none
|
||||
trexio_exit_code trexio_has_$group$(trexio_t* const file);
|
||||
#+end_src
|
||||
|
||||
**** Source code
|
||||
|
||||
#+begin_src c :tangle has_group_front.c
|
||||
trexio_exit_code
|
||||
trexio_has_$group$ (trexio_t* const file)
|
||||
{
|
||||
|
||||
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
||||
|
||||
assert(file->back_end < TREXIO_INVALID_BACK_END);
|
||||
|
||||
switch (file->back_end) {
|
||||
|
||||
case TREXIO_TEXT:
|
||||
return trexio_text_has_$group$(file);
|
||||
|
||||
case TREXIO_HDF5:
|
||||
#ifdef HAVE_HDF5
|
||||
return trexio_hdf5_has_$group$(file);
|
||||
#else
|
||||
return TREXIO_BACK_END_MISSING;
|
||||
#endif
|
||||
/*
|
||||
case TREXIO_JSON:
|
||||
return trexio_json_has_$group$(file);
|
||||
break;
|
||||
,*/
|
||||
}
|
||||
|
||||
return TREXIO_FAILURE;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
*** Fortran templates for front end
|
||||
|
||||
The ~Fortran~ templates that provide an access to the ~C~ API calls from Fortran.
|
||||
These templates are based on the use of ~iso_c_binding~. Pointers have to be passed by value.
|
||||
|
||||
#+begin_src f90 :tangle has_group_front_fortran.f90
|
||||
interface
|
||||
integer(trexio_exit_code) function trexio_has_$group$ (trex_file) bind(C)
|
||||
use, intrinsic :: iso_c_binding
|
||||
import
|
||||
integer(c_int64_t), intent(in), value :: trex_file
|
||||
end function trexio_has_$group$
|
||||
end interface
|
||||
#+end_src
|
||||
|
||||
*** Python templates for front end
|
||||
|
||||
#+begin_src python :tangle has_group_front.py
|
||||
def has_$group$(trexio_file) -> bool:
|
||||
"""Check that $group$ group exists in the TREXIO file.
|
||||
|
||||
Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.
|
||||
|
||||
Returns:
|
||||
True if the variable exists, False otherwise
|
||||
|
||||
Raises:
|
||||
- trexio.Error if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.
|
||||
- Exception from some other error (e.g. RuntimeError).
|
||||
"""
|
||||
|
||||
rc = pytr.trexio_has_$group$(trexio_file.pytrexio_s)
|
||||
if rc == TREXIO_FAILURE:
|
||||
raise Error(rc)
|
||||
|
||||
return rc == TREXIO_SUCCESS
|
||||
#+end_src
|
||||
|
||||
** Templates for front end has/read/write a single numerical attribute
|
||||
*** Introduction
|
||||
|
||||
@ -3924,7 +4016,7 @@ def read_$group_dset$(trexio_file, dim = None) -> list:
|
||||
"""
|
||||
|
||||
$group_dset_dim$ = read_$group_dset_dim$(trexio_file)
|
||||
|
||||
|
||||
dims_list = [$group_dset_dim_list$]
|
||||
dim_real = 1
|
||||
for i in range($group_dset_rank$):
|
||||
|
@ -179,6 +179,38 @@ trexio_hdf5_deinit (trexio_t* const file)
|
||||
}
|
||||
#+end_src
|
||||
|
||||
* Template for HDF5 has a group
|
||||
|
||||
#+begin_src c :tangle hrw_group_hdf5.h :exports none
|
||||
trexio_exit_code trexio_hdf5_has_$group$ (trexio_t* const file);
|
||||
#+end_src
|
||||
|
||||
|
||||
#+begin_src c :tangle has_group_hdf5.c
|
||||
trexio_exit_code
|
||||
trexio_hdf5_has_$group$ (trexio_t* const file)
|
||||
{
|
||||
|
||||
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
||||
|
||||
const trexio_hdf5_t* f = (const trexio_hdf5_t*) file;
|
||||
|
||||
struct H5G_info_t group_info;
|
||||
|
||||
/* H5Gget_info return info about the HDF5 group as a group_info struct */
|
||||
herr_t status = H5Gget_info(f->$group$_group, &group_info);
|
||||
if (status < 0) return TREXIO_FAILURE;
|
||||
|
||||
/* If nlinks==0 --> the group is empty, i.e. non-existent */
|
||||
if (group_info.nlinks == (hsize_t) 0) {
|
||||
return TREXIO_HAS_NOT;
|
||||
} else {
|
||||
return TREXIO_SUCCESS;
|
||||
}
|
||||
|
||||
}
|
||||
#+end_src
|
||||
|
||||
* Template for HDF5 has/read/write a numerical attribute
|
||||
|
||||
#+begin_src c :tangle hrw_attr_num_hdf5.h :exports none
|
||||
|
@ -112,6 +112,22 @@ trexio_exit_code trexio_text_inquire(const char* file_name);
|
||||
trexio_exit_code trexio_text_deinit(trexio_t* const file);
|
||||
trexio_exit_code trexio_text_lock(trexio_t* const file);
|
||||
trexio_exit_code trexio_text_unlock(trexio_t* const file);
|
||||
bool trexio_text_file_exists(const char* file_name);
|
||||
#+end_src
|
||||
|
||||
#+begin_src c :tangle basic_text.c
|
||||
bool
|
||||
trexio_text_file_exists (const char* file_name)
|
||||
{
|
||||
/* Check if the file with "file_name" exists */
|
||||
struct stat st;
|
||||
|
||||
int rc = stat(file_name, &st);
|
||||
|
||||
bool file_exists = rc == 0;
|
||||
|
||||
return file_exists;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+begin_src c :tangle basic_text.c
|
||||
@ -493,6 +509,47 @@ trexio_text_read_$group$ (trexio_text_t* const file)
|
||||
}
|
||||
#+end_src
|
||||
|
||||
* Template for text has a group
|
||||
|
||||
#+begin_src c :tangle hrw_group_text.h :exports none
|
||||
trexio_exit_code trexio_text_has_$group$(trexio_t* const file);
|
||||
#+end_src
|
||||
|
||||
#+begin_src c :tangle has_group_text.c
|
||||
trexio_exit_code
|
||||
trexio_text_has_$group$ (trexio_t* const file)
|
||||
{
|
||||
|
||||
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
||||
|
||||
/* Flush the group to make sure the group.txt file is created */
|
||||
if (file->mode != 'r') {
|
||||
trexio_exit_code rc = trexio_text_flush_$group$((trexio_text_t*) file);
|
||||
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
|
||||
}
|
||||
|
||||
/* Build the file name */
|
||||
char $group$_full_path[TREXIO_MAX_FILENAME_LENGTH];
|
||||
|
||||
const char* $group$_file_name = "/$group$.txt";
|
||||
|
||||
strncpy ($group$_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||
strncat ($group$_full_path, $group$_file_name,
|
||||
TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name));
|
||||
|
||||
if ($group$_full_path[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') return TREXIO_FAILURE;
|
||||
|
||||
bool file_exists;
|
||||
file_exists = trexio_text_file_exists($group$_full_path);
|
||||
|
||||
if (file_exists) {
|
||||
return TREXIO_SUCCESS;
|
||||
} else {
|
||||
return TREXIO_HAS_NOT;
|
||||
}
|
||||
}
|
||||
#+end_src
|
||||
|
||||
* Template for text flush a group
|
||||
|
||||
#+begin_src c :tangle flush_group_text.h :exports none
|
||||
@ -1063,6 +1120,23 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file,
|
||||
rc = fclose(f_wSize);
|
||||
if (rc != 0) return TREXIO_FILE_ERROR;
|
||||
|
||||
const char $group$_file_name[256] = "/$group$.txt";
|
||||
|
||||
memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH);
|
||||
/* Copy directory name in file_full_path */
|
||||
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||
/* Append name of the file with sparse data */
|
||||
strncat (file_full_path, $group$_file_name,
|
||||
TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name));
|
||||
|
||||
bool file_exists = trexio_text_file_exists(file_full_path);
|
||||
|
||||
/* Create an empty file for the trexio_text_has_group to work */
|
||||
if (!file_exists) {
|
||||
FILE *fp = fopen(file_full_path, "ab+");
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/* Exit upon success */
|
||||
return TREXIO_SUCCESS;
|
||||
}
|
||||
@ -1533,6 +1607,24 @@ trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file,
|
||||
rc = fclose(f);
|
||||
if (rc != 0) return TREXIO_FILE_ERROR;
|
||||
|
||||
/* Additional part for the trexio_text_has_group to work */
|
||||
const char det_file_name[256] = "/determinant.txt";
|
||||
|
||||
memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH);
|
||||
/* Copy directory name in file_full_path */
|
||||
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||
/* Append name of the file with sparse data */
|
||||
strncat (file_full_path, det_file_name,
|
||||
TREXIO_MAX_FILENAME_LENGTH-strlen(det_file_name));
|
||||
|
||||
bool file_exists = trexio_text_file_exists(file_full_path);
|
||||
|
||||
/* Create an empty file for the trexio_text_has_group to work */
|
||||
if (!file_exists) {
|
||||
FILE *fp = fopen(file_full_path, "ab+");
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/* Exit upon success */
|
||||
return TREXIO_SUCCESS;
|
||||
}
|
||||
@ -1602,6 +1694,24 @@ trexio_exit_code trexio_text_write_determinant_coefficient(trexio_t* const file,
|
||||
rc = fclose(f_wSize);
|
||||
if (rc != 0) return TREXIO_FILE_ERROR;
|
||||
|
||||
/* Additional part for the trexio_text_has_group to work */
|
||||
const char det_file_name[256] = "/determinant.txt";
|
||||
|
||||
memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH);
|
||||
/* Copy directory name in file_full_path */
|
||||
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||
/* Append name of the file with sparse data */
|
||||
strncat (file_full_path, det_file_name,
|
||||
TREXIO_MAX_FILENAME_LENGTH-strlen(det_file_name));
|
||||
|
||||
bool file_exists = trexio_text_file_exists(file_full_path);
|
||||
|
||||
/* Create an empty file for the trexio_text_has_group to work */
|
||||
if (!file_exists) {
|
||||
FILE *fp = fopen(file_full_path, "ab+");
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/* Exit upon success */
|
||||
return TREXIO_SUCCESS;
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ for fname in files_todo['dset_sparse']:
|
||||
# populate group-related functions with mixed scheme
|
||||
for fname in files_todo['group']:
|
||||
# recursive scheme for delete_group functions
|
||||
if 'delete' in fname:
|
||||
if 'delete' in fname or 'has' in fname:
|
||||
recursive_populate_file(fname, template_paths, group_dict)
|
||||
# mixed (iterative+recursive) scheme [text backend]
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user