bes Updated for version 3.20.13
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 }
57 THROW(hcerr_copystream);
58 }
59 virtual ~hdfistream_obj(void) {
60 }
61 void operator=(const hdfistream_obj &) {
62 THROW(hcerr_copystream);
63 }
64 virtual void open(const char *filename = 0) = 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.length())
76 _filename = filename;
77 _file_id = _index = 0;
78 }
79 string _filename;
80 int32 _file_id;
81 int _index;
82};
83
85
86 public:
87 explicit hdfistream_sds(const string filename = "");
89 THROW(hcerr_copystream);
90 }
91 virtual ~ hdfistream_sds(void) {
92 _del();
93 }
94 void operator=(const hdfistream_sds &) {
95 THROW(hcerr_copystream);
96 }
97 virtual void open(const char *filename = 0); // open stream, seek to BOS
98 virtual void close(void); // close stream
99 virtual void seek(int index = 0); // seek the index'th SDS array
100 virtual void seek(const char *name); // seek the SDS array by name
101 virtual void seek_next(void); // seek the next SDS array
102 virtual void seek_ref(int ref); // seek the SDS array by ref
103 virtual void rewind(void); // position in front of first SDS
104 virtual bool bos(void) const; // positioned in front of the first SDS?
105 virtual bool eos(void) const; // 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() {
123 return _map_ce_vec;
124 }
125 bool is_map_ce_set() {
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 int32 _sds_id; // handle of open object in annotation interface
155 int32 _attr_index; // index of current attribute
156 int32 _dim_index; // index of current dimension
157 int32 _rank; // number of dimensions in SDS
158 int32 _nattrs; // number of attributes for this SDS
159 int32 _nsds; // number of SDS's in stream
160 int32 _nfattrs; // number of file attributes in this SDS's file
161 bool _meta;
162 struct slab {
163 bool set;
164 bool reduce_rank;
165 int32 start[hdfclass::MAXDIMS];
166 int32 edge[hdfclass::MAXDIMS];
167 int32 stride[hdfclass::MAXDIMS];
168 } _slab;
169 // Since a SDS can hold a Grid, there may be several different
170 // constraints (because a client might constrain each of the fields
171 // differently and want a Structure object back). I've added a vector of
172 // array_ce objects to hold all this CE information so that the
173 // operator>> method will be able to access it at the correct time. This
174 // new object holds only the information about constraints on the Grid's
175 // map vectors. The _slab member will hold the constraint on the Grid's
176 // array. Note that in many cases the constraints on the maps can be
177 // derived from the array's constraints, but sometimes a client will only
178 // ask for the maps and thus the array's constraint will be the entire
179 // array (the `default constraint').
180 vector < array_ce > _map_ce_vec; // Added 2/5/2002 jhrg
181 bool _map_ce_set;
182};
183
184// class for a stream reading annotations
186 public:
187 hdfistream_annot(const string filename = "");
188 hdfistream_annot(const string filename, int32 tag, int32 ref);
190 THROW(hcerr_copystream);
191 }
192 virtual ~ hdfistream_annot(void) {
193 _del();
194 }
195 void operator=(const hdfistream_annot &) {
196 THROW(hcerr_copystream);
197 }
198 virtual void open(const char *filename); // open file annotations
199 virtual void open(const char *filename, int32 tag, int32 ref);
200 // open tag/ref in file, seek to BOS
201 virtual void close(void); // close open file
202 virtual void seek(int index) // seek to index'th annot in stream
203 {
204 _index = index;
205 }
206 virtual void seek_next(void) {
207 _index++;
208 } // position to next annot
209 virtual bool eos(void) const {
210 return (_index >= (int) _an_ids.size());
211 }
212 virtual bool bos(void) const {
213 return (_index <= 0);
214 }
215 virtual void rewind(void) {
216 _index = 0;
217 }
218 virtual void set_annot_type(bool label, bool desc) // specify types of
219 {
220 _lab = label;
221 _desc = desc;
222 } // annots to read
223 hdfistream_annot & operator>>(string & an); // read current annotation
224 hdfistream_annot & operator>>(vector < string > &anv); // read all annots
225 protected:
226 void _init(const string filename = "");
227 void _init(const string filename, int32 tag, int32 ref);
228 void _del(void) {
229 close();
230 }
231 void _rewind(void) {
232 _index = 0;
233 }
234 void _get_anninfo(void);
235 void _get_file_anninfo(void);
236 void _get_obj_anninfo(void);
237 void _open(const char *filename);
238 int32 _an_id; // handle of open annotation interface
239 int32 _tag, _ref; // tag, ref currently pointed to
240 bool _lab; // if true, read labels
241 bool _desc; // if true, read descriptions
242 vector < int32 > _an_ids; // list of id's of anns in stream
243};
244
246 public:
247 hdfistream_vdata(const string filename = "");
249 THROW(hcerr_copystream);
250 }
251 virtual ~hdfistream_vdata(void) {
252 _del();
253 }
254 void operator=(const hdfistream_vdata &) {
255 THROW(hcerr_copystream);
256 }
257 virtual void open(const char *filename); // open stream, seek to BOS
258 virtual void open(const string & filename); // open stream, seek to BOS
259 virtual void close(void); // close stream
260 virtual void seek(int index = 0); // seek the index'th Vdata
261 virtual void seek(const char *name); // seek the Vdata by name
262 virtual void seek(const string & name); // seek the Vdata by name
263 virtual void seek_next(void) {
264 _seek_next();
265 } // seek the next Vdata in file
266 virtual void seek_ref(int ref); // seek the Vdata by ref
267 virtual void rewind(void) {
268 _rewind();
269 } // position in front of first Vdata
270 virtual bool bos(void) const // positioned in front of the first Vdata?
271 {
272 return (_index <= 0);
273 }
274 virtual bool eos(void) const // positioned past the last Vdata?
275 {
276 return (_index >= (int) _vdata_refs.size());
277 }
278 virtual bool eo_attr(void) const; // positioned past the last attribute?
279 void setmeta(bool val) {
280 _meta = val;
281 } // set metadata loading
282 bool setrecs(int32 begin, int32 end);
283 hdfistream_vdata & operator>>(hdf_vdata & hs); // read a Vdata
284 hdfistream_vdata & operator>>(vector < hdf_vdata > &hsv); // read all Vdata's
285 hdfistream_vdata & operator>>(hdf_attr & ha); // read an attribute
286 hdfistream_vdata & operator>>(vector < hdf_attr > &hav); // read all attributes
287
288 // This function causes memory leakings. Use other ways to carry out its role.
289#if 0
290 virtual bool isInternalVdata(int ref) const; // check reference against internal type
291#endif
292 protected:
293 void _init(void);
294 void _del(void) {
295 close();
296 }
297 void _get_fileinfo(void); // get Vdata info for the current file
298 void _seek_next(void); // find the next Vdata in the stream
299 void _seek(const char *name); // find the Vdata w/ specified name
300 void _seek(int32 ref); // find the index'th Vdata in the stream
301 void _rewind(void) // position to beginning of the stream
302 {
303 _index = _attr_index = 0;
304 if (_vdata_refs.size() > 0)
305 _seek(_vdata_refs[0]);
306 }
307 int32 _vdata_id; // handle of open object in annotation interface
308 int32 _attr_index; // index of current attribute
309 int32 _nattrs; // number of attributes for this Vdata
310 hdfistream_vdata & operator>>(hdf_field & hf); // read a field
311 hdfistream_vdata & operator>>(vector < hdf_field > &hfv); // read all fields
312 bool _meta;
313 vector < int32 > _vdata_refs; // list of refs for Vdata's in the file
314 struct {
315 bool set;
316 int32 begin;
317 int32 end;
318 } _recs;
319};
320
322 public:
323 explicit hdfistream_vgroup(const string filename = "");
325 THROW(hcerr_copystream);
326 }
327 virtual ~hdfistream_vgroup(void) {
328 _del();
329 }
330 void operator=(const hdfistream_vgroup &) {
331 THROW(hcerr_copystream);
332 }
333 virtual void open(const char *filename); // open stream, seek to BOS
334 virtual void open(const string & filename); // open stream, seek to BOS
335 virtual void close(void); // close stream
336 virtual void seek(int index = 0); // seek the index'th Vgroup
337 virtual void seek(const char *name); // seek the Vgroup by name
338 virtual void seek(const string & name); // seek the Vgroup by name
339 virtual void seek_next(void) {
340 _seek_next();
341 } // seek the next Vgroup in file
342 virtual void seek_ref(int ref); // seek the Vgroup by ref
343 virtual void rewind(void) {
344 _rewind();
345 } // position in front of first Vgroup
346 string memberName(int32 ref); // find the name of ref'd Vgroup in the stream
347 virtual bool bos(void) const // positioned in front of the first Vgroup?
348 {
349 return (_index <= 0);
350 }
351 virtual bool eos(void) const // positioned past the last Vgroup?
352 {
353 return (_index >= (int) _vgroup_refs.size());
354 }
355 virtual bool eo_attr(void) const; // positioned past the last attribute?
356 void setmeta(bool val) {
357 _meta = val;
358 } // set metadata loading
359 hdfistream_vgroup & operator>>(hdf_vgroup & hs); // read a Vgroup
360 hdfistream_vgroup & operator>>(vector < hdf_vgroup > &hsv); // read all Vgroup's
361 hdfistream_vgroup & operator>>(hdf_attr & ha); // read an attribute
362 hdfistream_vgroup & operator>>(vector < hdf_attr > &hav); // read all attributes
363 protected:
364 void _init(void);
365 void _del(void) {
366 close();
367 }
368 void _get_fileinfo(void); // get Vgroup info for the current file
369 void _seek_next(void); // find the next Vgroup in the stream
370 void _seek(const char *name); // find the Vgroup w/ specified name
371 void _seek(int32 ref); // find the index'th Vgroup in the stream
372 void _rewind(void) // position to beginning of the stream
373 {
374 _index = _attr_index = 0;
375 if (_vgroup_refs.size() > 0)
376 _seek(_vgroup_refs[0]);
377 }
378 string _memberName(int32 ref); // find the name of ref'd Vgroup in the stream
379 int32 _vgroup_id; // handle of open object in annotation interface
380#if 0
381 int32 _member_id; // handle of child object in this Vgroup
382#endif
383 int32 _attr_index; // index of current attribute
384 int32 _nattrs; // number of attributes for this Vgroup
385 bool _meta;
386 vector < int32 > _vgroup_refs; // list of refs for Vgroup's in the file
387 struct {
388 bool set;
389 int32 begin;
390 int32 end;
391 } _recs;
392};
393
394// Raster input stream class
396 public:
397 hdfistream_gri(const string filename = "");
399 THROW(hcerr_copystream);
400 }
401 virtual ~ hdfistream_gri(void) {
402 _del();
403 }
404 void operator=(const hdfistream_gri &) {
405 THROW(hcerr_copystream);
406 }
407 virtual void open(const char *filename = 0); // open stream
408 virtual void close(void); // close stream
409 virtual void seek(int index = 0); // seek the index'th image
410 virtual void seek(const char *name); // seek image by name
411 virtual void seek_next(void) {
412 seek(_index + 1);
413 } // seek the next RI
414 virtual void seek_ref(int ref); // seek the RI by ref
415 virtual void rewind(void); // position in front of first RI
416 virtual bool bos(void) const; // position in front of first RI?
417 virtual bool eos(void) const; // position past last RI?
418 virtual bool eo_attr(void) const; // positioned past last attribute?
419 virtual bool eo_pal(void) const; // positioned past last palette?
420 void setmeta(bool val) {
421 _meta = val;
422 } // set metadata loading
423 void setslab(vector < int >start, vector < int >edge,
424 vector < int >stride, bool reduce_rank = false);
425 void unsetslab(void) {
426 _slab.set = _slab.reduce_rank = false;
427 }
428 void setinterlace(int32 interlace_mode); // set interlace type for next read
429 hdfistream_gri & operator>>(hdf_gri & hr); // read a single RI
430 hdfistream_gri & operator>>(vector < hdf_gri > &hrv); // read all RI's
431 hdfistream_gri & operator>>(hdf_attr & ha); // read an attribute
432 hdfistream_gri & operator>>(vector < hdf_attr > &hav); // read all attributes
433 hdfistream_gri & operator>>(hdf_palette & hp); // read a palette
434 hdfistream_gri & operator>>(vector < hdf_palette > &hpv); // read all palettes
435 protected:
436 void _init(void);
437 void _del(void) {
438 close();
439 }
440 void _get_fileinfo(void); // get image info for the current file
441 void _get_iminfo(void); // get info about the current RI
442 void _close_ri(void); // close the current RI
443 void _rewind(void) {
444 _index = -1;
445 _attr_index = _pal_index = 0;
446 }
447 int32 _gr_id; // GR interface id -> can't get rid of this, needed for GRend()
448 int32 _ri_id; // handle for open raster object
449 int32 _attr_index; // index to current attribute
450 int32 _pal_index; // index to current palette
451 int32 _nri; // number of rasters in the stream
452 int32 _nattrs; // number of attributes for this RI
453 int32 _nfattrs; // number of file attributes in this RI's file
454 int32 _npals; // number of palettes, set to one for now
455 int32 _interlace_mode; // interlace mode for reading images
456 bool _meta; // metadata only
457 struct {
458 bool set;
459 bool reduce_rank;
460 int32 start[2];
461 int32 edge[2];
462 int32 stride[2];
463 } _slab;
464}; /* Note: multiple palettes is not supported in the current HDF 4.0 GR API */
465
466#endif // ifndef _HCSTREAM_H
467