bes  Updated for version 3.20.6
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 
43 using std::map;
44 using std::string;
45 using std::vector;
46 using std::ostream;
47 using std::endl;
48 using std::ostringstream;
49 
50 CSV_Header::CSV_Header()
51 {
52  _hdr = new map<string, CSV_Field*>;
53  _index2field = new map<int, string>;
54 }
55 
56 CSV_Header::~CSV_Header()
57 {
58  if (_hdr) {
59  map<string, CSV_Field*>::iterator i = _hdr->begin();
60  map<string, CSV_Field*>::iterator e = _hdr->end();
61  for (; i != e; i++) {
62  CSV_Field *f = (*i).second;
63  delete f;
64  (*i).second = 0;
65  }
66  delete _hdr;
67  _hdr = 0;
68  }
69  if (_index2field) {
70  delete _index2field;
71  _index2field = 0;
72  }
73 }
74 
75 bool CSV_Header::populate(vector<string> *headerinfo) const
76 {
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  throw BESInternalError(err.str(), __FILE__, __LINE__);
96  }
97  if (*(--headerinfo_s.end()) != '>') {
98  ostringstream err;
99  err << "malformed header information in column " << fieldIndex << ", missing type in " << headerinfo_s;
100  throw BESInternalError(err.str(), __FILE__, __LINE__);
101  }
102  fieldName = headerinfo_s.substr(0, lastPos);
103  fieldType = headerinfo_s.substr(lastPos + 1, headerinfo_l - lastPos - 2);
104 
105  CSV_Field* field = new CSV_Field();
106  field->insertName(fieldName);
107  field->insertType(fieldType);
108  field->insertIndex(fieldIndex);
109 
110  _hdr->insert(make_pair(fieldName, field));
111  _index2field->insert(make_pair(fieldIndex, fieldName));
112 
113  fieldIndex++;
114  }
115 
116  return true;
117 }
118 
119 CSV_Field *
120 CSV_Header::getField(const int& index)
121 {
122  CSV_Field *f = 0;
123  if (_index2field->find(index) != _index2field->end()) {
124  string fieldName = _index2field->find(index)->second;
125  f = _hdr->find(fieldName)->second;
126  }
127  else {
128  ostringstream err;
129  err << "Could not find field in column " << index;
130  throw BESInternalError(err.str(), __FILE__, __LINE__);
131  }
132  return f;
133 }
134 
135 CSV_Field *
136 CSV_Header::getField(const string& fieldName)
137 {
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 {
152  string type;
153  map<string, CSV_Field*>::iterator it = _hdr->find(fieldName);
154 
155  if (it != _hdr->end()) {
156  type = (it->second)->getType();
157  }
158  return type;
159 }
160 
161 void CSV_Header::getFieldList(vector<string> &list)
162 {
163  for (unsigned int index = 0; index < _index2field->size(); index++) {
164  list.push_back(_index2field->find(index)->second);
165  }
166 }
167 
168 void CSV_Header::dump(ostream &strm) const
169 {
170  strm << BESIndent::LMarg << "CSV_Header::dump - (" << (void *) this << ")" << endl;
171  BESIndent::Indent();
172  map<int, string>::const_iterator ii = _index2field->begin();
173  map<int, string>::const_iterator ie = _index2field->end();
174  for (; ii != ie; ii++) {
175  strm << BESIndent::LMarg << (*ii).first << ": " << (*ii).second << endl;
176  }
177  map<string, CSV_Field*>::const_iterator fi = _hdr->begin();
178  map<string, CSV_Field*>::const_iterator fe = _hdr->end();
179  for (; fi != fe; fi++) {
180  strm << BESIndent::LMarg << (*fi).first << ": " << endl;
181  BESIndent::Indent();
182  (*fi).second->dump(strm);
183  BESIndent::UnIndent();
184  }
185  BESIndent::UnIndent();
186 }
187 
CSV_Field
Definition: CSV_Field.h:42
CSV_Utils::slim
static void slim(std::string &str)
Strips leading and trailing double quotes from string.
Definition: CSV_Utils.cc:91
CSV_Header::dump
virtual void dump(std::ostream &strm) const
dump the contents of this object to the specified ostream
Definition: CSV_Header.cc:168
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43