bes  Updated for version 3.20.6
hcutil.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: hcutil.cc,v $ - misc utility routines for HDFClass
41 //
43 
44 #include "config_hdf.h"
45 
46 #include <string>
47 #include <vector>
48 
49 using std::vector;
50 using std::string;
51 
52 #include <mfhdf.h>
53 
54 #include <BESDebug.h>
55 
56 using std::endl; // Added when I removed 'using' from BESDebug.h
57 
58 #if 0
59 
60 // This function is not used and is broken. The loop depends on i being less
61 // than zero for termination, but i is an unsigned type.
62 vector < string > split(const string & str, const string & delim)
63 {
64  vector < string > rv;
65 
66  string::size_type len = str.length();
67  string::size_type dlen = delim.length();
68  for (string::size_type i = 0, previ = -dlen;; previ = i) {
69  i = str.find(delim, previ + dlen);
70  if (i == 0)
71  continue;
72  if (i < 0) {
73  if (previ + dlen < len)
74  rv.push_back(str.
75  substr(previ + dlen, (len - previ - dlen)));
76  break;
77  }
78  rv.push_back(str.substr(previ + dlen, (i - previ - dlen)));
79  }
80 
81  return rv;
82 }
83 #endif
84 
85 string join(const vector < string > &sv, const string & delim)
86 {
87  string str;
88  if (sv.size() > 0) {
89  str = sv[0];
90  for (int i = 1; i < (int) sv.size(); ++i)
91  str += (delim + sv[i]);
92  }
93  return str;
94 }
95 
96 bool SDSExists(const char *filename, const char *sdsname)
97 {
98 
99  int32 sd_id, index;
100  if ((sd_id = SDstart(filename, DFACC_RDONLY)) < 0) {
101  BESDEBUG("h4", "hcutil:96 SDstart for " << filename << " error" << endl);
102  return false;
103  }
104 
105  index = SDnametoindex(sd_id, (char *) sdsname);
106  if (SDend(sd_id) < 0)
107  BESDEBUG("h4", "hcutil: SDend error: " << HEstring((hdf_err_code_t)HEvalue(1)) << endl);
108 
109  return (index >= 0);
110 }
111 
112 bool GRExists(const char *filename, const char *grname)
113 {
114 
115  int32 file_id, gr_id, index;
116  if ((file_id = Hopen(filename, DFACC_RDONLY, 0)) < 0)
117  return false;
118  if ((gr_id = GRstart(file_id)) < 0)
119  return false;
120 
121  index = GRnametoindex(gr_id, (char *) grname);
122  GRend(gr_id);
123  Hclose(file_id);
124 
125  return (index >= 0);
126 }
127 
128 bool VdataExists(const char *filename, const char *vdname)
129 {
130 
131  int32 file_id, ref;
132  if ((file_id = Hopen(filename, DFACC_RDONLY, 0)) < 0)
133  return false;
134  // should return error if Vstart fails. here
135  if(Vstart(file_id)<0) {
136  BESDEBUG("h4", "Vstart " << filename << " error" << endl);
137  return false;
138  }
139  ref = VSfind(file_id, vdname);
140  Vend(file_id);
141  Hclose(file_id);
142 
143  return (ref > 0);
144 }