MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
File.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 
27 #ifndef FILE_H
28 #define FILE_H
29 #include <fstream>
30 #include <cstdlib>
31 
35 enum class FileType : unsigned char
36 {
40  NO_FILE = 0,
44  ONE_FILE = 1,
48  MULTIPLE_FILES = 2,
53 };
54 
55 
56 
57 std::string to_string_padded(unsigned int value);
58 
59 
63 std::ostream& operator<<(std::ostream&os, FileType fileType);
64 
68 std::istream& operator>>(std::istream&is, FileType&fileType);
69 
70 
78 class File
79 {
80 public:
81 
82 //constructors
86  File();
87 
91  virtual ~File();
92 
93  //set and get functions
97  std::fstream& getFstream();
98 
102  const std::string& getName() const;
103 
107  const std::string getFullName() const;
108 
113  void setName(const std::string& name);
114 
118  FileType getFileType() const;
119 
123  void setFileType(FileType fileType);
124 
128  unsigned int getCounter() const;
129 
133  void setCounter(unsigned int counter);
134 
138  std::fstream::openmode getOpenMode() const;
139 
143  void setOpenMode(std::fstream::openmode openMode);
144 
148  unsigned int getSaveCount() const;
149 
153  void setSaveCount(unsigned int saveCount);
154 
158  unsigned int getNextSavedTimeStep() const;
159 
163  void setNextSavedTimeStep(unsigned int nextSavedTimeStep);
164 
168  bool saveCurrentTimestep(unsigned int ntimeSteps);
169 
173  void read(std::istream& is);
174 
178  void write(std::ostream& os) const;
179 
184  friend std::ostream& operator <<(std::ostream& os, const File& o);
185 
190  friend std::istream& operator >>(std::istream& is, File &o);
191 
192 //member functions (other than set/get)
193 
198  bool open();
199 
203  bool open(std::fstream::openmode openMode);
204 
209  bool openNextFile();
210 
214  bool openNextFile(std::fstream::openmode openMode);
215 
219  void close();
220 
221 private:
225  std::string name_;
226 
230  std::fstream fstream_;
231 
237 
241  unsigned int counter_;
242 
246  std::fstream::openmode openMode_;
247 
252  unsigned int saveCount_;
253 
257  unsigned int nextSavedTimeStep_;
258 };
259 
260 #endif /* FILE_H */
FileType fileType_
fileType_ indicates the type of the files. Whether it is No file, one file or multiple file as descri...
Definition: File.h:236
each time-step will be written into/read from separate files numbered consecutively, with numbers padded by zeros to a minimum of four digits: name_.0000, name_.0001, ..
FileType getFileType() const
Gets the file type e.g. NOFILE, ONEFILE and MULTIPLE FILES. File::fileType_.
Definition: File.cc:202
std::fstream::openmode getOpenMode() const
Allows the user to know the file mode i.e. gets File::openMode_.
Definition: File.cc:260
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:60
void read(std::istream &is)
read function, which accepts an input stream std::istream.
Definition: File.cc:370
void setCounter(unsigned int counter)
Allows the user to set the file counter according to his need. Sets File::counter_.
Definition: File.cc:223
const std::string getFullName() const
Also allows to access the file name, however with additional information which is the file counter...
Definition: File.cc:170
unsigned int nextSavedTimeStep_
the time step at which the next write or read operation has to happen.
Definition: File.h:257
bool openNextFile()
This function should be called before a data corresponding to the new time step is written or read...
Definition: File.cc:230
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:35
unsigned int counter_
counts the number of the next file to be opened; needed if multiple files are written/read ...
Definition: File.h:241
unsigned int getSaveCount() const
Gets File::saveCount_.
Definition: File.cc:274
void close()
Closes the file by calling fstream_.close()
Definition: File.cc:360
std::istream & operator>>(std::istream &is, FileType &fileType)
Reads the FileType from an input stream 'is'.
Definition: File.cc:95
std::fstream::openmode openMode_
A variable to indicate how the file should be opened i.e. in, out, ... see http://en.cppreference.com (std::fstream::out by default)
Definition: File.h:246
unsigned int getCounter() const
In case of multiple files, File::getCounter() returns the the number (FILE::Counter_) of the next fil...
Definition: File.cc:216
std::ostream & operator<<(std::ostream &os, FileType fileType)
Writes the FileType as a human-readable string into the output stream 'os'.
Definition: File.cc:72
file will not be created/read
std::fstream & getFstream()
Allows to access the member variable File::fstream_.
Definition: File.cc:150
void setNextSavedTimeStep(unsigned int nextSavedTimeStep)
Sets File::nextSavedTimeStep_.
Definition: File.cc:298
File()
constructor
Definition: File.cc:119
all data will be written into/ read from a single file called name_
unsigned int getNextSavedTimeStep() const
Gets File::nextSavedTimeStep_.
Definition: File.cc:290
unsigned int saveCount_
Allows one to define the number of timesteps to be skipped to make a snap shot. E.g. TMax = 100, saveCount_ = 10, timeStep = 1; It stores data at t={0,10,20,30,40...100}. And if TMax =101, it stores data at t={0,10,20,30,...100,101}.
Definition: File.h:252
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_.
Definition: File.cc:282
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:402
void setFileType(FileType fileType)
Sets the type of file needed to write into or read from. File::fileType_.
Definition: File.cc:209
bool open()
Checks if the file stream fstream_ has any issues while opening. Alongside, it also increments the ne...
Definition: File.cc:318
bool saveCurrentTimestep(unsigned int ntimeSteps)
Definition: File.cc:307
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:412
std::string name_
name of the file.
Definition: File.h:225
virtual ~File()
destructor
Definition: File.cc:142
each time-step will be written into/read from separate files numbered consecutively: name_...
void setName(const std::string &name)
Sets the file name, e.g. "Name.data".
Definition: File.cc:195
Definition: File.h:78
void write(std::ostream &os) const
print function, which accepts an std::stringstream as input.
Definition: File.cc:386
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:162
std::fstream fstream_
Stream object used to read/write data files.
Definition: File.h:230
void setOpenMode(std::fstream::openmode openMode)
Allows the user to Sets File::openMode_.
Definition: File.cc:267