mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-12-23 04:43:57 +01:00
fixed string length for file name + comments
This commit is contained in:
parent
7afce779a8
commit
acdf982a94
@ -1020,25 +1020,29 @@ trexio_exit_code trexio_text_write_$group_sparse_dset$(trexio_t* const file,
|
|||||||
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
||||||
|
|
||||||
/* Build the name of the file with sparse data*/
|
/* Build the name of the file with sparse data*/
|
||||||
const char* $group_sparse_dset$_file_name = "/$group_sparse_dset$.txt";
|
/* The $group_sparse_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed? */
|
||||||
char file_abs_path[TREXIO_MAX_FILENAME_LENGTH];
|
const char $group_sparse_dset$_file_name[256] = "/$group_sparse_dset$.txt";
|
||||||
|
/* 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];
|
||||||
|
|
||||||
strncpy (file_abs_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
/* Copy directory name in file_full_path */
|
||||||
strncat (file_abs_path, $group_sparse_dset$_file_name,
|
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||||
|
/* Append name of the file with sparse data */
|
||||||
|
strncat (file_full_path, $group_sparse_dset$_file_name,
|
||||||
TREXIO_MAX_FILENAME_LENGTH-strlen($group_sparse_dset$_file_name));
|
TREXIO_MAX_FILENAME_LENGTH-strlen($group_sparse_dset$_file_name));
|
||||||
|
|
||||||
|
/* Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes */
|
||||||
FILE* f = fopen(file_abs_path, "a");
|
FILE* f = fopen(file_full_path, "a");
|
||||||
//TODO ERROR HANDLING
|
|
||||||
if(f == NULL) return TREXIO_FILE_ERROR;
|
if(f == NULL) return TREXIO_FILE_ERROR;
|
||||||
|
|
||||||
// read the currently written number of elements
|
/* Specify the line length in order to offset properly. For example, for 4-index quantities
|
||||||
// line_length is 69 because
|
the line_length is 69 because 10 per index + 4 spaces + 24 for floating point value + 1 for the new line char.
|
||||||
// 10 per index (40 in total) + 4 spaces + 24 for floating point value + 1 for newline char
|
CURRENTLY NO OFFSET IS USED WHEN WRITING !
|
||||||
// in general: 10*n_indices + n_indices + 24 + 1
|
*/
|
||||||
const uint64_t line_length = $group_sparse_dset_line_length$L;
|
const uint64_t line_length = $group_sparse_dset_line_length$L;
|
||||||
//fseek(f, (long) offset_file * line_length, SEEK_SET);
|
//fseek(f, (long) offset_file * line_length, SEEK_SET);
|
||||||
|
|
||||||
|
/* Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written */
|
||||||
int rc;
|
int rc;
|
||||||
for (uint64_t i=0L; i<size; ++i) {
|
for (uint64_t i=0L; i<size; ++i) {
|
||||||
|
|
||||||
@ -1053,6 +1057,7 @@ trexio_exit_code trexio_text_write_$group_sparse_dset$(trexio_t* const file,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Close the TXT file */
|
||||||
rc = fclose(f);
|
rc = fclose(f);
|
||||||
if(rc != 0) return TREXIO_FILE_ERROR;
|
if(rc != 0) return TREXIO_FILE_ERROR;
|
||||||
|
|
||||||
@ -1071,26 +1076,31 @@ trexio_exit_code trexio_text_read_$group_sparse_dset$(trexio_t* const file,
|
|||||||
{
|
{
|
||||||
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
||||||
|
|
||||||
/* Build the name of the file with sparse data*/
|
/* Build the name of the file with sparse data.
|
||||||
const char* $group_sparse_dset$_file_name = "/$group_sparse_dset$.txt";
|
The $group_sparse_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed?
|
||||||
char file_abs_path[TREXIO_MAX_FILENAME_LENGTH];
|
,*/
|
||||||
|
const char $group_sparse_dset$_file_name[256] = "/$group_sparse_dset$.txt";
|
||||||
|
/* 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];
|
||||||
|
|
||||||
strncpy (file_abs_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
/* Copy directory name in file_full_path */
|
||||||
strncat (file_abs_path, $group_sparse_dset$_file_name,
|
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||||
|
/* Append name of the file with sparse data */
|
||||||
|
strncat (file_full_path, $group_sparse_dset$_file_name,
|
||||||
TREXIO_MAX_FILENAME_LENGTH-strlen($group_sparse_dset$_file_name));
|
TREXIO_MAX_FILENAME_LENGTH-strlen($group_sparse_dset$_file_name));
|
||||||
|
|
||||||
|
/* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */
|
||||||
FILE* f = fopen(file_abs_path, "r");
|
FILE* f = fopen(file_full_path, "r");
|
||||||
if(f == NULL) return TREXIO_FILE_ERROR;
|
if(f == NULL) return TREXIO_FILE_ERROR;
|
||||||
|
|
||||||
// read the currently written number of elements
|
/* Specify the line length in order to offset properly. For example, for 4-index quantities
|
||||||
// line_length is 69 because
|
the line_length is 69 because 10 per index + 4 spaces + 24 for floating point value + 1 for the new line char
|
||||||
// 10 per index (40 in total) + 4 spaces + 24 for floating point value + 1 for newline char
|
*/
|
||||||
// in general: 10*n_indices + n_indices + 24 + 1
|
|
||||||
const uint64_t line_length = $group_sparse_dset_line_length$L;
|
const uint64_t line_length = $group_sparse_dset_line_length$L;
|
||||||
|
|
||||||
fseek(f, (long) offset_file * line_length, SEEK_SET);
|
fseek(f, (long) offset_file * line_length, SEEK_SET);
|
||||||
|
|
||||||
|
/* Read the data from the file and check the return code of fprintf to verify that > 0 bytes have been read or reached EOF */
|
||||||
int rc;
|
int rc;
|
||||||
for (uint64_t i=0L; i<size; ++i) {
|
for (uint64_t i=0L; i<size; ++i) {
|
||||||
|
|
||||||
@ -1098,7 +1108,7 @@ trexio_exit_code trexio_text_read_$group_sparse_dset$(trexio_t* const file,
|
|||||||
$group_sparse_dset_indices_scanf$,
|
$group_sparse_dset_indices_scanf$,
|
||||||
&value_sparse[i]);
|
&value_sparse[i]);
|
||||||
|
|
||||||
// TODO: find a way to indicate the number of elements being read (useful?)
|
// TODO: find a way to indicate the number of elements being read (useful?)
|
||||||
if (rc == EOF){
|
if (rc == EOF){
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
@ -1114,6 +1124,7 @@ trexio_exit_code trexio_text_read_$group_sparse_dset$(trexio_t* const file,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Close the TXT file */
|
||||||
rc = fclose(f);
|
rc = fclose(f);
|
||||||
if(rc != 0) return TREXIO_FILE_ERROR;
|
if(rc != 0) return TREXIO_FILE_ERROR;
|
||||||
|
|
||||||
@ -1127,15 +1138,21 @@ trexio_exit_code trexio_text_has_$group_sparse_dset$(trexio_t* const file)
|
|||||||
{
|
{
|
||||||
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
if (file == NULL) return TREXIO_INVALID_ARG_1;
|
||||||
|
|
||||||
/* Build the name of the file with sparse data*/
|
/* Build the name of the file with sparse data.
|
||||||
const char* $group_sparse_dset$_file_name = "/$group_sparse_dset$.txt";
|
The $group_sparse_dset$.txt is limited to 256 symbols for the moment. What are the chances that it will exceed?
|
||||||
char file_abs_path[TREXIO_MAX_FILENAME_LENGTH];
|
,*/
|
||||||
|
const char $group_sparse_dset$_file_name[256] = "/$group_sparse_dset$.txt";
|
||||||
|
/* 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];
|
||||||
|
|
||||||
strncpy (file_abs_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
/* Copy directory name in file_full_path */
|
||||||
strncat (file_abs_path, $group_sparse_dset$_file_name,
|
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
|
||||||
|
/* Append name of the file with sparse data */
|
||||||
|
strncat (file_full_path, $group_sparse_dset$_file_name,
|
||||||
TREXIO_MAX_FILENAME_LENGTH-strlen($group_sparse_dset$_file_name));
|
TREXIO_MAX_FILENAME_LENGTH-strlen($group_sparse_dset$_file_name));
|
||||||
|
|
||||||
if (access(file_abs_path, F_OK) == 0){
|
/* 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){
|
||||||
return TREXIO_SUCCESS;
|
return TREXIO_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
return TREXIO_HAS_NOT;
|
return TREXIO_HAS_NOT;
|
||||||
|
Loading…
Reference in New Issue
Block a user