CSV_Header.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include<iostream>
00036 #include<sstream>
00037 #include"CSV_Header.h"
00038
00039 CSV_Header::CSV_Header() {
00040 hdr = new map<string, CSV_Field*>;
00041 index2field = new map<int,string>;
00042 }
00043
00044 CSV_Header::~CSV_Header() {
00045 delete hdr;
00046 delete index2field;
00047 }
00048
00049 const bool CSV_Header::populate(vector<string>* foo) {
00050
00051 string::size_type lastPos;
00052
00053 string fieldName;
00054 string fieldType;
00055 int fieldIndex = 0;
00056
00057 for(vector<string>::iterator it = foo->begin(); it != foo->end(); it++) {
00058 slim(*it);
00059 lastPos = (*it).find_first_of("<",0);
00060 fieldName = (*it).substr(0,lastPos);
00061 fieldType = (*it).substr(lastPos + 1,(*it).length() - lastPos - 2);
00062
00063 CSV_Field* field = new CSV_Field();
00064 field->insertName(fieldName);
00065 field->insertType(fieldType);
00066 field->insertIndex(fieldIndex);
00067
00068 hdr->insert(make_pair(fieldName,field));
00069 index2field->insert(make_pair(fieldIndex,fieldName));
00070
00071 fieldIndex++;
00072 }
00073
00074 return true;
00075 }
00076
00077 CSV_Field* CSV_Header::getField(const int& index) throw(string) {
00078 if(index2field->find(index) != index2field->end()) {
00079 string fieldName = index2field->find(index)->second;
00080 return hdr->find(fieldName)->second;
00081 } else {
00082 ostringstream osstream;
00083 osstream << "Could not find field at index " << index << endl;
00084 throw osstream.str();
00085 }
00086 }
00087
00088 CSV_Field* CSV_Header::getField(const string& fieldName) throw(string) {
00089 if(hdr->find(fieldName) != hdr->end()) {
00090 return hdr->find(fieldName)->second;
00091 } else {
00092 ostringstream osstream;
00093 osstream << "Could not find field \"" << fieldName << "\"\n";
00094 throw osstream.str();
00095 }
00096 }
00097
00098 const string CSV_Header::getFieldType(const string& fieldName) {
00099 map<string,CSV_Field*>::iterator it = hdr->find(fieldName);
00100
00101 if(it != hdr->end())
00102 return (it->second)->getType();
00103 else
00104 return "";
00105 }
00106
00107 void CSV_Header::print() {
00108 for(unsigned int index = 0; index < index2field->size(); index++) {
00109 string field = index2field->find(index)->second;
00110 CSV_Field* csvField = hdr->find(field)->second;
00111 cout << csvField->getIndex() << "\t" << csvField->getType()
00112 << "\t" << csvField->getName() << "\n";
00113 }
00114 }
00115
00116 void slim(string& str) {
00117 if(*(--str.end()) == '\"' and *str.begin() == '\"')
00118 str = str.substr(1,str.length() - 2);
00119 }
00120
00121 vector<string> CSV_Header::getFieldList() {
00122 vector<string> fieldList;
00123 for(unsigned int index = 0; index < index2field->size(); index++) {
00124 fieldList.push_back(index2field->find(index)->second);
00125 }
00126 return fieldList;
00127 }