OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
annot.cc
Go to the documentation of this file.
1 // This file is part of the hdf4 data handler for the OPeNDAP data server.
2 
3 // Copyright (c) 2005 OPeNDAP, Inc.
4 // Author: James Gallagher <jgallagher@opendap.org>
5 //
6 // This is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your
9 // option) any later version.
10 //
11 // This software is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this software; if not, write to the Free Software Foundation,
18 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
21 
23 // Copyright 1996, by the California Institute of Technology.
24 // ALL RIGHTS RESERVED. United States Government Sponsorship
25 // acknowledged. Any commercial use must be negotiated with the
26 // Office of Technology Transfer at the California Institute of
27 // Technology. This software may be subject to U.S. export control
28 // laws and regulations. By accepting this software, the user
29 // agrees to comply with all applicable U.S. export laws and
30 // regulations. User has the responsibility to obtain export
31 // licenses, or other export authority as may be required before
32 // exporting such information to foreign countries or providing
33 // access to foreign persons.
34 
35 // U.S. Government Sponsorship under NASA Contract
36 // NAS7-1260 is acknowledged.
37 //
38 // Author: Todd.K.Karakashian@jpl.nasa.gov
39 //
40 // $RCSfile: annot.cc,v $ - input stream class for HDF annotations
41 //
43 
44 #include "config_hdf.h"
45 
46 #include <mfhdf.h>
47 
48 #ifdef __POWERPC__
49 #undef isascii
50 #endif
51 
52 #include <string>
53 #include <vector>
54 
55 #include <hcerr.h>
56 #include <hcstream.h>
57 #include <hdfclass.h>
58 
59 
60 //
61 // protected member functions
62 //
63 
64 // initialize hdfistream_annot members
65 void hdfistream_annot::_init(const string filename)
66 {
67  _an_id = _index = _tag = _ref = 0;
68  _file_id = 0; // The fix for ticket 1360. jhrg 8/7/09
69  _lab = _desc = true;
70  _an_ids = vector < int32 > ();
71  _filename = filename;
72  return;
73 }
74 
75 // initialize hdfistream_annot members, setting _tag, _ref to an object
76 void hdfistream_annot::_init(const string filename, int32 tag, int32 ref)
77 {
78  _init(filename);
79  _tag = tag;
80  _ref = ref;
81  return;
82 }
83 
84 // open a file using the annotation interface and set _filename, file_id, _an_id
85 void hdfistream_annot::_open(const char *filename)
86 {
87  if (_file_id != 0)
88  close();
89  if ((_file_id = Hopen(filename, DFACC_READ, 0)) < 0)
91  if ((_an_id = ANstart(_file_id)) < 0)
93  _filename = filename;
94  return;
95 }
96 
97 // retrieve information about annotations
99 {
100  if (bos())
102  else
104 }
105 
106 
107 // retrieve information about the file annotations for current file
109 {
110  int32 nlab, ndesc, junk, junk2;
111  if (ANfileinfo(_an_id, &nlab, &ndesc, &junk, &junk2) == FAIL)
113 
114  int32 _ann_id;
115  _an_ids = vector < int32 > ();
116  int i;
117  for (i = 0; _lab && i < nlab; ++i) {
118  if ((_ann_id = ANselect(_an_id, i, AN_FILE_LABEL)) == FAIL)
120  _an_ids.push_back(_ann_id);
121  }
122  for (i = 0; _desc && i < ndesc; ++i) {
123  if ((_ann_id = ANselect(_an_id, i, AN_FILE_DESC)) == FAIL)
125  _an_ids.push_back(_ann_id);
126  }
127  return;
128 }
129 
130 // retrieve information about the annotations for currently pointed-at object
132 {
133  int nlab = 0, ndesc = 0;
134  if (_desc &&
135  (ndesc = ANnumann(_an_id, AN_DATA_DESC, _tag, _ref)) == FAIL)
137  if (_lab &&
138  (nlab = ANnumann(_an_id, AN_DATA_LABEL, _tag, _ref)) == FAIL)
140  if (nlab + ndesc > 0) {
141  int32 *annlist = new int32[nlab + ndesc];
142  if (annlist == 0)
144  if (_desc &&
145  ANannlist(_an_id, AN_DATA_DESC, _tag, _ref, annlist) == FAIL) {
146  delete[]annlist;
147  annlist = 0;
149  }
150  if (_lab &&
151  ANannlist(_an_id, AN_DATA_LABEL, _tag, _ref,
152  annlist + ndesc) == FAIL) {
153  delete[]annlist;
154  annlist = 0;
156  }
157  // import into _an_ids vector
158  // try { // add this when STL supports exceptions
159  _an_ids = vector < int32 > (annlist[0], annlist[nlab + ndesc]);
160  // }
161  // catch(...) {
162  // delete []annlist; annlist = 0;
163  // THROW(hcerr_annlist);
164  // }
165  delete[]annlist;
166  }
167  return;
168 }
169 
170 
171 //
172 // public member functions
173 //
174 
175 hdfistream_annot::hdfistream_annot(const string filename):
176 hdfistream_obj(filename)
177 {
178  _init(filename);
179  if (_filename.length() != 0)
180  open(_filename.c_str());
181  return;
182 }
183 
184 hdfistream_annot::hdfistream_annot(const string filename, int32 tag, int32 ref):
185 hdfistream_obj(filename)
186 {
187  _init(filename);
188  open(_filename.c_str(), tag, ref);
189  return;
190 }
191 
192 void hdfistream_annot::open(const char *filename)
193 {
194  _open(filename); // get _an_id for open file
195  _tag = _ref = 0; // set tag, ref for file annotations
196  _get_anninfo(); // retrieve annotation info
197  return;
198 }
199 
200 void hdfistream_annot::open(const char *filename, int32 tag, int32 ref)
201 {
202  _open(filename); // get _an_id for open file
203  _tag = tag; // set tag, ref for object
204  _ref = ref;
205  _get_anninfo(); // retrieve annotation info
206  return;
207 }
208 
210 {
211  if (_an_id > 0)
212  (void) ANend(_an_id);
213  if (_file_id > 0)
214  (void) Hclose(_file_id);
215  _init();
216  return;
217 }
218 
220 {
221 
222  // delete any previous data in an
223  an = string();
224 
225  if (_an_id == 0 || _index < 0)
227  if (eos()) // do nothing if positioned past end of stream
228  return *this;
229 
230  int32 _ann_id = _an_ids[_index];
231  int32 ann_length = ANannlen(_ann_id) ;
232  char buf[ann_length+1];
233  if (ANreadann(_ann_id, buf, ann_length+1) < 0)
235  buf[ann_length] = '\0';
236  an = buf;
237  seek_next();
238 
239  return *this;
240 }
241 
243 {
244  for (string an; !eos();) {
245  *this >> an;
246  anv.push_back(an);
247  }
248  return *this;
249 }
250 
251 
252 // $Log: annot.cc,v $
253 // Revision 1.5.8.1.2.1 2004/02/23 02:08:03 rmorris
254 // There is some incompatibility between the use of isascii() in the hdf library
255 // and its use on OS X. Here we force in the #undef of isascii in the osx case.
256 //
257 // Revision 1.5.8.1 2003/05/21 16:26:58 edavis
258 // Updated/corrected copyright statements.
259 //
260 // Revision 1.5 2000/10/09 19:46:19 jimg
261 // Moved the CVS Log entries to the end of each file.
262 // Added code to catch Error objects thrown by the dap library.
263 // Changed the read() method's definition to match the dap library.
264 //
265 // Revision 1.4 1999/05/06 03:23:33 jimg
266 // Merged changes from no-gnu branch
267 //
268 // Revision 1.3 1999/05/05 23:33:43 jimg
269 // String --> string conversion
270 //
271 // Revision 1.2.6.1 1999/05/06 00:35:44 jimg
272 // Jakes String --> string changes
273 //
274 // Revision 1.2 1998/09/10 21:57:10 jehamby
275 // Fix incorrect checking of HDF return values and other incorrect HDF calls.
276 //
277 // Revision 1.1 1996/10/31 18:42:55 jimg
278 // Added.
279 //
280 // Revision 1.3 1996/06/14 23:07:37 todd
281 // Fixed minor bug in operator>>(string)
282 //
283 // Revision 1.2 1996/05/23 18:15:08 todd
284 // Added copyright statement.
285 //
286 // Revision 1.1 1996/04/19 01:19:55 todd
287 // Initial revision
288 //
void _open(const char *filename)
Definition: annot.cc:85
virtual bool eos(void) const
Definition: hcstream.h:209
virtual void open(const char *filename)
Definition: annot.cc:192
vector< int32 > _an_ids
Definition: hcstream.h:242
hdfistream_annot(const string filename="")
Definition: annot.cc:175
int32 _file_id
Definition: hcstream.h:80
hdfistream_annot & operator>>(string &an)
Definition: annot.cc:219
void _get_anninfo(void)
Definition: annot.cc:98
virtual void seek_next(void)
Definition: hcstream.h:206
void _get_file_anninfo(void)
Definition: annot.cc:108
#define THROW(x)
Definition: dhdferr.h:51
string _filename
Definition: hcstream.h:79
virtual bool bos(void) const
Definition: hcstream.h:212
void _init(const string filename="")
Definition: annot.cc:65
virtual void close(void)
Definition: annot.cc:209
void _get_obj_anninfo(void)
Definition: annot.cc:131