OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
genvec.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: genvec.cc,v $ - implementation of HDF generic vector class
41 //
43 
44 #include "config_hdf.h"
45 
46 #include <mfhdf.h>
47 
48 #ifdef __POWERPC__
49 #undef isascii
50 #endif
51 #include <sstream>
52 #include <string>
53 #include <vector>
54 
55 #include <InternalErr.h>
56 
57 #include <hcerr.h>
58 #include <hdfclass.h>
59 
60 using namespace std;
61 
62 // Convert an array of U with length nelts into an array of T by casting each
63 // element of array to a T.
64 template < class T, class U >
65  void ConvertArrayByCast(U * array, int nelts, T ** carray)
66 {
67  if (nelts == 0) {
68  *carray = 0;
69  return;
70  }
71  *carray = new T[nelts];
72  if (*carray == 0) // Harmless but should never be used.
74  for (int i = 0; i < nelts; ++i) {
75  *(*carray + i) = static_cast < T > (*(array + i));
76  }
77 }
78 
79 //
80 // protected member functions
81 //
82 
83 // Initialize an hdf_genvec from an input C array. If data, begin, end,
84 // stride are zero, a zero-length hdf_genvec of the specified type will be
85 // initialized.
86 void hdf_genvec::_init(int32 nt, void *data, int begin, int end,
87  int stride)
88 {
89 
90  // input checking: nt must be a valid HDF number type;
91  // data, nelts can optionally both together be 0.
92  int32 eltsize; // number of bytes per element
93  if ((eltsize = DFKNTsize(nt)) <= 0)
94  THROW(hcerr_dftype); // invalid number type
95  bool zerovec = (data == 0 && begin == 0 && end == 0 && stride == 0);
96  if (zerovec) { // if this is a zero-length vector
97  _nelts = 0;
98  _data = 0;
99  } else {
100  if (begin < 0 || end < 0 || stride <= 0 || end < begin)
101  THROW(hcerr_range); // invalid range given for subset of data
102  if (data == 0)
103  THROW(hcerr_invarr); // if specify a range, need a data array!
104 
105  // allocate memory for _data and assign _nt, _nelts
106  int nelts = (int) ((end - begin) / stride + 1);
107  _data = new char[nelts * eltsize]; // allocate memory
108  if (_data == 0)
110  if (stride == 1) // copy data directly
111  (void) memcpy(_data, (void *) ((char *) data + begin),
112  eltsize * nelts);
113  else {
114  for (int i = 0, j = begin; i < nelts; ++i, j += stride) // subsample data
115  memcpy((void *) ((char *) _data + i * eltsize),
116  (void *) ((char *) data + j * eltsize), eltsize);
117  }
118  _nelts = nelts; // assign number of elements
119  }
120  _nt = nt; // assign HDF number type
121  return;
122 }
123 
124 // initialize an empty hdf_genvec
126 {
127  _data = 0;
128  _nelts = _nt = 0;
129  return;
130 }
131 
132 // initialize hdf_genvec from another hdf_genvec
134 {
135  if (gv._nt == 0 && gv._nelts == 0 && gv._data == 0)
136  _init();
137  else if (gv._nelts == 0)
138  _init(gv._nt, 0, 0, 0, 0);
139  else
140  _init(gv._nt, gv._data, 0, gv._nelts - 1, 1);
141  return;
142 }
143 
144 // free up memory of hdf_genvec
146 {
147  delete[]_data;
148  _nelts = _nt = 0;
149  _data = 0;
150  return;
151 }
152 
153 
154 
155 //
156 // public member functions
157 //
158 
160 {
161  _init();
162  return;
163 }
164 
165 hdf_genvec::hdf_genvec(int32 nt, void *data, int begin, int end,
166  int stride)
167 {
168  _init(nt, data, begin, end, stride);
169  return;
170 }
171 
172 hdf_genvec::hdf_genvec(int32 nt, void *data, int nelts)
173 {
174  _init(nt, data, 0, nelts - 1, 1);
175  return;
176 }
177 
179 {
180  _init(gv);
181  return;
182 }
183 
185 {
186  _del();
187  return;
188 }
189 
191 {
192  if (this == &gv)
193  return *this;
194  _del();
195  _init(gv);
196  return *this;
197 }
198 
199 // An append method...
200 void hdf_genvec::append(int32 nt, const char *new_data, int32 nelts)
201 {
202  // input checking: nt must be a valid HDF number type;
203  // data, nelts can optionally both together be 0.
204  int32 eltsize; // number of bytes per element
205  if ((eltsize = DFKNTsize(nt)) <= 0)
206  THROW(hcerr_dftype); // invalid number type
207 
208  if (new_data == 0 && nelts == 0) { // if this is a zero-length vector
209  _nelts = 0;
210  _data = 0;
211  } else {
212  if (nelts == 0)
213  THROW(hcerr_range); // invalid range given for subset of data
214  if (new_data == 0)
215  THROW(hcerr_invarr); // if specify a range, need a data array!
216 
217  // allocate memory for _data and assign _nt, _nelts
218  char *d = new char[(_nelts + nelts) * eltsize]; // allocate memory
219  memcpy(d, _data, _nelts);
220  memcpy(d + _nelts, new_data, nelts);
221 
222  delete[]_data;
223 
224  _data = d;
225  _nelts += nelts; // assign number of elements
226  }
227 
228  _nt = nt; // assign HDF number type
229  return;
230 }
231 
232 // import new data into hdf_genvec (old data is deleted)
233 void hdf_genvec::import(int32 nt, void *data, int begin, int end,
234  int stride)
235 {
236  _del();
237  if (nt == 0)
238  _init();
239  else
240  _init(nt, data, begin, end, stride);
241  return;
242 }
243 
244 // import new data into hdf_genvec from a vector of strings
245 void hdf_genvec::import(int32 nt, const vector < string > &sv)
246 {
247  static char strbuf[hdfclass::MAXSTR];
248 
249  int eltsize = DFKNTsize(nt);
250  if (eltsize == 0)
252  if (sv.size() == 0) {
253  this->import(nt);
254  return;
255  }
256 
257  char *obuf = new char[DFKNTsize(nt) * sv.size()];
258  switch (nt) {
259  case DFNT_FLOAT32:{
260  float32 val;
261  for (int i = 0; i < (int) sv.size(); ++i) {
262  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
263  istringstream(strbuf) >> val;
264  *((float32 *) obuf + i) = val;
265  }
266  break;
267  }
268  case DFNT_FLOAT64:{
269  float64 val;
270  for (int i = 0; i < (int) sv.size(); ++i) {
271  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
272  istringstream(strbuf) >> val;
273  *((float64 *) obuf + i) = val;
274  }
275  break;
276  }
277  case DFNT_INT8:{
278  int8 val;
279  for (int i = 0; i < (int) sv.size(); ++i) {
280  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
281  istringstream iss(strbuf);
282  iss >> val;
283  *((int8 *) obuf + i) = val;
284  }
285  break;
286  }
287  case DFNT_INT16:{
288  int16 val;
289  for (int i = 0; i < (int) sv.size(); ++i) {
290  istringstream(strbuf) >> val;
291  *((int16 *) obuf + i) = val;
292  }
293  break;
294  }
295  case DFNT_INT32:{
296  int32 val;
297  for (int i = 0; i < (int) sv.size(); ++i) {
298  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
299  istringstream(strbuf) >> val;
300  *((int32 *) obuf + i) = val;
301  }
302  break;
303  }
304  case DFNT_UINT8:{
305  uint8 val;
306  for (int i = 0; i < (int) sv.size(); ++i) {
307  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
308  istringstream iss(strbuf);
309  iss >> val;
310  *((uint8 *) obuf + i) = val;
311  }
312  break;
313  }
314  case DFNT_UINT16:{
315  uint16 val;
316  for (int i = 0; i < (int) sv.size(); ++i) {
317  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
318  istringstream(strbuf) >> val;
319  *((uint16 *) obuf + i) = val;
320  }
321  break;
322  }
323  case DFNT_UINT32:{
324  uint32 val;
325  for (int i = 0; i < (int) sv.size(); ++i) {
326  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
327  istringstream(strbuf) >> val;
328  *((uint32 *) obuf + i) = val;
329  }
330  break;
331  }
332  case DFNT_UCHAR8:{
333  uchar8 val;
334  for (int i = 0; i < (int) sv.size(); ++i) {
335  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
336  istringstream iss(strbuf);
337  iss >> val;
338  *((uchar8 *) obuf + i) = val;
339  }
340  break;
341  }
342  case DFNT_CHAR8:{
343  char8 val;
344  for (int i = 0; i < (int) sv.size(); ++i) {
345  strncpy(strbuf, sv[i].c_str(), hdfclass::MAXSTR - 1);
346  istringstream iss(strbuf);
347  iss >> val;
348  *((char8 *) obuf + i) = val;
349  }
350  break;
351  }
352  default:
353  delete[] obuf;
355  }
356 
357  this->import(nt, obuf, (int) sv.size());
358  return;
359 }
360 
361 // export an hdf_genvec holding uint8 or uchar8 data to a uchar8 array
362 // Added export of int8 to uchar8. A bad idea, but needed to fix some
363 // clients. The same `fix' has been applied to some other mfuncs that follow.
364 // 1/13/98 jhrg.
365 //
366 // It looks like this code treats all 8-bit datatypes as the same the user
367 // has to know if they are signed or not. 4/8/2002 jhrg
368 uchar8 *hdf_genvec::export_uchar8(void) const
369 {
370  uchar8 *rv = 0;
371  if (_nt == DFNT_UINT8)
372  ConvertArrayByCast((uint8 *) _data, _nelts, &rv);
373  else if (_nt == DFNT_UCHAR8)
374  ConvertArrayByCast((uchar8 *) _data, _nelts, &rv);
375  // Added the following case. jhrg 1/13/98.
376 #if 0
377  else if (_nt == DFNT_INT8)
378  ConvertArrayByCast((int8 *) _data, _nelts, &rv);
379 #endif
380  else
382  return rv;
383 }
384 
385 // return the i'th element of the vector as a uchar8
386 uchar8 hdf_genvec::elt_uchar8(int i) const
387 {
388  uchar8 rv;
389  if (i < 0 || i > _nelts)
391  if (_nt == DFNT_UINT8)
392  rv = (uchar8) * ((uint8 *) _data + i);
393  else if (_nt == DFNT_UCHAR8)
394  rv = *((uchar8 *) _data + i);
395  // Added the following case. 1/13/98 jhrg.
396 #if 0
397  else if (_nt == DFNT_INT8)
398  rv = *((int8 *) _data + i);
399 #endif
400  else
402  return rv;
403 }
404 
405 // export an hdf_genvec holding uint8 or uchar8 data to a uchar8 vector
406 vector < uchar8 > hdf_genvec::exportv_uchar8(void) const
407 {
408  vector < uchar8 > rv = vector < uchar8 > (0);
409  uchar8 *dtmp = 0;
410  if (_nt == DFNT_UINT8) // cast to uchar8 array and export
411  ConvertArrayByCast((uint8 *) _data, _nelts, &dtmp);
412  else if (_nt == DFNT_UCHAR8)
413  dtmp = (uchar8 *) _data;
414  // Added the following case. 1/13/98 jhrg.
415 #if 0
416  else if (_nt == DFNT_INT8)
417  ConvertArrayByCast((int8 *) _data, _nelts, &dtmp);
418 #endif
419  else
421  rv = vector < uchar8 > (dtmp, dtmp + _nelts);
422  if (dtmp != (uchar8 *) _data)
423  delete[]dtmp;
424  return rv;
425 }
426 
427 // export an hdf_genvec holding int8 or char8 data to a char8 array
428 char8 *hdf_genvec::export_char8(void) const
429 {
430  char8 *rv = 0;
431  if (_nt == DFNT_INT8)
432  ConvertArrayByCast((int8 *) _data, _nelts, &rv);
433  else if (_nt == DFNT_CHAR8)
434  ConvertArrayByCast((char8 *) _data, _nelts, &rv);
435  else
437  return rv;
438 }
439 
440 // return the i'th element of the vector as a char8
441 char8 hdf_genvec::elt_char8(int i) const
442 {
443  char8 rv;
444  if (i < 0 || i > _nelts)
446  if (_nt == DFNT_INT8)
447  rv = (char8) * ((int8 *) _data + i);
448  else if (_nt == DFNT_CHAR8 || _nt == DFNT_UCHAR8)
449  rv = *((char8 *) _data + i);
450  else
452  return rv;
453 }
454 
455 // export an hdf_genvec holding int8 or char8 data to a char8 vector
456 vector < char8 > hdf_genvec::exportv_char8(void) const
457 {
458  vector < char8 > rv = vector < char8 > (0);
459  char8 *dtmp = 0;
460  if (_nt == DFNT_INT8) // cast to char8 array and export
461  ConvertArrayByCast((int8 *) _data, _nelts, &dtmp);
462  else if (_nt == DFNT_CHAR8)
463  ConvertArrayByCast((char8 *) _data, _nelts, &dtmp);
464 // dtmp = (char8 *)_data;
465  else
467  if (!dtmp)
468  throw InternalErr(__FILE__, __LINE__, "No data returned for the character array.");
469  rv = vector < char8 > (dtmp, dtmp + _nelts);
470  if (dtmp != (char8 *) _data)
471  delete[]dtmp;
472  return rv;
473 }
474 
475 // export an hdf_genvec holding uchar8 or uint8 data to a uint8 array
476 uint8 *hdf_genvec::export_uint8(void) const
477 {
478  uint8 *rv = 0;
479  if (_nt == DFNT_UCHAR8 || _nt == DFNT_CHAR8)
480  ConvertArrayByCast((uchar8 *) _data, _nelts, &rv);
481  else if (_nt == DFNT_UINT8)
482  ConvertArrayByCast((uint8 *) _data, _nelts, &rv);
483  else
485  return rv;
486 }
487 
488 // return the i'th element of the vector as a uint8
489 uint8 hdf_genvec::elt_uint8(int i) const
490 {
491  uint8 rv;
492  if (i < 0 || i > _nelts)
494  if (_nt == DFNT_UCHAR8 || _nt == DFNT_CHAR8)
495  rv = (uint8) * ((uchar8 *) _data + i);
496  else if (_nt == DFNT_UINT8)
497  rv = *((uint8 *) _data + i);
498  else
500  return rv;
501 }
502 
503 // export an hdf_genvec holding uchar8 or uint8 data to a uint8 vector
504 vector < uint8 > hdf_genvec::exportv_uint8(void) const
505 {
506  vector < uint8 > rv = vector < uint8 > (0);
507  uint8 *dtmp = 0;
508  if (_nt == DFNT_UCHAR8 || _nt == DFNT_CHAR8) // cast to uint8 array and export
509  ConvertArrayByCast((uchar8 *) _data, _nelts, &dtmp);
510  else if (_nt == DFNT_UINT8)
511  dtmp = (uint8 *) _data;
512  else
514 
515  rv = vector < uint8 > (dtmp, dtmp + _nelts);
516  if (dtmp != (uint8 *) _data)
517  delete[]dtmp;
518  return rv;
519 }
520 
521 // export an hdf_genvec holding char8 or int8 data to a int8 array
522 int8 *hdf_genvec::export_int8(void) const
523 {
524  int8 *rv = 0;
525  if (_nt == DFNT_CHAR8)
526  ConvertArrayByCast((char8 *) _data, _nelts, &rv);
527  else if (_nt == DFNT_INT8)
528  ConvertArrayByCast((int8 *) _data, _nelts, &rv);
529  else
531  return rv;
532 }
533 
534 // return the i'th element of the vector as a int8
535 int8 hdf_genvec::elt_int8(int i) const
536 {
537  int8 rv;
538  if (i < 0 || i > _nelts)
540  if (_nt == DFNT_CHAR8)
541  rv = (int8) * ((char8 *) _data + i);
542  else if (_nt == DFNT_INT8)
543  rv = *((int8 *) _data + i);
544  else
546  return rv;
547 }
548 
549 // export an hdf_genvec holding int8 data to a int8 vector
550 vector < int8 > hdf_genvec::exportv_int8(void) const
551 {
552  vector < int8 > rv = vector < int8 > (0);
553  int8 *dtmp = 0;
554  if (_nt == DFNT_CHAR8) // cast to int8 array and export
555  ConvertArrayByCast((char8 *) _data, _nelts, &dtmp);
556  else if (_nt == DFNT_INT8)
557  dtmp = (int8 *) _data;
558  else
560  rv = vector < int8 > (dtmp, dtmp + _nelts);
561  if (dtmp != (int8 *) _data)
562  delete[]dtmp;
563  return rv;
564 }
565 
566 // export an hdf_genvec holding uchar8, uint8 or uint16 data to a uint16 array
567 uint16 *hdf_genvec::export_uint16(void) const
568 {
569  uint16 *rv = 0;
570  if (_nt == DFNT_UCHAR8) // cast to uint16 array and export
571  ConvertArrayByCast((uchar8 *) _data, _nelts, &rv);
572  else if (_nt == DFNT_UINT8) // cast to uint16 array and export
573  ConvertArrayByCast((uint8 *) _data, _nelts, &rv);
574  else if (_nt == DFNT_UINT16)
575  ConvertArrayByCast((uint16 *) _data, _nelts, &rv);
576  else
578  return rv;
579 }
580 
581 // return the i'th element of the vector as a uint16
582 uint16 hdf_genvec::elt_uint16(int i) const
583 {
584  if (i < 0 || i > _nelts)
586  if (_nt == DFNT_UCHAR8)
587  return (uint16) * ((uchar8 *) _data + i);
588  else if (_nt == DFNT_UINT8)
589  return (uint16) * ((uint8 *) _data + i);
590  else if (_nt == DFNT_UINT16)
591  return *((uint16 *) _data + i);
592  else
594  return 0;
595 }
596 
597 // export an hdf_genvec holding uchar8, uint8 or uint16 data to a uint16 vector
598 vector < uint16 > hdf_genvec::exportv_uint16(void) const
599 {
600  vector < uint16 > rv = vector < uint16 > (0);
601  uint16 *dtmp = 0;
602  if (_nt == DFNT_UCHAR8) // cast to uint16 array and export
603  ConvertArrayByCast((uchar8 *) _data, _nelts, &dtmp);
604  else if (_nt == DFNT_UINT8) // cast to uint16 array and export
605  ConvertArrayByCast((uint8 *) _data, _nelts, &dtmp);
606  else if (_nt == DFNT_UINT16)
607  dtmp = (uint16 *) _data;
608  else
610  rv = vector < uint16 > (dtmp, dtmp + _nelts);
611  if (dtmp != (uint16 *) _data)
612  delete[]dtmp;
613  return rv;
614 }
615 
616 // export an hdf_genvec holding uchar8, char8, uint8, int8 or int16 data to
617 // an int16 array
618 int16 *hdf_genvec::export_int16(void) const
619 {
620  int16 *rv = 0;
621  if (_nt == DFNT_UCHAR8) // cast to int16 array and export
622  ConvertArrayByCast((uchar8 *) _data, _nelts, &rv);
623  else if (_nt == DFNT_CHAR8) // cast to int16 array and export
624  ConvertArrayByCast((char8 *) _data, _nelts, &rv);
625  else if (_nt == DFNT_UINT8) // cast to int16 array and export
626  ConvertArrayByCast((uint8 *) _data, _nelts, &rv);
627  else if (_nt == DFNT_INT8) // cast to int16 array and export
628  ConvertArrayByCast((int8 *) _data, _nelts, &rv);
629  else if (_nt == DFNT_INT16)
630  ConvertArrayByCast((int16 *) _data, _nelts, &rv);
631  else
633  return rv;
634 }
635 
636 // return the i'th element of the vector as a int16
637 int16 hdf_genvec::elt_int16(int i) const
638 {
639  if (i < 0 || i > _nelts)
641  if (_nt == DFNT_UCHAR8)
642  return (int16) (*((uchar8 *) _data + i));
643  else if (_nt == DFNT_CHAR8)
644  return (int16) (*((char8 *) _data + i));
645  else if (_nt == DFNT_UINT8)
646  return (int16) (*((uint8 *) _data + i));
647  else if (_nt == DFNT_INT8)
648  return (int16) (*((int8 *) _data + i));
649  else if (_nt == DFNT_INT16)
650  return *((int16 *) _data + i);
651  else
653  return 0;
654 }
655 
656 // export an hdf_genvec holding int8 or int16 data to an int16 vector
657 vector < int16 > hdf_genvec::exportv_int16(void) const
658 {
659  vector < int16 > rv = vector < int16 > (0);
660  int16 *dtmp = 0;
661  if (_nt == DFNT_UCHAR8) // cast to int16 array and export
662  ConvertArrayByCast((uchar8 *) _data, _nelts, &dtmp);
663  else if (_nt == DFNT_CHAR8) // cast to int16 array and export
664  ConvertArrayByCast((char8 *) _data, _nelts, &dtmp);
665  else if (_nt == DFNT_UINT8) // cast to int16 array and export
666  ConvertArrayByCast((uint8 *) _data, _nelts, &dtmp);
667  else if (_nt == DFNT_INT8) // cast to int16 array and export
668  ConvertArrayByCast((int8 *) _data, _nelts, &dtmp);
669  else if (_nt == DFNT_INT16)
670  dtmp = (int16 *) _data;
671  else
673  rv = vector < int16 > (dtmp, dtmp + _nelts);
674  if (dtmp != (int16 *) _data)
675  delete[]dtmp;
676  return rv;
677 }
678 
679 // export an hdf_genvec holding uchar8, uint8, uint16 or uint32 data to a
680 // uint32 array
681 uint32 *hdf_genvec::export_uint32(void) const
682 {
683  uint32 *rv = 0;
684  if (_nt == DFNT_UCHAR8) // cast to uint32 array and export
685  ConvertArrayByCast((uchar8 *) _data, _nelts, &rv);
686  else if (_nt == DFNT_UINT8) // cast to uint32 array and export
687  ConvertArrayByCast((uint8 *) _data, _nelts, &rv);
688  else if (_nt == DFNT_UINT16) // cast to uint32 array and export
689  ConvertArrayByCast((uint16 *) _data, _nelts, &rv);
690  else if (_nt == DFNT_UINT32)
691  ConvertArrayByCast((uint32 *) _data, _nelts, &rv);
692  else
694  return rv;
695 }
696 
697 // return the i'th element of the vector as a uint32
698 uint32 hdf_genvec::elt_uint32(int i) const
699 {
700  if (i < 0 || i > _nelts)
702  if (_nt == DFNT_UCHAR8)
703  return (uint32) (*((uchar8 *) _data + i));
704  else if (_nt == DFNT_UINT8)
705  return (uint32) (*((uint8 *) _data + i));
706  else if (_nt == DFNT_UINT16)
707  return (uint32) (*((uint16 *) _data + i));
708  else if (_nt == DFNT_UINT32)
709  return *((uint32 *) _data + i);
710  else
712  return 0;
713 }
714 
715 // export an hdf_genvec holding uchar8, uint8, uint16 or uint32 data to a
716 // uint32 vector
717 vector < uint32 > hdf_genvec::exportv_uint32(void) const
718 {
719  vector < uint32 > rv = vector < uint32 > (0);
720  uint32 *dtmp = 0;
721  if (_nt == DFNT_UCHAR8) // cast to uint32 array and export
722  ConvertArrayByCast((uchar8 *) _data, _nelts, &dtmp);
723  else if (_nt == DFNT_UINT8) // cast to uint32 array and export
724  ConvertArrayByCast((uint8 *) _data, _nelts, &dtmp);
725  else if (_nt == DFNT_UINT16) // cast to uint32 array and export
726  ConvertArrayByCast((uint16 *) _data, _nelts, &dtmp);
727  else if (_nt == DFNT_UINT32)
728  dtmp = (uint32 *) _data;
729  else
731  rv = vector < uint32 > (dtmp, dtmp + _nelts);
732  if (dtmp != (uint32 *) _data)
733  delete[]dtmp;
734  return rv;
735 }
736 
737 // export an hdf_genvec holding uchar8, char8, uint8, int8, uint16, int16 or
738 // int32 data to a int32 array
739 int32 *hdf_genvec::export_int32(void) const
740 {
741  int32 *rv = 0;
742  if (_nt == DFNT_UCHAR8) // cast to int32 array and export
743  ConvertArrayByCast((uchar8 *) _data, _nelts, &rv);
744  else if (_nt == DFNT_CHAR8) // cast to int32 array and export
745  ConvertArrayByCast((char8 *) _data, _nelts, &rv);
746  else if (_nt == DFNT_UINT8) // cast to int32 array and export
747  ConvertArrayByCast((uint8 *) _data, _nelts, &rv);
748  else if (_nt == DFNT_INT8) // cast to int32 array and export
749  ConvertArrayByCast((int8 *) _data, _nelts, &rv);
750  else if (_nt == DFNT_UINT16)
751  ConvertArrayByCast((uint16 *) _data, _nelts, &rv);
752  else if (_nt == DFNT_INT16)
753  ConvertArrayByCast((int16 *) _data, _nelts, &rv);
754  else if (_nt == DFNT_INT32)
755  ConvertArrayByCast((int32 *) _data, _nelts, &rv);
756  else
758  return rv;
759 }
760 
761 // return the i'th element of the vector as a int32
762 int32 hdf_genvec::elt_int32(int i) const
763 {
764  if (i < 0 || i > _nelts)
766  if (_nt == DFNT_UCHAR8)
767  return (int32) (*((uchar8 *) _data + i));
768  else if (_nt == DFNT_CHAR8)
769  return (int32) (*((char8 *) _data + i));
770  else if (_nt == DFNT_UINT8)
771  return (int32) (*((uint8 *) _data + i));
772  else if (_nt == DFNT_INT8)
773  return (int32) (*((int8 *) _data + i));
774  else if (_nt == DFNT_UINT16)
775  return (int32) (*((uint16 *) _data + i));
776  else if (_nt == DFNT_INT16)
777  return (int32) (*((int16 *) _data + i));
778  else if (_nt == DFNT_INT32)
779  return *((int32 *) _data + i);
780  else
782  return 0;
783 }
784 
785 // export an hdf_genvec holding uchar8, char8, uint8, int8, uint16, int16 or
786 // int32 data to a int32 vector
787 vector < int32 > hdf_genvec::exportv_int32(void) const
788 {
789  vector < int32 > rv = vector < int32 > (0);
790  int32 *dtmp = 0;
791  if (_nt == DFNT_UCHAR8) // cast to int32 array and export
792  ConvertArrayByCast((uchar8 *) _data, _nelts, &dtmp);
793  else if (_nt == DFNT_CHAR8) // cast to int32 array and export
794  ConvertArrayByCast((char8 *) _data, _nelts, &dtmp);
795  else if (_nt == DFNT_UINT8) // cast to int32 array and export
796  ConvertArrayByCast((uint8 *) _data, _nelts, &dtmp);
797  else if (_nt == DFNT_INT8) // cast to int32 array and export
798  ConvertArrayByCast((int8 *) _data, _nelts, &dtmp);
799  else if (_nt == DFNT_UINT16) // cast to int32 array and export
800  ConvertArrayByCast((uint16 *) _data, _nelts, &dtmp);
801  else if (_nt == DFNT_INT16) // cast to int32 array and export
802  ConvertArrayByCast((int16 *) _data, _nelts, &dtmp);
803  else if (_nt == DFNT_INT32)
804  dtmp = (int32 *) _data;
805  else
807  rv = vector < int32 > (dtmp, dtmp + _nelts);
808  if (dtmp != (int32 *) _data)
809  delete[]dtmp;
810  return rv;
811 }
812 
813 // export an hdf_genvec holding float32 data to a float32 array
814 float32 *hdf_genvec::export_float32(void) const
815 {
816  float32 *rv = 0;
817  if (_nt != DFNT_FLOAT32)
819  else
820  ConvertArrayByCast((float32 *) _data, _nelts, &rv);
821  return rv;
822 }
823 
824 // return the i'th element of the vector as a float32
825 float32 hdf_genvec::elt_float32(int i) const
826 {
827  if (i < 0 || i > _nelts)
829  if (_nt != DFNT_FLOAT32)
831  return *((float32 *) _data + i);
832 }
833 
834 // export an hdf_genvec holding float32 data to a float32 vector
835 vector < float32 > hdf_genvec::exportv_float32(void) const
836 {
837  if (_nt != DFNT_FLOAT32) {
839  return vector < float32 > (0);
840  } else
841  return vector < float32 > ((float32 *) _data,
842  (float32 *) _data + _nelts);
843 }
844 
845 // export an hdf_genvec holding float32 or float64 data to a float64 array
846 float64 *hdf_genvec::export_float64(void) const
847 {
848  float64 *rv = 0;
849  if (_nt == DFNT_FLOAT64)
850  ConvertArrayByCast((float64 *) _data, _nelts, &rv);
851  else if (_nt == DFNT_FLOAT32) // cast to float64 array and export
852  ConvertArrayByCast((float32 *) _data, _nelts, &rv);
853  else
855  return rv;
856 }
857 
858 // return the i'th element of the vector as a float64
859 float64 hdf_genvec::elt_float64(int i) const
860 {
861  if (i < 0 || i > _nelts)
863  if (_nt == DFNT_FLOAT64)
864  return *((float64 *) _data + i);
865  else if (_nt == DFNT_FLOAT32)
866  return (float64) (*((float32 *) _data + i));
867  else
869  return 0;
870 }
871 
872 // export an hdf_genvec holding float32 or float64 data to a float64 vector
873 vector < float64 > hdf_genvec::exportv_float64(void) const
874 {
875  vector < float64 > rv = vector < float64 > (0);
876  float64 *dtmp = 0;
877  if (_nt == DFNT_FLOAT32) // cast to float64 array and export
878  ConvertArrayByCast((float32 *) _data, _nelts, &dtmp);
879  else if (_nt == DFNT_FLOAT64)
880  dtmp = (float64 *) _data;
881  else
883  rv = vector < float64 > (dtmp, dtmp + _nelts);
884  if (dtmp != (float64 *) _data)
885  delete[]dtmp;
886  return rv;
887 }
888 
889 // export an hdf_genvec holding char data to a string
890 string hdf_genvec::export_string(void) const
891 {
892  if (_nt != DFNT_CHAR8 && _nt != DFNT_UCHAR8) {
894  }
895  else {
896  if (_data == 0)
897  return string();
898  else
899  return string((char *) _data, _nelts);
900  }
901 }
902 
903 // print all of the elements of hdf_genvec to a vector of string
904 void hdf_genvec::print(vector < string > &sv) const
905 {
906  if (_nelts > 0)
907  print(sv, 0, _nelts - 1, 1);
908  return;
909 }
910 
911 // print the elements of hdf_genvec to a vector of string; start with initial
912 // element "begin", end with "end" and increment by "stride" elements.
913 void hdf_genvec::print(vector < string > &sv, int begin, int end,
914  int stride) const
915 {
916  if (begin < 0 || begin > _nelts || stride < 1 || end < 0 || end < begin
917  || stride <= 0 || end > _nelts - 1)
919  if (_nt == DFNT_CHAR8 || _nt == DFNT_UCHAR8) {
920  string sub;
921  sub = string((char *) _data + begin, (end - begin + 1));
922  if (stride > 1) {
923  string x;
924  for (int i = 0; i < (end - begin + 1); i += stride)
925  x += sub[i];
926  sub = x;
927  }
928  sv.push_back(sub);
929  } else {
930 #if 0
931  char buf[hdfclass::MAXSTR];
932 #endif
933  int i;
934  switch (_nt) {
935 #if 0
936  case DFNT_UCHAR8:
937  for (i = begin; i <= end; i += stride) {
938  ostrstream(buf, hdfclass::MAXSTR) <<
939  (int) *((uchar8 *) _data + i) << ends;
940  sv.push_back(string(buf));
941  }
942  break;
943 #endif
944  case DFNT_UINT8:
945  for (i = begin; i <= end; i += stride) {
946  ostringstream buf;
947  buf << (unsigned int) *((uint8 *) _data + i);
948  sv.push_back(buf.str());
949  }
950  break;
951  case DFNT_INT8:
952  for (i = begin; i <= end; i += stride) {
953  ostringstream buf;
954  buf << (int) *((int8 *) _data + i);
955  sv.push_back(buf.str());
956  }
957  break;
958  case DFNT_UINT16:
959  for (i = begin; i <= end; i += stride) {
960  ostringstream buf;
961  buf << *((uint16 *) _data + i);
962  sv.push_back(buf.str());
963  }
964  break;
965  case DFNT_INT16:
966  for (i = begin; i <= end; i += stride) {
967  ostringstream buf;
968  buf << *((int16 *) _data + i);
969  sv.push_back(buf.str());
970  }
971  break;
972  case DFNT_UINT32:
973  for (i = begin; i <= end; i += stride) {
974  ostringstream buf;
975  buf << *((uint32 *) _data + i);
976  sv.push_back(buf.str());
977  }
978  break;
979  case DFNT_INT32:
980  for (i = begin; i <= end; i += stride) {
981  ostringstream buf;
982  buf << *((int32 *) _data + i);
983  sv.push_back(buf.str());
984  }
985  break;
986  case DFNT_FLOAT32:
987  for (i = begin; i <= end; i += stride) {
988  ostringstream buf;
989  buf << *((float32 *) _data + i);
990  sv.push_back(buf.str());
991  }
992  break;
993  case DFNT_FLOAT64:
994  for (i = begin; i <= end; i += stride) {
995  ostringstream buf;
996  buf << *((float64 *) _data + i);
997  sv.push_back(buf.str());
998  }
999  break;
1000  }
1001  }
1002  return;
1003 }
1004 
1005 // $Log: genvec.cc,v $
1006 // Revision 1.7.4.1.2.1 2004/02/23 02:08:03 rmorris
1007 // There is some incompatibility between the use of isascii() in the hdf library
1008 // and its use on OS X. Here we force in the #undef of isascii in the osx case.
1009 //
1010 // Revision 1.7.4.1 2003/05/21 16:26:58 edavis
1011 // Updated/corrected copyright statements.
1012 //
1013 // Revision 1.7 2003/01/31 02:08:37 jimg
1014 // Merged with release-3-2-7.
1015 //
1016 // Revision 1.6.4.3 2002/12/18 23:32:50 pwest
1017 // gcc3.2 compile corrections, mainly regarding the using statement. Also,
1018 // missing semicolon in .y file
1019 //
1020 // Revision 1.6.4.2 2002/04/11 03:15:43 jimg
1021 // Minor change to the ConvertArrayByCast template function. Still, avoid using
1022 // this if possible.
1023 //
1024 // Revision 1.6.4.1 2001/10/30 06:36:35 jimg
1025 // Added genvec::append(...) method.
1026 // Fixed up some comments in genvec.
1027 // Changed genvec's data member from void * to char * to quell warnings
1028 // about void * being passed to delete.
1029 //
1030 // Revision 1.6 2000/10/09 19:46:19 jimg
1031 // Moved the CVS Log entries to the end of each file.
1032 // Added code to catch Error objects thrown by the dap library.
1033 // Changed the read() method's definition to match the dap library.
1034 //
1035 // Revision 1.5 1999/05/06 03:23:33 jimg
1036 // Merged changes from no-gnu branch
1037 //
1038 // Revision 1.4 1999/05/05 23:33:43 jimg
1039 // String --> string conversion
1040 //
1041 // Revision 1.3.6.1 1999/05/06 00:35:45 jimg
1042 // Jakes String --> string changes
1043 //
1044 // Revision 1.3 1998/09/10 21:33:24 jehamby
1045 // Map DFNT_CHAR8 and DFNT_UCHAR8 to Byte instead of string in SDS.
1046 //
1047 // Revision 1.2 1998/02/05 20:14:29 jimg
1048 // DODS now compiles with gcc 2.8.x
1049 //
1050 // Revision 1.1 1996/10/31 18:42:56 jimg
1051 // Added.
1052 //
1053 // Revision 1.12 1996/10/14 23:07:53 todd
1054 // Added a new import function to hdf_genvec to allow import from a vector
1055 // of string.
1056 //
1057 // Revision 1.11 1996/08/22 20:54:14 todd
1058 // Rewrite of export function suite to correctly support casts. Now all casts towards
1059 // integers of greater size are supported. (E.g., char8 -> int8 ->int16 -> int32, but
1060 // disallow int16 -> uint32 or uint32 -> int32).
1061 //
1062 // Revision 1.10 1996/08/21 23:18:59 todd
1063 // Added mfuncs to return the i'th element of a genvec.
1064 //
1065 // Revision 1.9 1996/07/22 17:12:20 todd
1066 // Changed export_*() mfuncs so they return a C++ array. Added exportv_*() mfuncs
1067 // to return STL vectors.
1068 //
1069 // Revision 1.8 1996/06/18 21:49:21 todd
1070 // Fixed pointer expressions so they would be in conformance with proper usage of void *
1071 //
1072 // Revision 1.7 1996/06/18 18:37:42 todd
1073 // Added support for initialization from a subsampled array to the _init() and
1074 // constructor mfuncs; also added support for print() to output a subsample of
1075 // the hdf_genvec.
1076 // Added copyright notice.
1077 //
1078 // Revision 1.6 1996/05/02 18:10:51 todd
1079 // Fixed a bug in print() and in export_string().
1080 //
1081 // Revision 1.5 1996/04/23 21:11:50 todd
1082 // Fixed declaration of print mfunc so it was const correct.
1083 //
1084 // Revision 1.4 1996/04/18 19:06:37 todd
1085 // Added print() mfunc
1086 //
1087 // Revision 1.3 1996/04/04 01:11:30 todd
1088 // Added support for empty vectors that have a number type.
1089 // Added import() public member function.
1090 //
1091 // Revision 1.2 1996/04/03 00:18:18 todd
1092 // Fixed a bug in _init(int32, void *, int)
1093 //
1094 // Revision 1.1 1996/04/02 20:36:43 todd
1095 // Initial revision
1096 //
uint16 elt_uint16(int i) const
Definition: genvec.cc:582
float64 * export_float64(void) const
Definition: genvec.cc:846
hdf_genvec & operator=(const hdf_genvec &gv)
Definition: genvec.cc:190
int16 * export_int16(void) const
Definition: genvec.cc:618
void _init(void)
Definition: genvec.cc:125
uint16 * export_uint16(void) const
Definition: genvec.cc:567
void import(int32 nt, void *data, int nelts)
Definition: hdfclass.h:91
char8 * export_char8(void) const
Definition: genvec.cc:428
STL namespace.
uint8 elt_uint8(int i) const
Definition: genvec.cc:489
int32 elt_int32(int i) const
Definition: genvec.cc:762
int32 * export_int32(void) const
Definition: genvec.cc:739
vector< float64 > exportv_float64(void) const
Definition: genvec.cc:873
void ConvertArrayByCast(U *array, int nelts, T **carray)
Definition: genvec.cc:65
char * _data
Definition: hdfclass.h:145
char8 elt_char8(int i) const
Definition: genvec.cc:441
float64 elt_float64(int i) const
Definition: genvec.cc:859
vector< uint8 > exportv_uint8(void) const
Definition: genvec.cc:504
uint8 * export_uint8(void) const
Definition: genvec.cc:476
uchar8 * export_uchar8(void) const
Definition: genvec.cc:368
vector< uint32 > exportv_uint32(void) const
Definition: genvec.cc:717
void print(vector< string > &strv) const
Definition: genvec.cc:904
vector< int32 > exportv_int32(void) const
Definition: genvec.cc:787
hdf_genvec(void)
Definition: genvec.cc:159
void append(int32 nt, const char *new_data, int32 nelts)
Definition: genvec.cc:200
int16 elt_int16(int i) const
Definition: genvec.cc:637
#define THROW(x)
Definition: dhdferr.h:51
vector< uint16 > exportv_uint16(void) const
Definition: genvec.cc:598
int8 * export_int8(void) const
Definition: genvec.cc:522
vector< int8 > exportv_int8(void) const
Definition: genvec.cc:550
int8 elt_int8(int i) const
Definition: genvec.cc:535
int32 _nt
Definition: hdfclass.h:143
vector< float32 > exportv_float32(void) const
Definition: genvec.cc:835
uchar8 elt_uchar8(int i) const
Definition: genvec.cc:386
uint32 * export_uint32(void) const
Definition: genvec.cc:681
vector< char8 > exportv_char8(void) const
Definition: genvec.cc:456
vector< uchar8 > exportv_uchar8(void) const
Definition: genvec.cc:406
virtual ~hdf_genvec(void)
Definition: genvec.cc:184
vector< int16 > exportv_int16(void) const
Definition: genvec.cc:657
float32 * export_float32(void) const
Definition: genvec.cc:814
void _init(const hdf_genvec &gv)
Definition: genvec.cc:133
int _nelts
Definition: hdfclass.h:144
string export_string(void) const
Definition: genvec.cc:890
void _del(void)
Definition: genvec.cc:145
uint32 elt_uint32(int i) const
Definition: genvec.cc:698
float32 elt_float32(int i) const
Definition: genvec.cc:825