bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
hcstream.h
1#ifndef _HCSTREAM_H
2#define _HCSTREAM_H
3
5// This file is part of the hdf4 data handler for the OPeNDAP data server.
6
7// Copyright (c) 2005 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This is free software; you can redistribute it and/or modify it under the
11// terms of the GNU Lesser General Public License as published by the Free
12// Software Foundation; either version 2.1 of the License, or (at your
13// option) any later version.
14//
15// This software is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18// License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public License
21// along with this software; if not, write to the Free Software Foundation,
22// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26// Copyright 1996, by the California Institute of Technology.
27// ALL RIGHTS RESERVED. United States Government Sponsorship
28// acknowledged. Any commercial use must be negotiated with the
29// Office of Technology Transfer at the California Institute of
30// Technology. This software may be subject to U.S. export control
31// laws and regulations. By accepting this software, the user
32// agrees to comply with all applicable U.S. export laws and
33// regulations. User has the responsibility to obtain export
34// licenses, or other export authority as may be required before
35// exporting such information to foreign countries or providing
36// access to foreign persons.
37
38// U.S. Government Sponsorship under NASA Contract
39// NAS7-1260 is acknowledged.
40//
41// Author: Todd.K.Karakashian@jpl.nasa.gov
42//
44
45#include <string>
46#include <vector>
47
48#include <hcerr.h>
49#include <hdfclass.h>
50
51class hdfistream_obj { // base class for streams reading HDF objects
52 public:
53 explicit hdfistream_obj(const string filename = "") {
54 _init(filename);
55 }
56 hdfistream_obj(const hdfistream_obj &) {
57 THROW(hcerr_copystream);
58 }
59 virtual ~hdfistream_obj(void) = default;
60
61 void operator=(const hdfistream_obj &) {
62 THROW(hcerr_copystream);
63 }
64 virtual void open(const char *filename = nullptr) = 0; // open stream
65 virtual void close(void) = 0; // close stream
66 virtual void seek(int index = 0) = 0; // seek to index'th object
67 virtual void seek_next(void) = 0; // seek to next object in stream
68 virtual void rewind(void) = 0; // rewind to beginning of stream
69 virtual bool bos(void) const = 0; // are we at beginning of stream?
70 virtual bool eos(void) const = 0; // are we at end of stream?
71 virtual int index(void) const {
72 return _index;
73 } // return current position protected:
74 void _init(const string& filename = "") {
75 if (filename.size())
76 _filename = filename;
77 _file_id = _index = 0;
78 }
79 string _filename;
80 int32 _file_id;
81 int _index;
82};
83
84class hdfistream_sds:public hdfistream_obj {
85
86 public:
87 explicit hdfistream_sds(const string& filename = "");
88 hdfistream_sds(const hdfistream_sds &):hdfistream_obj(*this) {
89 THROW(hcerr_copystream);
90 }
91 ~ hdfistream_sds(void) override{
92 _del();
93 }
94 void operator=(const hdfistream_sds &) {
95 THROW(hcerr_copystream);
96 }
97 void open(const char *filename = nullptr) override; // open stream, seek to BOS
98 void close(void) override; // close stream
99 void seek(int index = 0) override; // seek the index'th SDS array
100 virtual void seek(const char *name); // seek the SDS array by name
101 void seek_next(void) override; // seek the next SDS array
102 virtual void seek_ref(int ref); // seek the SDS array by ref
103 void rewind(void) override; // position in front of first SDS
104 bool bos(void) const override; // positioned in front of the first SDS?
105 bool eos(void) const override; // positioned past the last SDS?
106 virtual bool eo_attr(void) const; // positioned past the last attribute?
107 virtual bool eo_dim(void) const; // positioned past the last dimension?
108 void setmeta(bool val) {
109 _meta = val;
110 } // set metadata loading
111 void setslab(vector < int >start, vector < int >edge,
112 vector < int >stride, bool reduce_rank = false);
113 void setslab(int *start, int *edge, int *stride, bool reduce_rank =
114 false);
115 void unsetslab(void) {
116 _slab.set = _slab.reduce_rank = false;
117 }
118 void set_map_ce(const vector < array_ce > &a_ce) {
119 _map_ce_set = true;
120 _map_ce_vec = a_ce;
121 }
122 vector < array_ce > get_map_ce() const {
123 return _map_ce_vec;
124 }
125 bool is_map_ce_set() const {
126 return _map_ce_set;
127 }
128 hdfistream_sds & operator>>(hdf_attr & ha); // read an attribute
129 hdfistream_sds & operator>>(vector < hdf_attr > &hav); // read all atributes
130 hdfistream_sds & operator>>(hdf_sds & hs); // read an SDS
131 hdfistream_sds & operator>>(vector < hdf_sds > &hsv); // read all SDS's
132 hdfistream_sds & operator>>(hdf_dim & hd); // read a dimension
133 hdfistream_sds & operator>>(vector < hdf_dim > &hdv); // read all dims
134 protected:
135 void _init(void);
136 void _del(void) {
137 close();
138 }
139 void _get_fileinfo(void); // get SDS info for the current file
140 void _get_sdsinfo(void); // get info about the current SDS
141 void _close_sds(void); // close the open SDS
142 void _seek_next_arr(void); // find the next SDS array in the stream
143 void _seek_arr(int index); // find the index'th SDS array in the stream
144 void _seek_arr(const string & name); // find the SDS array w/ specified name
145 void _seek_arr_ref(int ref); // find the SDS array in the stream by ref
146 void _rewind(void) {
147 _index = -1;
148 _attr_index = _dim_index = 0;
149 }
150 // position to the beginning of the stream
151 static const string long_name; // label
152 static const string units;
153 static const string format;
154 private:
155 int32 _sds_id; // handle of open object in annotation interface
156 int32 _attr_index; // index of current attribute
157 int32 _dim_index; // index of current dimension
158 int32 _rank; // number of dimensions in SDS
159 int32 _nattrs; // number of attributes for this SDS
160 int32 _nsds; // number of SDS's in stream
161 int32 _nfattrs; // number of file attributes in this SDS's file
162 bool _meta;
163 struct slab {
164 bool set;
165 bool reduce_rank;
166 int32 start[hdfclass::MAXDIMS];
167 int32 edge[hdfclass::MAXDIMS];
168 int32 stride[hdfclass::MAXDIMS];
169 } _slab;
170 // Since a SDS can hold a Grid, there may be several different
171 // constraints (because a client might constrain each of the fields
172 // differently and want a Structure object back). I've added a vector of
173 // array_ce objects to hold all this CE information so that the
174 // operator>> method will be able to access it at the correct time. This
175 // new object holds only the information about constraints on the Grid's
176 // map vectors. The _slab member will hold the constraint on the Grid's
177 // array. Note that in many cases the constraints on the maps can be
178 // derived from the array's constraints, but sometimes a client will only
179 // ask for the maps and thus the array's constraint will be the entire
180 // array (the `default constraint').
181 vector < array_ce > _map_ce_vec; // Added 2/5/2002 jhrg
182 bool _map_ce_set;
183};
184
185// class for a stream reading annotations
186class hdfistream_annot:public hdfistream_obj {
187 public:
188 explicit hdfistream_annot(const string& filename);
189 hdfistream_annot(const string& filename, int32 tag, int32 ref);
190 hdfistream_annot(const hdfistream_annot &):hdfistream_obj(*this) {
191 THROW(hcerr_copystream);
192 }
193 ~ hdfistream_annot(void) override {
194 _del();
195 }
196 void operator=(const hdfistream_annot &) {
197 THROW(hcerr_copystream);
198 }
199 void open(const char *filename) override; // open file annotations
200 virtual void open(const char *filename, int32 tag, int32 ref);
201 // open tag/ref in file, seek to BOS
202 void close(void) override; // close open file
203 void seek(int index) override // seek to index'th annot in stream
204 {
205 _index = index;
206 }
207 void seek_next(void) override {
208 _index++;
209 } // position to next annot
210 bool eos(void) const override{
211 return (_index >= (int) _an_ids.size());
212 }
213 bool bos(void) const override {
214 return (_index <= 0);
215 }
216 void rewind(void) override {
217 _index = 0;
218 }
219 virtual void set_annot_type(bool label, bool desc) // specify types of
220 {
221 _lab = label;
222 _desc = desc;
223 } // annots to read
224 hdfistream_annot & operator>>(string & an); // read current annotation
225 hdfistream_annot & operator>>(vector < string > &anv); // read all annots
226 protected:
227 void _init(const string& filename = "");
228 void _init(const string& filename, int32 tag, int32 ref);
229 void _del(void) {
230 close();
231 }
232 void _rewind(void) {
233 _index = 0;
234 }
235 void _get_anninfo(void);
236 void _get_file_anninfo(void);
237 void _get_obj_anninfo(void);
238 void _open(const char *filename);
239
240private:
241 int32 _an_id; // handle of open annotation interface
242 int32 _tag;
243 int32 _ref; // tag, ref currently pointed to
244 bool _lab; // if true, read labels
245 bool _desc; // if true, read descriptions
246 vector < int32 > _an_ids; // list of id's of anns in stream
247};
248
249class hdfistream_vdata:public hdfistream_obj {
250 public:
251 explicit hdfistream_vdata(const string& filename = "");
252 hdfistream_vdata(const hdfistream_vdata &) : hdfistream_obj(*this) {
253 THROW(hcerr_copystream);
254 }
255 ~hdfistream_vdata(void) override{
256 _del();
257 }
258 void operator=(const hdfistream_vdata &) {
259 THROW(hcerr_copystream);
260 }
261 void open(const char *filename) override; // open stream, seek to BOS
262 virtual void open(const string & filename); // open stream, seek to BOS
263 void close(void) override; // close stream
264 void seek(int index = 0) override; // seek the index'th Vdata
265 virtual void seek(const char *name); // seek the Vdata by name
266 virtual void seek(const string & name); // seek the Vdata by name
267 void seek_next(void) override {
268 _seek_next();
269 } // seek the next Vdata in file
270 virtual void seek_ref(int ref); // seek the Vdata by ref
271 void rewind(void) override {
272 _rewind();
273 } // position in front of first Vdata
274 bool bos(void) const override // positioned in front of the first Vdata?
275 {
276 return (_index <= 0);
277 }
278 bool eos(void) const override // positioned past the last Vdata?
279 {
280 return (_index >= (int) _vdata_refs.size());
281 }
282 virtual bool eo_attr(void) const; // positioned past the last attribute?
283 void setmeta(bool val) {
284 _meta = val;
285 } // set metadata loading
286 bool setrecs(int32 begin, int32 end);
287 hdfistream_vdata & operator>>(hdf_vdata & hs); // read a Vdata
288 hdfistream_vdata & operator>>(vector < hdf_vdata > &hsv); // read all Vdata's
289 hdfistream_vdata & operator>>(hdf_attr & ha); // read an attribute
290 hdfistream_vdata & operator>>(vector < hdf_attr > &hav); // read all attributes
291
292 // This function causes memory leakings. Use other ways to carry out its role.
293#if 0
294 virtual bool isInternalVdata(int ref) const; // check reference against internal type
295#endif
296 protected:
297 void _init(void);
298 void _del(void) {
299 close();
300 }
301 void _get_fileinfo(void); // get Vdata info for the current file
302 void _seek_next(void); // find the next Vdata in the stream
303 void _seek(const char *name); // find the Vdata w/ specified name
304 void _seek(int32 ref); // find the index'th Vdata in the stream
305 void _rewind(void) // position to beginning of the stream
306 {
307 _index = _attr_index = 0;
308 if (_vdata_refs.empty() == false)
309 _seek(_vdata_refs[0]);
310 }
311
312 hdfistream_vdata & operator>>(hdf_field & hf); // read a field
313 hdfistream_vdata & operator>>(vector < hdf_field > &hfv); // read all fields
314
315private:
316 int32 _vdata_id; // handle of open object in annotation interface
317 int32 _attr_index; // index of current attribute
318 int32 _nattrs; // number of attributes for this Vdata
319 bool _meta;
320 vector < int32 > _vdata_refs; // list of refs for Vdata's in the file
321 struct {
322 bool set;
323 int32 begin;
324 int32 end;
325 } _recs;
326};
327
328class hdfistream_vgroup:public hdfistream_obj {
329 public:
330 explicit hdfistream_vgroup(const string & filename = "");
331 hdfistream_vgroup(const hdfistream_vgroup &):hdfistream_obj(*this) {
332 THROW(hcerr_copystream);
333 }
334 ~hdfistream_vgroup(void) override {
335 _del();
336 }
337 void operator=(const hdfistream_vgroup &) {
338 THROW(hcerr_copystream);
339 }
340 void open(const char *filename) override; // open stream, seek to BOS
341 virtual void open(const string & filename); // open stream, seek to BOS
342 void close(void) override; // close stream
343 void seek(int index = 0) override; // seek the index'th Vgroup
344 virtual void seek(const char *name); // seek the Vgroup by name
345 virtual void seek(const string & name); // seek the Vgroup by name
346 void seek_next(void) override {
347 _seek_next();
348 } // seek the next Vgroup in file
349 virtual void seek_ref(int ref); // seek the Vgroup by ref
350 void rewind(void) override {
351 _rewind();
352 } // position in front of first Vgroup
353 string memberName(int32 ref) const; // find the name of ref'd Vgroup in the stream
354 bool bos(void) const override // positioned in front of the first Vgroup?
355 {
356 return (_index <= 0);
357 }
358
359 bool eos(void) const override // positioned past the last Vgroup?
360 {
361 return (_index >= (int) _vgroup_refs.size());
362 }
363 virtual bool eo_attr(void) const; // positioned past the last attribute?
364 void setmeta(bool val) {
365 _meta = val;
366 } // set metadata loading
367 hdfistream_vgroup & operator>>(hdf_vgroup & hs); // read a Vgroup
368 hdfistream_vgroup & operator>>(vector < hdf_vgroup > &hsv); // read all Vgroup's
369 hdfistream_vgroup & operator>>(hdf_attr & ha); // read an attribute
370 hdfistream_vgroup & operator>>(vector < hdf_attr > &hav); // read all attributes
371 protected:
372 void _init(void);
373 void _del(void) {
374 close();
375 }
376 void _get_fileinfo(void); // get Vgroup info for the current file
377 void _seek_next(void); // find the next Vgroup in the stream
378 void _seek(const char *name); // find the Vgroup w/ specified name
379 void _seek(int32 ref); // find the index'th Vgroup in the stream
380 void _rewind(void) // position to beginning of the stream
381 {
382 _index = _attr_index = 0;
383 if (_vgroup_refs.empty() == false)
384 _seek(_vgroup_refs[0]);
385 }
386 string _memberName(int32 ref) const; // find the name of ref'd Vgroup in the stream
387
388private:
389 int32 _vgroup_id; // handle of open object in annotation interface
390#if 0
391 int32 _member_id; // handle of child object in this Vgroup
392#endif
393 int32 _attr_index; // index of current attribute
394 int32 _nattrs; // number of attributes for this Vgroup
395 bool _meta;
396 vector < int32 > _vgroup_refs; // list of refs for Vgroup's in the file
397 struct {
398 bool set;
399 int32 begin;
400 int32 end;
401 } _recs;
402};
403
404// Raster input stream class
405class hdfistream_gri:public hdfistream_obj {
406 public:
407 explicit hdfistream_gri(const string& filename = "");
408 hdfistream_gri(const hdfistream_gri &):hdfistream_obj(*this) {
409 THROW(hcerr_copystream);
410 }
411 ~ hdfistream_gri(void) override {
412 _del();
413 }
414 void operator=(const hdfistream_gri &) {
415 THROW(hcerr_copystream);
416 }
417 void open(const char *filename = nullptr) override; // open stream
418 void close(void) override; // close stream
419 void seek(int index = 0) override; // seek the index'th image
420 virtual void seek(const char *name); // seek image by name
421 void seek_next(void) override {
422 seek(_index + 1);
423 } // seek the next RI
424 virtual void seek_ref(int ref); // seek the RI by ref
425 void rewind(void) override; // position in front of first RI
426 bool bos(void) const override; // position in front of first RI?
427 bool eos(void) const override; // position past last RI?
428 virtual bool eo_attr(void) const; // positioned past last attribute?
429 virtual bool eo_pal(void) const; // positioned past last palette?
430 void setmeta(bool val) {
431 _meta = val;
432 } // set metadata loading
433 void setslab(vector < int >start, vector < int >edge,
434 vector < int >stride, bool reduce_rank = false);
435 void unsetslab(void) {
436 _slab.set = _slab.reduce_rank = false;
437 }
438 void setinterlace(int32 interlace_mode); // set interlace type for next read
439 hdfistream_gri & operator>>(hdf_gri & hr); // read a single RI
440 hdfistream_gri & operator>>(vector < hdf_gri > &hrv); // read all RI's
441 hdfistream_gri & operator>>(hdf_attr & ha); // read an attribute
442 hdfistream_gri & operator>>(vector < hdf_attr > &hav); // read all attributes
443 hdfistream_gri & operator>>(hdf_palette & hp); // read a palette
444 hdfistream_gri & operator>>(vector < hdf_palette > &hpv); // read all palettes
445 protected:
446 void _init(void);
447 void _del(void) {
448 close();
449 }
450 void _get_fileinfo(void); // get image info for the current file
451 void _get_iminfo(void); // get info about the current RI
452 void _close_ri(void); // close the current RI
453 void _rewind(void) {
454 _index = -1;
455 _attr_index = _pal_index = 0;
456 }
457 private:
458 int32 _gr_id; // GR interface id -> can't get rid of this, needed for GRend()
459 int32 _ri_id; // handle for open raster object
460 int32 _attr_index; // index to current attribute
461 int32 _pal_index; // index to current palette
462 int32 _nri; // number of rasters in the stream
463 int32 _nattrs; // number of attributes for this RI
464 int32 _nfattrs; // number of file attributes in this RI's file
465 int32 _npals; // number of palettes, set to one for now
466 int32 _interlace_mode; // interlace mode for reading images
467 bool _meta; // metadata only
468 struct {
469 bool set;
470 bool reduce_rank;
471 int32 start[2];
472 int32 edge[2];
473 int32 stride[2];
474 } _slab;
475}; /* Note: multiple palettes is not supported in the current HDF 4.0 GR API */
476
477#endif // ifndef _HCSTREAM_H
478
STL class.