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