CSV_Header.cc

Go to the documentation of this file.
00001 // CSV_Header.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"CSV_Header.h"
00038 
00039 CSV_Header::CSV_Header() {
00040   hdr = new map<string, CSV_Field*>;
00041   index2field = new map<int,string>;
00042 }
00043 
00044 CSV_Header::~CSV_Header() {
00045   delete hdr;
00046   delete index2field;
00047 }
00048 
00049 const bool CSV_Header::populate(vector<string>* foo) {
00050   
00051   string::size_type lastPos;
00052 
00053   string fieldName;
00054   string fieldType;
00055   int fieldIndex = 0;
00056 
00057   for(vector<string>::iterator it = foo->begin(); it != foo->end(); it++) {
00058     slim(*it);
00059     lastPos = (*it).find_first_of("<",0);
00060     fieldName = (*it).substr(0,lastPos);
00061     fieldType = (*it).substr(lastPos + 1,(*it).length() - lastPos - 2);
00062     
00063     CSV_Field* field = new CSV_Field();
00064     field->insertName(fieldName);
00065     field->insertType(fieldType);
00066     field->insertIndex(fieldIndex);
00067     
00068     hdr->insert(make_pair(fieldName,field));
00069     index2field->insert(make_pair(fieldIndex,fieldName));
00070     
00071     fieldIndex++;
00072   }
00073 
00074   return true;
00075 }
00076 
00077 CSV_Field* CSV_Header::getField(const int& index) throw(string) {
00078   if(index2field->find(index) != index2field->end()) {
00079     string fieldName = index2field->find(index)->second;
00080     return hdr->find(fieldName)->second;
00081   } else {
00082     ostringstream osstream;
00083     osstream << "Could not find field at index " << index << endl;
00084     throw osstream.str();
00085   }
00086 }
00087 
00088 CSV_Field* CSV_Header::getField(const string& fieldName) throw(string) {
00089   if(hdr->find(fieldName) != hdr->end()) {
00090     return hdr->find(fieldName)->second;
00091   } else {
00092     ostringstream osstream;
00093     osstream <<  "Could not find field \"" << fieldName << "\"\n";
00094     throw osstream.str();
00095   }
00096 }
00097 
00098 const string CSV_Header::getFieldType(const string& fieldName) {
00099   map<string,CSV_Field*>::iterator it = hdr->find(fieldName);
00100   
00101   if(it != hdr->end())
00102     return (it->second)->getType();
00103   else
00104     return "";
00105 }
00106 
00107 void CSV_Header::print() {
00108   for(unsigned int index = 0; index < index2field->size(); index++) {
00109     string field = index2field->find(index)->second;
00110     CSV_Field* csvField = hdr->find(field)->second;
00111     cout << csvField->getIndex() << "\t" << csvField->getType() 
00112          << "\t" << csvField->getName() << "\n";
00113   }
00114 }
00115 
00116 void slim(string& str) {
00117   if(*(--str.end()) == '\"' and *str.begin() == '\"')
00118     str = str.substr(1,str.length() - 2);
00119 }
00120 
00121 vector<string> CSV_Header::getFieldList() {
00122   vector<string> fieldList;
00123   for(unsigned int index = 0; index < index2field->size(); index++) {
00124     fieldList.push_back(index2field->find(index)->second);
00125   }
00126   return fieldList;
00127 }

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