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