1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-23 12:56:53 +01:00

Fixed append problem

This commit is contained in:
Anthony Scemama 2021-05-06 19:40:27 +02:00
parent 1cfa313d07
commit 181fe988f3
2 changed files with 28 additions and 78 deletions

View File

@ -65,11 +65,11 @@ ifeq ($(FC),flang)
endif endif
OBJECT_FILES= trexio.o trexio_text.o trexio_hdf5.o OBJECT_FILES= trexio.o trexio_text.o trexio_hdf5.o
SOURCE_FILES= test.c trexio.c trexio_hdf5.c trexio_text.c SOURCE_FILES= trexio.c trexio_hdf5.c trexio_text.c
HEADER_FILES= $(trexio_h) trexio_text.h trexio_hdf5.h trexio_s.h HEADER_FILES= $(trexio_h) trexio_text.h trexio_hdf5.h trexio_s.h
.PHONY: all fortran clean maintainer-clean install .PHONY: all fortran clean maintainer-clean install sources FORCE
.POSIX: .POSIX:
.SUFFIXES: .SUFFIXES:
@ -137,3 +137,7 @@ Makefile: Makefile.in ../config.status
../config.status: ../configure ../config.status: ../configure
cd .. && ./config.status --recheck cd .. && ./config.status --recheck
sources: FORCE
cd ../tools && ./build_trexio.sh
FORCE:

View File

