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