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