OpenLB 1.8.1
Loading...
Searching...
No Matches
filesIO.hh File Reference
#include "filesIO.h"
#include <cstdlib>
+ Include dependency graph for filesIO.hh:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool directoryExists (const std::string &path)
 
bool createDirectory (const std::string &path)
 
bool deleteDirectory (const std::string &path)
 
bool fileExists (const std::string &name)
 
template<typename T >
void saveVector1D (const std::string &filePath, const std::vector< T > &vec)
 
template<typename T >
void readVector1D (const std::string &filePath, std::vector< T > &vec)
 
template<typename T >
void saveVector3D (const std::string &filePath, const std::vector< std::vector< std::vector< T > > > &vec)
 
template<typename T >
void readVector3D (const std::string &filePath, std::vector< std::vector< std::vector< T > > > &vec)
 

Function Documentation

◆ createDirectory()

bool createDirectory ( const std::string & path)

Definition at line 37 of file filesIO.hh.

38{
39 return (mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
40}
+ Here is the caller graph for this function:

◆ deleteDirectory()

bool deleteDirectory ( const std::string & path)

Definition at line 42 of file filesIO.hh.

43{
44 std::string command = "rm -rf " + path;
45 return (std::system(command.c_str()) == 0);
46}

◆ directoryExists()

bool directoryExists ( const std::string & path)

Definition at line 31 of file filesIO.hh.

32{
33 struct stat statbuf;
34 return (stat(path.c_str(), &statbuf) == 0 && S_ISDIR(statbuf.st_mode));
35}
+ Here is the caller graph for this function:

◆ fileExists()

bool fileExists ( const std::string & name)

Definition at line 48 of file filesIO.hh.

49{
50 std::ifstream f(name.c_str());
51 return f.good();
52}
std::string name()
Returns name of FIELD for human consumption.
Definition fields.h:49
+ Here is the caller graph for this function:

◆ readVector1D()

template<typename T >
void readVector1D ( const std::string & filePath,
std::vector< T > & vec )

Definition at line 67 of file filesIO.hh.

68{
69 std::ifstream in(filePath, std::ios::binary);
70 if (!in.is_open())
71 throw std::runtime_error("Cannot open file for reading: " + filePath);
72
73 std::size_t size;
74 in.read(reinterpret_cast<char*>(&size), sizeof(size));
75 vec.resize(size);
76 in.read(reinterpret_cast<char*>(vec.data()), size * sizeof(T));
77}
+ Here is the caller graph for this function:

◆ readVector3D()

template<typename T >
void readVector3D ( const std::string & filePath,
std::vector< std::vector< std::vector< T > > > & vec )

Definition at line 102 of file filesIO.hh.

103{
104 std::ifstream in(filePath, std::ios::binary);
105 if (!in.is_open())
106 throw std::runtime_error("Cannot open file for reading: " + filePath);
107
108 std::size_t outerSize;
109 in.read(reinterpret_cast<char*>(&outerSize), sizeof(outerSize));
110 vec.resize(outerSize);
111
112 for (auto& midVec : vec) {
113 std::size_t midSize;
114 in.read(reinterpret_cast<char*>(&midSize), sizeof(midSize));
115 midVec.resize(midSize);
116
117 for (auto& innerVec : midVec) {
118 std::size_t innerSize;
119 in.read(reinterpret_cast<char*>(&innerSize), sizeof(innerSize));
120 innerVec.resize(innerSize);
121 in.read(reinterpret_cast<char*>(innerVec.data()), innerSize * sizeof(T));
122 }
123 }
124}

◆ saveVector1D()

template<typename T >
void saveVector1D ( const std::string & filePath,
const std::vector< T > & vec )

Definition at line 55 of file filesIO.hh.

56{
57 std::ofstream out(filePath, std::ios::binary);
58 if (!out.is_open())
59 throw std::runtime_error("Cannot open file for writing: " + filePath);
60
61 std::size_t size = vec.size();
62 out.write(reinterpret_cast<const char*>(&size), sizeof(size));
63 out.write(reinterpret_cast<const char*>(vec.data()), size * sizeof(T));
64}
+ Here is the caller graph for this function:

◆ saveVector3D()

template<typename T >
void saveVector3D ( const std::string & filePath,
const std::vector< std::vector< std::vector< T > > > & vec )

Definition at line 80 of file filesIO.hh.

81{
82 std::ofstream out(filePath, std::ios::binary);
83 if (!out.is_open())
84 throw std::runtime_error("Cannot open file for writing: " + filePath);
85
86 std::size_t outerSize = vec.size();
87 out.write(reinterpret_cast<const char*>(&outerSize), sizeof(outerSize));
88
89 for (const auto& midVec : vec) {
90 std::size_t midSize = midVec.size();
91 out.write(reinterpret_cast<const char*>(&midSize), sizeof(midSize));
92
93 for (const auto& innerVec : midVec) {
94 std::size_t innerSize = innerVec.size();
95 out.write(reinterpret_cast<const char*>(&innerSize), sizeof(innerSize));
96 out.write(reinterpret_cast<const char*>(innerVec.data()), innerSize * sizeof(T));
97 }
98 }
99}