bes  Updated for version 3.20.10
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(): _row_number(0) {
47  _stream_in = new fstream();
48 }
49 
50 CSV_Reader::~CSV_Reader() {
51  if (_stream_in) {
52  if (_stream_in->is_open()) {
53  _stream_in->close();
54  }
55  delete _stream_in;
56  _stream_in = 0;
57  }
58 }
59 
60 bool
61 CSV_Reader::open(const string &filepath) {
62  bool ret = false;
63  _filepath = filepath;
64  _stream_in->open(filepath.c_str(), fstream::in);
65  if (!(_stream_in->fail()) && _stream_in->is_open()) {
66  _row_number = 0;
67  ret = true;
68  }
69  return ret;
70 }
71 
72 bool
73 CSV_Reader::close() const {
74  bool ret = false;
75  if (_stream_in) {
76  _stream_in->close();
77  if (!(_stream_in->bad()) && !(_stream_in->is_open())) {
78  ret = true;
79  }
80  }
81  return ret;
82 }
83 
84 bool
85 CSV_Reader::eof() const {
86  return _stream_in->eof();
87 }
88 
89 void
90 CSV_Reader::reset() {
91  _row_number = 0;
92  _stream_in->seekg(ios::beg);
93 }
94 
95 
96 void
97 CSV_Reader::get(vector<string> &row) {
98  string line;
99 
100  // Read and ignore empty lines of comment lines. Comment lines
101  // must start with a '#'. Pretty primitive; if more is needed,
102  // add a function to test for a comment line. jhrg 3/11/21
103  do {
104  getline(*_stream_in, line);
105  // when we reach EOF, line.length() is zero and that condition
106  // (i.e., when row is zero in CSV_Utils::split() below) signals
107  // EOF to this handler. jhrg 3/11/21
108  } while(!_stream_in->eof() && (line.length() == 0 || line[0] == '#'));
109 
110  CSV_Utils::split(line, ',', row);
111  _row_number++;
112 }
113 
114 void
115 CSV_Reader::dump(ostream &strm) const {
116  strm << BESIndent::LMarg << "CSV_Reader::dump - ("
117  << (void *) this << ")" << endl;
118  BESIndent::Indent();
119  if (_stream_in) {
120  strm << BESIndent::LMarg << "File " << _filepath << " is open" << endl;
121  strm << BESIndent::LMarg << "Current row " << _row_number << endl;
122  }
123  else {
124  strm << BESIndent::LMarg << "No stream opened at this time" << endl;
125  }
126  BESIndent::UnIndent();
127 }
virtual void dump(std::ostream &strm) const
dump the contents of this object to the specified ostream
Definition: CSV_Reader.cc:115
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