CSV_Obj.cc

Go to the documentation of this file.
00001 // CSV_Obj.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
00007 // Author: Stephan Zednik <zednik@ucar.edu> and Patrick West <pwest@ucar.edu>
00008 // and Jose Garcia <jgarcia@ucar.edu>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 // 
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 // 
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact University Corporation for Atmospheric Research at
00025 // 3080 Center Green Drive, Boulder, CO 80301
00026  
00027 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00028 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00029 //
00030 // Authors:
00031 //      zednik      Stephan Zednik <zednik@ucar.edu>
00032 //      pwest       Patrick West <pwest@ucar.edu>
00033 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
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; //throw error?
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 }

Generated on Sat Aug 22 06:06:27 2009 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.6.0