Jpp  15.0.1-rc.1-highqe
the software that should make you happy
 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 "JLang/JUUID.hh"
11 #include "JLang/JException.hh"
12 
13 #include "Jeep/JPrint.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JEEP {}
21 namespace JPP { using namespace JEEP; }
22 
23 namespace JEEP {
24 
25  using JLANG::JUUID;
26  using JLANG::JNoValue;
27 
28  /**
29  * Type definition of comment block.
30  */
32 
33 
34  /**
35  * Auxiliary class for comment.
36  *
37  * A comment starts with a JComment::START_COMMENT and ends with a JComment::CLOSE_COMMENT.\n
38  * The text between the quotes JComment::QUOTE is considered as one block which may cross newlines, etc.\n
39  * The comments are appended.
40  */
41  struct JComment :
42  public JComment_t
43  {
44  enum {
45  UUID_t = 0 //!< index of UUID
46  };
47 
48  static const char START_COMMENT = '#'; //!< start comment
49  static const char CLOSE_COMMENT = '\n'; //!< close comment
50  static const char START_SPECIAL = '$'; //!< start special comment
51  static const char CLOSE_SPECIAL = '$'; //!< close special comment
52  static const char QUOTE = '"'; //!< quote
53 
54 
55  /**
56  * Default constructor.
57  */
59  {}
60 
61 
62  /**
63  * Update this comment with given UUID.
64  *
65  * \param uuid UUID
66  * \return this comment
67  */
68  JComment& update(const JUUID& uuid)
69  {
70  if (!this->hasUUID()) {
71  if ((int) this->size() <= UUID_t)
72  this->resize(UUID_t + 1);
73  else
74  this->insert(this->begin() + UUID_t, value_type());
75  }
76 
77  (*this)[UUID_t] = MAKE_STRING(START_SPECIAL << uuid << CLOSE_SPECIAL);
78 
79  return *this;
80  }
81 
82 
83  /**
84  * Update this comment with random UUID.
85  *
86  * \return this comment
87  */
89  {
90  return this->update(JUUID::rndm());
91  }
92 
93 
94  /**
95  * Add comment.
96  *
97  * \param comment comment
98  * \return this comment
99  */
100  JComment& add(const std::string& comment)
101  {
102  this->push_back(comment);
103 
104  return this->update();
105  }
106 
107 
108  /**
109  * Add comment block.
110  *
111  * \param comment comment block
112  * \return this comment
113  */
114  JComment& add(const JComment_t& comment)
115  {
116  for (const_iterator i = comment.begin(); i != comment.end(); ++i) {
117  this->push_back(*i);
118  }
119 
120  return this->update();
121  }
122 
123 
124  /**
125  * Check if this comment has UUID.
126  *
127  * \return true if UUID present; else false
128  */
129  bool hasUUID() const
130  {
131  return (UUID_t < (int) this->size() && is_special((*this)[UUID_t]));
132  }
133 
134 
135  /**
136  * Get UUID.
137  *
138  * \return UUID
139  */
140  JUUID getUUID() const
141  {
142  if (this->hasUUID()) {
143 
144  const std::string& buffer = (*this)[UUID_t];
145 
146  return JUUID::valueOf(buffer.substr(1, buffer.size() - 2));
147 
148  } else {
149 
150  THROW(JNoValue, "No UUID in comment.");
151  }
152  }
153 
154 
155  /**
156  * Read comment from input.
157  *
158  * \param in input stream
159  * \param comment comment
160  * \return input stream
161  */
162  friend inline std::istream& operator>>(std::istream& in, JComment& comment)
163  {
164  using namespace std;
165 
166  while (in.peek() == (int) START_COMMENT) {
167 
168  in.get();
169 
170  if (in.peek() == (int) ' ') {
171  in.get();
172  }
173 
174  int c;
175  string buffer;
176 
177  while ((c = in.get()) != EOF && c != (int) CLOSE_COMMENT) {
178 
179  buffer.push_back((char) c);
180 
181  if (c == (int) QUOTE) {
182 
183  while ((c = in.get()) != EOF && c != (int) QUOTE) {
184  buffer.push_back((char) c);
185  }
186 
187  buffer.push_back(QUOTE);
188  }
189  }
190 
191  comment.push_back(buffer);
192  }
193 
194  return in;
195  }
196 
197 
198  /**
199  * Write comment to output.
200  *
201  * \param out output stream
202  * \param comment comment
203  * \return output stream
204  */
205  friend inline std::ostream& operator<<(std::ostream& out, const JComment& comment)
206  {
207  using namespace std;
208 
209  for (JComment::const_iterator i = comment.begin(); i != comment.end(); ++i) {
210  out << START_COMMENT << ' ' << *i << CLOSE_COMMENT;
211  }
212 
213  return out << flush;
214  }
215 
216 
217  /**
218  * Check if given string is special.
219  *
220  * \param buffer input string
221  * \return true if special; else false
222  */
223  static bool is_special(const std::string& buffer)
224  {
225  const int N = buffer.size();
226 
227  return (N > 2 &&
228  buffer[0] == START_SPECIAL &&
229  buffer[N-1] == CLOSE_SPECIAL);
230  }
231  };
232 }
233 
234 #endif
static const char START_COMMENT
start comment
Definition: JComment.hh:48
Exceptions.
JComment()
Default constructor.
Definition: JComment.hh:58
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
static bool is_special(const std::string &buffer)
Check if given string is special.
Definition: JComment.hh:223
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
Exception for missing value.
Definition: JException.hh:198
static const JUUID & rndm()
Generate random UUID.
Definition: JUUID.hh:67
friend std::istream & operator>>(std::istream &in, JComment &comment)
Read comment from input.
Definition: JComment.hh:162
I/O formatting auxiliaries.
JComment & update(const JUUID &uuid)
Update this comment with given UUID.
Definition: JComment.hh:68
index of UUID
Definition: JComment.hh:45
JUUID getUUID() const
Get UUID.
Definition: JComment.hh:140
bool hasUUID() const
Check if this comment has UUID.
Definition: JComment.hh:129
JComment & update()
Update this comment with random UUID.
Definition: JComment.hh:88
Auxiliary class for comment.
Definition: JComment.hh:41
static const char CLOSE_COMMENT
close comment
Definition: JComment.hh:49
friend std::ostream & operator<<(std::ostream &out, const JComment &comment)
Write comment to output.
Definition: JComment.hh:205
Simple wrapper for UUID.
Definition: JUUID.hh:22
JComment & add(const JComment_t &comment)
Add comment block.
Definition: JComment.hh:114
static const char START_SPECIAL
start special comment
Definition: JComment.hh:50
static const char QUOTE
quote
Definition: JComment.hh:52
static JUUID valueOf(const std::string &buffer)
Extract UUID.
Definition: JUUID.hh:113
JComment & add(const std::string &comment)
Add comment.
Definition: JComment.hh:100
std::vector< std::string > JComment_t
Type definition of comment block.
Definition: JComment.hh:31
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:41
static const char CLOSE_SPECIAL
close special comment
Definition: JComment.hh:51