Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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
20namespace JEEP {}
21namespace JPP { using namespace JEEP; }
22
23namespace 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 constexpr char START_COMMENT = '#'; //!< start comment
49 static constexpr char CLOSE_COMMENT = '\n'; //!< close comment
50 static constexpr char START_SPECIAL = '$'; //!< start special comment
51 static constexpr char CLOSE_SPECIAL = '$'; //!< close special comment
52 static constexpr 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 */
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
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
I/O formatting auxiliaries.
#define MAKE_STRING(A)
Make string.
Definition JPrint.hh:63
Exception for missing value.
General puprpose classes and methods.
std::vector< std::string > JComment_t
Type definition of comment block.
Definition JComment.hh:31
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for comment.
Definition JComment.hh:43
static constexpr char START_SPECIAL
start special comment
Definition JComment.hh:50
friend std::ostream & operator<<(std::ostream &out, const JComment &comment)
Write comment to output.
Definition JComment.hh:205
JComment & update()
Update this comment with random UUID.
Definition JComment.hh:88
static constexpr char QUOTE
quote
Definition JComment.hh:52
bool hasUUID() const
Check if this comment has UUID.
Definition JComment.hh:129
JUUID getUUID() const
Get UUID.
Definition JComment.hh:140
static bool is_special(const std::string &buffer)
Check if given string is special.
Definition JComment.hh:223
static constexpr char START_COMMENT
start comment
Definition JComment.hh:48
JComment()
Default constructor.
Definition JComment.hh:58
friend std::istream & operator>>(std::istream &in, JComment &comment)
Read comment from input.
Definition JComment.hh:162
JComment & add(const JComment_t &comment)
Add comment block.
Definition JComment.hh:114
@ UUID_t
index of UUID
Definition JComment.hh:45
static constexpr char CLOSE_COMMENT
close comment
Definition JComment.hh:49
JComment & add(const std::string &comment)
Add comment.
Definition JComment.hh:100
JComment & update(const JUUID &uuid)
Update this comment with given UUID.
Definition JComment.hh:68
static constexpr char CLOSE_SPECIAL
close special comment
Definition JComment.hh:51
Simple wrapper for UUID.
Definition JUUID.hh:24
static const JUUID & rndm()
Generate random UUID.
Definition JUUID.hh:78
static JUUID valueOf(const std::string &buffer)
Extract UUID.
Definition JUUID.hh:124