CEPS  24.01
Cardiac ElectroPhysiology Simulator
FileReader.cpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
35 
37 {
38 }
39 
41  m_fileName(fileName)
42 {
43 }
44 
46  m_fileName(that.m_fileName)
47 {
48 }
49 
52 {
53  if (ceps::isDifferentPtr(this, &that))
54  m_fileName = that.m_fileName;
55  return *this;
56 }
57 
59 {
60  this->close();
61 }
62 
63 void FileReader::setFileName(const CepsString &fileName)
64 {
65  m_fileName = fileName;
66  return;
67 }
68 
69 CepsBool
71 {
72  m_file.open(m_fileName.c_str(), std::ifstream::in);
73 
74  if (! m_file.good())
75  {
76  CEPS_WARNS("Could not open file " << m_fileName);
77  return false;
78  }
79 
80  return true;
81 }
82 
83 void
85 {
86  if (m_file.is_open())
87  m_file.close();
88  return;
89 }
90 
93 {
94  return m_file.good();
95 }
96 
97 void
99 {
100  // m_file.seekg(0, std::ios::beg); Does not work if EOF has been hit
101  this->close();
102  this->open();
103  return;
104 }
105 
106 CepsBool
108 {
109  CepsString keyword;
110  if (m_file.good())
111  {
112  m_file >> keyword;
113  while (! m_file.eof() && (keyword.compare(pattern) != 0))
114  m_file >> keyword;
115 
116  // Check that we did not end because of eof
117  if (checkEOF())
118  {
119  CEPS_WARNS("No more keyword: " << pattern << " in file " << m_fileName);
120  return false;
121  }
122 
123  // pattern has been found
124  return true;
125  }
126  CEPS_WARNS("Unexpected EOF. Keyword " << pattern << " not found in file" << m_fileName);
127  return false;
128 }
129 
130 CepsUInt
132 {
133  // close and reopen the file
134  close();
135  if (open())
136  {
137  CepsString keyword;
138  do
139  m_file >> keyword;
140  while (keyword.compare (pattern) != 0 && ! m_file.eof ());
141 
142  // Check that we did not end because of eof
143  if (checkEOF())
144  return 0U;
145 
146  // pattern has been found
147  return 1U;
148  }
149 
150  return 0U;
151 }
152 
153 CepsUInt
155 {
156  CepsString line;
157  CepsUInt i, size = keywords.size ();
158  while (m_file.good ())
159  {
160  m_file >> line;
161  for (i = 0; i < size; i++)
162  if (line == keywords[i])
163  return 1;
164  }
165 
166  m_file.close ();
167  m_file.open (m_fileName.c_str (), std::ifstream::in);
168 
169  return 0;
170 }
171 
174 {
175  return m_fileName;
176 }
177 
178 CepsUInt
180 {
181  CepsString dummy;
182  CepsUInt i = 0;
183  if (! lines)
184  return 0;
185 
186  while (m_file.good() && i < lines)
187  {
188  dummy.clear();
189  std::getline(m_file, dummy);
190  i++;
191  }
192 
193  if (! m_file.good())
194  {
195  CEPS_WARNS("Could not skip " << lines << " lines in file " << m_fileName);
196  return 1;
197  }
198  return 0;
199 }
200 
201 void
203 {
204  std::getline(m_file, line);
205  return;
206 }
207 
208 CepsInt
210 {
211  CepsInt lineIndex = 0;
212  CepsString line;
213  while (std::getline(m_file, line))
214  {
215  // std::getline appends at end of CepsString, so it must be cleaned
216  if (line.find(word) != CepsString::npos)
217  return lineIndex;
218  lineIndex++;
219  line.clear ();
220  }
221  // not found
222  this->close ();
223  this->open ();
224  return -1;
225 }
226 
227 CepsUInt
229 {
230  if (m_file.eof())
231  {
232  CEPS_WARNS("End of file " << m_fileName << " reached.");
233  return 1;
234  }
235  return 0;
236 }
#define CEPS_WARNS(message)
Writes a warning in the debug log and in the terminal (stderr).
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
std::vector< _Type, _Alloc > CepsVector
C++ vector.
Definition: CepsTypes.hpp:155
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
int32_t CepsInt
Need 32 bit integer.
Definition: CepsTypes.hpp:106
Base class that regroups common reader functionalities.
Definition: FileReader.hpp:43
virtual CepsBool good()
true if stream is still readable
Definition: FileReader.cpp:92
FileReader & operator=(const FileReader &that)
Copy constructor.
Definition: FileReader.cpp:51
virtual ~FileReader()
Destructor. Calls FileReader::close.
Definition: FileReader.cpp:58
CepsUInt checkEOF()
Checks if the reader reached end of file.
Definition: FileReader.cpp:228
CepsInt lineIndex(const CepsString &word)
Index of first line starting with word, search starting from the current stream position.
Definition: FileReader.cpp:209
CepsUInt findNextOf(const CepsVector< CepsString > &patterns)
Advance stream until one of patterns is found.
Definition: FileReader.cpp:154
void reset()
Set file stream to the beginning of the file.
Definition: FileReader.cpp:98
virtual CepsBool open()
Opens the designated file in read mode.
Definition: FileReader.cpp:70
FileReader()
Alternative constructor.
Definition: FileReader.cpp:36
void oneLine(CepsString &line)
Get one line from the stream.
Definition: FileReader.cpp:202
CepsUInt skipLines(CepsUInt nbLines)
Skip given number of lines.
Definition: FileReader.cpp:179
void setFileName(const CepsString &fileName)
Set file to read.
Definition: FileReader.cpp:63
CepsString m_fileName
file to open
Definition: FileReader.hpp:145
CepsBool findNext(const CepsString &pattern)
Advance stream until pattern is found.
Definition: FileReader.cpp:107
CepsString getFileName()
Name of parsed file.
Definition: FileReader.cpp:173
std::ifstream m_file
stream
Definition: FileReader.hpp:146
virtual void close()
Close the file.
Definition: FileReader.cpp:84
CepsUInt find(const CepsString &pattern)
Advance stream until pattern is found.
Definition: FileReader.cpp:131
CepsBool isDifferentPtr(_Type *xptr, U *yptr)
Tells if two pointers designate two different addresses.
Definition: CepsMemory.hpp:86