bes  Updated for version 3.20.10
CSV_Header.cc
1 // CSV_Header.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Stephan Zednik <zednik@ucar.edu> and Patrick West <pwest@ucar.edu>
8 // and Jose Garcia <jgarcia@ucar.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
28 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
29 //
30 // Authors:
31 // zednik Stephan Zednik <zednik@ucar.edu>
32 // pwest Patrick West <pwest@ucar.edu>
33 // jgarcia Jose Garcia <jgarcia@ucar.edu>
34 
35 #include <iostream>
36 #include <sstream>
37 
38 #include "CSV_Header.h"
39 #include "CSV_Utils.h"
40 
41 #include <BESInternalError.h>
42 #include <BESSyntaxUserError.h>
43 
44 #include <BESLog.h>
45 
46 using std::map;
47 using std::string;
48 using std::vector;
49 using std::ostream;
50 using std::endl;
51 using std::ostringstream;
52 
53 CSV_Header::CSV_Header() {
54  _hdr = new map<string, CSV_Field *>;
55  _index2field = new map<int, string>;
56 }
57 
58 CSV_Header::~CSV_Header() {
59  if (_hdr) {
60  map<string, CSV_Field *>::iterator i = _hdr->begin();
61  map<string, CSV_Field *>::iterator e = _hdr->end();
62  for (; i != e; i++) {
63  CSV_Field *f = (*i).second;
64  delete f;
65  (*i).second = 0;
66  }
67  delete _hdr;
68  _hdr = 0;
69  }
70  if (_index2field) {
71  delete _index2field;
72  _index2field = 0;
73  }
74 }
75 
76 bool CSV_Header::populate(vector<string> *headerinfo) const {
77  string::size_type lastPos;
78 
79  string fieldName;
80  string fieldType;
81  int fieldIndex = 0;
82 
83  vector<string>::iterator it = headerinfo->begin();
84  vector<string>::iterator et = headerinfo->end();
85  for (; it != et; it++) {
86  string headerinfo_s = (*it);
87  CSV_Utils::slim(headerinfo_s);
88  string::size_type headerinfo_l = headerinfo_s.length();
89 
90  // lastPos = headerinfo_s.find_first_of( "<" ) ; not used. jg 3/25/11
91  lastPos = headerinfo_s.find_first_of("<", 0);
92  if (lastPos == string::npos) {
93  ostringstream err;
94  err << "Malformed header information in column " << fieldIndex << ", missing type in '" << headerinfo_s << "'";
95  ERROR_LOG(err.str());
96  throw BESSyntaxUserError(err.str(), __FILE__, __LINE__);
97  }
98  if (*(--headerinfo_s.end()) != '>') {
99  ostringstream err;
100  err << "Malformed header information in column " << fieldIndex << ", missing type in '" << headerinfo_s << "'";
101  ERROR_LOG(err.str());
102  throw BESSyntaxUserError(err.str(), __FILE__, __LINE__);
103  }
104  fieldName = headerinfo_s.substr(0, lastPos);
105  fieldType = headerinfo_s.substr(lastPos + 1, headerinfo_l - lastPos - 2);
106 
107  CSV_Field *field = new CSV_Field();
108  field->insertName(fieldName);
109  field->insertType(fieldType);
110  field->insertIndex(fieldIndex);
111 
112  _hdr->insert(make_pair(fieldName, field));
113  _index2field->insert(make_pair(fieldIndex, fieldName));
114 
115  fieldIndex++;
116  }
117 
118  return true;
119 }
120 
121 CSV_Field *
122 CSV_Header::getField(const int &index) {
123  CSV_Field *f = 0;
124  if (_index2field->find(index) != _index2field->end()) {
125  string fieldName = _index2field->find(index)->second;
126  f = _hdr->find(fieldName)->second;
127  }
128  else {
129  ostringstream err;
130  err << "Could not find field in column " << index;
131  throw BESInternalError(err.str(), __FILE__, __LINE__);
132  }
133  return f;
134 }
135 
136 CSV_Field *
137 CSV_Header::getField(const string &fieldName) {
138  CSV_Field *f = 0;
139  if (_hdr->find(fieldName) != _hdr->end()) {
140  f = _hdr->find(fieldName)->second;
141  }
142  else {
143  ostringstream err;
144  err << "Could not find field \"" << fieldName;
145  throw BESInternalError(err.str(), __FILE__, __LINE__);
146  }
147  return f;
148 }
149 
150 const string CSV_Header::getFieldType(const string &fieldName) {
151  string type;
152  map<string, CSV_Field *>::iterator it = _hdr->find(fieldName);
153 
154  if (it != _hdr->end()) {
155  type = (it->second)->getType();
156  }
157  return type;
158 }
159 
160 void CSV_Header::getFieldList(vector<string> &list) {
161  for (unsigned int index = 0; index < _index2field->size(); index++) {
162  list.push_back(_index2field->find(index)->second);
163  }
164 }
165 
166 void CSV_Header::dump(ostream &strm) const {
167  strm << BESIndent::LMarg << "CSV_Header::dump - (" << (void *) this << ")" << endl;
168  BESIndent::Indent();
169  map<int, string>::const_iterator ii = _index2field->begin();
170  map<int, string>::const_iterator ie = _index2field->end();
171  for (; ii != ie; ii++) {
172  strm << BESIndent::LMarg << (*ii).first << ": " << (*ii).second << endl;
173  }
174  map<string, CSV_Field *>::const_iterator fi = _hdr->begin();
175  map<string, CSV_Field *>::const_iterator fe = _hdr->end();
176  for (; fi != fe; fi++) {
177  strm << BESIndent::LMarg << (*fi).first << ": " << endl;
178  BESIndent::Indent();
179  (*fi).second->dump(strm);
180  BESIndent::UnIndent();
181  }
182  BESIndent::UnIndent();
183 }
184 
exception thrown if internal error encountered
error thrown if there is a user syntax error in the request or any other user error
virtual void dump(std::ostream &strm) const
dump the contents of this object to the specified ostream
Definition: CSV_Header.cc:166
static void slim(std::string &str)
Strips leading and trailing double quotes from string.
Definition: CSV_Utils.cc:72