bes  Updated for version 3.20.6
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 using std::ostream;
40 using std::fstream;
41 using std::endl;
42 using std::ios;
43 using std::string;
44 using std::vector;
45 
46 CSV_Reader::CSV_Reader()
47 {
48  _stream_in = new fstream() ;
49 }
50 
51 CSV_Reader::~CSV_Reader()
52 {
53  if( _stream_in )
54  {
55  if( _stream_in->is_open() )
56  {
57  _stream_in->close();
58  }
59  delete _stream_in;
60  _stream_in = 0 ;
61  }
62 }
63 
64 bool
65 CSV_Reader::open( const string& filepath )
66 {
67  bool ret = false ;
68  _filepath = filepath ;
69  _stream_in->open( filepath.c_str(), fstream::in ) ;
70  if( !(_stream_in->fail()) && _stream_in->is_open() )
71  {
72  ret = true ;
73  }
74  return ret ;
75 }
76 
77 bool
78 CSV_Reader::close() const
79 {
80  bool ret = false ;
81  if( _stream_in )
82  {
83  _stream_in->close() ;
84  if( !(_stream_in->bad()) && !(_stream_in->is_open()) )
85  {
86  ret = true ;
87  }
88  }
89  return ret ;
90 }
91 
92 bool
93 CSV_Reader::eof() const
94 {
95  return _stream_in->eof() ;
96 }
97 
98 void
99 CSV_Reader::reset()
100 {
101  _stream_in->seekg( ios::beg ) ;
102 }
103 
104 
105 void
106 CSV_Reader::get( vector<string> &row )
107 {
108  string line ;
109 
110  getline( *_stream_in, line ) ;
111  CSV_Utils::split( line, ',', row ) ;
112 }
113 
114 void
115 CSV_Reader::dump( ostream &strm ) const
116 {
117  strm << BESIndent::LMarg << "CSV_Reader::dump - ("
118  << (void *)this << ")" << endl ;
119  BESIndent::Indent() ;
120  if( _stream_in )
121  {
122  strm << BESIndent::LMarg << "File " << _filepath << " is open" << endl ;
123  }
124  else
125  {
126  strm << BESIndent::LMarg << "No stream opened at this time" << endl ;
127  }
128  BESIndent::UnIndent() ;
129 }
CSV_Utils::split
static void split(const std::string &str, char delimiter, std::vector< std::string > &tokens)
Splits a string into separate strings based on the delimiter.
Definition: CSV_Utils.cc:52
CSV_Reader::dump
virtual void dump(std::ostream &strm) const
dump the contents of this object to the specified ostream
Definition: CSV_Reader.cc:115