38#include "CSV_Header.h"
41#include <BESInternalError.h>
42#include <BESSyntaxUserError.h>
51using std::ostringstream;
53CSV_Header::CSV_Header() {
54 _hdr =
new map<string, CSV_Field *>;
55 _index2field =
new map<int, string>;
58CSV_Header::~CSV_Header() {
60 map<string, CSV_Field *>::iterator i = _hdr->begin();
61 map<string, CSV_Field *>::iterator e = _hdr->end();
63 CSV_Field *f = (*i).second;
76bool 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.size();
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 <<
"'";
96 throw BESSyntaxUserError(err.str(), __FILE__, __LINE__);
98 if (*(--headerinfo_s.end()) !=
'>') {
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__);
104 fieldName = headerinfo_s.substr(0, lastPos);
105 fieldType = headerinfo_s.substr(lastPos + 1, headerinfo_l - lastPos - 2);
107 CSV_Field *field =
new CSV_Field();
108 field->insertName(fieldName);
109 field->insertType(fieldType);
110 field->insertIndex(fieldIndex);
112 _hdr->insert(make_pair(fieldName, field));
113 _index2field->insert(make_pair(fieldIndex, fieldName));
122CSV_Header::getField(
const int &index) {
124 if (_index2field->find(index) != _index2field->end()) {
125 string fieldName = _index2field->find(index)->second;
126 f = _hdr->find(fieldName)->second;
130 err <<
"Could not find field in column " << index;
131 throw BESInternalError(err.str(), __FILE__, __LINE__);
137CSV_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;
145 throw BESInternalError(err.str(), __FILE__, __LINE__);
150const string CSV_Header::getFieldType(
const string &fieldName) {
152 map<string, CSV_Field *>::iterator it = _hdr->find(fieldName);
154 if (it != _hdr->end()) {
155 type = (it->second)->getType();
160void CSV_Header::getFieldList(vector<string> &
list) {
161 for (
unsigned int index = 0; index < _index2field->size(); index++) {
162 list.push_back(_index2field->find(index)->second);
167 strm << BESIndent::LMarg <<
"CSV_Header::dump - (" << (
void *)
this <<
")" << endl;
171 for (; ii != ie; ii++) {
172 strm << BESIndent::LMarg << (*ii).first <<
": " << (*ii).second << endl;
176 for (; fi != fe; fi++) {
177 strm << BESIndent::LMarg << (*fi).first <<
": " << endl;
179 (*fi).second->dump(strm);
180 BESIndent::UnIndent();
182 BESIndent::UnIndent();
static void slim(std::string &str)
Strips leading and trailing double quotes from string.