Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JStringFacet.hh
Go to the documentation of this file.
1#ifndef __JLANG__JSTRINGFACET__
2#define __JLANG__JSTRINGFACET__
3
4#include <istream>
5#include <ostream>
6#include <locale>
7#include <string>
8#include <iterator>
9#include <limits>
10
11
12/**
13 * \author mdejong
14 */
15
16namespace JLANG {}
17namespace JPP { using namespace JLANG; }
18
19namespace JLANG {
20
21
22 /**
23 * Facet class to specify parsing of a JLANG::JString object.
24 * This class extends the std::locale::facet class.
25 */
26 class JStringFacet :
27 public std::locale::facet
28 {
29 public:
30
31 static std::locale::id id;
32
33
34 typedef std::istreambuf_iterator<char, std::char_traits<char> > istreambuf_iterator;
35 typedef std::ostreambuf_iterator<char, std::char_traits<char> > ostreambuf_iterator;
36
37
38
39 /**
40 * Constructor.
41 *
42 * \param refs reference count
43 */
44 JStringFacet(std::size_t refs = 0) :
45 std::locale::facet(refs)
46 {}
47
48
49 /**
50 * Clone this facet.
51 *
52 * \return pointer to newly created facet
53 */
54 virtual JStringFacet* clone() const
55 {
56 return new JStringFacet();
57 }
58
59
60 /**
61 * Get string.
62 *
63 * \param __begin begin position of input stream
64 * \param __end end position of input stream
65 * \param format format
66 * \param result status after input operation
67 * \param buffer output string
68 * \return position of input stream
69 */
71 const istreambuf_iterator __end,
72 const std::ios_base& format,
73 std::ios_base::iostate& result,
74 std::string& buffer) const
75 {
76 return do_get(__begin, __end, format, result, buffer);
77 }
78
79
80 /**
81 * Put string.
82 *
83 * \param out begin position of output stream
84 * \param format format
85 * \param c fill character
86 * \param buffer input string
87 * \return position of output stream buffer
88 */
90 const std::ios_base& format,
91 const char c,
92 const std::string& buffer) const
93 {
94 return do_put(out, format, c, buffer);
95 }
96
97
98 /**
99 * Ignore characters until next end of line.
100 *
101 * \param in input stream
102 * \return input stream
103 */
104 inline std::istream& ignore(std::istream& in) const
105 {
107 in.setstate(std::ios_base::eofbit);
108 }
109
110 return in;
111 }
112
113
114 /**
115 * Read characters until next end of line.
116 *
117 * \param in input stream
118 * \param buffer output string
119 * \return input stream
120 */
121 inline std::istream& getline(std::istream& in,
122 std::string& buffer) const
123 {
124 using namespace std;
125
126 ios_base::iostate state = in.rdstate();
127
128 if (do_getline(in, istreambuf_iterator(), state, buffer) == istreambuf_iterator()) {
129 in.setstate(std::ios_base::eofbit);
130 }
131
132 if (state != ios_base::goodbit && state != ios_base::eofbit) {
133 in.setstate(state);
134 }
135
136 return in;
137 }
138
139
140 /**
141 * Get index for stream associated facet data.
142 *
143 * \return index
144 */
145 static int getIndex()
146 {
147 static int i = std::ios_base::xalloc();
148
149 return i;
150 }
151
152
153 protected:
154 /**
155 * Get string.
156 *
157 * \param __begin begin position of input stream
158 * \param __end end position of input stream
159 * \param format format
160 * \param result status after input operation
161 * \param buffer output string
162 * \return position of input stream
163 */
165 const istreambuf_iterator __end,
166 const std::ios_base& format,
167 std::ios_base::iostate& result,
168 std::string& buffer) const
169 {
170 using namespace std;
171
172 result = (ios_base::iostate) 0; // reset I/O status
173
174 streamsize n = format.width(); // number of characters to read
175
176 if (n == 0) {
177 n = numeric_limits<streamsize>::max();
178 }
179
180 istreambuf_iterator i = __begin;
181
182 if (i == __end) {
183
184 result |= ios_base::failbit;
185 result |= ios_base::eofbit;
186
187 } else {
188
189 buffer.clear();
190
191 buffer.push_back(*i);
192
193 for (++i, --n; i != __end && n != 0; ++i, --n) {
194 buffer.push_back(*i);
195 }
196
197 if (i == __end) {
198 result |= ios_base::eofbit;
199 }
200 }
201
202 return i;
203 }
204
205
206 /**
207 * Put string.
208 *
209 * \param out begin position of output stream
210 * \param format format
211 * \param c fill character
212 * \param buffer input string
213 * \return current position of output stream
214 */
216 const std::ios_base& format,
217 const char c,
218 const std::string& buffer) const
219 {
220 using namespace std;
221
222 if (format.flags() & ios_base::right) {
223 for (streamsize i = buffer.size(); i < format.width(); ++i, ++out) {
224 *out = c;
225 }
226 }
227
228 for (string::const_iterator i = buffer.begin(); i != buffer.end(); ++i, ++out) {
229 *out = *i;
230 }
231
232 if (format.flags() & ios_base::left) {
233 for (streamsize i = buffer.size(); i < format.width(); ++i, ++out) {
234 *out = c;
235 }
236 }
237
238 return out;
239 }
240
241
242 /**
243 * Ignore characters until next end of line.
244 *
245 * \param __begin begin position of input stream
246 * \param __end end position of input stream
247 * \return position of input stream
248 */
250 const istreambuf_iterator __end) const
251 {
252 istreambuf_iterator i = __begin;
253
254 while (i != __end && *i != '\n') {
255 ++i;
256 }
257
258 while (i != __end && *i == '\n') {
259 ++i; // skip end of line(s)
260 }
261
262 return i;
263 }
264
265
266 /**
267 * Read string.
268 *
269 * \param __begin begin position of input stream
270 * \param __end end position of input stream
271 * \param result status after input operation
272 * \param buffer output string
273 * \return position of input stream
274 */
276 const istreambuf_iterator __end,
277 std::ios_base::iostate& result,
278 std::string& buffer) const
279 {
280 using namespace std;
281
282 istreambuf_iterator i = __begin;
283
284 if (i == __end) {
285
286 result |= ios_base::failbit;
287 result |= ios_base::eofbit;
288
289 } else {
290
291 buffer.clear();
292
293 for ( ; i != __end && *i != '\n'; ++i) {
294 buffer.push_back(*i);
295 }
296
297 if (i != __end) {
298 ++i; // skip end of line
299 }
300 }
301
302 return i;
303 }
304
305
306 private:
307
308 JStringFacet(const JStringFacet&); // not defined
309 void operator=(const JStringFacet&); // not defined
310 };
311}
312
313#endif
Facet class to specify parsing of a JLANG::JString object.
JStringFacet(const JStringFacet &)
virtual ostreambuf_iterator do_put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const
Put string.
ostreambuf_iterator put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const
Put string.
std::istream & ignore(std::istream &in) const
Ignore characters until next end of line.
static std::locale::id id
virtual JStringFacet * clone() const
Clone this facet.
istreambuf_iterator get(const istreambuf_iterator __begin, const istreambuf_iterator __end, const std::ios_base &format, std::ios_base::iostate &result, std::string &buffer) const
Get string.
static int getIndex()
Get index for stream associated facet data.
std::istream & getline(std::istream &in, std::string &buffer) const
Read characters until next end of line.
JStringFacet(std::size_t refs=0)
Constructor.
virtual istreambuf_iterator do_get(const istreambuf_iterator __begin, const istreambuf_iterator __end, const std::ios_base &format, std::ios_base::iostate &result, std::string &buffer) const
Get string.
std::istreambuf_iterator< char, std::char_traits< char > > istreambuf_iterator
std::ostreambuf_iterator< char, std::char_traits< char > > ostreambuf_iterator
virtual istreambuf_iterator do_ignore(const istreambuf_iterator __begin, const istreambuf_iterator __end) const
Ignore characters until next end of line.
void operator=(const JStringFacet &)
virtual istreambuf_iterator do_getline(const istreambuf_iterator __begin, const istreambuf_iterator __end, std::ios_base::iostate &result, std::string &buffer) const
Read string.
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).