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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
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 |
4663
|
46 hdf5_fstreambase (const char *name, int mode, int /* prot */ = 0) |
4634
|
47 { |
|
48 if (mode == std::ios::in) |
|
49 file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT); |
|
50 else if (mode == std::ios::out) |
|
51 file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
|
52 |
|
53 if (file_id < 0) |
|
54 std::ios::setstate (std::ios::badbit); |
|
55 |
|
56 current_item = 0; |
|
57 } |
|
58 |
|
59 void close () |
|
60 { |
|
61 if (file_id >= 0) |
|
62 { |
|
63 if (H5Fclose (file_id) < 0) |
|
64 std::ios::setstate (std::ios::badbit); |
|
65 file_id = -1; |
|
66 } |
|
67 } |
|
68 |
|
69 void open (const char *name, int mode, int prot = 0) |
|
70 { |
|
71 clear (); |
|
72 |
|
73 if (mode == std::ios::in) |
|
74 file_id = H5Fopen (name, H5F_ACC_RDONLY, H5P_DEFAULT); |
|
75 else if (mode == std::ios::out) |
|
76 file_id = H5Fcreate (name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
|
77 |
|
78 if (file_id < 0) |
|
79 std::ios::setstate (std::ios::badbit); |
|
80 |
|
81 current_item = 0; |
|
82 } |
|
83 }; |
|
84 |
|
85 // input and output streams, subclassing istream and ostream |
|
86 // so that we can pass them for stream parameters in the functions below. |
|
87 |
|
88 class hdf5_ifstream : public hdf5_fstreambase, public std::istream |
|
89 { |
|
90 public: |
|
91 |
|
92 hdf5_ifstream () : hdf5_fstreambase (), std::istream (0) { } |
|
93 |
|
94 hdf5_ifstream (const char *name, int mode = std::ios::in, int prot = 0) |
|
95 : hdf5_fstreambase (name, mode, prot), std::istream (0) { } |
|
96 |
|
97 void open (const char *name, int mode = std::ios::in, int prot = 0) |
|
98 { hdf5_fstreambase::open (name, mode, prot); } |
|
99 }; |
|
100 |
|
101 class hdf5_ofstream : public hdf5_fstreambase, public std::ostream |
|
102 { |
|
103 public: |
|
104 |
|
105 hdf5_ofstream () : hdf5_fstreambase (), std::ostream (0) { } |
|
106 |
|
107 hdf5_ofstream (const char *name, int mode = std::ios::out, int prot = 0) |
|
108 : hdf5_fstreambase (name, mode, prot), std::ostream (0) { } |
|
109 |
|
110 void open (const char *name, int mode = std::ios::out, int prot = 0) |
|
111 { hdf5_fstreambase::open (name, mode, prot); } |
|
112 }; |
|
113 |
4687
|
114 // Callback data structure for passing data to hdf5_read_next_data, below. |
|
115 |
|
116 struct |
|
117 hdf5_callback_data |
|
118 { |
|
119 hdf5_callback_data (void) |
|
120 : name (), global (false), tc (), doc () { } |
|
121 |
|
122 // the following fields are set by hdf5_read_data on successful return: |
|
123 |
|
124 // the name of the variable |
|
125 std::string name; |
|
126 |
|
127 // whether it is global |
|
128 bool global; |
|
129 |
|
130 // the value of the variable, in Octave form |
|
131 octave_value tc; |
|
132 |
|
133 // a documentation string (NULL if none) |
|
134 std::string doc; |
|
135 }; |
|
136 |
|
137 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
138 extern hid_t |
|
139 save_type_to_hdf5 (save_type st) |
|
140 #endif |
|
141 |
|
142 extern hid_t |
|
143 hdf5_make_complex_type (hid_t num_type); |
|
144 |
|
145 extern bool |
|
146 hdf5_types_compatible (hid_t t1, hid_t t2); |
|
147 |
|
148 extern herr_t |
|
149 hdf5_read_next_data (hid_t group_id, const char *name, void *dv); |
|
150 |
|
151 extern bool |
|
152 add_hdf5_data (hid_t loc_id, const octave_value& tc, |
|
153 const std::string& name, const std::string& doc, |
|
154 bool mark_as_global, bool save_as_floats); |
|
155 |
4634
|
156 extern std::string |
4687
|
157 read_hdf5_data (std::istream& is, const std::string& filename, bool& global, |
|
158 octave_value& tc, std::string& doc); |
4634
|
159 |
|
160 extern bool |
|
161 save_hdf5_data (std::ostream& os, const octave_value& tc, |
|
162 const std::string& name, const std::string& doc, |
|
163 bool mark_as_global, bool save_as_floats); |
|
164 |
|
165 #endif |
|
166 |
4695
|
167 #endif |
|
168 |
4634
|
169 /* |
|
170 ;;; Local Variables: *** |
|
171 ;;; mode: C++ *** |
|
172 ;;; End: *** |
|
173 */ |