42#include <BESInternalError.h>
43#include <BESNotFoundError.h>
44#include <BESSyntaxUserError.h>
52using std::ostringstream;
56 _reader =
new CSV_Reader();
57 _header =
new CSV_Header();
58 _data =
new vector<CSV_Data*>();
74 vector<CSV_Data*>::iterator i = _data->begin();
75 vector<CSV_Data*>::iterator e = _data->end();
88bool CSV_Obj::open(
const string& filepath)
90 return _reader->open(filepath);
95 vector<string> txtLine;
98 while (!_reader->eof()) {
99 _reader->get(txtLine);
102 if (_header->populate(&txtLine)) {
103 for (
unsigned int i = 0; i < txtLine.size(); i++) {
104 _data->push_back(
new CSV_Data());
109 else if (!txtLine.empty()) {
111 vector<CSV_Data *>::iterator it = _data->begin();
112 vector<CSV_Data *>::iterator et = _data->end();
113 for (; it != et; it++) {
116 string token = txtLine.at(index);
118 CSV_Field *f = _header->getField(index);
121 err <<
" Attempting to add value " << token <<
" to field " << index <<
", field does not exist";
122 ERROR_LOG(err.str());
123 throw BESInternalError(err.str(), __FILE__, __LINE__);
125 d->insert(f, &token);
127 catch (
const std::out_of_range &) {
129 err <<
"Error in CSV dataset, too few data elements on line " << _reader->get_row_number();
131 ERROR_LOG(err.str());
132 throw BESSyntaxUserError(err.str(), __FILE__, __LINE__);
141void CSV_Obj::getFieldList(vector<string> &
list)
143 _header->getFieldList(list);
146string CSV_Obj::getFieldType(
const string& fieldName)
148 return _header->getFieldType(fieldName);
151int CSV_Obj::getRecordCount()
153 CSV_Data* alphaField = _data->at(0);
154 string type = alphaField->getType();
156 if (type.compare(
string(STRING)) == 0) {
157 return ((vector<string>*) alphaField->getData())->size();
159 else if (type.compare(
string(FLOAT32)) == 0) {
160 return ((vector<float>*) alphaField->getData())->size();
162 else if (type.compare(
string(FLOAT64)) == 0) {
163 return ((vector<double>*) alphaField->getData())->size();
165 else if (type.compare(
string(INT16)) == 0) {
166 return ((vector<short>*) alphaField->getData())->size();
168 else if (type.compare(
string(INT32)) == 0) {
169 return ((vector<int>*) alphaField->getData())->size();
177CSV_Obj::getFieldData(
const string& field)
180 CSV_Field *f = _header->getField(field);
182 int index = f->getIndex();
183 CSV_Data *d = _data->at(index);
188 string err = (string)
"Unable to get data for field " + field;
189 throw BESInternalError(err, __FILE__, __LINE__);
193 string err = (string)
"Unable to get data for field " + field +
", no such field exists";
194 throw BESInternalError(err, __FILE__, __LINE__);
199vector<string> CSV_Obj::getRecord(
const int rowNum)
201 vector<string> record;
205 int maxRows = getRecordCount();
206 if (rowNum > maxRows) {
208 err <<
"Attempting to retrieve row " << rowNum <<
" of " << maxRows;
209 throw BESInternalError(err.str(), __FILE__, __LINE__);
212 vector<string> fieldList;
213 getFieldList(fieldList);
214 vector<string>::iterator it = fieldList.begin();
215 vector<string>::iterator et = fieldList.end();
216 for (; it != et; it++) {
217 string fieldName = (*it);
219 fieldData = getFieldData(fieldName);
220 CSV_Field *f = _header->getField(fieldName);
223 err <<
"Unable to retrieve data for field " << fieldName <<
" on row " << rowNum;
224 throw BESInternalError(err.str(), __FILE__, __LINE__);
228 if (type.compare(
string(STRING)) == 0) {
229 record.push_back(((vector<string>*) fieldData)->at(rowNum));
231 else if (type.compare(
string(FLOAT32)) == 0) {
232 oss << ((vector<float>*) fieldData)->at(rowNum);
233 record.push_back(oss.str());
235 else if (type.compare(
string(FLOAT64)) == 0) {
236 oss << ((vector<double>*) fieldData)->at(rowNum);
237 record.push_back(oss.str());
239 else if (type.compare(
string(INT16)) == 0) {
240 oss << ((vector<short>*) fieldData)->at(rowNum);
241 record.push_back(oss.str());
243 else if (type.compare(
string(INT32)) == 0) {
244 oss << ((vector<int>*) fieldData)->at(rowNum);
245 record.push_back(oss.str());
254 strm << BESIndent::LMarg <<
"CSV_Obj::dump - (" << (
void *)
this <<
")" << endl;
257 strm << BESIndent::LMarg <<
"reader:" << endl;
260 BESIndent::UnIndent();
263 strm << BESIndent::LMarg <<
"header:" << endl;
266 BESIndent::UnIndent();
269 strm << BESIndent::LMarg <<
"data:" << endl;
271 BESIndent::UnIndent();
virtual void dump(std::ostream &strm) const
dump the contents of this object to the specified ostream
static void slim(std::string &str)
Strips leading and trailing double quotes from string.