40 #include "CSV_Utils.h"
42 #include <BESInternalError.h>
43 #include <BESNotFoundError.h>
49 _data =
new vector<CSV_Data*>();
65 vector<CSV_Data*>::iterator i = _data->begin();
66 vector<CSV_Data*>::iterator e = _data->end();
79 bool CSV_Obj::open(
const string& filepath)
81 return _reader->open(filepath);
86 vector<string> txtLine;
89 while (!_reader->eof()) {
90 _reader->get(txtLine);
93 if (_header->populate(&txtLine)) {
94 for (
unsigned int i = 0; i < txtLine.size(); i++) {
100 else if (!txtLine.empty()) {
102 vector<CSV_Data *>::iterator it = _data->begin();
103 vector<CSV_Data *>::iterator et = _data->end();
104 for (; it != et; it++) {
106 string token = txtLine.at(index);
111 err <<
" Attempting to add value " << token <<
" to field " << index <<
", field does not exist";
114 d->insert(f, &token);
122 void CSV_Obj::getFieldList(vector<string> &list)
124 _header->getFieldList(list);
127 string CSV_Obj::getFieldType(
const string& fieldName)
129 return _header->getFieldType(fieldName);
132 int CSV_Obj::getRecordCount()
134 CSV_Data* alphaField = _data->at(0);
135 string type = alphaField->getType();
137 if (type.compare(
string(STRING)) == 0) {
138 return ((vector<string>*) alphaField->getData())->size();
140 else if (type.compare(
string(FLOAT32)) == 0) {
141 return ((vector<float>*) alphaField->getData())->size();
143 else if (type.compare(
string(FLOAT64)) == 0) {
144 return ((vector<double>*) alphaField->getData())->size();
146 else if (type.compare(
string(INT16)) == 0) {
147 return ((vector<short>*) alphaField->getData())->size();
149 else if (type.compare(
string(INT32)) == 0) {
150 return ((vector<int>*) alphaField->getData())->size();
158 CSV_Obj::getFieldData(
const string& field)
163 int index = f->getIndex();
169 string err = (string)
"Unable to get data for field " + field;
174 string err = (string)
"Unable to get data for field " + field +
", no such field exists";
180 vector<string> CSV_Obj::getRecord(
const int rowNum)
182 vector<string> record;
186 int maxRows = getRecordCount();
187 if (rowNum > maxRows) {
189 err <<
"Attempting to retrieve row " << rowNum <<
" of " << maxRows;
193 vector<string> fieldList;
194 getFieldList(fieldList);
195 vector<string>::iterator it = fieldList.begin();
196 vector<string>::iterator et = fieldList.end();
197 for (; it != et; it++) {
198 string fieldName = (*it);
200 fieldData = getFieldData(fieldName);
201 CSV_Field *f = _header->getField(fieldName);
204 err <<
"Unable to retrieve data for field " << fieldName <<
" on row " << rowNum;
209 if (type.compare(
string(STRING)) == 0) {
210 record.push_back(((vector<string>*) fieldData)->at(rowNum));
212 else if (type.compare(
string(FLOAT32)) == 0) {
213 oss << ((vector<float>*) fieldData)->at(rowNum);
214 record.push_back(oss.str());
216 else if (type.compare(
string(FLOAT64)) == 0) {
217 oss << ((vector<double>*) fieldData)->at(rowNum);
218 record.push_back(oss.str());
220 else if (type.compare(
string(INT16)) == 0) {
221 oss << ((vector<short>*) fieldData)->at(rowNum);
222 record.push_back(oss.str());
224 else if (type.compare(
string(INT32)) == 0) {
225 oss << ((vector<int>*) fieldData)->at(rowNum);
226 record.push_back(oss.str());
235 strm << BESIndent::LMarg <<
"CSV_Obj::dump - (" << (
void *)
this <<
")" << endl;
238 strm << BESIndent::LMarg <<
"reader:" << endl;
241 BESIndent::UnIndent();
244 strm << BESIndent::LMarg <<
"header:" << endl;
247 BESIndent::UnIndent();
250 strm << BESIndent::LMarg <<
"data:" << endl;
252 BESIndent::UnIndent();