Jpp  19.1.0-rc.1
the software that should make you happy
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 
7 #include "JLang/JStreamState.hh"
8 
9 
10 /**
11  * \author mdejong
12  */
13 
14 namespace JLANG {}
15 namespace JPP { using namespace JLANG; }
16 
17 namespace JLANG {
18 
19  /**
20  * Wrapper class around STL stringstream class to facilitate optional loading of data from file.
21  */
22  class JStringStream :
23  public std::stringstream,
24  public JStreamState
25  {
26  public:
27  /**
28  * Constructor.
29  *
30  * This constructor loads all data from given input stream.
31  *
32  * \param in input stream
33  */
34  JStringStream(std::istream& in) :
35  std::stringstream(),
36  JStreamState(in, static_cast<std::istream&>(*this))
37  {
38  using namespace std;
39 
40  istream::sentry sentry(in); // skips white spaces
41 
42  if (sentry) {
43  (*this) << in.rdbuf();
44  }
45  }
46 
47 
48  /**
49  * Load data from file with name corresponding to current contents.
50  *
51  * Note that if the file exists but is empty,
52  * the state of the stream is set to fail by C++ standard.
53  */
54  void load()
55  {
56  using namespace std;
57 
58  ifstream in(this->str().c_str());
59 
60  if (in) {
61 
62  this->str(""); // reset
63 
64  (*this) << in.rdbuf(); // copy
65 
66  in.close();
67  }
68  }
69  };
70 }
71 
72 #endif
This class can be used to temporarily exchange the states between streams.
Definition: JStreamState.hh:23
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).
Definition: JSTDTypes.hh:14