CSV_Obj.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<iomanip>
00038
00039 #include"CSV_Obj.h"
00040
00041 CSV_Obj::CSV_Obj() {
00042 reader = new CSV_Reader();
00043 header = new CSV_Header();
00044 data = new vector<CSV_Data*>();
00045 }
00046
00047 CSV_Obj::~CSV_Obj() {
00048 reader->close();
00049 delete reader;
00050 delete header;
00051 delete data;
00052 }
00053
00054 bool CSV_Obj::open(const string& filepath) {
00055 return reader->open(filepath);
00056 }
00057
00058 void CSV_Obj::printField(const string& field) {
00059
00060 void* fieldData;
00061 string type;
00062
00063 try {
00064 cout << "FIELD: " << field << endl;
00065 fieldData = getFieldData(field);
00066 type = header->getField(field)->getType();
00067
00068 if(type.compare(STRING) == 0) {
00069 for(vector<string>::iterator it = ((vector<string>*)fieldData)->begin();
00070 it != ((vector<string>*)fieldData)->end();
00071 it++) {
00072 cout << *it << endl;
00073 }
00074 } else if(type.compare(FLOAT32) == 0) {
00075 for(vector<float>::iterator it = ((vector<float>*)fieldData)->begin();
00076 it != ((vector<float>*)fieldData)->end();
00077 it++) {
00078 cout << *it << endl;
00079 }
00080 } else if(type.compare(FLOAT64) == 0) {
00081 for(vector<double>::iterator it = ((vector<double>*)fieldData)->begin();
00082 it != ((vector<double>*)fieldData)->end();
00083 it++) {
00084 cout << *it << endl;
00085 }
00086 } else if(type.compare(INT16) == 0) {
00087 for(vector<short>::iterator it = ((vector<short>*)fieldData)->begin();
00088 it != ((vector<short>*)fieldData)->end();
00089 it++) {
00090 cout << *it << endl;
00091 }
00092 } else if(type.compare(INT32) == 0) {
00093 for(vector<int>::iterator it = ((vector<int>*)fieldData)->begin();
00094 it != ((vector<int>*)fieldData)->end();
00095 it++) {
00096 cout << *it << endl;
00097 }
00098 }
00099 cout << endl;
00100 } catch(string& error) { }
00101 }
00102
00103 void CSV_Obj::load() {
00104 vector<string> txtLine;
00105 bool OnHeader = true;
00106 reader->reset();
00107 while(!reader->eof()) {
00108 txtLine = reader->get();
00109 if(OnHeader) {
00110 if(header->populate(&txtLine)) {
00111 for(unsigned int i = 0; i < txtLine.size(); i++) {
00112 data->push_back(new CSV_Data());
00113 }
00114 OnHeader = false;
00115 }
00116 } else if(!txtLine.empty()) {
00117 int index = 0;
00118 for(vector<CSV_Data*>::iterator it = data->begin(); it != data->end(); it++) {
00119 try {
00120 string token = txtLine.at(index);
00121 slim(token);
00122 (*it)->insert(header->getField(index), &token);
00123 } catch(...) {
00124 (*it)->insert(header->getField(index),new string(""));
00125 }
00126 index++;
00127 }
00128 txtLine.clear();
00129 }
00130 }
00131 }
00132
00133 vector<string> CSV_Obj::getFieldList() {
00134 return header->getFieldList();
00135 }
00136
00137 string CSV_Obj::getFieldType(const string& fieldName) {
00138 return header->getFieldType(fieldName);
00139 }
00140
00141 int CSV_Obj::getRecordCount() {
00142 CSV_Data* alphaField = data->at(0);
00143 string type = alphaField->getType();
00144
00145 if(type.compare(string(STRING)) == 0) {
00146 return ((vector<string>*)alphaField->getData())->size();
00147 } else if(type.compare(string(FLOAT32)) == 0) {
00148 return ((vector<float>*)alphaField->getData())->size();
00149 } else if(type.compare(string(FLOAT64)) == 0) {
00150 return ((vector<double>*)alphaField->getData())->size();
00151 } else if(type.compare(string(INT16)) == 0) {
00152 return ((vector<short>*)alphaField->getData())->size();
00153 } else if(type.compare(string(INT32)) == 0) {
00154 return ((vector<int>*)alphaField->getData())->size();
00155 } else {
00156 return -1;
00157 }
00158 }
00159
00160 void* CSV_Obj::getFieldData(const string& field) {
00161 try {
00162 int index = header->getField(field)->getIndex();
00163 return data->at(index)->getData();
00164 } catch(string error) {
00165 cerr << error << endl;
00166 return (void *)0;
00167 }
00168 }
00169
00170 vector<string> CSV_Obj::getRecord(const int rowNum) {
00171 vector<string> record;
00172 void* fieldData;
00173 string type;
00174
00175 if(rowNum > getRecordCount())
00176 return record;
00177
00178 vector<string> fieldList = getFieldList();
00179 for(vector<string>::iterator it = fieldList.begin(); it != fieldList.end(); it++) {
00180 ostringstream oss;
00181 fieldData = getFieldData(*it);
00182 type = header->getField(*it)->getType();
00183
00184 if(type.compare(string(STRING)) == 0) {
00185 record.push_back(((vector<string>*)fieldData)->at(rowNum));
00186 } else if(type.compare(string(FLOAT32)) == 0) {
00187 oss << ((vector<float>*)fieldData)->at(rowNum);
00188 record.push_back(oss.str());
00189 } else if(type.compare(string(FLOAT64)) == 0) {
00190 oss << ((vector<double>*)fieldData)->at(rowNum);
00191 record.push_back(oss.str());
00192 } else if(type.compare(string(INT16)) == 0) {
00193 oss << ((vector<short>*)fieldData)->at(rowNum);
00194 record.push_back(oss.str());
00195 } else if(type.compare(string(INT32)) == 0) {
00196 oss << ((vector<int>*)fieldData)->at(rowNum);
00197 record.push_back(oss.str());
00198 }
00199 }
00200
00201 return record;
00202 }