Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4
7
8namespace storm {
9namespace io {
10
18inline void openFile(std::string const& filepath, std::ofstream& filestream, bool append = false, bool silent = false) {
19 if (append) {
20 filestream.open(filepath, std::ios::app);
21 } else {
22 filestream.open(filepath);
23 }
24 STORM_LOG_THROW(filestream, storm::exceptions::FileIoException, "Could not open file " << filepath << ".");
25 filestream.precision(std::cout.precision());
26 if (!silent) {
27 STORM_PRINT_AND_LOG("Write to file " << filepath << ".\n");
28 }
29}
30
37inline void openFile(std::string const& filepath, std::ifstream& filestream) {
38 filestream.open(filepath);
39 STORM_LOG_THROW(filestream, storm::exceptions::FileIoException, "Could not open file " << filepath << ".");
40}
41
47inline void closeFile(std::ofstream& stream) {
48 stream.close();
49}
50
56inline void closeFile(std::ifstream& stream) {
57 stream.close();
58}
59
66inline bool fileExistsAndIsReadable(std::string const& filename) {
67 // Test by opening an input file stream and testing the stream flags.
68 std::ifstream filestream;
69 filestream.open(filename);
70 return filestream.good();
71}
72
79template<class CharT, class Traits, class Allocator>
80inline std::basic_istream<CharT, Traits>& getline(std::basic_istream<CharT, Traits>& input, std::basic_string<CharT, Traits, Allocator>& str) {
81 auto& res = std::getline(input, str);
82 // Remove linebreaks
83 std::string::reverse_iterator rit = str.rbegin();
84 while (rit != str.rend()) {
85 if (*rit == '\r' || *rit == '\n') {
86 ++rit;
87 str.pop_back();
88 } else {
89 break;
90 }
91 }
92 return res;
93}
94
95} // namespace io
96} // namespace storm
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
#define STORM_PRINT_AND_LOG(message)
Definition macros.h:68
std::basic_istream< CharT, Traits > & getline(std::basic_istream< CharT, Traits > &input, std::basic_string< CharT, Traits, Allocator > &str)
Overloaded getline function which handles different types of newline ( and \r).
Definition file.h:80
void closeFile(std::ofstream &stream)
Close the given file after writing.
Definition file.h:47
bool fileExistsAndIsReadable(std::string const &filename)
Tests whether the given file exists and is readable.
Definition file.h:66
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
Definition file.h:18
LabParser.cpp.
Definition cli.cpp:18