1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2003-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ls_hdf5_h)
27 #define octave_ls_hdf5_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 
33 #include "oct-hdf5-types.h"
34 #include "ov.h"
35 
36 // first, we need to define our own dummy stream subclass, since
37 // HDF5 needs to do its own file i/o
38 
39 // hdf5_fstreambase is used for both input and output streams, modeled
40 // on the fstreambase class in <fstream.h>
41 
42 class hdf5_fstreambase : virtual public std::ios
43 {
44 public:
45 
46   // HDF5 uses an "id" to refer to an open file
47   octave_hdf5_id file_id;
48 
49   // keep track of current item index in the file
50   int current_item;
51 
hdf5_fstreambase()52   hdf5_fstreambase () : file_id (-1), current_item () { }
53 
~hdf5_fstreambase()54   ~hdf5_fstreambase () { close (); }
55 
56   hdf5_fstreambase (const char *name, int mode, int /* prot */ = 0);
57 
58   void close (void);
59 
60   void open (const char *name, int mode, int);
61 
62   void open_create (const char *name, int mode);
63 };
64 
65 // input and output streams, subclassing istream and ostream
66 // so that we can pass them for stream parameters in the functions below.
67 
68 class hdf5_ifstream : public hdf5_fstreambase, public std::istream
69 {
70 public:
71 
hdf5_ifstream()72   hdf5_ifstream () : hdf5_fstreambase (), std::istream (nullptr) { }
73 
74   hdf5_ifstream (const char *name, int mode = std::ios::in | std::ios::binary,
75                  int prot = 0)
hdf5_fstreambase(name,mode,prot)76     : hdf5_fstreambase (name, mode, prot), std::istream (nullptr) { }
77 
78   void open (const char *name, int mode = std::ios::in | std::ios::binary,
79              int prot = 0)
80   { hdf5_fstreambase::open (name, mode, prot); }
81 };
82 
83 class hdf5_ofstream : public hdf5_fstreambase, public std::ostream
84 {
85 public:
86 
hdf5_ofstream()87   hdf5_ofstream () : hdf5_fstreambase (), std::ostream (nullptr) { }
88 
89   hdf5_ofstream (const char *name, int mode = std::ios::out | std::ios::binary,
90                  int prot = 0)
hdf5_fstreambase(name,mode,prot)91     : hdf5_fstreambase (name, mode, prot), std::ostream (nullptr) { }
92 
93   void open (const char *name, int mode = std::ios::out | std::ios::binary,
94              int prot = 0)
95   { hdf5_fstreambase::open (name, mode, prot); }
96 };
97 
98 // Callback data structure for passing data to hdf5_read_next_data, below.
99 
100 struct hdf5_callback_data
101 {
hdf5_callback_datahdf5_callback_data102   hdf5_callback_data (void)
103     : name (), global (false), tc (), doc () { }
104 
105   // the following fields are set by hdf5_read_data on successful return:
106 
107   // the name of the variable
108   std::string name;
109 
110   // whether it is global
111   bool global;
112 
113   // the value of the variable, in Octave form
114   octave_value tc;
115 
116   // a documentation string (NULL if none)
117   std::string doc;
118 };
119 
120 extern OCTINTERP_API octave_hdf5_id
121 save_type_to_hdf5 (save_type st);
122 
123 extern OCTINTERP_API octave_hdf5_id
124 hdf5_make_complex_type (octave_hdf5_id num_type);
125 
126 extern OCTINTERP_API bool
127 hdf5_types_compatible (octave_hdf5_id t1, octave_hdf5_id t2);
128 
129 extern OCTINTERP_API octave_hdf5_err
130 hdf5_read_next_data (octave_hdf5_id group_id, const char *name, void *dv);
131 
132 extern OCTINTERP_API octave_hdf5_err
133 hdf5_h5g_iterate (octave_hdf5_id loc_id, const char *name, int *idx,
134                   void *operator_data);
135 
136 extern OCTINTERP_API bool
137 add_hdf5_data (octave_hdf5_id loc_id, const octave_value& tc,
138                const std::string& name, const std::string& doc,
139                bool mark_global, bool save_as_floats);
140 
141 extern OCTINTERP_API int
142 save_hdf5_empty (octave_hdf5_id loc_id, const char *name, const dim_vector& d);
143 
144 extern OCTINTERP_API int
145 load_hdf5_empty (octave_hdf5_id loc_id, const char *name, dim_vector& d);
146 
147 extern OCTINTERP_API std::string
148 read_hdf5_data (std::istream& is,  const std::string& filename, bool& global,
149                 octave_value& tc, std::string& doc,
150                 const string_vector& argv, int argv_idx, int argc);
151 
152 extern OCTINTERP_API bool
153 save_hdf5_data (std::ostream& os, const octave_value& tc,
154                 const std::string& name, const std::string& doc,
155                 bool mark_global, bool save_as_floats);
156 
157 extern OCTINTERP_API bool
158 hdf5_check_attr (octave_hdf5_id loc_id, const char *attr_name);
159 
160 extern OCTINTERP_API bool
161 hdf5_get_scalar_attr (octave_hdf5_id loc_id, octave_hdf5_id type_id,
162                       const char *attr_name, void *buf);
163 
164 extern OCTINTERP_API octave_hdf5_err
165 hdf5_add_attr (octave_hdf5_id loc_id, const char *attr_name);
166 
167 
168 extern OCTINTERP_API octave_hdf5_err
169 hdf5_add_scalar_attr (octave_hdf5_id loc_id, octave_hdf5_id type_id,
170                       const char *attr_name, void *buf);
171 
172 #endif
173