GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ls-hdf5.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_ls_hdf5_h)
24 #define octave_ls_hdf5_h 1
25 
26 #if defined (HAVE_HDF5)
27 
28 #include "oct-hdf5.h"
29 
30 // first, we need to define our own dummy stream subclass, since
31 // HDF5 needs to do its own file i/o
32 
33 // hdf5_fstreambase is used for both input and output streams, modeled
34 // on the fstreambase class in <fstream.h>
35 
36 class hdf5_fstreambase : virtual public std::ios
37 {
38 public:
39 
40  // HDF5 uses an "id" to refer to an open file
41  hid_t file_id;
42 
43  // keep track of current item index in the file
45 
47 
49 
50  hdf5_fstreambase (const char *name, int mode, int /* prot */ = 0)
51  : file_id (-1), current_item (-1)
52  {
53  if (mode & std::ios::in)
54  file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT);
55  else if (mode & std::ios::out)
56  {
57  if (mode & std::ios::app && H5Fis_hdf5 (name) > 0)
58  file_id = H5Fopen (name, H5F_ACC_RDWR, H5P_DEFAULT);
59  else
60  file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT,
61  H5P_DEFAULT);
62  }
63  if (file_id < 0)
64  std::ios::setstate (std::ios::badbit);
65 
66  current_item = 0;
67  }
68 
69  void close ()
70  {
71  if (file_id >= 0)
72  {
73  if (H5Fclose (file_id) < 0)
74  std::ios::setstate (std::ios::badbit);
75  file_id = -1;
76  }
77  }
78 
79  void open (const char *name, int mode, int)
80  {
81  clear ();
82 
83  if (mode & std::ios::in)
84  file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT);
85  else if (mode & std::ios::out)
86  {
87  if (mode & std::ios::app && H5Fis_hdf5 (name) > 0)
88  file_id = H5Fopen (name, H5F_ACC_RDWR, H5P_DEFAULT);
89  else
90  file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT,
91  H5P_DEFAULT);
92  }
93  if (file_id < 0)
94  std::ios::setstate (std::ios::badbit);
95 
96  current_item = 0;
97  }
98 };
99 
100 // input and output streams, subclassing istream and ostream
101 // so that we can pass them for stream parameters in the functions below.
102 
103 class hdf5_ifstream : public hdf5_fstreambase, public std::istream
104 {
105 public:
106 
107  hdf5_ifstream () : hdf5_fstreambase (), std::istream (0) { }
108 
109  hdf5_ifstream (const char *name, int mode = std::ios::in|std::ios::binary,
110  int prot = 0)
111  : hdf5_fstreambase (name, mode, prot), std::istream (0) { }
112 
113  void open (const char *name, int mode = std::ios::in|std::ios::binary,
114  int prot = 0)
115  { hdf5_fstreambase::open (name, mode, prot); }
116 };
117 
118 class hdf5_ofstream : public hdf5_fstreambase, public std::ostream
119 {
120 public:
121 
122  hdf5_ofstream () : hdf5_fstreambase (), std::ostream (0) { }
123 
124  hdf5_ofstream (const char *name, int mode = std::ios::out|std::ios::binary,
125  int prot = 0)
126  : hdf5_fstreambase (name, mode, prot), std::ostream (0) { }
127 
128  void open (const char *name, int mode = std::ios::out|std::ios::binary,
129  int prot = 0)
130  { hdf5_fstreambase::open (name, mode, prot); }
131 };
132 
133 // Callback data structure for passing data to hdf5_read_next_data, below.
134 
135 struct
137 {
139  : name (), global (false), tc (), doc () { }
140 
141  // the following fields are set by hdf5_read_data on successful return:
142 
143  // the name of the variable
144  std::string name;
145 
146  // whether it is global
147  bool global;
148 
149  // the value of the variable, in Octave form
151 
152  // a documentation string (NULL if none)
153  std::string doc;
154 };
155 
156 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS
157 extern OCTINTERP_API hid_t
158 save_type_to_hdf5 (save_type st)
159 #endif
160 
161 extern OCTINTERP_API hid_t
162 hdf5_make_complex_type (hid_t num_type);
163 
164 extern OCTINTERP_API bool
165 hdf5_types_compatible (hid_t t1, hid_t t2);
166 
167 extern OCTINTERP_API herr_t
168 hdf5_read_next_data (hid_t group_id, const char *name, void *dv);
169 
170 extern OCTINTERP_API bool
171 add_hdf5_data (hid_t loc_id, const octave_value& tc,
172  const std::string& name, const std::string& doc,
173  bool mark_as_global, bool save_as_floats);
174 
175 extern OCTINTERP_API int
176 save_hdf5_empty (hid_t loc_id, const char *name, const dim_vector d);
177 
178 extern OCTINTERP_API int
179 load_hdf5_empty (hid_t loc_id, const char *name, dim_vector &d);
180 
181 extern OCTINTERP_API std::string
182 read_hdf5_data (std::istream& is, const std::string& filename, bool& global,
183  octave_value& tc, std::string& doc,
184  const string_vector& argv, int argv_idx, int argc);
185 
186 extern OCTINTERP_API bool
187 save_hdf5_data (std::ostream& os, const octave_value& tc,
188  const std::string& name, const std::string& doc,
189  bool mark_as_global, bool save_as_floats);
190 
191 extern OCTINTERP_API bool
192 hdf5_check_attr (hid_t loc_id, const char *attr_name);
193 
194 extern OCTINTERP_API bool
195 hdf5_get_scalar_attr (hid_t loc_id, hid_t type_id, const char *attr_name,
196  void *buf);
197 
198 extern OCTINTERP_API herr_t
199 hdf5_add_attr (hid_t loc_id, const char *attr_name);
200 
201 
202 extern OCTINTERP_API herr_t
203 hdf5_add_scalar_attr (hid_t loc_id, hid_t type_id,
204  const char *attr_name, void *buf);
205 
206 #ifdef USE_64_BIT_IDX_T
207 #define H5T_NATIVE_IDX H5T_NATIVE_LONG
208 #else
209 #define H5T_NATIVE_IDX H5T_NATIVE_INT
210 #endif
211 
212 #endif
213 
214 #endif