CEPS  24.01
Cardiac ElectroPhysiology Simulator
FileWriter.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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
34 
36  m_binary(binary)
37 {
38 }
39 
41  CepsObject(),
42  m_fileName(ceps::getDir(file) + "/" + ceps::getFilename(file)),
43  m_binary(binary)
44 {
45 }
46 
48 {
49  close();
50 }
51 
54 {
55  return m_fileName;
56 }
57 
58 void
60 {
61  // Create directory if needed
62  CepsString command = "mkdir -p " + ceps::getDir(m_fileName);
63  ceps::execute(command,false,true);
64 
65  if (m_binary)
66  m_file.open(m_fileName.c_str(),std::ofstream::trunc | std::ofstream::binary);
67  else
68  m_file.open(m_fileName.c_str(),std::ofstream::trunc);
69 
70  CEPS_ABORT_IF(not m_file.good(),
71  "File writer: could not create file " << m_fileName
72  );
73  return;
74 }
75 
76 void
78 {
79  m_file.close();
80  return;
81 }
82 
85 {
86  return m_file.good();
87 }
88 
89 void
91 {
92  m_file.seekp(0,std::ios::beg);
93 }
94 
95 void
97 {
98  m_file << s;
99  return;
100 }
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
Base class for other (big) CEPS classes. All classes can get a pointer to this base class and also co...
Definition: CepsObject.hpp:40
void open()
Creates file or cleans previous content.
Definition: FileWriter.cpp:59
void reset()
The file stream is placed at the beginning of the file.
Definition: FileWriter.cpp:90
CepsString m_fileName
file to write
Definition: FileWriter.hpp:98
std::ofstream m_file
corresponding stream
Definition: FileWriter.hpp:99
void close()
Close current file.
Definition: FileWriter.cpp:77
FileWriter(CepsBool binary=false)
Constructor.
Definition: FileWriter.cpp:35
void write(const CepsString &s)
Single line writing.
Definition: FileWriter.cpp:96
CepsString getFileName()
Name of parsed file.
Definition: FileWriter.cpp:53
CepsBool good()
Tells if file is ok.
Definition: FileWriter.cpp:84
CepsBool m_binary
binary output if true
Definition: FileWriter.hpp:100
~FileWriter() override
Constructor.
Definition: FileWriter.cpp:47
A namespace for all utility methods.
CepsString getDir(const CepsString &str)
Get a substring of s, from beginning of s to the last '/' character. Example: "/home/someone/file....
Definition: CepsString.cpp:521
CepsString execute(CepsString command, CepsBool withErr=false, CepsBool abortOnErr=false)
Not really a parallel thing. Calls system() and deals with return code.
CepsString getFilename(const CepsString &str)
Returns a substring corresponding to the string after the last '/' character. Example: "/home/someone...
Definition: CepsString.cpp:556