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