MercuryDPM  Alpha
 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 #include "Logger.h"
34 
36 // ///Allows to read a FileType using the operator>>, e.g., is >> fileType;
37 // ///It converts unsigned int to the enum FileType
38 //std::istream& operator >>(std::istream& is, FileType& i)
39 //{
40 // unsigned int tmp;
41 // if (is >> tmp)
42 // i = static_cast<FileType>(tmp);
43 // return is;
44 //}
45 //
46 // ///Allows to write a FileType using the operator<<, e.g., os << fileType;
47 // ///It converts the enum FileType to unsigned int
48 //std::ostream& operator <<(std::ostream& is, FileType& i)
49 //{
50 // is << static_cast<unsigned int>(i);
51 // return is;
52 //}
61 std::string to_string_padded(unsigned int value)
62 {
63  std::ostringstream out;
64  out << std::setw(4) << std::setfill('0') << value;
65  return out.str();
66 }
67 
73 std::ostream& operator<<(std::ostream&os, FileType fileType)
74 {
75  if (fileType == FileType::NO_FILE)
76  os << "NO_FILE";
77  else if (fileType == FileType::ONE_FILE)
78  os << "ONE_FILE";
79  else if (fileType == FileType::MULTIPLE_FILES)
80  os << "MULTIPLE_FILES";
81  else if (fileType == FileType::MULTIPLE_FILES_PADDED)
82  os << "MULTIPLE_FILES_PADDED";
83  else
84  {
85  std::cerr << "FileType not recognized" << std::endl;
86  exit(-1);
87  }
88  return os;
89 }
90 
96 std::istream& operator>>(std::istream&is, FileType&fileType)
97 {
98  std::string fileTypeString;
99  is >> fileTypeString;
100  if (!fileTypeString.compare("NO_FILE"))
101  fileType = FileType::NO_FILE;
102  else if (!fileTypeString.compare("ONE_FILE"))
103  fileType = FileType::ONE_FILE;
104  else if (!fileTypeString.compare("MULTIPLE_FILES"))
105  fileType = FileType::MULTIPLE_FILES;
106  else if (!fileTypeString.compare("MULTIPLE_FILES_PADDED"))
108  else
109  {
110  std::cerr << "FileType not recognized" << std::endl;
111  exit(-1);
112  }
113  return is;
114 }
115 
121 {
122  saveCount_ = 0;
123 
124  // file name has to be set by the user
125  name_ = "out";
126 
127  // file closed by default
128 
129  // output into a single file by default
131 
132  // file counter set to 0 by default
133  counter_ = 0;
134  nextSavedTimeStep_ = 0;
135 
136  // file output file by default
137  openMode_ = std::fstream::out;
138 }
139 
144 {
145 }
146 
151 std::fstream& File::getFstream()
152 {
153  return fstream_;
154 }
155 
156 //void File::setFstream(const std::fstream& file)
157 //{
158 // this->fstream_ = file;
159 //}
163 const std::string& File::getName() const
164 {
165  return name_;
166 }
171 const std::string File::getFullName() const
172 {
173  //get the full file name
174  std::stringstream fullFileName("");
175  fullFileName << name_;
177  {
178  fullFileName << "." << getCounter();
179  }
181  {
182  fullFileName << ".";
183  if (getCounter() < 1000)
184  fullFileName << "0";
185  if (getCounter() < 100)
186  fullFileName << "0";
187  if (getCounter() < 10)
188  fullFileName << "0";
189  fullFileName << getCounter();
190  }
191  return fullFileName.str();
192 }
196 void File::setName(const std::string& name)
197 {
198  this->name_ = name;
199 }
204 {
205  return fileType_;
206 }
211 {
212  fileType_ = fileType;
213 }
217 unsigned int File::getCounter() const
218 {
219  return counter_;
220 }
224 void File::setCounter(unsigned int counter)
225 {
226  counter_ = counter;
227 }
232 {
233  //++counter_;
234 
236  {
237  File::close();
238  return File::open();
239  }
240 // else if (getFileType() == FileType::ONE_FILE && !getFstream().is_open())
241 // {
242 // return File::open();
243 // }
244  else
245  {
246  return true;
247  }
248 }
253 bool File::openNextFile(std::fstream::openmode openMode)
254 {
255  setOpenMode(openMode);
256  return openNextFile();
257 }
261 std::fstream::openmode File::getOpenMode() const
262 {
263  return openMode_;
264 }
268 void File::setOpenMode(std::fstream::openmode openMode)
269 {
270  openMode_ = openMode;
271 }
275 unsigned int File::getSaveCount() const
276 {
277  return saveCount_;
278 }
283 void File::setSaveCount(unsigned int saveCount)
284 {
286  saveCount_ = saveCount;
287 }
292 unsigned int File::getNextSavedTimeStep() const
293 {
294  return nextSavedTimeStep_;
295 }
300 void File::setNextSavedTimeStep(unsigned int nextSavedTimeStep)
301 {
302  nextSavedTimeStep_ = nextSavedTimeStep;
303 }
309 bool File::saveCurrentTimestep(unsigned int ntimeSteps)
310 {
311  //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
312  //std::cout << (ntimeSteps>=nextSavedTimeStep_) << " " << (getFileType()!= FileType::NO_FILE) << std::endl;
313  return ntimeSteps>=nextSavedTimeStep_ && getFileType()!= FileType::NO_FILE && open();
314 }
321 {
322  if (getName().compare("") == 0)
323  {
324  std::cerr << "Error: Name must be set before opening file" << std::endl;
325  throw;
326  }
327 
328  //close old file if multi-file output
330  fstream_.close();
331 
332  //open new file for multi-file output
333 
334  if (!fstream_.is_open())
335  {
336  fstream_.open(getFullName().c_str(), openMode_);
337  if (!fstream_.is_open())
338  {
339  //logger(WARN, "Could not open % with open mode %", getFullName(), openMode_);
340  return false;
341  }
342  }
344  #ifdef DEBUG_OUTPUT
345  std::cout << "open " << getFullName() << std::endl;
346  #endif
348  counter_++;
349  return true;
350 }
354 bool File::open(std::fstream::openmode openMode)
355 {
356  setOpenMode(openMode);
357  return open();
358 }
363 {
364  fstream_.close();
365  //std::cerr << "Closing " << getFullName() << std::endl;
366 }
372 void File::read(std::istream& is)
373 {
374  std::string dummy;
375  is >> dummy;
376  if (!dummy.compare("name"))
377  is >> name_ >> dummy;
378  is >> fileType_;
379  is >> dummy >> saveCount_;
380  is >> dummy >> counter_;
381  is >> dummy >> nextSavedTimeStep_;
382 }
388 void File::write(std::ostream& os) const
389 {
390  //only write name if it differs from the default name
391  if (getFullName().compare(name_))
392  os << "name " << name_ << " ";
393  os << "fileType " << fileType_;
394  os << " saveCount " << saveCount_;
395  os << " counter " << counter_;
396  os << " nextSavedTimeStep " << nextSavedTimeStep_;
398 }
404 std::ostream& operator <<(std::ostream& os, const File& o)
405 {
406  o.write(os);
407  return os;
408 }
414 std::istream& operator >>(std::istream& is, File &o)
415 {
416  o.read(is);
417  return (is);
418 }
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:203
std::fstream::openmode getOpenMode() const
Allows the user to know the file mode i.e. gets File::openMode_.
Definition: File.cc:261
void read(std::istream &is)
read function, which accepts an input stream std::istream.
Definition: File.cc:372
void setCounter(unsigned int counter)
Allows the user to set the file counter according to his need. Sets File::counter_.
Definition: File.cc:224
const std::string getFullName() const
Also allows to access the file name, however with additional information which is the file counter...
Definition: File.cc:171
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:231
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:275
void close()
Closes the file by calling fstream_.close()
Definition: File.cc:362
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:217
file will not be created/read
std::fstream & getFstream()
Allows to access the member variable File::fstream_.
Definition: File.cc:151
void setNextSavedTimeStep(unsigned int nextSavedTimeStep)
Sets File::nextSavedTimeStep_.
Definition: File.cc:300
File()
constructor
Definition: File.cc:120
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:96
unsigned int getNextSavedTimeStep() const
Gets File::nextSavedTimeStep_.
Definition: File.cc:292
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:283
void setFileType(FileType fileType)
Sets the type of file needed to write into or read from. File::fileType_.
Definition: File.cc:210
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:61
bool open()
Checks if the file stream fstream_ has any issues while opening. Alongside, it also increments the ne...
Definition: File.cc:320
bool saveCurrentTimestep(unsigned int ntimeSteps)
Definition: File.cc:309
bool compare(std::istream &is, std::string s)
Definition: Helpers.cc:754
std::string name_
name of the file.
Definition: File.h:225
virtual ~File()
destructor
Definition: File.cc:143
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:73
void setName(const std::string &name)
Sets the file name, e.g. "Name.data".
Definition: File.cc:196
Definition: File.h:78
void write(std::ostream &os) const
print function, which accepts an std::stringstream as input.
Definition: File.cc:388
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:163
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:268