Warning, /include/Geant4/toolx/hdf5/pages is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef toolx_hdf5_pages
0005 #define toolx_hdf5_pages
0006
0007 #include "T_tools"
0008 #include "tools"
0009
0010 #include <tools/S_STRING>
0011
0012 #ifdef TOOLS_MEM
0013 #include <tools/mem>
0014 #endif
0015
0016 #include <ostream>
0017
0018 namespace toolx {
0019 namespace hdf5 {
0020
0021 TOOLS_GLOBAL_STRING(pages)
0022 TOOLS_GLOBAL_STRING(entries)
0023 TOOLS_GLOBAL_STRING(columns)
0024 TOOLS_GLOBAL_STRING(names)
0025 TOOLS_GLOBAL_STRING(forms)
0026
0027 class pages {
0028 TOOLS_SCLASS(toolx::hdf5::pages)
0029 public:
0030 pages(std::ostream& a_out,
0031 hid_t a_group,const std::string& a_name,const std::string& a_form,
0032 bool a_write,unsigned int a_compress)
0033 :m_out(a_out),m_name(a_name),m_form(a_form)
0034 //,m_type(0),m_size(0)
0035 ,m_group(-1),m_dataset(-1),m_write(a_write),m_compress(a_compress),m_entries(0),m_pos(0){
0036 #ifdef TOOLS_MEM
0037 tools::mem::increment(s_class().c_str());
0038 #endif
0039 if(m_write) {
0040 m_group = toolx_H5Gcreate(a_group,m_name.c_str(),0);
0041 if(m_group<0) {
0042 m_out << "pages::pages : can't create group for column " << m_name << "." << std::endl;
0043 m_group = -1;
0044 return;
0045 }
0046 if(!write_atb(m_group,"class",s_class())) {
0047 m_out << "pages::pages : write_atb(class) failed." << std::endl;
0048 ::H5Gclose(m_group);
0049 m_group = -1;
0050 return;
0051 }
0052 int v = 1;
0053 if (!write_scalar_atb<int>(m_group,"version",v)) {
0054 m_out << "pages::pages : write_scalar_atb(version) failed." << std::endl;
0055 ::H5Gclose(m_group);
0056 m_group = -1;
0057 return;
0058 }
0059 } else {
0060 m_group = toolx_H5Gopen(a_group,m_name.c_str());
0061 if(m_group<0) {
0062 m_out << "pages::pages : can't open group for column " << m_name << "." << std::endl;
0063 m_group = -1;
0064 return;
0065 }
0066 if(!read_scalar<tools::uint64>(m_group,s_entries(),m_entries)) {
0067 m_out << "pages::pages : read_scalar(entries) failed." << std::endl;
0068 ::H5Gclose(m_group);
0069 m_group = -1;
0070 return;
0071 }
0072 }
0073 }
0074 virtual ~pages(){
0075 if(m_write) {
0076 if(!write_scalar<tools::uint64>(m_group,s_entries(),m_entries)) {
0077 m_out << "pages::~pages : write_scalar(entries) failed." << std::endl;
0078 }
0079 if(m_dataset>=0) ::H5Dclose(m_dataset);
0080 }
0081 ::H5Gclose(m_group);
0082 #ifdef TOOLS_MEM
0083 tools::mem::decrement(s_class().c_str());
0084 #endif
0085 }
0086 protected:
0087 pages(const pages& a_from):m_out(a_from.m_out){
0088 #ifdef TOOLS_MEM
0089 tools::mem::increment(s_class().c_str());
0090 #endif
0091 }
0092 pages& operator=(const pages&){return *this;}
0093 public:
0094 bool is_valid() const {return m_group<0?false:true;}
0095
0096 const std::string& name() const {return m_name;}
0097 const std::string& form() const {return m_form;}
0098 //int type() const {return m_type;}
0099 //unsigned int size() const {return m_size;}
0100 tools::uint64 entries() const {return m_entries;}
0101 tools::uint64 pos() const {return m_pos;}
0102 void reset_pos() {m_pos = 0;}
0103
0104 template <class TYPE>
0105 bool write_page(size_t a_size,const TYPE* a_array) {
0106 if(!m_pos) {
0107 if(!write_array<TYPE>(m_group,s_pages(),a_size,a_array,a_size?a_size:32,m_compress)) {
0108 m_out << "pages::write_page : write_array<TYPE>() failed. Pos " << m_pos << std::endl;
0109 return false;
0110 }
0111 m_dataset = toolx_H5Dopen(m_group,s_pages().c_str());
0112 if(m_dataset<0) {
0113 m_out << "pages::write_page : H5Dopen failed. Pos " << m_pos << std::endl;
0114 return false;
0115 }
0116 } else {
0117 if(!write_append_array_dataset<TYPE>(m_dataset,a_size,a_array)) {
0118 m_out << "pages::write_page : write_append_array_dataset<TYPE>() failed. Pos " << m_pos << std::endl;
0119 return false;
0120 }
0121 }
0122 m_pos += a_size;
0123 m_entries = m_pos;
0124 return true;
0125 }
0126
0127 template <class TYPE>
0128 bool read_page(size_t a_size,TYPE* a_array) {
0129 //it is assumed that a_array can contain a_size*sizeof(TYPE) bytes.
0130 unsigned int _size = a_size;
0131 unsigned int n = 0;
0132 TYPE* array = 0;
0133 if(!read_sub_array<TYPE>(m_group,s_pages(),(unsigned int)m_pos,(unsigned int)_size,n,array)) {
0134 m_out << "pages::read_page : read_sub_array<TYPE>() failed." << std::endl;
0135 return false;
0136 }
0137 if(n!=_size) {
0138 m_out << "pages::read_page : size mismatch. Requested " << _size << ", got "<< n << "." << std::endl;
0139 delete [] array;
0140 return false;
0141 }
0142
0143 {TYPE* rpos = (TYPE*)array;
0144 TYPE* wpos = (TYPE*)a_array;
0145 for(size_t i=0;i<n;i++,rpos++,wpos++) *wpos = *rpos;
0146 for(size_t i=n;i<_size;i++,rpos++,wpos++) *wpos = TYPE();}
0147
0148 delete [] array;
0149
0150 m_pos += n;
0151 return true;
0152 }
0153
0154 template <class TYPE>
0155 bool write_vlen(size_t a_size,const TYPE* a_array) {
0156 if(!m_pos) {
0157 if(!hdf5::write_vlen<TYPE>(m_group,s_pages(),a_size,a_array,a_size?a_size:32,m_compress)) {
0158 m_out << "pages::write_vlen : write_vlen<TYPE>() failed. Pos " << m_pos << std::endl;
0159 return false;
0160 }
0161 m_dataset = toolx_H5Dopen(m_group,s_pages().c_str());
0162 if(m_dataset<0) {
0163 m_out << "pages::write_vlen : H5Dopen failed. Pos " << m_pos << std::endl;
0164 return false;
0165 }
0166 } else {
0167 if(!write_append_vlen_dataset<TYPE>(m_dataset,a_size,a_array)) {
0168 m_out << "pages::write_vlen : write_append_vlen_dataset<TYPE>() failed. Pos " << m_pos << std::endl;
0169 return false;
0170 }
0171 }
0172 m_pos++;
0173 m_entries++;
0174 return true;
0175 }
0176
0177 template <class TYPE>
0178 bool read_vlen(size_t& a_size,TYPE*& a_array) {
0179 //it is assumed that a_array can contain a_size*sizeof(TYPE) bytes.
0180 unsigned int sz;
0181 if(!read_sub_vlen<TYPE>(m_group,s_pages(),(unsigned int)m_pos,sz,a_array)) {
0182 m_out << "pages::read_vlen : read_sub_vlen<TYPE>() failed." << std::endl;
0183 a_size = 0;
0184 a_array = 0;
0185 return false;
0186 }
0187 a_size = sz;
0188 m_pos++;
0189 m_entries++;
0190 return true;
0191 }
0192
0193 bool write_string(const std::string& a_string) {
0194 if(!m_pos) {
0195 if(!hdf5::write_string_dataset(m_group,s_pages(),a_string,128,m_compress)) { //32=>enforce extendable.
0196 m_out << "pages::write_string : hdf5::write_string() failed. Pos " << m_pos << std::endl;
0197 return false;
0198 }
0199 m_dataset = toolx_H5Dopen(m_group,s_pages().c_str());
0200 if(m_dataset<0) {
0201 m_out << "pages::write_string : H5Dopen failed. Pos " << m_pos << std::endl;
0202 return false;
0203 }
0204 } else {
0205 if(!write_append_string_dataset(m_dataset,a_string)) {
0206 m_out << "pages::write_string : write_append_string_dataset() failed. Pos " << m_pos << std::endl;
0207 return false;
0208 }
0209 }
0210 m_pos++;
0211 m_entries++;
0212 return true;
0213 }
0214
0215 bool read_string(std::string& a_string) {
0216 if(!read_sub_string(m_group,s_pages(),(unsigned int)m_pos,a_string)) {
0217 m_out << "pages::read_string : read_sub_string() failed." << std::endl;
0218 a_string.clear();
0219 return false;
0220 }
0221 m_pos++;
0222 m_entries++;
0223 return true;
0224 }
0225
0226 /*
0227 bool write_string(const std::string& a_string) {return write_vlen<char>(a_string.size(),a_string.c_str());}
0228
0229 bool read_string(std::string& a_string) {
0230 size_t sz;char* _data;
0231 if(!read_vlen<char>(sz,_data)) {a_string.clear();return false;}
0232 a_string.resize(sz);
0233 for(size_t index=0;index<sz;index++) a_string[index] = _data[index];
0234 delete [] _data;
0235 return true;
0236 }
0237 */
0238
0239 /*
0240 bool write_strings(size_t a_number,void* a_array) {
0241 size_t sz = m_size*a_number;
0242 char* buffer = new char[sz];
0243 {char* pos = buffer;
0244 for(size_t index=0;index<sz;index++,pos++) *pos = ' ';}
0245
0246 {char** strings = (char**)a_array;
0247 char* pos = buffer;
0248 size_t ls;
0249 for(size_t index=0;index<a_number;index++) {
0250 char* ss = strings[index];
0251 ls = ::strlen(ss);
0252 ::memcpy(pos,ss,ls); //do not copy the ending 0.
0253 *(pos+m_size-1) = 0;
0254 pos += m_size;
0255 }}
0256
0257 if(!m_pos) {
0258 unsigned int chunked = 32*m_size; //<size>A strings.
0259 if(!write_array<char>(m_group,s_pages(),sz,buffer,chunked,m_compress)) {
0260 m_out << "pages::write_strings : write_strings() failed. Pos " << m_pos << std::endl;
0261 return false;
0262 }
0263 m_dataset = toolx_H5Dopen(m_group,s_pages().c_str());
0264 if(m_dataset<0) {
0265 m_out << "pages::write_strings : H5Dopen failed. Pos " << m_pos << std::endl;
0266 return false;
0267 }
0268 } else {
0269 if(!write_append_array_dataset<char>(m_dataset,sz,buffer)) {
0270 m_out << "pages::write_strings : write_append_strings() failed. Pos " << m_pos << std::endl;
0271 return false;
0272 }
0273 }
0274 m_pos += a_number;
0275 m_entries = m_pos;
0276 return true;
0277 }
0278
0279 bool read_strings(size_t a_size,char* a_array) {
0280 unsigned int _size = a_size*m_size;
0281 unsigned int n = 0;
0282 char* array = 0;
0283 if(!read_sub_array<char>(m_group,s_pages(),m_pos,_size,n,array)) {
0284 m_out << "pages::read_strings : read_sub_array<char>() failed." << std::endl;
0285 return false;
0286 }
0287 if(n!=_size) {
0288 m_out << "pages::read_strings : size mismatch. Requested " << _size << ", got "<< n << "." << std::endl;
0289 delete [] array;
0290 return false;
0291 }
0292 char* pos = array;
0293 {char** strings = (char**)a_array;
0294 for(size_t index=0;index<a_size;index++) {
0295 ::memcpy(strings[index],pos,m_size*sizeof(char));
0296 pos += m_size;
0297 }}
0298 delete [] array;
0299 m_pos += n;
0300 return true;
0301 }
0302 */
0303 protected:
0304 std::ostream& m_out;
0305 std::string m_name;
0306 std::string m_form;
0307 //int m_type;
0308 //unsigned int m_size;
0309 hid_t m_group;
0310 hid_t m_dataset;
0311 bool m_write;
0312 unsigned int m_compress; //if write.
0313 tools::uint64 m_entries;
0314 tools::uint64 m_pos;
0315 };
0316
0317 }}
0318
0319 #endif