FileIOHelpers.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 MERCURYDPM_FILEIO_HELPERS_H
6 #define MERCURYDPM_FILEIO_HELPERS_H
7 
8 #include "Logger.h"
9 #include "Math/ExtendedMath.h"
10 
11 #include <fstream>
12 #include <string>
13 #include <vector>
14 
15 namespace helpers
16 {
20  bool writeToFile(const std::string & filename, const std::string & filecontent);
21 
25  void writeCommandLineToFile(const std::string & filename, const int argc, char * const argv[]);
26 
30  bool addToFile(const std::string & filename, const std::string & filecontent);
31 
35  bool fileExists(const std::string & strFilename);
36 
40  bool openFile(std::fstream& file, const std::string & filename, std::fstream::openmode mode);
41 
42  std::vector<double> readArrayFromFile(const std::string & filename, int& n, int& m);
43 
44  void more(const std::string & filename, unsigned nLines = constants::unsignedMax);
45 
53  bool createDirectory(const std::string & directory, bool allowExists = false);
54 
56 
67  template<typename T>
68  bool readOptionalVariable(std::istream& is, const std::string& name, T& variable)
69  {
72  const auto pos = is.tellg();
73  std::string dummy;
74  is >> dummy;
75  if (dummy == name)
76  {
77  is >> variable;
78  return true;
79  } else {
80  is.seekg(pos);
81  return false;
82  }
83  }
84 
106  template<typename T>
107  T readFromFile(const std::string & fileName, const std::string & varName, const T defaultValue)
108  {
109  //open filestream
110  std::ifstream is(fileName.c_str(), std::ios::in);
111  if (is.fail())
112  {
113  logger(INFO, "readFromFile: file % could not be opened, variable % set to default value %",
114  fileName, varName, defaultValue);
115  return defaultValue;
116  }
117 
118  //read in variables, until the right one is fount
119  std::string s;
120  while (!is.eof())
121  {
122  is >> s;
123  if (s == varName)
124  {
125  T value;
126  is >> value;
127  logger(INFO, "readFromFile: variable % set to % ", varName, value);
128  return value;
129  }
130  }
131 
132  //if the right variable is never found
133  logger(WARN, "readFromFile: variable % not set in file %, using default value % ", varName, fileName, defaultValue);
134  return defaultValue;
135  }
136 }
137 
138 #endif // MERCURYDPM_FILEIO_HELPERS_H
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
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:33
RealScalar s
Definition: level1_cplx_impl.h:130
int * m
Definition: level2_cplx_impl.h:294
#define INFO(i)
Definition: mumps_solver.h:54
squared absolute value
Definition: GlobalFunctions.h:87
string filename
Definition: MergeRestartFiles.py:39
string fileName
Definition: UniformPSDSelfTest.py:10
const unsigned unsignedMax
Definition: GeneralDefine.h:25
Definition: CommandLineHelpers.h:11
bool readOptionalVariable(std::istream &is, const std::string &name, T &variable)
Reads optional variables in the restart file.
Definition: FileIOHelpers.h:68
void more(const std::string &filename, unsigned nLines=constants::unsignedMax)
Definition: FileIOHelpers.cc:151
void writeCommandLineToFile(const std::string &filename, const int argc, char *const argv[])
Writes a string to a file.
Definition: FileIOHelpers.cc:47
bool createDirectory(const std::string &directory, bool allowExists=false)
Creates a directory.
Definition: FileIOHelpers.cc:177
bool fileExists(const std::string &strFilename)
Function to check if a file exists, is used to check if a run has already need done.
Definition: FileIOHelpers.cc:77
bool openFile(std::fstream &file, const std::string &filename, std::fstream::openmode mode)
Provides a simple interface for opening a file.
Definition: FileIOHelpers.cc:115
T readFromFile(const std::string &fileName, const std::string &varName, const T defaultValue)
Definition: FileIOHelpers.h:107
std::string getPath()
Definition: FileIOHelpers.cc:206
std::vector< double > readArrayFromFile(const std::string &filename, int &n, int &m)
Definition: FileIOHelpers.cc:128
bool writeToFile(const std::string &filename, const std::string &filecontent)
Writes a string to a file.
Definition: FileIOHelpers.cc:29
bool addToFile(const std::string &filename, const std::string &filecontent)
Adds a string to an existing file.
Definition: FileIOHelpers.cc:59
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
string name
Definition: plotDoE.py:33