bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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
39using std::ostream;
40using std::fstream;
41using std::endl;
42using std::ios;
43using std::string;
44using std::vector;
45
46CSV_Reader::CSV_Reader(): _row_number(0) {
47 _stream_in = new fstream();
48}
49
50CSV_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
60bool
61CSV_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
72bool
73CSV_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
84bool
85CSV_Reader::eof() const {
86 return _stream_in->eof();
87}
88
89void
90CSV_Reader::reset() {
91 _row_number = 0;
92 _stream_in->seekg(ios::beg);
93}
94
95
96void
97CSV_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.size() 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.size() == 0 || line[0] == '#'));
109
110 CSV_Utils::split(line, ',', row);
111 _row_number++;
112}
113
114void
115CSV_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
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