Jpp 21.0.1
the software that should make you happy
Loading...
Searching...
No Matches
JStringStream.hh
Go to the documentation of this file.
1#ifndef __JLANG__JSTRINGSTREAM__
2#define __JLANG__JSTRINGSTREAM__
3
4#include <sstream>
5#include <fstream>
6#include <filesystem>
7
9
10
11/**
12 * \author mdejong
13 */
14
15namespace JLANG {}
16namespace JPP { using namespace JLANG; }
17
18namespace JLANG {
19
20 /**
21 * Wrapper class around STL stringstream class to facilitate optional loading of data from file.
22 */
24 public std::stringstream,
25 public JStreamState
26 {
27 public:
28 /**
29 * Constructor.
30 *
31 * This constructor loads all data from given input stream.
32 *
33 * \param in input stream
34 */
35 JStringStream(std::istream& in) :
36 std::stringstream(),
37 JStreamState(in, static_cast<std::istream&>(*this))
38 {
39 using namespace std;
40
41 istream::sentry sentry(in); // skips white spaces
42
43 if (sentry) {
44
45 static_cast<stringstream&>(*this) << in.rdbuf(); // copy
46
47 error_code error;
48
49 if (filesystem::is_regular_file(this->str(), error)) {
50 this->load();
51 }
52 }
53 }
54
55
56 /**
57 * Load data from file with name corresponding to current contents.
58 *
59 * Note that if the file exists but is empty,
60 * the state of the stream is set to fail by C++ standard.
61 */
62 void load()
63 {
64 using namespace std;
65
66 ifstream in(this->str().c_str());
67
68 if (in) {
69
70 this->str(""); // reset
71
72 static_cast<stringstream&>(*this) << in.rdbuf(); // copy
73
74 in.close();
75 }
76 }
77 };
78}
79
80#endif
This class can be used to temporarily exchange the states between streams.
Wrapper class around STL stringstream class to facilitate optional loading of data from file.
void load()
Load data from file with name corresponding to current contents.
JStringStream(std::istream &in)
Constructor.
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).