1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-07-22 10:47:43 +02:00

return error code from trexio_open [C, Fortran]

This commit is contained in:
q-posev 2021-09-22 14:50:22 +02:00
parent b7dc5fc49f
commit 9457a826d2
19 changed files with 122 additions and 67 deletions

View File

@ -703,23 +703,33 @@ struct trexio_back_end_s {
*** C
#+begin_src c :tangle prefix_front.h :exports none
trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t back_end);
trexio_t* trexio_open(const char* file_name, const char mode,
const back_end_t back_end, trexio_exit_code* const rc_open);
#+end_src
#+begin_src c :tangle prefix_front.c
trexio_t*
trexio_open(const char* file_name, const char mode,
const back_end_t back_end)
const back_end_t back_end, trexio_exit_code* const rc_open)
{
if (file_name == NULL) return NULL;
if (file_name[0] == '\0') return NULL;
if (file_name == NULL || file_name[0] == '\0') {
*rc_open = TREXIO_INVALID_ARG_1;
return NULL;
}
/* Check overflow in file_name */
if (back_end < 0) return NULL;
if (back_end >= TREXIO_INVALID_BACK_END) return NULL;
if (back_end < 0 || back_end >= TREXIO_INVALID_BACK_END) {
*rc_open = TREXIO_INVALID_ARG_3;
return NULL;
}
if (mode != 'r' && mode != 'w') return NULL;
if (mode != 'r' && mode != 'w') {
*rc_open = TREXIO_INVALID_ARG_2;
return NULL;
}
if (rc_open == NULL) return NULL;
trexio_t* result = NULL;
void* result_tmp = NULL;
@ -744,17 +754,18 @@ trexio_open(const char* file_name, const char mode,
assert (result != NULL); /* TODO: Error handling */
/* Data for the parent type */
strncpy(result->file_name, file_name, TREXIO_MAX_FILENAME_LENGTH);
if (result->file_name[TREXIO_MAX_FILENAME_LENGTH-1] != '\0') {
*rc_open = TREXIO_INVALID_ARG_1;
free(result);
return NULL;
}
strncpy(result->version, PACKAGE_VERSION, 16);
if (result->version[15] != '\0') {
*rc_open = TREXIO_FAILURE;
free(result);
return NULL;
}
@ -792,12 +803,14 @@ trexio_open(const char* file_name, const char mode,
}
if (rc != TREXIO_SUCCESS) {
*rc_open = TREXIO_OPEN_ERROR;
free(result);
return NULL;
}
rc = trexio_has_metadata_package_version(result);
if (rc == TREXIO_FAILURE) {
*rc_open = TREXIO_OPEN_ERROR;
free(result);
return NULL;
}
@ -821,6 +834,7 @@ trexio_open(const char* file_name, const char mode,
}
if (rc != TREXIO_SUCCESS) {
*rc_open = TREXIO_OPEN_ERROR;
free(result);
return NULL;
}
@ -847,10 +861,12 @@ trexio_open(const char* file_name, const char mode,
}
if (rc != TREXIO_SUCCESS) {
*rc_open = TREXIO_LOCK_ERROR;
free(result);
return NULL;
}
*rc_open = TREXIO_SUCCESS;
return result;
}
#+end_src
@ -859,12 +875,13 @@ trexio_open(const char* file_name, const char mode,
#+begin_src f90 :tangle prefix_fortran.f90
interface
integer(8) function trexio_open_c (filename, mode, backend) bind(C, name="trexio_open")
integer(8) function trexio_open_c (filename, mode, backend, rc_open) bind(C, name="trexio_open")
use, intrinsic :: iso_c_binding
import
character(kind=c_char), dimension(*) :: filename
character, intent(in), value :: mode
integer(trexio_backend), intent(in), value :: backend
integer(trexio_exit_code), intent(out) :: rc_open
end function trexio_open_c
end interface
#+end_src
@ -3056,18 +3073,19 @@ def has_$group_str$(trexio_file) -> bool:
#+begin_src f90 :tangle helper_fortran.f90
contains
integer(8) function trexio_open (filename, mode, backend)
integer(8) function trexio_open (filename, mode, backend, rc_open)
use, intrinsic :: iso_c_binding, only : c_null_char
implicit none
character(len=*), intent(in) :: filename
character, intent(in), value :: mode
integer(trexio_backend), intent(in), value :: backend
integer(trexio_exit_code), intent(out) :: rc_open
character(len=len_trim(filename)+1) :: filename_c
integer :: rc
integer(trexio_exit_code) :: rc
filename_c = trim(filename) // c_null_char
trexio_open = trexio_open_c(filename_c, mode, backend)
if (trexio_open == 0_8) then
trexio_open = trexio_open_c(filename_c, mode, backend, rc_open)
if (trexio_open == 0_8 .or. rc_open /= TREXIO_SUCCESS) then
return
endif
rc = trexio_set_one_based(trexio_open)

View File

@ -72,7 +72,7 @@ int test_write(const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// check that certain data does not exist in the file
@ -97,7 +97,7 @@ int test_write(const char* file_name, const back_end_t backend) {
assert (rc == TREXIO_SUCCESS);
// reopen file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// check if the written data exists in the file
@ -125,7 +125,7 @@ int test_write(const char* file_name, const back_end_t backend) {
assert (rc == TREXIO_SUCCESS);
// open file again in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write some missing blocks (e.g. if forgot last time)
@ -156,7 +156,7 @@ int test_read(const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open existing file on 'read' mode [created by test_write]
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read nucleus_num
@ -211,7 +211,7 @@ int test_read(const char* file_name, const back_end_t backend) {
const char* file_name2 = "test_nonexisting";
trexio_t* file2 = NULL;
file2 = trexio_open(file_name2, 'r', backend);
file2 = trexio_open(file_name2, 'r', backend, &rc);
assert (file2 == NULL);
/*================= END OF TEST =====================*/

View File

@ -34,7 +34,7 @@ static int test_write_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -65,7 +65,7 @@ static int test_has_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written dataset exists
@ -100,7 +100,7 @@ static int test_read_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -34,7 +34,7 @@ static int test_write_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -65,7 +65,7 @@ static int test_has_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written dataset exists
@ -100,7 +100,7 @@ static int test_read_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -21,7 +21,7 @@ static int test_write_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -52,7 +52,7 @@ static int test_has_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written dataset exists
@ -87,7 +87,7 @@ static int test_read_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -21,7 +21,7 @@ static int test_write_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -52,7 +52,7 @@ static int test_has_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written dataset exists
@ -87,7 +87,7 @@ static int test_read_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -33,7 +33,7 @@ static int test_write_dset_str (const char* file_name, const back_end_t backend)
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -65,7 +65,7 @@ static int test_has_dset_str (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written dataset of strings exists
@ -100,7 +100,7 @@ static int test_read_dset_str (const char* file_name, const back_end_t backend)
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -33,7 +33,7 @@ static int test_write_dset_str (const char* file_name, const back_end_t backend)
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -65,7 +65,7 @@ static int test_has_dset_str (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written dataset of strings exists
@ -100,7 +100,7 @@ static int test_read_dset_str (const char* file_name, const back_end_t backend)
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -20,7 +20,7 @@ static int test_write_num (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -55,7 +55,7 @@ static int test_has_num (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written num variable exists
@ -90,7 +90,7 @@ static int test_read_num (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -20,7 +20,7 @@ static int test_write_num (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -55,7 +55,7 @@ static int test_has_num (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written num variable exists
@ -90,7 +90,7 @@ static int test_read_num (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -35,7 +35,7 @@ static int test_write_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -75,7 +75,7 @@ static int test_has_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written dataset exists
@ -106,7 +106,7 @@ static int test_read_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -35,7 +35,7 @@ static int test_write_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write numerical attribute in an empty file
@ -75,7 +75,7 @@ static int test_has_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written dataset exists
@ -106,7 +106,7 @@ static int test_read_dset (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read numerical attribute from the file

View File

@ -21,7 +21,7 @@ static int test_write_str (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write string attribute in an empty file
@ -49,7 +49,7 @@ static int test_has_str (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written string attribute exists
@ -83,7 +83,7 @@ static int test_read_str (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read string attribute from the file

View File

@ -21,7 +21,7 @@ static int test_write_str (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write string attribute in an empty file
@ -49,7 +49,7 @@ static int test_has_str (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// check that the previously written string attribute exists
@ -83,7 +83,7 @@ static int test_read_str (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
// read string attribute from the file

View File

@ -19,8 +19,9 @@ static int test_open_w (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
@ -42,8 +43,9 @@ static int test_open_r (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
@ -65,8 +67,9 @@ static int test_open_void (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file == NULL);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
/*================= END OF TEST ==================*/

View File

@ -19,8 +19,9 @@ static int test_open_w (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
@ -42,8 +43,9 @@ static int test_open_r (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
@ -65,8 +67,9 @@ static int test_open_void (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'read' mode
file = trexio_open(file_name, 'r', backend);
file = trexio_open(file_name, 'r', backend, &rc);
assert (file == NULL);
fprintf(stderr, "%s \n", trexio_string_of_error(rc));
/*================= END OF TEST ==================*/

View File

@ -47,7 +47,7 @@ static int test_write (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write the data
@ -95,7 +95,7 @@ static int test_overwrite (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// check that the previously written data cannot be overwritten

View File

@ -47,7 +47,7 @@ static int test_write (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// write the data
@ -95,7 +95,7 @@ static int test_overwrite (const char* file_name, const back_end_t backend) {
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend);
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
// check that the previously written data cannot be overwritten

View File

@ -9,12 +9,16 @@ program test_trexio
call test_read('test_write_f.dir', TREXIO_TEXT)
call system('rm -rf test_write_f.dir')
call test_read_void('test_write_f.dir', TREXIO_TEXT)
call system('rm -rf test_write_f.h5')
print *, 'call test_write(''test_write_f.h5'', TREXIO_HDF5)'
call test_write('test_write_f.h5', TREXIO_HDF5)
print *, 'call test_read(''test_write_f.h5'', TREXIO_HDF5)'
call test_read('test_write_f.h5', TREXIO_HDF5)
call system('rm -rf test_write_f.h5')
call test_read_void('test_write_f.h5', TREXIO_HDF5)
end program test_trexio
@ -68,7 +72,8 @@ subroutine test_write(file_name, back_end)
! ================= START OF TEST ===================== !
trex_file = trexio_open(file_name, 'w', back_end)
trex_file = trexio_open(file_name, 'w', back_end, rc)
call trexio_assert(rc, TREXIO_SUCCESS)
rc = trexio_has_nucleus_num(trex_file)
call trexio_assert(rc, TREXIO_HAS_NOT, 'SUCCESS HAS NOT 1')
@ -144,8 +149,8 @@ subroutine test_read(file_name, back_end)
! ================= START OF TEST ===================== !
trex_file = trexio_open(file_name, 'r', back_end)
trex_file = trexio_open(file_name, 'r', back_end, rc)
call trexio_assert(rc, TREXIO_SUCCESS)
rc = trexio_read_nucleus_num(trex_file, num_read)
call trexio_assert(rc, TREXIO_SUCCESS)
@ -214,3 +219,29 @@ subroutine test_read(file_name, back_end)
end subroutine test_read
subroutine test_read_void(file_name, back_end)
! ============ Test read of non-existing file =============== !
use trexio
implicit none
character*(*), intent(in) :: file_name
integer, intent(in) :: back_end
integer(8) :: trex_file
integer :: rc = 1
character(128) :: str
! ================= START OF TEST ===================== !
trex_file = trexio_open(file_name, 'r', back_end, rc)
call trexio_assert(rc, TREXIO_OPEN_ERROR)
call trexio_string_of_error(rc, str)
print *, trim(str)
! ================= END OF TEST ===================== !
end subroutine test_read_void