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