4634
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4634
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_ls_hdf5_h) |
|
25 #define octave_ls_hdf5_h 1 |
|
26 |
4695
|
27 #if defined (HAVE_HDF5) |
|
28 |
4634
|
29 // first, we need to define our own dummy stream subclass, since |
|
30 // HDF5 needs to do its own file i/o |
|
31 |
|
32 // hdf5_fstreambase is used for both input and output streams, modeled |
|
33 // on the fstreambase class in <fstream.h> |
|
34 |
|
35 class hdf5_fstreambase : virtual public std::ios |
|
36 { |
|
37 public: |
|
38 |
|
39 // HDF5 uses an "id" to refer to an open file |
|
40 hid_t file_id; |
|
41 |
|
42 // keep track of current item index in the file |
|
43 int current_item; |
|
44 |
|
45 hdf5_fstreambase () { file_id = -1; } |
|
46 |
6760
|
47 ~hdf5_fstreambase () { close (); } |
|
48 |
4663
|
49 hdf5_fstreambase (const char *name, int mode, int /* prot */ = 0) |
4634
|
50 { |
5255
|
51 if (mode & std::ios::in) |
4634
|
52 file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT); |
5255
|
53 else if (mode & std::ios::out) |
6760
|
54 { |
|
55 if (mode & std::ios::app && H5Fis_hdf5 (name) > 0) |
|
56 file_id = H5Fopen (name, H5F_ACC_RDWR, H5P_DEFAULT); |
|
57 else |
|
58 file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT, |
|
59 H5P_DEFAULT); |
|
60 } |
4634
|
61 if (file_id < 0) |
|
62 std::ios::setstate (std::ios::badbit); |
|
63 |
|
64 current_item = 0; |
|
65 } |
|
66 |
|
67 void close () |
|
68 { |
|
69 if (file_id >= 0) |
|
70 { |
|
71 if (H5Fclose (file_id) < 0) |
|
72 std::ios::setstate (std::ios::badbit); |
|
73 file_id = -1; |
|
74 } |
|
75 } |
|
76 |
5728
|
77 void open (const char *name, int mode, int) |
4634
|
78 { |
|
79 clear (); |
|
80 |
5255
|
81 if (mode & std::ios::in) |
4634
|
82 file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT); |
5255
|
83 else if (mode & std::ios::out) |
6760
|
84 { |
|
85 if (mode & std::ios::app && H5Fis_hdf5 (name) > 0) |
|
86 file_id = H5Fopen (name, H5F_ACC_RDWR, H5P_DEFAULT); |
|
87 else |
|
88 file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT, |
|
89 H5P_DEFAULT); |
|
90 } |
4634
|
91 if (file_id < 0) |
|
92 std::ios::setstate (std::ios::badbit); |
|
93 |
|
94 current_item = 0; |
|
95 } |
|
96 }; |
|
97 |
|
98 // input and output streams, subclassing istream and ostream |
|
99 // so that we can pass them for stream parameters in the functions below. |
|
100 |
|
101 class hdf5_ifstream : public hdf5_fstreambase, public std::istream |
|
102 { |
|
103 public: |
|
104 |
|
105 hdf5_ifstream () : hdf5_fstreambase (), std::istream (0) { } |
|
106 |
5255
|
107 hdf5_ifstream (const char *name, int mode = std::ios::in|std::ios::binary, |
|
108 int prot = 0) |
4634
|
109 : hdf5_fstreambase (name, mode, prot), std::istream (0) { } |
|
110 |
5255
|
111 void open (const char *name, int mode = std::ios::in|std::ios::binary, |
|
112 int prot = 0) |
4634
|
113 { hdf5_fstreambase::open (name, mode, prot); } |
|
114 }; |
|
115 |
|
116 class hdf5_ofstream : public hdf5_fstreambase, public std::ostream |
|
117 { |
|
118 public: |
|
119 |
|
120 hdf5_ofstream () : hdf5_fstreambase (), std::ostream (0) { } |
|
121 |
5255
|
122 hdf5_ofstream (const char *name, int mode = std::ios::out|std::ios::binary, |
|
123 int prot = 0) |
4634
|
124 : hdf5_fstreambase (name, mode, prot), std::ostream (0) { } |
|
125 |
5255
|
126 void open (const char *name, int mode = std::ios::out|std::ios::binary, |
|
127 int prot = 0) |
4634
|
128 { hdf5_fstreambase::open (name, mode, prot); } |
|
129 }; |
|
130 |
4687
|
131 // Callback data structure for passing data to hdf5_read_next_data, below. |
|
132 |
|
133 struct |
|
134 hdf5_callback_data |
|
135 { |
|
136 hdf5_callback_data (void) |
|
137 : name (), global (false), tc (), doc () { } |
|
138 |
|
139 // the following fields are set by hdf5_read_data on successful return: |
|
140 |
|
141 // the name of the variable |
|
142 std::string name; |
|
143 |
|
144 // whether it is global |
|
145 bool global; |
|
146 |
|
147 // the value of the variable, in Octave form |
|
148 octave_value tc; |
|
149 |
|
150 // a documentation string (NULL if none) |
|
151 std::string doc; |
|
152 }; |
|
153 |
|
154 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
6693
|
155 extern OCTINTERP_API hid_t |
4687
|
156 save_type_to_hdf5 (save_type st) |
|
157 #endif |
|
158 |
6693
|
159 extern OCTINTERP_API hid_t |
4687
|
160 hdf5_make_complex_type (hid_t num_type); |
|
161 |
6693
|
162 extern OCTINTERP_API bool |
4687
|
163 hdf5_types_compatible (hid_t t1, hid_t t2); |
|
164 |
6693
|
165 extern OCTINTERP_API herr_t |
4687
|
166 hdf5_read_next_data (hid_t group_id, const char *name, void *dv); |
|
167 |
6693
|
168 extern OCTINTERP_API bool |
4687
|
169 add_hdf5_data (hid_t loc_id, const octave_value& tc, |
|
170 const std::string& name, const std::string& doc, |
|
171 bool mark_as_global, bool save_as_floats); |
|
172 |
6693
|
173 extern OCTINTERP_API int |
4805
|
174 save_hdf5_empty (hid_t loc_id, const char *name, const dim_vector d); |
|
175 |
6693
|
176 extern OCTINTERP_API int |
4805
|
177 load_hdf5_empty (hid_t loc_id, const char *name, dim_vector &d); |
|
178 |
6693
|
179 extern OCTINTERP_API std::string |
4687
|
180 read_hdf5_data (std::istream& is, const std::string& filename, bool& global, |
|
181 octave_value& tc, std::string& doc); |
4634
|
182 |
6693
|
183 extern OCTINTERP_API bool |
4634
|
184 save_hdf5_data (std::ostream& os, const octave_value& tc, |
|
185 const std::string& name, const std::string& doc, |
|
186 bool mark_as_global, bool save_as_floats); |
|
187 |
5351
|
188 #ifdef IDX_TYPE_LONG |
|
189 #define H5T_NATIVE_IDX H5T_NATIVE_LONG |
|
190 #else |
|
191 #define H5T_NATIVE_IDX H5T_NATIVE_INT |
|
192 #endif |
|
193 |
4634
|
194 #endif |
|
195 |
4695
|
196 #endif |
|
197 |
4634
|
198 /* |
|
199 ;;; Local Variables: *** |
|
200 ;;; mode: C++ *** |
|
201 ;;; End: *** |
|
202 */ |