bes  Updated for version 3.20.6
annot.cc
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)
90  THROW(hcerr_openfile);
91  if ((_an_id = ANstart(_file_id)) < 0)
92  THROW(hcerr_anninit);
93  _filename = filename;
94  return;
95 }
96 
97 // retrieve information about annotations
98 void hdfistream_annot::_get_anninfo(void)
99 {
100  if (bos())
101  _get_file_anninfo();
102  else
103  _get_obj_anninfo();
104 }
105 
106 
107 // retrieve information about the file annotations for current file
108 void hdfistream_annot::_get_file_anninfo(void)
109 {
110  int32 nlab;
111  int32 ndesc;
112  int32 junk;
113  int32 junk2;
114  if (ANfileinfo(_an_id, &nlab, &ndesc, &junk, &junk2) == FAIL)
115  THROW(hcerr_anninfo);
116 
117  int32 _ann_id;
118  _an_ids = vector < int32 > ();
119  int i;
120  for (i = 0; _lab && i < nlab; ++i) {
121  if ((_ann_id = ANselect(_an_id, i, AN_FILE_LABEL)) == FAIL)
122  THROW(hcerr_anninfo);
123  _an_ids.push_back(_ann_id);
124  }
125  for (i = 0; _desc && i < ndesc; ++i) {
126  if ((_ann_id = ANselect(_an_id, i, AN_FILE_DESC)) == FAIL)
127  THROW(hcerr_anninfo);
128  _an_ids.push_back(_ann_id);
129  }
130  return;
131 }
132 
133 // retrieve information about the annotations for currently pointed-at object
134 void hdfistream_annot::_get_obj_anninfo(void)
135 {
136  int nlab = 0, ndesc = 0;
137  if (_desc &&
138  (ndesc = ANnumann(_an_id, AN_DATA_DESC, _tag, _ref)) == FAIL)
139  THROW(hcerr_anninfo);
140  if (_lab &&
141  (nlab = ANnumann(_an_id, AN_DATA_LABEL, _tag, _ref)) == FAIL)
142  THROW(hcerr_anninfo);
143  if (nlab + ndesc > 0) {
144  int32 *annlist = new int32[nlab + ndesc];
145  if (annlist == 0)
146  THROW(hcerr_annlist);
147  if (_desc &&
148  ANannlist(_an_id, AN_DATA_DESC, _tag, _ref, annlist) == FAIL) {
149  delete[]annlist;
150  annlist = 0;
151  THROW(hcerr_annlist);
152  }
153  if (_lab &&
154  ANannlist(_an_id, AN_DATA_LABEL, _tag, _ref,
155  annlist + ndesc) == FAIL) {
156  delete[]annlist;
157  annlist = 0;
158  THROW(hcerr_annlist);
159  }
160  // import into _an_ids vector
161  // try { // add this when STL supports exceptions
162  _an_ids = vector < int32 > (annlist[0], annlist[nlab + ndesc]);
163  // }
164  // catch(...) {
165  // delete []annlist; annlist = 0;
166  // THROW(hcerr_annlist);
167  // }
168  delete[]annlist;
169  }
170  return;
171 }
172 
173 
174 //
175 // public member functions
176 //
177 
178 hdfistream_annot::hdfistream_annot(const string filename):
179 hdfistream_obj(filename)
180 {
181  _init(filename);
182  if (_filename.length() != 0)
183  open(_filename.c_str());
184  return;
185 }
186 
187 hdfistream_annot::hdfistream_annot(const string filename, int32 tag, int32 ref):
188 hdfistream_obj(filename)
189 {
190  _init(filename);
191  open(_filename.c_str(), tag, ref);
192  return;
193 }
194 
195 void hdfistream_annot::open(const char *filename)
196 {
197  _open(filename); // get _an_id for open file
198  _tag = _ref = 0; // set tag, ref for file annotations
199  _get_anninfo(); // retrieve annotation info
200  return;
201 }
202 
203 void hdfistream_annot::open(const char *filename, int32 tag, int32 ref)
204 {
205  _open(filename); // get _an_id for open file
206  _tag = tag; // set tag, ref for object
207  _ref = ref;
208  _get_anninfo(); // retrieve annotation info
209  return;
210 }
211 
212 void hdfistream_annot::close(void)
213 {
214  if (_an_id > 0)
215  (void) ANend(_an_id);
216  if (_file_id > 0)
217  (void) Hclose(_file_id);
218  _init();
219  return;
220 }
221 
222 hdfistream_annot & hdfistream_annot::operator>>(string & an)
223 {
224 
225  // delete any previous data in an
226  an = string();
227 
228  if (_an_id == 0 || _index < 0)
229  THROW(hcerr_invstream);
230  if (eos()) // do nothing if positioned past end of stream
231  return *this;
232 
233  int32 _ann_id = _an_ids[_index];
234  int32 ann_length = ANannlen(_ann_id) ;
235  char buf[ann_length+1];
236  if (ANreadann(_ann_id, buf, ann_length+1) < 0)
237  THROW(hcerr_annread);
238  buf[ann_length] = '\0';
239  an = buf;
240  seek_next();
241 
242  return *this;
243 }
244 
245 hdfistream_annot & hdfistream_annot::operator>>(vector < string > &anv)
246 {
247  for (string an; !eos();) {
248  *this >> an;
249  anv.push_back(an);
250  }
251  return *this;
252 }
253 
254 
255 // $Log: annot.cc,v $
256 // Revision 1.5.8.1.2.1 2004/02/23 02:08:03 rmorris
257 // There is some incompatibility between the use of isascii() in the hdf library
258 // and its use on OS X. Here we force in the #undef of isascii in the osx case.
259 //
260 // Revision 1.5.8.1 2003/05/21 16:26:58 edavis
261 // Updated/corrected copyright statements.
262 //
263 // Revision 1.5 2000/10/09 19:46:19 jimg
264 // Moved the CVS Log entries to the end of each file.
265 // Added code to catch Error objects thrown by the dap library.
266 // Changed the read() method's definition to match the dap library.
267 //
268 // Revision 1.4 1999/05/06 03:23:33 jimg
269 // Merged changes from no-gnu branch
270 //
271 // Revision 1.3 1999/05/05 23:33:43 jimg
272 // String --> string conversion
273 //
274 // Revision 1.2.6.1 1999/05/06 00:35:44 jimg
275 // Jakes String --> string changes
276 //
277 // Revision 1.2 1998/09/10 21:57:10 jehamby
278 // Fix incorrect checking of HDF return values and other incorrect HDF calls.
279 //
280 // Revision 1.1 1996/10/31 18:42:55 jimg
281 // Added.
282 //
283 // Revision 1.3 1996/06/14 23:07:37 todd
284 // Fixed minor bug in operator>>(string)
285 //
286 // Revision 1.2 1996/05/23 18:15:08 todd
287 // Added copyright statement.
288 //
289 // Revision 1.1 1996/04/19 01:19:55 todd
290 // Initial revision
291 //
hcerr_annlist
Definition: hcerr.h:153
hcerr_invstream
Definition: hcerr.h:111
hcerr_anninfo
Definition: hcerr.h:146
hdfistream_obj
Definition: hcstream.h:51
hcerr_openfile
Definition: hcerr.h:125
hdfistream_annot
Definition: hcstream.h:185
hcerr_annread
Definition: hcerr.h:160
hcerr_anninit
Definition: hcerr.h:139