MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
File.cc
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 #include "File.h"
28 
29 #include <string>
30 #include <sstream>
31 #include <iostream>
32 #include <iomanip>
33 
35 // ///Allows to read a FileType using the operator>>, e.g., is >> fileType;
36 // ///It converts unsigned int to the enum FileType
37 //std::istream& operator >>(std::istream& is, FileType& i)
38 //{
39 // unsigned int tmp;
40 // if (is >> tmp)
41 // i = static_cast<FileType>(tmp);
42 // return is;
43 //}
44 //
45 // ///Allows to write a FileType using the operator<<, e.g., os << fileType;
46 // ///It converts the enum FileType to unsigned int
47 //std::ostream& operator <<(std::ostream& is, FileType& i)
48 //{
49 // is << static_cast<unsigned int>(i);
50 // return is;
51 //}
60 std::string to_string_padded(unsigned int value)
61 {
62  std::ostringstream out;
63  out << std::setw(4) << std::setfill('0') << value;
64  return out.str();
65 }
66 
72 std::ostream& operator<<(std::ostream&os, FileType fileType)
73 {
74  if (fileType == FileType::NO_FILE)
75  os << "NO_FILE";
76  else if (fileType == FileType::ONE_FILE)
77  os << "ONE_FILE";
78  else if (fileType == FileType::MULTIPLE_FILES)
79  os << "MULTIPLE_FILES";
80  else if (fileType == FileType::MULTIPLE_FILES_PADDED)
81  os << "MULTIPLE_FILES_PADDED";
82  else
83  {
84  std::cerr << "FileType not recognized" << std::endl;
85  exit(-1);
86  }
87  return os;
88 }
89 
95 std::istream& operator>>(std::istream&is, FileType&fileType)
96 {
97  std::string fileTypeString;
98  is >> fileTypeString;
99  if (!fileTypeString.compare("NO_FILE"))
100  fileType = FileType::NO_FILE;
101  else if (!fileTypeString.compare("ONE_FILE"))
102  fileType = FileType::ONE_FILE;
103  else if (!fileTypeString.compare("MULTIPLE_FILES"))
104  fileType = FileType::MULTIPLE_FILES;
105  else if (!fileTypeString.compare("MULTIPLE_FILES_PADDED"))
107  else
108  {
109  std::cerr << "FileType not recognized" << std::endl;
110  exit(-1);
111  }
112  return is;
113 }
114 
120 {
121  saveCount_ = 0;
122 
123  // file name has to be set by the user
124  name_ = "";
125 
126  // file closed by default
127 
128  // output into a single file by default
130 
131  // file counter set to 0 by default
132  counter_ = 0;
133  nextSavedTimeStep_ = 0;
134 
135  // file output file by default
136  openMode_ = std::fstream::out;
137 }
138 
143 {
144 }
145 
150 std::fstream& File::getFstream()
151 {
152  return fstream_;
153 }
154 
155 //void File::setFstream(const std::fstream& file)
156 //{
157 // this->fstream_ = file;
158 //}
162 const std::string& File::getName() const
163 {
164  return name_;
165 }
170 const std::string File::getFullName() const
171 {
172  //get the full file name
173  std::stringstream fullFileName("");
174  fullFileName << name_;
176  {
177  fullFileName << "." << getCounter();
178  }
180  {
181  fullFileName << ".";
182  if (getCounter() < 1000)
183  fullFileName << "0";
184  if (getCounter() < 100)
185  fullFileName << "0";
186  if (getCounter() < 10)
187  fullFileName << "0";
188  fullFileName << getCounter();
189  }
190  return fullFileName.str();
191 }
195 void File::setName(const std::string& name)
196 {
197  this->name_ = name;
198 }
203 {
204  return fileType_;
205 }
210 {
211  fileType_ = fileType;
212 }
216 unsigned int File::getCounter() const
217 {
218  return counter_;
219 }
223 void File::setCounter(unsigned int counter)
224 {
225  counter_ = counter;
226 }
231 {
232  //++counter_;
233 
235  {
236  File::close();
237  return File::open();
238  }
239 // else if (getFileType() == FileType::ONE_FILE && !getFstream().is_open())
240 // {
241 // return File::open();
242 // }
243  else
244  {
245  return true;
246  }
247 }
252 bool File::openNextFile(std::fstream::openmode openMode)
253 {
254  setOpenMode(openMode);
255  return openNextFile();
256 }
260 std::fstream::openmode File::getOpenMode() const
261 {
262  return openMode_;
263 }
267 void File::setOpenMode(std::fstream::openmode openMode)
268 {
269  openMode_ = openMode;
270 }
274 unsigned int File::getSaveCount() const
275 {
276  return saveCount_;
277 }
282 void File::setSaveCount(unsigned int saveCount)
283 {
284  saveCount_ = saveCount;
285 }
290 unsigned int File::getNextSavedTimeStep() const
291 {
292  return nextSavedTimeStep_;
293 }
298 void File::setNextSavedTimeStep(unsigned int nextSavedTimeStep)
299 {
300  nextSavedTimeStep_ = nextSavedTimeStep;
301 }
307 bool File::saveCurrentTimestep(unsigned int ntimeSteps)
308 {
309  //check if this timestep should be written, if the file type is not NO_FILE, then open the file and check if the file was successfully opened
310  //std::cout << (ntimeSteps>=nextSavedTimeStep_) << " " << (getFileType()!= FileType::NO_FILE) << std::endl;
311  return ntimeSteps>=nextSavedTimeStep_ && getFileType()!= FileType::NO_FILE && open();
312 }
319 {
320  if (getName().compare("") == 0)
321  {
322  std::cerr << "Error: Name must be set before opening file" << std::endl;
323  throw;
324  }
325 
326  //close old file if multi-file output
328  fstream_.close();
329 
330  //open new file for multi-file output
331 
332  if (!fstream_.is_open())
333  {
334  fstream_.open(getFullName().c_str(), openMode_);
335  if (!fstream_.is_open())
336  {
337  std::cerr << "Error in opening " << getFullName() <<" with open mode "<< openMode_ << std::endl;
338  return false;
339  }
340  }
342  #ifdef DEBUG_OUTPUT
343  std::cout << "open " << getFullName() << std::endl;
344  #endif
346  counter_++;
347  return true;
348 }
352 bool File::open(std::fstream::openmode openMode)
353 {
354  setOpenMode(openMode);
355  return open();
356 }
361 {
362  fstream_.close();
363  //std::cerr << "Closing " << getFullName() << std::endl;
364 }
370 void File::read(std::istream& is)
371 {
372  std::string dummy;
373  is >> dummy;
374  if (!dummy.compare("name"))
375  is >> name_ >> dummy;
376  is >> fileType_;
377  is >> dummy >> saveCount_;
378  is >> dummy >> counter_;
379  is >> dummy >> nextSavedTimeStep_;
380 }
386 void File::write(std::ostream& os) const
387 {
388  //only write name if it differs from the default name
389  if (getFullName().compare(name_))
390  os << "name " << name_ << " ";
391  os << "fileType " << fileType_;
392  os << " saveCount " << saveCount_;
393  os << " counter " << counter_;
394  os << " nextSavedTimeStep " << nextSavedTimeStep_;
396 }
402 std::ostream& operator <<(std::ostream& os, const File& o)
403 {
404  o.write(os);
405  return os;
406 }
412 std::istream& operator >>(std::istream& is, File &o)
413 {
414  o.read(is);
415  return (is);
416 }
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
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::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
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_
std::istream & operator>>(std::istream &is, FileType &fileType)
Reads the FileType from an input stream 'is'.
Definition: File.cc:95
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
void setFileType(FileType fileType)
Sets the type of file needed to write into or read from. File::fileType_.
Definition: File.cc:209
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
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
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_...
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
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