1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-10-02 14:31:05 +02:00

cppcheck fixes

This commit is contained in:
Anthony Scemama 2022-01-07 11:50:55 +01:00
parent 98e303ffe2
commit 64081e46ad
3 changed files with 105 additions and 75 deletions

View File

@ -775,7 +775,7 @@ trexio_t* trexio_open(const char* file_name, const char mode,
#+begin_src c :tangle prefix_front.c #+begin_src c :tangle prefix_front.c
trexio_t* trexio_t*
trexio_open(const char* file_name, const char mode, trexio_open(const char* file_name, const char mode,
const back_end_t back_end, trexio_exit_code* const rc_open) const back_end_t back_end, trexio_exit_code* const rc_open)
{ {
if (file_name == NULL || file_name[0] == '\0') { if (file_name == NULL || file_name[0] == '\0') {
@ -1171,7 +1171,7 @@ def close(trexio_file):
#+begin_src python #+begin_src python
{ {
"nucleus": { "nucleus": {
"num" : [ "int" , [ ] ] "num" : [ "int" , [ ] ]
, "charge" : [ "float", [ "nucleus.num" ] ] , "charge" : [ "float", [ "nucleus.num" ] ]
, "coord" : [ "float", [ "nucleus.num", "3" ] ] , "coord" : [ "float", [ "nucleus.num", "3" ] ]
, "label" : [ "str" , [ "nucleus.num" ] ] , "label" : [ "str" , [ "nucleus.num" ] ]
@ -2625,46 +2625,76 @@ trexio_write_$group_dset$(trexio_t* const file,
rc = trexio_read_$group_dset_sparse_dim$_64(file, &num); rc = trexio_read_$group_dset_sparse_dim$_64(file, &num);
if (rc != TREXIO_SUCCESS) return rc; if (rc != TREXIO_SUCCESS) return rc;
int32_t* index_sparse_p = (int32_t*) index_sparse;
// shift indices to be zero-based if Fortran API is used // shift indices to be zero-based if Fortran API is used
if (file->one_based) { if (file->one_based) {
uint64_t index_size = rank * buffer_size; uint64_t index_size = rank * buffer_size;
index_sparse_p = CALLOC(index_size, int32_t);
int32_t* index_sparse_p = CALLOC(index_size, int32_t);
if (index_sparse_p == NULL) return TREXIO_ALLOCATION_FAILED; if (index_sparse_p == NULL) return TREXIO_ALLOCATION_FAILED;
for (uint64_t i=0; i<index_size; ++i){ for (uint64_t i=0; i<index_size; ++i){
index_sparse_p[i] = index_sparse[i] - 1; index_sparse_p[i] = index_sparse[i] - 1;
} }
} switch (file->back_end) {
switch (file->back_end) { case TREXIO_TEXT:
rc = trexio_text_write_$group_dset$(file, offset_file, buffer_size, num,
size_max, index_sparse_p, value_sparse);
break;
case TREXIO_TEXT: case TREXIO_HDF5:
rc = trexio_text_write_$group_dset$(file, offset_file, buffer_size, num, size_max, index_sparse_p, value_sparse);
break;
case TREXIO_HDF5:
#ifdef HAVE_HDF5 #ifdef HAVE_HDF5
rc = trexio_hdf5_write_$group_dset$(file, offset_file, buffer_size, num, index_sparse_p, value_sparse); rc = trexio_hdf5_write_$group_dset$(file, offset_file, buffer_size, num,
break; index_sparse_p, value_sparse);
break;
#else #else
rc = TREXIO_BACK_END_MISSING; rc = TREXIO_BACK_END_MISSING;
break; break;
#endif #endif
/* /*
case TREXIO_JSON: case TREXIO_JSON:
rc = trexio_json_write_$group_dset$(...); rc = trexio_json_write_$group_dset$(...);
break; break;
,*/ ,*/
default: default:
rc = TREXIO_FAILURE; /* Impossible case */ rc = TREXIO_FAILURE; /* Impossible case */
break; break;
} }
// free the memory allocated to shift indices to be zero-based FREE(index_sparse_p);
if (file->one_based) FREE(index_sparse_p);
}
else {
switch (file->back_end) {
case TREXIO_TEXT:
rc = trexio_text_write_$group_dset$(file, offset_file, buffer_size, num,
size_max, index_sparse, value_sparse);
break;
case TREXIO_HDF5:
#ifdef HAVE_HDF5
rc = trexio_hdf5_write_$group_dset$(file, offset_file, buffer_size, num,
index_sparse, value_sparse);
break;
#else
rc = TREXIO_BACK_END_MISSING;
break;
#endif
/*
case TREXIO_JSON:
rc = trexio_json_write_$group_dset$(...);
break;
,*/
default:
rc = TREXIO_FAILURE; /* Impossible case */
break;
}
}
return rc; return rc;
} }

View File

@ -301,9 +301,9 @@ trexio_hdf5_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $
/* High-level H5LT API. No need to deal with dataspaces and datatypes */ /* High-level H5LT API. No need to deal with dataspaces and datatypes */
herr_t status = H5LTread_dataset(f->$group$_group, herr_t status = H5LTread_dataset(f->$group$_group,
$GROUP_DSET$_NAME, $GROUP_DSET$_NAME,
H5T_$GROUP_DSET_H5_DTYPE$, H5T_$GROUP_DSET_H5_DTYPE$,
$group_dset$); $group_dset$);
if (status < 0) return TREXIO_FAILURE; if (status < 0) return TREXIO_FAILURE;
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
@ -323,10 +323,10 @@ trexio_hdf5_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$*
if ( H5LTfind_dataset(f->$group$_group, $GROUP_DSET$_NAME) != 1 ) { if ( H5LTfind_dataset(f->$group$_group, $GROUP_DSET$_NAME) != 1 ) {
const herr_t status = H5LTmake_dataset(f->$group$_group, const herr_t status = H5LTmake_dataset(f->$group$_group,
$GROUP_DSET$_NAME, $GROUP_DSET$_NAME,
(int) rank, (const hsize_t*) dims, (int) rank, (const hsize_t*) dims,
H5T_$GROUP_DSET_H5_DTYPE$, H5T_$GROUP_DSET_H5_DTYPE$,
$group_dset$); $group_dset$);
if (status < 0) return TREXIO_FAILURE; if (status < 0) return TREXIO_FAILURE;
} else { } else {
@ -335,9 +335,9 @@ trexio_hdf5_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$*
if (dset_id <= 0) return TREXIO_INVALID_ID; if (dset_id <= 0) return TREXIO_INVALID_ID;
const herr_t status = H5Dwrite(dset_id, const herr_t status = H5Dwrite(dset_id,
H5T_$GROUP_DSET_H5_DTYPE$, H5T_$GROUP_DSET_H5_DTYPE$,
H5S_ALL, H5S_ALL, H5P_DEFAULT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
$group_dset$); $group_dset$);
H5Dclose(dset_id); H5Dclose(dset_id);
if (status < 0) return TREXIO_FAILURE; if (status < 0) return TREXIO_FAILURE;
@ -740,8 +740,8 @@ trexio_hdf5_write_$group_dset$ (trexio_t* const file, const char** $group_dset$,
/* code to write dataset */ /* code to write dataset */
status = H5Dwrite(dset_id, memtype, status = H5Dwrite(dset_id, memtype,
H5S_ALL, H5S_ALL, H5P_DEFAULT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
$group_dset$); $group_dset$);
H5Dclose(dset_id); H5Dclose(dset_id);
H5Tclose(memtype); H5Tclose(memtype);

View File

@ -291,7 +291,7 @@ trexio_text_read_$group$ (trexio_text_t* const file)
strncpy ($group$->file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH); strncpy ($group$->file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
strncat ($group$->file_name, $group$_file_name, strncat ($group$->file_name, $group$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name)); TREXIO_MAX_FILENAME_LENGTH-strlen($group$_file_name));
if ($group$->file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') { if ($group$->file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
FREE($group$); FREE($group$);
@ -344,19 +344,19 @@ 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);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
} }
rc = fscanf(f, "%" SCNu64 "\n", &($group$->dims_$group_dset$[i])); rc = fscanf(f, "%" SCNu64 "\n", &($group$->dims_$group_dset$[i]));
assert(!(rc != 1)); assert(!(rc != 1));
if (rc != 1) { if (rc != 1) {
FREE(buffer); FREE(buffer);
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];
@ -451,11 +451,11 @@ trexio_text_read_$group$ (trexio_text_t* const file)
rc = fscanf(f, " %1023[^\n]", $group$->$group_str$); rc = fscanf(f, " %1023[^\n]", $group$->$group_str$);
assert(!(rc != 1)); assert(!(rc != 1));
if (rc != 1) { if (rc != 1) {
FREE(buffer); FREE(buffer);
fclose(f); fclose(f);
FREE($group$->$group_str$); FREE($group$->$group_str$);
FREE($group$); FREE($group$);
return NULL; return NULL;
} }
} }
@ -476,22 +476,22 @@ trexio_text_read_$group$ (trexio_text_t* const file)
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);
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_format_scanf$", &($group$->$group_dset$[i])); rc = fscanf(f, "%$group_dset_format_scanf$", &($group$->$group_dset$[i]));
assert(!(rc != 1)); assert(!(rc != 1));
if (rc != 1) { if (rc != 1) {
FREE(buffer); FREE(buffer);
fclose(f); fclose(f);
FREE($group$->$group_dset$); FREE($group$->$group_dset$);
FREE($group$); FREE($group$);
return NULL; return NULL;
} }
} }
// END REPEAT GROUP_DSET_NUM // END REPEAT GROUP_DSET_NUM
@ -742,7 +742,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, const $gro
#+begin_src c :tangle read_dset_data_text.c #+begin_src c :tangle read_dset_data_text.c
trexio_exit_code trexio_exit_code
trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$,
const uint32_t rank, const uint64_t* dims) const uint32_t rank, const uint64_t* dims)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -771,7 +771,7 @@ trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $
#+begin_src c :tangle write_dset_data_text.c #+begin_src c :tangle write_dset_data_text.c
trexio_exit_code trexio_exit_code
trexio_text_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group_dset$, trexio_text_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group_dset$,
const uint32_t rank, const uint64_t* dims) const uint32_t rank, const uint64_t* dims)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -1038,7 +1038,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file,
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* Append name of the file with sparse data */ /* Append name of the file with sparse data */
strncat (file_full_path, $group_dset$_file_name, strncat (file_full_path, $group_dset$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name)); TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name));
/* Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes */ /* Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes */
FILE* f = fopen(file_full_path, "a"); FILE* f = fopen(file_full_path, "a");
@ -1069,7 +1069,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file,
This is error-prone due to the fact that for large files (>2 GB) in 32-bit systems ftell will fail. This is error-prone due to the fact that for large files (>2 GB) in 32-bit systems ftell will fail.
One can use ftello function which is adapted for large files. One can use ftello function which is adapted for large files.
For now, we can use front-end-provided size_start, which has been checked for INT64_MAX overflow. For now, we can use front-end-provided size_start, which has been checked for INT64_MAX overflow.
*/ ,*/
int64_t io_start_pos = size_start * line_length; int64_t io_start_pos = size_start * line_length;
/* Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written */ /* Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written */
@ -1077,7 +1077,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file,
for (uint64_t i=0UL; i < (uint64_t) size; ++i) { for (uint64_t i=0UL; i < (uint64_t) size; ++i) {
rc = fprintf(f, format_str, rc = fprintf(f, format_str,
$group_dset_sparse_indices_printf$, $group_dset_sparse_indices_printf$,
*(value_sparse + i)); ,*(value_sparse + i));
if(rc <= 0) { if(rc <= 0) {
fclose(f); fclose(f);
return TREXIO_FAILURE; return TREXIO_FAILURE;
@ -1135,7 +1135,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file,
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* Append name of the file with sparse data */ /* Append name of the file with sparse data */
strncat (file_full_path, $group_dset$_file_name, strncat (file_full_path, $group_dset$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name)); TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name));
/* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */ /* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */
FILE* f = fopen(file_full_path, "r"); FILE* f = fopen(file_full_path, "r");
@ -1168,7 +1168,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file,
if(fgets(buffer, 1023, f) == NULL){ if(fgets(buffer, 1023, f) == NULL){
fclose(f); fclose(f);
*eof_read_size = count; ,*eof_read_size = count;
return TREXIO_END; return TREXIO_END;
} else { } else {
@ -1201,7 +1201,7 @@ trexio_exit_code trexio_text_read_$group_dset$_size(trexio_t* const file, int64_
/* Build the name of the file with sparse data. /* Build the name of the file with sparse data.
The $group_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed? The $group_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed?
*/ ,*/
const char $group_dset$_file_name[256] = "/$group_dset$.txt.size"; const char $group_dset$_file_name[256] = "/$group_dset$.txt.size";
/* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */
char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; char file_full_path[TREXIO_MAX_FILENAME_LENGTH];
@ -1210,7 +1210,7 @@ trexio_exit_code trexio_text_read_$group_dset$_size(trexio_t* const file, int64_
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* Append name of the file with sparse data */ /* Append name of the file with sparse data */
strncat (file_full_path, $group_dset$_file_name, strncat (file_full_path, $group_dset$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name)); TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name));
/* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */ /* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */
FILE* f = fopen(file_full_path, "r"); FILE* f = fopen(file_full_path, "r");
@ -1228,7 +1228,7 @@ trexio_exit_code trexio_text_read_$group_dset$_size(trexio_t* const file, int64_
size_accum += size_item; size_accum += size_item;
} else { } else {
fclose(f); fclose(f);
*size_max = -1L; ,*size_max = -1L;
return TREXIO_INT_SIZE_OVERFLOW; return TREXIO_INT_SIZE_OVERFLOW;
} }
} }
@ -1238,7 +1238,7 @@ trexio_exit_code trexio_text_read_$group_dset$_size(trexio_t* const file, int64_
if(rc != 0) return TREXIO_FILE_ERROR; if(rc != 0) return TREXIO_FILE_ERROR;
/* Overwrite the value at the input address and return TREXIO_SUCCESS */ /* Overwrite the value at the input address and return TREXIO_SUCCESS */
*size_max = size_accum; ,*size_max = size_accum;
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} }
@ -1251,7 +1251,7 @@ trexio_exit_code trexio_text_has_$group_dset$(trexio_t* const file)
/* Build the name of the file with sparse data. /* Build the name of the file with sparse data.
The $group_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed? The $group_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed?
*/ ,*/
const char $group_dset$_file_name[256] = "/$group_dset$.txt"; const char $group_dset$_file_name[256] = "/$group_dset$.txt";
/* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */
char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; char file_full_path[TREXIO_MAX_FILENAME_LENGTH];
@ -1260,7 +1260,7 @@ trexio_exit_code trexio_text_has_$group_dset$(trexio_t* const file)
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* Append name of the file with sparse data */ /* Append name of the file with sparse data */
strncat (file_full_path, $group_dset$_file_name, strncat (file_full_path, $group_dset$_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name)); TREXIO_MAX_FILENAME_LENGTH-strlen($group_dset$_file_name));
/* Check the return code of access function to determine whether the file with sparse data exists or not */ /* Check the return code of access function to determine whether the file with sparse data exists or not */
if (access(file_full_path, F_OK) == 0){ if (access(file_full_path, F_OK) == 0){