FileIOHelpers.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, 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 #ifndef MERCURYDPM_FILEIO_HELPERS_H
27 #define MERCURYDPM_FILEIO_HELPERS_H
28 
29 #include "Logger.h"
30 #include "Math/ExtendedMath.h"
31 
32 #include <fstream>
33 #include <string>
34 #include <vector>
35 
36 namespace helpers
37 {
41  bool writeToFile(std::string filename, std::string filecontent);
42 
46  void writeCommandLineToFile(const std::string filename, const int argc, char * const argv[]);
47 
51  bool addToFile(std::string filename, std::string filecontent);
52 
56  bool fileExists(std::string strFilename);
57 
61  bool openFile(std::fstream& file, std::string filename, std::fstream::openmode mode);
62 
63  std::vector<double> readArrayFromFile(std::string filename, int& n, int& m);
64 
65  void more(std::string filename, unsigned nLines = constants::unsignedMax);
66 
67  bool createDirectory(std::string);
68 
69  std::string getPath();
70 
81  template<typename T>
82  bool readOptionalVariable(std::istream& is, const std::string& name, T& variable)
83  {
86  const auto pos = is.tellg();
87  std::string dummy;
88  is >> dummy;
89  if (dummy == name)
90  {
91  is >> variable;
92  return true;
93  } else {
94  is.seekg(pos);
95  return false;
96  }
97  }
98 
120  template<typename T>
121  T readFromFile(const std::string fileName, const std::string varName, const T defaultValue)
122  {
123  //open filestream
124  std::ifstream is(fileName.c_str(), std::ios::in);
125  if (is.fail())
126  {
127  logger(INFO, "readFromFile: file % could not be opened, variable % set to default value %",
128  fileName, varName, defaultValue);
129  return defaultValue;
130  }
131 
132  //read in variables, until the right one is fount
133  std::string s;
134  while (!is.eof())
135  {
136  is >> s;
137  if (s == varName)
138  {
139  T value;
140  is >> value;
141  logger(INFO, "readFromFile: variable % set to % ", varName, value);
142  return value;
143  }
144  }
145 
146  //if the right variable is never found
147  logger(WARN, "readFromFile: variable % not set in file %, using default value % ", varName, fileName, defaultValue);
148  return defaultValue;
149  }
150 }
151 
152 #endif // MERCURYDPM_FILEIO_HELPERS_H
const unsigned n
Definition: CG3DPackingUnitTest.cpp:32
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
const unsigned unsignedMax
Definition: GeneralDefine.h:46
Definition: CommandLineHelpers.h:32
bool readOptionalVariable(std::istream &is, const std::string &name, T &variable)
Reads optional variables in the restart file.
Definition: FileIOHelpers.h:82
void writeCommandLineToFile(const std::string filename, const int argc, char *const argv[])
Writes a string to a file.
Definition: FileIOHelpers.cc:76
bool openFile(std::fstream &file, std::string filename, std::fstream::openmode mode)
Provides a simple interface for opening a file.
Definition: FileIOHelpers.cc:145
bool createDirectory(std::string)
Definition: FileIOHelpers.cc:203
bool addToFile(std::string filename, std::string filecontent)
Adds a string to an existing file.
Definition: FileIOHelpers.cc:89
void more(std::string filename, unsigned nLines=constants::unsignedMax)
Definition: FileIOHelpers.cc:181
std::string getPath()
Definition: FileIOHelpers.cc:224
std::vector< double > readArrayFromFile(std::string filename, int &n, int &m)
Definition: FileIOHelpers.cc:158
T readFromFile(const std::string fileName, const std::string varName, const T defaultValue)
Definition: FileIOHelpers.h:121
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: FileIOHelpers.cc:58
bool fileExists(std::string strFilename)
Function to check if a file exists, is used to check if a run has already need done.
Definition: FileIOHelpers.cc:107
std::string name
Definition: MercuryProb.h:48