File.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef FILE_H
6 #define FILE_H
7 
8 #include <fstream>
9 #include "GeneralDefine.h"
10 
11 // A value of File::lastSavedTimeStep_ = NEVER indicates a file was never been written
13 const unsigned NEVER = static_cast<const unsigned int>(-1);
14 
18 enum class FileType : unsigned char
19 {
23  NO_FILE = 0,
27  ONE_FILE = 1,
31  MULTIPLE_FILES = 2,
36 };
37 
38 
39 std::string to_string_padded(unsigned int value);
40 
44 std::ostream& operator<<(std::ostream& os, FileType fileType);
45 
49 std::istream& operator>>(std::istream& is, FileType& fileType);
50 
58 class File
59 {
60 public:
61 
62  //constructors
66  File();
67 
71  File(const File&);
72 
76  virtual ~File();
77 
78 
79  //set and get functions
83  std::fstream& getFstream();
84 
88  const std::string& getName() const;
89 
93  const std::string getFullName() const;
94 
98  const std::string getFullName(unsigned) const;
99 
104  void setName(const std::string& name);
105 
109  FileType getFileType() const;
110 
114  void setFileType(FileType fileType);
115 
119  unsigned int getCounter() const;
120 
124  void setCounter(unsigned int counter);
125 
127 
129 
133  std::fstream::openmode getOpenMode() const;
134 
138  void setOpenMode(std::fstream::openmode openMode);
139 
143  unsigned int getSaveCount() const;
144 
149 
153  void setSaveCount(unsigned int saveCount);
154 
158  unsigned int getLastSavedTimeStep() const;
159 
163  void setLastSavedTimeStep(unsigned int lastSavedTimeStep);
164 
168  bool saveCurrentTimeStep(unsigned int ntimeSteps);
169  bool saveCurrentTimeStepNoFileTypeCheck(unsigned int ntimeSteps);
170 
174  void read(std::istream& is);
175 
179  void write(std::ostream& os) const;
180 
185  friend std::ostream& operator<<(std::ostream& os, const File& o);
186 
191  friend std::istream& operator>>(std::istream& is, File& o);
192 
193  //member functions (other than set/get)
194 
199  bool open();
200 
204  bool openWrite(unsigned);
205 
206  bool openWriteNoAppend(unsigned);
207 
211  bool open(std::fstream::openmode openMode);
212 
216  void close();
217 
221  void setlogarithmicSaveCount(const Mdouble logarithmicSaveCountBase);
222 
223 private:
228 
232  std::fstream fstream_;
233 
239 
243  unsigned int counter_;
244 
248  std::fstream::openmode openMode_;
249 
254  unsigned int saveCount_;
255 
261 
265  unsigned int lastSavedTimeStep_;
266 };
267 
268 #endif /* FILE_H */
const unsigned NEVER
Definition: File.h:13
std::string to_string_padded(unsigned int value)
Pads the number This function tries to pad the number to 4 digits, which is used when you create mult...
Definition: File.cc:22
FileType
With FileType options, one is able to choose if data is to be read/written from/into no or single or ...
Definition: File.h:19
@ MULTIPLE_FILES
each time-step will be written into/read from separate files numbered consecutively: name_....
@ MULTIPLE_FILES_PADDED
each time-step will be written into/read from separate files numbered consecutively,...
@ NO_FILE
file will not be created/read
@ ONE_FILE
all data will be written into/ read from a single file called name_
std::istream & operator>>(std::istream &is, FileType &fileType)
Reads the FileType from an input stream 'is'.
Definition: File.cc:56
double Mdouble
Definition: GeneralDefine.h:13
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition: IO.h:222
Definition: File.h:59
unsigned int saveCount_
Allows one to define the number of time steps to be skipped to make a snap shot. E....
Definition: File.h:254
std::fstream & getFstream()
Allows to access the member variable File::fstream_.
Definition: File.cc:131
void setOpenMode(std::fstream::openmode openMode)
Allows the user to Sets File::openMode_.
Definition: File.cc:225
unsigned int getLastSavedTimeStep() const
Gets File::nextSavedTimeStep_.
Definition: File.cc:271
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:143
void read(std::istream &is)
read function, which accepts an input stream std::istream.
Definition: File.cc:395
void setlogarithmicSaveCount(const Mdouble logarithmicSaveCountBase)
the function to set the user input base of logarithmic saving count
Definition: File.cc:261
std::fstream::openmode getOpenMode() const
Allows the user to know the file mode i.e. gets File::openMode_.
Definition: File.cc:217
FileType fileType_
fileType_ indicates the type of the files. Whether it is No file, one file or multiple file as descri...
Definition: File.h:238
void decreaseCounter()
Definition: File.h:128
void setName(const std::string &name)
Sets the file name, e.g. "Name.data".
Definition: File.cc:176
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_.
Definition: File.cc:251
bool saveCurrentTimeStep(unsigned int ntimeSteps)
determined if this time step has to be written; if so, opens the output file
Definition: File.cc:290
const std::string getFullName() const
Also allows to access the file name, however with additional information which is the file counter,...
Definition: File.cc:148
std::fstream fstream_
Stream object used to read/write data files.
Definition: File.h:232
void writeFirstAndLastTimeStep()
Sets File::saveCount_ to the highest possible value such that only the first and last time step is wr...
Definition: File.cc:242
void increaseCounter()
Definition: File.h:126
unsigned int counter_
counts the number of already opened files, i.e. counter=1 means .0000 exists
Definition: File.h:243
void write(std::ostream &os) const
print function, which accepts an std::stringstream as input.
Definition: File.cc:420
unsigned int lastSavedTimeStep_
the time step at which the next write or read operation has to happen.
Definition: File.h:265
void close()
Closes the file by calling fstream_.close()
Definition: File.cc:385
void setLastSavedTimeStep(unsigned int lastSavedTimeStep)
Sets File::nextSavedTimeStep_.
Definition: File.cc:280
std::fstream::openmode openMode_
A variable to indicate how the file should be opened i.e. in, out, ... see http://en....
Definition: File.h:248
Mdouble logarithmicSaveCountBase_
the switch allow user to set saveCount in logarithmic timescale with equal distance ,...
Definition: File.h:260
void setCounter(unsigned int counter)
Allows the user to set the file counter according to his need. Sets File::counter_.
Definition: File.cc:209
bool openWriteNoAppend(unsigned)
Definition: File.cc:376
std::string name_
name of the file.
Definition: File.h:227
bool open()
Checks if the file stream fstream_ has any issues while opening. Alongside, it also increments the ne...
Definition: File.cc:325
friend std::istream & operator>>(std::istream &is, File &o)
Operator overloading used to read data from the input stream into member variables of an object of cl...
Definition: File.cc:451
void setFileType(FileType fileType)
Sets the type of file needed to write into or read from. File::fileType_.
Definition: File.cc:193
virtual ~File()
destructor
unsigned int getCounter() const
In case of multiple files, File::getCounter() returns the the number (FILE::Counter_) of the next fil...
Definition: File.cc:201
bool openWrite(unsigned)
First sets openmode to write (and append in some cases), then calls open().
Definition: File.cc:359
File()
constructor
Definition: File.cc:79
bool saveCurrentTimeStepNoFileTypeCheck(unsigned int ntimeSteps)
Definition: File.cc:295
FileType getFileType() const
Gets the file type e.g. NOFILE, ONEFILE and MULTIPLE FILES. File::fileType_.
Definition: File.cc:185
friend std::ostream & operator<<(std::ostream &os, const File &o)
Operator overloading used to write data obtained from an object of class File into an output stream....
Definition: File.cc:440
unsigned int getSaveCount() const
Gets File::saveCount_.
Definition: File.cc:233
squared absolute value
Definition: GlobalFunctions.h:87
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
string name
Definition: plotDoE.py:33