@ -79,11 +79,11 @@
#+begin_src c :tangle struct_text_group_dset.h #+begin_src c :tangle struct_text_group_dset.h
typedef struct $group$_s { typedef struct $group$_s {
uint64_t $group_num$; uint64_t $group_num$;
FILE* file;
$group_dset_dtype$* $group_dset$; $group_dset_dtype$* $group_dset$;
uint32_t rank_$group_dset$; uint32_t rank_$group_dset$;
uint32_t to_flush; uint32_t to_flush;
uint64_t dims_$group_dset$[16]; uint64_t dims_$group_dset$[16];
char file_name[TREXIO_MAX_FILENAME_LENGTH];
} $group$_t; } $group$_t;
#+end_src #+end_src
@ -95,8 +95,8 @@ typedef struct rdm_s {
uint32_t to_flush; uint32_t to_flush;
uint32_t padding; uint32_t padding;
double* one_e; double* one_e;
FILE* file; char file_name[TREXIO_MAX_FILENAME_LENGTH];
char* two_e_file_name; char two_e_file_name[TREXIO_MAX_FILENAME_LENGTH];
} rdm_t; } rdm_t;
#+end_src #+end_src
@ -143,7 +143,7 @@ trexio_text_init (trexio_t* const file)
/* 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";
char* file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char); char file_name[TREXIO_MAX_FILENAME_LENGTH];
if (file_name == NULL) { if (file_name == NULL) {
return TREXIO_ALLOCATION_FAILED; return TREXIO_ALLOCATION_FAILED;
@ -153,12 +153,10 @@ trexio_text_init (trexio_t* const file)
strncat (file_name, lock_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen(lock_file_name)); strncat (file_name, lock_file_name, TREXIO_MAX_FILENAME_LENGTH-strlen(lock_file_name));
if (file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') { if (file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
FREE(file_name); return TREXIO_LOCK_ERROR;
return TREXIO_ALLOCATION_FAILED;
} }
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
FREE(file_name);
if (f->lock_file <= 0) { if (f->lock_file <= 0) {
return TREXIO_FAILURE; return TREXIO_FAILURE;
@ -274,25 +272,23 @@ trexio_text_read_$group$ (trexio_text_t* const file)
/* Build the file name */ /* Build the file name */
const char* $group$_file_name = "/$group$.txt"; const char* $group$_file_name = "/$group$.txt";
char * file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char);
if (file_name == NULL) { if ($group$->file_name == NULL) {
FREE($group$); FREE($group$);
return NULL; return NULL;
} }
strncpy (file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH); strncpy ($group$->file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
strncat (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 (file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') { if ($group$->file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
FREE(file_name);
FREE($group$); FREE($group$);
return NULL; return NULL;
} }
/* If the file exists, read it */ /* If the file exists, read it */
FILE* f = fopen(file_name,"r"); FILE* f = fopen($group$->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 */
@ -303,7 +299,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
sz = (sz < 1024) ? (1024) : (sz); sz = (sz < 1024) ? (1024) : (sz);
char* buffer = CALLOC(sz, char); char* buffer = CALLOC(sz, char);
if (buffer == NULL) { if (buffer == NULL) {
FREE(file_name);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
@ -315,7 +310,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
rc = fscanf(f, "%1023s", buffer); rc = fscanf(f, "%1023s", buffer);
if ((rc != 1) || (strcmp(buffer, "rank_$group_dset$") != 0)) { if ((rc != 1) || (strcmp(buffer, "rank_$group_dset$") != 0)) {
FREE(buffer); FREE(buffer);
FREE(file_name);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
@ -324,7 +318,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
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);
FREE(file_name);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
@ -341,7 +334,6 @@ 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);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
@ -351,7 +343,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
assert(!(rc != 1)); assert(!(rc != 1));
if (rc != 1) { if (rc != 1) {
FREE(buffer); FREE(buffer);
FREE(file_name);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
@ -367,7 +358,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
assert(!((rc != 1) || (strcmp(buffer, "$group_num$") != 0))); assert(!((rc != 1) || (strcmp(buffer, "$group_num$") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_num$") != 0)) { if ((rc != 1) || (strcmp(buffer, "$group_num$") != 0)) {
FREE(buffer); FREE(buffer);
FREE(file_name);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
@ -377,7 +367,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
assert(!(rc != 1)); assert(!(rc != 1));
if (rc != 1) { if (rc != 1) {
FREE(buffer); FREE(buffer);
FREE(file_name);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
@ -390,7 +379,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
assert (!($group$->$group_dset$ == NULL)); assert (!($group$->$group_dset$ == NULL));
if ($group$->$group_dset$ == NULL) { if ($group$->$group_dset$ == NULL) {
FREE(buffer); FREE(buffer);
FREE(file_name);
fclose(f); fclose(f);
FREE($group$); FREE($group$);
return NULL; return NULL;
@ -400,7 +388,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
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);
fclose(f); fclose(f);
FREE($group$->$group_dset$); FREE($group$->$group_dset$);
FREE($group$); FREE($group$);
@ -412,7 +399,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
assert(!(rc != 1)); assert(!(rc != 1));
if (rc != 1) { if (rc != 1) {
FREE(buffer); FREE(buffer);
FREE(file_name);
fclose(f); fclose(f);
FREE($group$->$group_dset$); FREE($group$->$group_dset$);
FREE($group$); FREE($group$);
@ -426,20 +412,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
f = NULL; f = NULL;
} }
if (file->parent.mode == 'w') {
$group$->file = fopen(file_name,"a");
} else {
$group$->file = fopen(file_name,"r");
}
FREE(file_name);
assert (!($group$->file == NULL));
if ($group$->file == NULL) {
FREE($group$->$group_dset$);
FREE($group$);
return NULL;
}
fseek($group$->file, 0L, SEEK_SET);
file->$group$ = $group$; file->$group$ = $group$;
return $group$; return $group$;
@ -467,9 +439,10 @@ trexio_text_flush_$group$ (trexio_text_t* const file)
if ($group$->to_flush == 0) return TREXIO_SUCCESS; if ($group$->to_flush == 0) return TREXIO_SUCCESS;
FILE* f = $group$->file; assert (file->parent.mode == 'w');
FILE* f = fopen($group$->file_name, "w");
if (f == NULL) return TREXIO_INVALID_ARG_1; if (f == NULL) return TREXIO_INVALID_ARG_1;
fseek(f, 0L, SEEK_SET);
/* Write the dimensioning variables */ /* Write the dimensioning variables */
// START REPEAT GROUP_DSET // START REPEAT GROUP_DSET
@ -498,7 +471,7 @@ trexio_text_flush_$group$ (trexio_text_t* const file)
} }
// END REPEAT GROUP_DSET // END REPEAT GROUP_DSET
fflush(f); fclose(f);
$group$->to_flush = 0; $group$->to_flush = 0;
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
@ -528,11 +501,6 @@ trexio_text_free_$group$ (trexio_text_t* const file)
$group$_t* $group$ = file->$group$; $group$_t* $group$ = file->$group$;
if ($group$ == NULL) return TREXIO_SUCCESS; if ($group$ == NULL) return TREXIO_SUCCESS;
if ($group$->file != NULL) {
fclose($group$->file);
$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$);
@ -722,27 +690,23 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
assert (rdm != NULL); assert (rdm != NULL);
rdm->one_e = NULL; rdm->one_e = NULL;
rdm->two_e_file_name = NULL; rdm->two_e_file_name[0] = '\0';
rdm->file = NULL;
rdm->to_flush = 0; rdm->to_flush = 0;
/* Try to open the file. If the file does not exist, return */ /* Try to open the file. If the file does not exist, return */
const char* rdm_file_name = "/rdm.txt"; const char* rdm_file_name = "/rdm.txt";
char* file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH, char);
assert (file_name != NULL); strncpy (rdm->file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
strncpy (file_name, file->parent.file_name, TREXIO_MAX_FILENAME_LENGTH);
strncat (file_name, rdm_file_name, strncat (rdm->file_name, rdm_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(rdm_file_name)); TREXIO_MAX_FILENAME_LENGTH-strlen(rdm_file_name));
if (file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') { if (rdm->file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
FREE(file_name);
FREE(rdm); FREE(rdm);
return NULL; return NULL;
} }
/* If the file exists, read it */ /* If the file exists, read it */
FILE* f = fopen(file_name,"r"); FILE* f = fopen(rdm->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 */
@ -782,13 +746,10 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
rc = fscanf(f, "%1023s", buffer); rc = fscanf(f, "%1023s", buffer);
assert (rc == 1); assert (rc == 1);
rdm->two_e_file_name = CALLOC(TREXIO_MAX_FILENAME_LENGTH,char);
strncpy(rdm->two_e_file_name, buffer, 1024); strncpy(rdm->two_e_file_name, buffer, 1024);
if (rdm->two_e_file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') { if (rdm->two_e_file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
FREE(file_name);
FREE(buffer); FREE(buffer);
FREE(rdm->one_e); FREE(rdm->one_e);
FREE(rdm->two_e_file_name);
FREE(rdm); FREE(rdm);
fclose(f); fclose(f);
return NULL; return NULL;
@ -798,12 +759,6 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* const file) {
fclose(f); fclose(f);
f = NULL; f = NULL;
} }
if (file->parent.mode == 'w') {
rdm->file = fopen(file_name,"a");
} else {
rdm->file = fopen(file_name,"r");
}
FREE(file_name);
file->rdm = rdm ; file->rdm = rdm ;
return rdm; return rdm;
} }
@ -826,9 +781,9 @@ trexio_exit_code trexio_text_flush_rdm(trexio_text_t* const file) {
if (rdm->to_flush == 0) return TREXIO_SUCCESS; if (rdm->to_flush == 0) return TREXIO_SUCCESS;
FILE* f = rdm->file;
FILE* f = fopen(rdm->file_name,"w");
assert (f != NULL); assert (f != NULL);
fseek(f, 0L, SEEK_SET);
/* Write the dimensioning variables */ /* Write the dimensioning variables */
fprintf(f, "num %" PRIu64 "\n", rdm->dim_one_e); fprintf(f, "num %" PRIu64 "\n", rdm->dim_one_e);
@ -842,7 +797,7 @@ trexio_exit_code trexio_text_flush_rdm(trexio_text_t* const file) {
fprintf(f, "two_e_file_name\n"); fprintf(f, "two_e_file_name\n");
fprintf(f, "%s\n", rdm->two_e_file_name); fprintf(f, "%s\n", rdm->two_e_file_name);
fflush(f); fclose(f);
rdm->to_flush = 0; rdm->to_flush = 0;
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} }
@ -868,19 +823,10 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* const file) {
rdm_t* const rdm = file->rdm; rdm_t* const rdm = file->rdm;
if (rdm == NULL) return TREXIO_SUCCESS; if (rdm == NULL) return TREXIO_SUCCESS;
if (rdm->file != NULL) {
fclose(rdm->file);
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) {
FREE (rdm->two_e_file_name);
}
free (rdm); free (rdm);
file->rdm = NULL; file->rdm = NULL;
return TREXIO_SUCCESS; return TREXIO_SUCCESS;