22 archivecontext::archivecontext(std::string
const & filename,
bool write,
bool replace,
bool compress,
bool memory)
24 , write_(write || replace)
25 , replace_(!memory && replace)
28 , filename_new_(filename)
33 archivecontext::~archivecontext() {
37 void archivecontext::grant(
bool write,
bool replace) {
38 if (!write_ && (write || replace)) {
40 write_ = write || replace;
41 replace_ = !memory_ && replace;
46 void archivecontext::construct() {
49 property_type prop_id(H5Pcreate(H5P_FILE_ACCESS));
50 check_error(H5Pset_fapl_core(prop_id, 1 << 20,
true));
51 #ifndef ALPS_HDF5_CLOSE_GREEDY 52 check_error(H5Pset_fclose_degree(prop_id, H5F_CLOSE_SEMI));
55 if ((file_id_ = H5Fopen(filename_new_.c_str(), H5F_ACC_RDWR, prop_id)) < 0) {
56 property_type fcrt_id(H5Pcreate(H5P_FILE_CREATE));
57 check_error(H5Pset_link_creation_order(fcrt_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)));
58 check_error(H5Pset_attr_creation_order(fcrt_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)));
59 check_error(file_id_ = H5Fcreate(filename_new_.c_str(), H5F_ACC_TRUNC, fcrt_id, prop_id));
61 }
else if ((file_id_ = H5Fopen(filename_new_.c_str(), H5F_ACC_RDONLY, prop_id)) < 0)
62 throw archive_not_found(
"file does not exists or is not a valid hdf5 archive: " + filename_new_ +
ALPS_STACKTRACE);
64 check_error(file_id_);
67 throw std::logic_error(
"'Replace' functionality is not yet implemented by hdf5::archive" 72 if (write_ && replace_ ) {
73 throw std::logic_error(
"'Replace' functionality is not yet implemented by hdf5::archive" 81 if (!std::ifstream(filename_new_.c_str(),std::ios::in).good())
82 throw archive_not_found(
"file cannot be read or does not exist: " + filename_new_ +
ALPS_STACKTRACE);
83 if (check_error(H5Fis_hdf5(filename_new_.c_str())) == 0)
84 throw archive_error(
"no valid hdf5 file: " + filename_new_ +
ALPS_STACKTRACE);
86 #ifndef ALPS_HDF5_CLOSE_GREEDY 87 property_type ALPS_HDF5_FILE_ACCESS(H5Pcreate(H5P_FILE_ACCESS));
88 check_error(H5Pset_fclose_degree(ALPS_HDF5_FILE_ACCESS, H5F_CLOSE_SEMI));
90 #define ALPS_HDF5_FILE_ACCESS H5P_DEFAULT 93 if ((file_id_ = H5Fopen(filename_new_.c_str(), H5F_ACC_RDWR, ALPS_HDF5_FILE_ACCESS)) < 0) {
94 property_type fcrt_id(H5Pcreate(H5P_FILE_CREATE));
95 check_error(H5Pset_link_creation_order(fcrt_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)));
96 check_error(H5Pset_attr_creation_order(fcrt_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)));
97 check_error(file_id_ = H5Fcreate(filename_new_.c_str(), H5F_ACC_TRUNC, fcrt_id, ALPS_HDF5_FILE_ACCESS));
100 check_error(file_id_ = H5Fopen(filename_new_.c_str(), H5F_ACC_RDONLY, ALPS_HDF5_FILE_ACCESS));
101 #ifdef ALPS_HDF5_CLOSE_GREEDY 102 #undef(ALPS_HDF5_FILE_ACCESS) 107 void archivecontext::destruct(
bool abort) {
109 H5Fflush(file_id_, H5F_SCOPE_GLOBAL);
110 #ifndef ALPS_HDF5_CLOSE_GREEDY 112 H5Fget_obj_count(file_id_, H5F_OBJ_DATATYPE) > 0
113 || H5Fget_obj_count(file_id_, H5F_OBJ_ALL) - H5Fget_obj_count(file_id_, H5F_OBJ_FILE) > 0
115 std::cerr <<
"Not all resources closed in file '" << filename_new_ <<
"'" << std::endl;
119 if (H5Fclose(file_id_) < 0)
120 std::cerr <<
"Error in " 128 <<
error().invoke(file_id_)
131 throw std::logic_error(
"'Replace' functionality is not yet implemented by hdf5::archive" 137 }
catch (std::exception & ex) {
139 std::cerr <<
"Error destroying HDF5 context of file '" << filename_new_ <<
"'\n" << ex.what() << std::endl;
error_type< T >::type error(T const &arg)
#define ALPS_STRINGIFY(arg)