CSV_Reader.cc

Go to the documentation of this file.
00001 // CSV_Reader.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"CSV_Reader.h"
00036 
00037 CSV_Reader::CSV_Reader() {
00038   stream_in = new fstream();
00039 }
00040 
00041 CSV_Reader::~CSV_Reader() {
00042   if(stream_in->is_open())
00043     stream_in->close();
00044   delete stream_in;
00045 }
00046 
00047 const bool CSV_Reader::open(const string& filepath) {
00048   stream_in->open(filepath.c_str(),fstream::in);
00049   if(stream_in->fail() or !(stream_in->is_open()))
00050     return false;
00051   else
00052     return true;
00053 }
00054 
00055 const bool CSV_Reader::close() {
00056   stream_in->close();
00057   if(stream_in->bad() || stream_in->is_open())
00058     return false;
00059   else
00060     return true;
00061 }
00062 
00063 const bool CSV_Reader::eof() {
00064   return stream_in->eof();
00065 }
00066 
00067 void CSV_Reader::reset() {
00068   stream_in->seekg(ios::beg);
00069 }
00070 
00071 vector<string> CSV_Reader::get() {
00072   vector<string> foo;
00073   string bar;
00074 
00075   getline(*stream_in, bar);
00076   foo = split(bar,",");
00077   return foo;
00078 }
00079 
00080 vector<string> split(const string& str, const string& delimiters) {
00081   vector<string> tokens;
00082   string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00083   string::size_type pos = str.find_first_not_of(delimiters, lastPos);
00084 
00085   while(string::npos != pos || string::npos != lastPos) {
00086     if(lastPos != pos)
00087       tokens.push_back(str.substr(lastPos, pos - lastPos));
00088     lastPos = str.find_first_not_of(delimiters, pos);
00089     pos = str.find_first_of(delimiters, lastPos);
00090   }
00091 
00092   return tokens;
00093 }
00094 

Generated on Thu Feb 11 09:13:16 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.4.7