38 #include "CSV_Header.h"
39 #include "CSV_Utils.h"
41 #include <BESInternalError.h>
48 using std::ostringstream;
50 CSV_Header::CSV_Header()
52 _hdr =
new map<string, CSV_Field*>;
53 _index2field =
new map<int, string>;
56 CSV_Header::~CSV_Header()
59 map<string, CSV_Field*>::iterator i = _hdr->begin();
60 map<string, CSV_Field*>::iterator e = _hdr->end();
75 bool CSV_Header::populate(vector<string> *headerinfo)
const
77 string::size_type lastPos;
83 vector<string>::iterator it = headerinfo->begin();
84 vector<string>::iterator et = headerinfo->end();
85 for (; it != et; it++) {
86 string headerinfo_s = (*it);
88 string::size_type headerinfo_l = headerinfo_s.length();
91 lastPos = headerinfo_s.find_first_of(
"<", 0);
92 if (lastPos == string::npos) {
94 err <<
"malformed header information in column " << fieldIndex <<
", missing type in " << headerinfo_s;
97 if (*(--headerinfo_s.end()) !=
'>') {
99 err <<
"malformed header information in column " << fieldIndex <<
", missing type in " << headerinfo_s;
102 fieldName = headerinfo_s.substr(0, lastPos);
103 fieldType = headerinfo_s.substr(lastPos + 1, headerinfo_l - lastPos - 2);
106 field->insertName(fieldName);
107 field->insertType(fieldType);
108 field->insertIndex(fieldIndex);
110 _hdr->insert(make_pair(fieldName, field));
111 _index2field->insert(make_pair(fieldIndex, fieldName));
120 CSV_Header::getField(
const int& index)
123 if (_index2field->find(index) != _index2field->end()) {
124 string fieldName = _index2field->find(index)->second;
125 f = _hdr->find(fieldName)->second;
129 err <<
"Could not find field in column " << index;
136 CSV_Header::getField(
const string& fieldName)
139 if (_hdr->find(fieldName) != _hdr->end()) {
140 f = _hdr->find(fieldName)->second;
144 err <<
"Could not find field \"" << fieldName;
150 const string CSV_Header::getFieldType(
const string& fieldName)
153 map<string, CSV_Field*>::iterator it = _hdr->find(fieldName);
155 if (it != _hdr->end()) {
156 type = (it->second)->getType();
161 void CSV_Header::getFieldList(vector<string> &list)
163 for (
unsigned int index = 0; index < _index2field->size(); index++) {
164 list.push_back(_index2field->find(index)->second);
170 strm << BESIndent::LMarg <<
"CSV_Header::dump - (" << (
void *)
this <<
")" << endl;
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;
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;
182 (*fi).second->dump(strm);
183 BESIndent::UnIndent();
185 BESIndent::UnIndent();