Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JComment.hh
Go to the documentation of this file.
1 #ifndef __JEEP__JCOMMENT__
2 #define __JEEP__JCOMMENT__
3 
4 #include <istream>
5 #include <ostream>
6 #include <sstream>
7 #include <string>
8 #include <vector>
9 
10 #include "Jeep/JPrint.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JEEP {}
18 namespace JPP { using namespace JEEP; }
19 
20 namespace JEEP {
21 
22  /**
23  * Auxiliary class for comment.
24  *
25  * A comment starts with a JComment::START_COMMENT and ends with a JComment::CLOSE_COMMENT.\n
26  * The text between the quotes JComment::QUOTE is considered as one block which may cross newlines, etc.\n
27  * The comments are appended.
28  */
29  struct JComment :
30  public std::vector<std::string>
31  {
32 
33  static const char START_COMMENT = '#'; //!< start of comment
34  static const char CLOSE_COMMENT = '\n'; //!< close of comment
35  static const char QUOTE = '"'; //!< quote
36 
37 
38  /**
39  * Default constructor.
40  */
42  {}
43 
44 
45  /**
46  * Add comment.
47  *
48  * \param comment comment
49  * \return this comment
50  */
51  JComment& add(const std::string& comment)
52  {
53  this->push_back(comment);
54 
55  return *this;
56  }
57 
58 
59  /**
60  * Add comment.
61  *
62  * \param comment comment
63  * \return this comment
64  */
65  JComment& add(const JComment& comment)
66  {
67  for (const_iterator i = comment.begin(); i != comment.end(); ++i) {
68  this->push_back(*i);
69  }
70 
71  return *this;
72  }
73 
74 
75  /**
76  * Read comment from input.
77  *
78  * \param in input stream
79  * \param comment comment
80  * \return input stream
81  */
82  friend inline std::istream& operator>>(std::istream& in, JComment& comment)
83  {
84  using namespace std;
85 
86  while (in.peek() == (int) START_COMMENT) {
87 
88  in.get();
89 
90  if (in.peek() == (int) ' ') {
91  in.get();
92  }
93 
94  int c;
95  string buffer;
96 
97  while ((c = in.get()) != EOF && c != (int) CLOSE_COMMENT) {
98 
99  buffer.push_back((char) c);
100 
101  if (c == (int) QUOTE) {
102 
103  while ((c = in.get()) != EOF && c != (int) QUOTE) {
104  buffer.push_back((char) c);
105  }
106 
107  buffer.push_back(QUOTE);
108  }
109  }
110 
111  comment.push_back(buffer);
112  }
113 
114  return in;
115  }
116 
117 
118  /**
119  * Write comment to output.
120  *
121  * \param out output stream
122  * \param comment comment
123  * \return output stream
124  */
125  friend inline std::ostream& operator<<(std::ostream& out, const JComment& comment)
126  {
127  using namespace std;
128 
129  for (JComment::const_iterator i = comment.begin(); i != comment.end(); ++i) {
130  out << START_COMMENT << ' ' << *i << CLOSE_COMMENT;
131  }
132 
133  return out << flush;
134  }
135  };
136 }
137 
138 #endif
static const char START_COMMENT
start of comment
Definition: JComment.hh:33
JComment()
Default constructor.
Definition: JComment.hh:41
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
friend std::istream & operator>>(std::istream &in, JComment &comment)
Read comment from input.
Definition: JComment.hh:82
I/O formatting auxiliaries.
JComment & add(const JComment &comment)
Add comment.
Definition: JComment.hh:65
Auxiliary class for comment.
Definition: JComment.hh:29
static const char CLOSE_COMMENT
close of comment
Definition: JComment.hh:34
friend std::ostream & operator<<(std::ostream &out, const JComment &comment)
Write comment to output.
Definition: JComment.hh:125
static const char QUOTE
quote
Definition: JComment.hh:35
JComment & add(const std::string &comment)
Add comment.
Definition: JComment.hh:51