pktools 2.6.7
Processing Kernel for geospatial data
FileReaderAscii.cc
1/**********************************************************************
2FileReaderAscii.cc: class to read ASCII files using (colum based)
3Copyright (C) 2008-2013 Pieter Kempeneers
4
5This file is part of pktools
6
7pktools is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12pktools is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with pktools. If not, see <http://www.gnu.org/licenses/>.
19***********************************************************************/
20
21#include <iostream>
22#include "FileReaderAscii.h"
23
24FileReaderAscii::FileReaderAscii(void)
25 : m_min(0),m_max(0),m_minRow(0),m_maxRow(0),m_fs(' '),m_comment('#'){
26}
27
28FileReaderAscii::FileReaderAscii(const std::string& filename)
29 : m_min(0),m_max(0),m_minRow(0),m_maxRow(0),m_fs(' '),m_comment('#'){
30 open(filename);
31}
32
33FileReaderAscii::FileReaderAscii(const std::string& filename, const char& fieldseparator)
34 : m_min(0),m_max(0),m_minRow(0),m_maxRow(0),m_fs(fieldseparator),m_comment('#'){
35 open(filename);
36}
37
38FileReaderAscii::~FileReaderAscii(void)
39{
40}
41
42void FileReaderAscii::open(const std::string& filename){
43 m_filename=filename;
44 m_ifstream.open(filename.c_str(),std::ios_base::in);
45 if(!(m_ifstream)){
46 std::string errorString;
47 errorString="Error: could not open file ";
48 errorString+=filename;
49 throw(errorString);
50 }
51}
52
53void FileReaderAscii::close(){
54 m_ifstream.close();
55 // m_ifstream.clear();
56}
57
58unsigned int FileReaderAscii::nrOfCol(bool checkCols, bool verbose){
59 reset();
60 unsigned int totalCol=0;
61 unsigned int nrow=0;
62 if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
63 if(verbose)
64 std::cout << "reading csv file " << m_filename << std::endl;
65 std::string csvRecord;
66 while(getline(m_ifstream,csvRecord)){//read a line
67 std::istringstream csvstream(csvRecord);
68 std::string item;
69 unsigned int ncol=0;
70 bool isComment=false;
71 while(getline(csvstream,item,m_fs)){//read a column
72 if(verbose)
73 std::cout << item << " ";
74 size_t pos=item.find(m_comment);
75 if(pos!=std::string::npos){
76 if(pos>0)
77 item=item.substr(0,pos-1);
78 else
79 break;
80 if(verbose)
81 std::cout << "comment found, string is " << item << std::endl;
82 isComment=true;
83 }
84 ++ncol;
85 if(isComment)
86 break;
87 }
88 if(verbose)
89 std::cout << std::endl << "number of columns: " << ncol << std::endl;
90 if(!totalCol)
91 totalCol=ncol;
92 else if(checkCols){
93 if(totalCol!=ncol){
94 std::ostringstream ess;
95 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << "!=" << totalCol << ")" << std::endl;
96 throw(ess.str());
97 }
98 ++nrow;
99 }
100 else
101 break;
102 }
103 }
104 else{//space or tab delimited fields
105 if(verbose)
106 std::cout << "space or tab delimited fields" << std::endl;
107 std::string spaceRecord;
108 while(!getline(m_ifstream, spaceRecord).eof()){
109 if(verbose>1)
110 std::cout << spaceRecord << std::endl;
111 std::istringstream lineStream(spaceRecord);
112 std::string item;
113 unsigned int ncol=0;
114 bool isComment=false;
115 while(lineStream >> item){
116 if(verbose)
117 std::cout << item << " ";
118 size_t pos=item.find(m_comment);
119 if(pos!=std::string::npos){
120 if(pos>0)
121 item=item.substr(0,pos-1);
122 else
123 break;
124 if(verbose)
125 std::cout << "comment found, string is " << item << std::endl;
126 isComment=true;
127 }
128 ++ncol;
129 if(isComment)
130 break;
131 }
132 if(verbose)
133 std::cout << std::endl << "number of columns: " << ncol << std::endl;
134 if(!totalCol)
135 totalCol=ncol;
136 else if(checkCols){
137 if(totalCol!=ncol){
138 std::ostringstream ess;
139 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << "!=" << totalCol << ")" << std::endl;
140 throw(ess.str());
141 }
142 }
143 else
144 break;
145 ++nrow;
146 }
147 }
148 return totalCol;
149}
150
151unsigned int FileReaderAscii::nrOfRow(bool checkCols, bool verbose){
152 reset();
153 unsigned int totalCol=0;
154 unsigned int nrow=0;
155 unsigned int ncomment=0;
156 if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
157 if(verbose)
158 std::cout << "reading csv file " << m_filename << std::endl;
159 std::string csvRecord;
160 while(getline(m_ifstream,csvRecord)){//read a line
161 std::istringstream csvstream(csvRecord);
162 std::string item;
163 unsigned int ncol=0;
164 bool isComment=false;
165 while(getline(csvstream,item,m_fs)){//read a column
166 if(verbose)
167 std::cout << item << " ";
168 size_t pos=item.find(m_comment);
169 if(pos!=std::string::npos){
170 if(pos>0){
171 if(verbose)
172 std::cout << "comment found, string is " << item << std::endl;
173 isComment=true;
174 }
175 else{
176 ++ncomment;
177 break;
178 }
179 }
180 ++ncol;
181 if(isComment)
182 break;
183 }
184 if(verbose)
185 std::cout << std::endl;
186 if(checkCols){
187 if(totalCol!=ncol){
188 std::ostringstream ess;
189 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << "!=" << totalCol << ")" << std::endl;
190 throw(ess.str());
191 }
192 }
193 ++nrow;
194 }
195 }
196 else{//space or tab delimited fields
197 if(verbose)
198 std::cout << "space or tab delimited fields" << std::endl;
199 std::string spaceRecord;
200 int totalCol=0;
201 while(!getline(m_ifstream, spaceRecord).eof()){
202 if(verbose>1)
203 std::cout << spaceRecord << std::endl;
204 std::istringstream lineStream(spaceRecord);
205 std::string item;
206 unsigned int ncol=0;
207 bool isComment=false;
208 while(lineStream >> item){
209 if(verbose)
210 std::cout << item << " ";
211 size_t pos=item.find(m_comment);
212 if(pos!=std::string::npos){
213 if(pos>0){
214 if(verbose)
215 std::cout << "comment found, string is " << item << std::endl;
216 isComment=true;
217 }
218 else{
219 ++ncomment;
220 break;
221 }
222 }
223 ++ncol;
224 if(isComment)
225 break;
226 }
227 if(verbose)
228 std::cout << std::endl << "number of columns: " << ncol << std::endl;
229 if(checkCols){
230 if(totalCol!=ncol){
231 std::ostringstream ess;
232 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << "!=" << totalCol << ")" << std::endl;
233 throw(ess.str());
234 }
235 }
236 ++nrow;
237 }
238 }
239 return nrow;
240}
241