ls-hdf5.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2003-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_ls_hdf5_h)
00024 #define octave_ls_hdf5_h 1
00025 
00026 #if defined (HAVE_HDF5)
00027 
00028 #include "oct-hdf5.h"
00029 
00030 // first, we need to define our own dummy stream subclass, since
00031 // HDF5 needs to do its own file i/o
00032 
00033 // hdf5_fstreambase is used for both input and output streams, modeled
00034 // on the fstreambase class in <fstream.h>
00035 
00036 class hdf5_fstreambase : virtual public std::ios
00037 {
00038 public:
00039 
00040   // HDF5 uses an "id" to refer to an open file
00041   hid_t file_id;
00042 
00043   // keep track of current item index in the file
00044   int current_item;
00045 
00046   hdf5_fstreambase () : file_id (-1), current_item () { }
00047 
00048   ~hdf5_fstreambase () { close (); }
00049 
00050   hdf5_fstreambase (const char *name, int mode, int /* prot */ = 0)
00051     : file_id (-1), current_item (-1)
00052     {
00053       if (mode & std::ios::in)
00054         file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT);
00055       else if (mode & std::ios::out)
00056         {
00057           if (mode & std::ios::app && H5Fis_hdf5 (name) > 0)
00058             file_id = H5Fopen (name, H5F_ACC_RDWR, H5P_DEFAULT);
00059           else
00060             file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT,
00061                                  H5P_DEFAULT);
00062         }
00063       if (file_id < 0)
00064         std::ios::setstate (std::ios::badbit);
00065 
00066       current_item = 0;
00067     }
00068 
00069   void close ()
00070     {
00071       if (file_id >= 0)
00072         {
00073           if (H5Fclose (file_id) < 0)
00074             std::ios::setstate (std::ios::badbit);
00075           file_id = -1;
00076         }
00077     }
00078 
00079   void open (const char *name, int mode, int)
00080     {
00081       clear ();
00082 
00083       if (mode & std::ios::in)
00084         file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT);
00085       else if (mode & std::ios::out)
00086         {
00087           if (mode & std::ios::app && H5Fis_hdf5 (name) > 0)
00088             file_id = H5Fopen (name, H5F_ACC_RDWR, H5P_DEFAULT);
00089           else
00090             file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT,
00091                                  H5P_DEFAULT);
00092         }
00093       if (file_id < 0)
00094         std::ios::setstate (std::ios::badbit);
00095 
00096       current_item = 0;
00097     }
00098 };
00099 
00100 // input and output streams, subclassing istream and ostream
00101 // so that we can pass them for stream parameters in the functions below.
00102 
00103 class hdf5_ifstream : public hdf5_fstreambase, public std::istream
00104 {
00105 public:
00106 
00107   hdf5_ifstream () : hdf5_fstreambase (), std::istream (0) { }
00108 
00109   hdf5_ifstream (const char *name, int mode = std::ios::in|std::ios::binary,
00110                  int prot = 0)
00111     : hdf5_fstreambase (name, mode, prot), std::istream (0) { }
00112 
00113   void open (const char *name, int mode = std::ios::in|std::ios::binary,
00114              int prot = 0)
00115     { hdf5_fstreambase::open (name, mode, prot); }
00116 };
00117 
00118 class hdf5_ofstream : public hdf5_fstreambase, public std::ostream
00119 {
00120 public:
00121 
00122   hdf5_ofstream () : hdf5_fstreambase (), std::ostream (0) { }
00123 
00124   hdf5_ofstream (const char *name, int mode = std::ios::out|std::ios::binary,
00125                  int prot = 0)
00126     : hdf5_fstreambase (name, mode, prot), std::ostream (0) { }
00127 
00128   void open (const char *name, int mode = std::ios::out|std::ios::binary,
00129              int prot = 0)
00130     { hdf5_fstreambase::open (name, mode, prot); }
00131 };
00132 
00133 // Callback data structure for passing data to hdf5_read_next_data, below.
00134 
00135 struct
00136 hdf5_callback_data
00137 {
00138   hdf5_callback_data (void)
00139     : name (), global (false), tc (), doc () { }
00140 
00141   // the following fields are set by hdf5_read_data on successful return:
00142 
00143   // the name of the variable
00144   std::string name;
00145 
00146   // whether it is global
00147   bool global;
00148 
00149   // the value of the variable, in Octave form
00150   octave_value tc;
00151 
00152   // a documentation string (NULL if none)
00153   std::string doc;
00154 };
00155 
00156 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS
00157 extern OCTINTERP_API hid_t
00158 save_type_to_hdf5 (save_type st)
00159 #endif
00160 
00161 extern OCTINTERP_API hid_t
00162 hdf5_make_complex_type (hid_t num_type);
00163 
00164 extern OCTINTERP_API bool
00165 hdf5_types_compatible (hid_t t1, hid_t t2);
00166 
00167 extern OCTINTERP_API herr_t
00168 hdf5_read_next_data (hid_t group_id, const char *name, void *dv);
00169 
00170 extern OCTINTERP_API bool
00171 add_hdf5_data (hid_t loc_id, const octave_value& tc,
00172                const std::string& name, const std::string& doc,
00173                bool mark_as_global, bool save_as_floats);
00174 
00175 extern OCTINTERP_API int
00176 save_hdf5_empty (hid_t loc_id, const char *name, const dim_vector d);
00177 
00178 extern OCTINTERP_API int
00179 load_hdf5_empty (hid_t loc_id, const char *name, dim_vector &d);
00180 
00181 extern OCTINTERP_API std::string
00182 read_hdf5_data (std::istream& is,  const std::string& filename, bool& global,
00183                 octave_value& tc, std::string& doc);
00184 
00185 extern OCTINTERP_API bool
00186 save_hdf5_data (std::ostream& os, const octave_value& tc,
00187                 const std::string& name, const std::string& doc,
00188                 bool mark_as_global, bool save_as_floats);
00189 
00190 extern OCTINTERP_API bool
00191 hdf5_check_attr (hid_t loc_id, const char *attr_name);
00192 
00193 extern OCTINTERP_API bool
00194 hdf5_get_scalar_attr (hid_t loc_id, hid_t type_id, const char *attr_name,
00195                       void *buf);
00196 
00197 extern OCTINTERP_API herr_t
00198 hdf5_add_attr (hid_t loc_id, const char *attr_name);
00199 
00200 
00201 extern OCTINTERP_API herr_t
00202 hdf5_add_scalar_attr (hid_t loc_id, hid_t type_id,
00203                       const char *attr_name, void *buf);
00204 
00205 #ifdef IDX_TYPE_LONG
00206 #define H5T_NATIVE_IDX H5T_NATIVE_LONG
00207 #else
00208 #define H5T_NATIVE_IDX H5T_NATIVE_INT
00209 #endif
00210 
00211 #endif
00212 
00213 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines