bes  Updated for version 3.20.5
CSV_Reader.cc
1 // CSV_Reader.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Stephan Zednik <zednik@ucar.edu> and Patrick West <pwest@ucar.edu>
8 // and Jose Garcia <jgarcia@ucar.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
28 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
29 //
30 // Authors:
31 // zednik Stephan Zednik <zednik@ucar.edu>
32 // pwest Patrick West <pwest@ucar.edu>
33 // jgarcia Jose Garcia <jgarcia@ucar.edu>
34 
35 #include "CSV_Reader.h"
36 #include "CSV_Utils.h"
37 #include "BESUtil.h"
38 
39 CSV_Reader::CSV_Reader()
40 {
41  _stream_in = new fstream() ;
42 }
43 
44 CSV_Reader::~CSV_Reader()
45 {
46  if( _stream_in )
47  {
48  if( _stream_in->is_open() )
49  {
50  _stream_in->close();
51  }
52  delete _stream_in;
53  _stream_in = 0 ;
54  }
55 }
56 
57 bool
58 CSV_Reader::open( const string& filepath )
59 {
60  bool ret = false ;
61  _filepath = filepath ;
62  _stream_in->open( filepath.c_str(), fstream::in ) ;
63  if( !(_stream_in->fail()) && _stream_in->is_open() )
64  {
65  ret = true ;
66  }
67  return ret ;
68 }
69 
70 bool
71 CSV_Reader::close() const
72 {
73  bool ret = false ;
74  if( _stream_in )
75  {
76  _stream_in->close() ;
77  if( !(_stream_in->bad()) && !(_stream_in->is_open()) )
78  {
79  ret = true ;
80  }
81  }
82  return ret ;
83 }
84 
85 bool
86 CSV_Reader::eof() const
87 {
88  return _stream_in->eof() ;
89 }
90 
91 void
92 CSV_Reader::reset()
93 {
94  _stream_in->seekg( ios::beg ) ;
95 }
96 
97 
98 void
99 CSV_Reader::get( vector<string> &row )
100 {
101  string line ;
102 
103  getline( *_stream_in, line ) ;
104  CSV_Utils::split( line, ',', row ) ;
105 }
106 
107 void
108 CSV_Reader::dump( ostream &strm ) const
109 {
110  strm << BESIndent::LMarg << "CSV_Reader::dump - ("
111  << (void *)this << ")" << endl ;
112  BESIndent::Indent() ;
113  if( _stream_in )
114  {
115  strm << BESIndent::LMarg << "File " << _filepath << " is open" << endl ;
116  }
117  else
118  {
119  strm << BESIndent::LMarg << "No stream opened at this time" << endl ;
120  }
121  BESIndent::UnIndent() ;
122 }
CSV_Utils::split
static void split(const string &str, char delimiter, vector< string > &tokens)
Splits a string into separate strings based on the delimiter.
Definition: CSV_Utils.cc:48
CSV_Reader::dump
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
Definition: CSV_Reader.cc:108