40 #include "CSV_Utils.h"
42 #include <BESInternalError.h>
43 #include <BESNotFoundError.h>
49 using std::ostringstream;
55 _data =
new vector<CSV_Data*>();
71 vector<CSV_Data*>::iterator i = _data->begin();
72 vector<CSV_Data*>::iterator e = _data->end();
85 bool CSV_Obj::open(
const string& filepath)
87 return _reader->open(filepath);
92 vector<string> txtLine;
95 while (!_reader->eof()) {
96 _reader->get(txtLine);
99 if (_header->populate(&txtLine)) {
100 for (
unsigned int i = 0; i < txtLine.size(); i++) {
106 else if (!txtLine.empty()) {
108 vector<CSV_Data *>::iterator it = _data->begin();
109 vector<CSV_Data *>::iterator et = _data->end();
110 for (; it != et; it++) {
112 string token = txtLine.at(index);
117 err <<
" Attempting to add value " << token <<
" to field " << index <<
", field does not exist";
120 d->insert(f, &token);
128 void CSV_Obj::getFieldList(vector<string> &list)
130 _header->getFieldList(list);
133 string CSV_Obj::getFieldType(
const string& fieldName)
135 return _header->getFieldType(fieldName);
138 int CSV_Obj::getRecordCount()
140 CSV_Data* alphaField = _data->at(0);
141 string type = alphaField->getType();
143 if (type.compare(
string(STRING)) == 0) {
144 return ((vector<string>*) alphaField->getData())->size();
146 else if (type.compare(
string(FLOAT32)) == 0) {
147 return ((vector<float>*) alphaField->getData())->size();
149 else if (type.compare(
string(FLOAT64)) == 0) {
150 return ((vector<double>*) alphaField->getData())->size();
152 else if (type.compare(
string(INT16)) == 0) {
153 return ((vector<short>*) alphaField->getData())->size();
155 else if (type.compare(
string(INT32)) == 0) {
156 return ((vector<int>*) alphaField->getData())->size();
164 CSV_Obj::getFieldData(
const string& field)
169 int index = f->getIndex();
175 string err = (string)
"Unable to get data for field " + field;
180 string err = (string)
"Unable to get data for field " + field +
", no such field exists";
186 vector<string> CSV_Obj::getRecord(
const int rowNum)
188 vector<string> record;
192 int maxRows = getRecordCount();
193 if (rowNum > maxRows) {
195 err <<
"Attempting to retrieve row " << rowNum <<
" of " << maxRows;
199 vector<string> fieldList;
200 getFieldList(fieldList);
201 vector<string>::iterator it = fieldList.begin();
202 vector<string>::iterator et = fieldList.end();
203 for (; it != et; it++) {
204 string fieldName = (*it);
206 fieldData = getFieldData(fieldName);
207 CSV_Field *f = _header->getField(fieldName);
210 err <<
"Unable to retrieve data for field " << fieldName <<
" on row " << rowNum;
215 if (type.compare(
string(STRING)) == 0) {
216 record.push_back(((vector<string>*) fieldData)->at(rowNum));
218 else if (type.compare(
string(FLOAT32)) == 0) {
219 oss << ((vector<float>*) fieldData)->at(rowNum);
220 record.push_back(oss.str());
222 else if (type.compare(
string(FLOAT64)) == 0) {
223 oss << ((vector<double>*) fieldData)->at(rowNum);
224 record.push_back(oss.str());
226 else if (type.compare(
string(INT16)) == 0) {
227 oss << ((vector<short>*) fieldData)->at(rowNum);
228 record.push_back(oss.str());
230 else if (type.compare(
string(INT32)) == 0) {
231 oss << ((vector<int>*) fieldData)->at(rowNum);
232 record.push_back(oss.str());
241 strm << BESIndent::LMarg <<
"CSV_Obj::dump - (" << (
void *)
this <<
")" << endl;
244 strm << BESIndent::LMarg <<
"reader:" << endl;
247 BESIndent::UnIndent();
250 strm << BESIndent::LMarg <<
"header:" << endl;
253 BESIndent::UnIndent();
256 strm << BESIndent::LMarg <<
"data:" << endl;
258 BESIndent::UnIndent();