Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JEquationFacet.hh
Go to the documentation of this file.
1#ifndef __JLANG__JEQUATIONFACET__
2#define __JLANG__JEQUATIONFACET__
3
4#include <istream>
5#include <ostream>
6#include <locale>
7#include <string>
8#include <iterator>
9#include <cstdio>
10#include <limits>
11
12#include "JLang/JStringFacet.hh"
14
15
16/**
17 * \author mdejong
18 */
19
20namespace JLANG {}
21namespace JPP { using namespace JLANG; }
22
23namespace JLANG {
24
25
26 /**
27 * Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
28 * Tokens are defined as a piece of text delimited by various markers according the facet.
29 * The list of markers is defined by the JLANG::JEquationParameters data structure.
30 * This class extends the JLANG::JStringFacet and JLANG::JEquationParameters classes.
31 */
33 public JStringFacet,
35 {
36 public:
37
40
41
42 /**
43 * Default constructor.
44 */
49
50
51 /**
52 * Constructor.
53 *
54 * \param parameters equation parameters
55 */
58 JEquationParameters(parameters)
59 {}
60
61
62 /**
63 * Clone this facet.
64 *
65 * \return pointer to newly created facet
66 */
67 virtual JEquationFacet* clone() const override
68 {
69 return new JEquationFacet(static_cast<const JEquationParameters&>(*this));
70 }
71
72
73 /**
74 * Get character.
75 *
76 * \param __begin begin position of input stream
77 * \param __end end position of input stream
78 * \param format format
79 * \param result status after input operation
80 * \param buffer output character
81 * \return position of input stream
82 */
84 const istreambuf_iterator __end,
85 const std::ios_base& format,
86 std::ios_base::iostate& result,
87 char& buffer) const
88 {
89 return do_get(__begin, __end, format, result, buffer);
90 }
91
92
93 /**
94 * Put character.
95 *
96 * \param out begin position of output stream
97 * \param format format
98 * \param c fill character
99 * \param buffer input character
100 * \return position of output stream buffer
101 */
103 const std::ios_base& format,
104 const char c,
105 const char buffer) const
106 {
107 return do_put(out, format, c, buffer);
108 }
109
110
111 /**
112 * Get combined prefix for output.
113 *
114 * \param prefix prefix
115 * \param name name
116 * \return prefix
117 */
118 const std::string getPrefix(const std::string& prefix, const std::string& name) const
119 {
120 if (prefix.empty())
121 return name;
122 else
123 return prefix + getDefaultDivision() + name;
124 }
125
126
127 /**
128 * Pop white spaces.
129 *
130 * \param in input stream
131 * \return input stream
132 */
133 std::istream& pop(std::istream& in) const
134 {
135 try {
136 for (int c; (c = in.peek()) != EOF && isWhiteSpace((char) c); ) { in.get(); }
137 }
138 catch(const std::exception& error) {};
139
140 return in;
141 }
142
143 protected:
144 /**
145 * Get string.
146 *
147 * \param __begin begin position of input stream
148 * \param __end end position of input stream
149 * \param format format
150 * \param result status after input operation
151 * \param buffer output string
152 * \return position of input stream
153 */
155 const istreambuf_iterator __end,
156 const std::ios_base& format,
157 std::ios_base::iostate& result,
158 std::string& buffer) const override
159 {
160 using namespace std;
161
162 result = (ios_base::iostate) 0; // reset I/O status
163
164 streamsize n = format.width(); // number of characters to read
165
166 if (n == 0) {
167 n = numeric_limits<streamsize>::max();
168 }
169
170 istreambuf_iterator i = __begin;
171
172 while (i != __end && isWhiteSpace(*i)) {
173 ++i;
174 }
175
176 if (i == __end) {
177
178 result |= ios_base::failbit;
179 result |= ios_base::eofbit;
180
181 } else if (isSeparator(*i) ||
182 isEndOfLine(*i) ||
183 isDivision (*i)) {
184
185 result |= ios_base::failbit;
186
187 } else {
188
189 buffer.clear();
190 buffer.push_back(*i);
191
192 for (++i, --n; i != __end && n != 0 && (!isWhiteSpace(*i) &&
193 !isSeparator (*i) &&
194 !isEndOfLine (*i) &&
195 !isDivision (*i)); ++i, --n)
196 buffer.push_back(*i);
197
198 if (i == __end) {
199 result |= ios_base::eofbit;
200 }
201 }
202
203 return i;
204 }
205
206
207 /**
208 * Get character.
209 *
210 * \param __begin begin position of input stream
211 * \param __end end position of input stream
212 * \param format format
213 * \param result status after input operation
214 * \param buffer output character
215 * \return position of input stream
216 */
218 const istreambuf_iterator __end,
219 const std::ios_base& format,
220 std::ios_base::iostate& result,
221 char& buffer) const
222 {
223 using namespace std;
224
225 result = (ios_base::iostate) 0; // reset I/O status
226
227 istreambuf_iterator i = __begin;
228
229 while (i != __end && isWhiteSpace(*i)) {
230 ++i;
231 }
232
233 if (i == __end) {
234
235 result |= ios_base::failbit;
236 result |= ios_base::eofbit;
237
238 } else if (!isDivision(*i) && !isSeparator(*i)) {
239
240 result |= ios_base::failbit;
241
242 } else {
243
244 buffer = *i++;
245 }
246
247 return i;
248 }
249
250
251 /**
252 * Put string.
253 *
254 * \param out begin position of output stream
255 * \param format format
256 * \param c fill character
257 * \param buffer input string
258 * \return current position of output stream
259 */
261 const std::ios_base& format,
262 const char c,
263 const std::string& buffer) const override
264 {
265 using namespace std;
266
267 if (format.flags() & ios_base::right) {
268 for (streamsize i = buffer.size(); i < format.width(); ++i, ++out) {
269 *out = c;
270 }
271 }
272
273 for (string::const_iterator i = buffer.begin(); i != buffer.end(); ++i, ++out) {
274 *out = *i;
275 }
276
277 if (format.flags() & ios_base::left) {
278 for (streamsize i = buffer.size(); i < format.width(); ++i, ++out) {
279 *out = c;
280 }
281 }
282
283 return out;
284 }
285
286
287 /**
288 * Put character.
289 *
290 * \param out begin position of output stream
291 * \param format format
292 * \param c fill character
293 * \param buffer input character
294 * \return current position of output stream
295 */
297 const std::ios_base& format,
298 const char c,
299 const char buffer) const
300 {
301 using namespace std;
302
303 if (format.flags() & ios_base::right) {
304 for (streamsize i = 1; i < format.width(); ++i, ++out) {
305 *out = c;
306 }
307 }
308
309 *out = buffer;
310 ++out;
311
312 if (format.flags() & ios_base::left) {
313 for (streamsize i = 1; i < format.width(); ++i, ++out) {
314 *out = c;
315 }
316 }
317
318 return out;
319 }
320
321
322 /**
323 * \cond NEVER
324 * Ignore characters until next end of line.
325 *
326 * \param __begin begin position of input stream
327 * \param __end end position of input stream
328 * \return position of input stream
329 * \endcond
330 */
331 /*
332 virtual istreambuf_iterator do_ignore(const istreambuf_iterator __begin,
333 const istreambuf_iterator __end) const
334 {
335 istreambuf_iterator i = __begin;
336
337 while (i != __end && !isEndOfLine(*i)) {
338 ++i;
339 }
340
341 while (i != __end && isEndOfLine(*i)) {
342 ++i; // skip end of line(s)
343 }
344
345 return i;
346 }
347 */
348
349 /**
350 * Read string.
351 *
352 * \param __begin begin position of input stream
353 * \param __end end position of input stream
354 * \param result status after input operation
355 * \param buffer output string
356 * \return position of input stream
357 */
359 const istreambuf_iterator __end,
360 std::ios_base::iostate& result,
361 std::string& buffer) const override
362 {
363 using namespace std;
364
365 result = (ios_base::iostate) 0; // reset I/O status
366
367 istreambuf_iterator i = __begin;
368
369 while (i != __end && isWhiteSpace(*i) && !isEndOfLine(*i)) {
370 ++i;
371 }
372
373 if (i == __end) {
374
375 result |= ios_base::failbit;
376 result |= ios_base::eofbit;
377
378 } else if (isEndOfLine(*i)) {
379
380 buffer.clear();
381
382 } else {
383
384 buffer.clear();
385
386 for (int count = 0; i != __end && (count != 0 || !isEndOfLine(*i)); ++i) {
387
388 buffer.push_back(*i);
389
390 if (isLeftBracket (*i)) {
391 ++count;
392 } else if (isRightBracket(*i)) {
393 --count;
394 }
395 }
396
397 while (i != __end && isEndOfLine(*i)) {
398 ++i; // skip end of line(s)
399 }
400 }
401
402 if (getComment() != "") {
403
404 string::size_type pos = buffer.find(getComment());
405
406 if (pos != string::npos) {
407 buffer.erase(pos);
408 }
409 }
410
411 return i;
412 }
413
414 private:
415
416 JEquationFacet(const JEquationFacet&); // not defined
417 void operator=(const JEquationFacet&); // not defined
418 };
419
420
421 /**
422 * Auxiliary class for end of line.
423 */
425 {
426 /**
427 * Default constructor.
428 */
430 index(0)
431 {}
432
433
434 /**
435 * Constructor.
436 *
437 * \param i index
438 * \return this JEndOfLine
439 */
440 const JEndOfLine& operator()(const unsigned int i) const
441 {
442 index = i;
443
444 return *this;
445 }
446
447
448 /**
449 * Print end of line.
450 *
451 * \param out output stream
452 * \param eol end of line
453 * \return output stream
454 */
455 friend inline std::ostream& operator<<(std::ostream& out, const JEndOfLine& eol)
456 {
457 using namespace std;
458
459 if (has_facet<JEquationFacet>(out.getloc()))
460 out << use_facet<JEquationFacet>(out.getloc()).getPreferredEndOfLine(eol.index);
461 else
462 out << '\n';
463
464 eol.index = 0;
465
466 return out;
467 }
468
469 private:
470 mutable unsigned int index;
471 };
472
473
474 /**
475 * Type definition of stream manipulator for equational I/O.
476 */
478
479
480 /**
481 * Parse facet.
482 *
483 * \param in input stream
484 * \param facet facet
485 * \return input stream
486 */
487 inline std::istream& operator>>(std::istream& in, const JLANG::JEquationFacet& facet)
488 {
489 using namespace std;
490
491 in.imbue(locale(in.getloc(), facet.clone()));
492
493 return in;
494 }
495
496
497 /**
498 * Parse facet.
499 *
500 * \param out output stream
501 * \param facet facet
502 * \return output stream
503 */
504 inline std::ostream& operator<<(std::ostream& out, const JLANG::JEquationFacet& facet)
505 {
506 using namespace std;
507
508 out.imbue(locale(out.getloc(), facet.clone()));
509
510 return out;
511 }
512
513
514 /**
515 * Print white space.
516 *
517 * \param out output stream
518 * \return output stream
519 */
520 inline std::ostream& white_space(std::ostream& out)
521 {
522 using namespace std;
523 using namespace JLANG;
524
525 if (has_facet<JEquationFacet>(out.getloc()))
526 out << use_facet<JEquationFacet>(out.getloc()).getDefaultWhiteSpace();
527 else
528 out << ' ';
529
530 return out;
531 }
532
533
534 /**
535 * Print division.
536 *
537 * \param out output stream
538 * \return output stream
539 */
540 inline std::ostream& division(std::ostream& out)
541 {
542 using namespace std;
543 using namespace JLANG;
544
545 if (has_facet<JEquationFacet>(out.getloc()))
546 out << use_facet<JEquationFacet>(out.getloc()).getDefaultDivision();
547 else
548 out << '.';
549
550 return out;
551 }
552
553
554 /**
555 * Print separator.
556 *
557 * \param out output stream
558 * \return output stream
559 */
560 inline std::ostream& separator(std::ostream& out)
561 {
562 using namespace std;
563 using namespace JLANG;
564
565 if (has_facet<JEquationFacet>(out.getloc()))
566 out << use_facet<JEquationFacet>(out.getloc()).getDefaultSeparator();
567 else
568 out << '=';
569
570 return out;
571 }
572
573
574 /**
575 * Print end of line.
576 */
578
579
580 /**
581 * Print left bracket.
582 *
583 * \param out output stream
584 * \return output stream
585 */
586 inline std::ostream& left_bracket(std::ostream& out)
587 {
588 using namespace std;
589 using namespace JLANG;
590
591 if (has_facet<JEquationFacet>(out.getloc()))
592 out << use_facet<JEquationFacet>(out.getloc()).getLeftBracket();
593 else
594 out << '(';
595
596 return out;
597 }
598
599
600 /**
601 * Print right bracket.
602 *
603 * \param out output stream
604 * \return output stream
605 */
606 inline std::ostream& right_bracket(std::ostream& out)
607 {
608 using namespace std;
609 using namespace JLANG;
610
611 if (has_facet<JEquationFacet>(out.getloc()))
612 out << use_facet<JEquationFacet>(out.getloc()).getRightBracket();
613 else
614 out << ')';
615
616 return out;
617 }
618}
619
620#endif
Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
void operator=(const JEquationFacet &)
JEquationFacet(const JEquationFacet &)
istreambuf_iterator get(const istreambuf_iterator __begin, const istreambuf_iterator __end, const std::ios_base &format, std::ios_base::iostate &result, char &buffer) const
Get character.
std::istream & pop(std::istream &in) const
Pop white spaces.
ostreambuf_iterator put(ostreambuf_iterator out, const std::ios_base &format, const char c, const char buffer) const
Put character.
virtual istreambuf_iterator do_getline(const istreambuf_iterator __begin, const istreambuf_iterator __end, std::ios_base::iostate &result, std::string &buffer) const override
Read string.
JEquationFacet(const JEquationParameters &parameters)
Constructor.
JEquationFacet()
Default 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 override
Get string.
const std::string getPrefix(const std::string &prefix, const std::string &name) const
Get combined prefix for output.
virtual istreambuf_iterator do_get(const istreambuf_iterator __begin, const istreambuf_iterator __end, const std::ios_base &format, std::ios_base::iostate &result, char &buffer) const
Get character.
virtual JEquationFacet * clone() const override
Clone this facet.
virtual ostreambuf_iterator do_put(ostreambuf_iterator out, const std::ios_base &format, const char c, const char buffer) const
Put character.
virtual ostreambuf_iterator do_put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const override
Put string.
Simple data structure to support I/O of equations (see class JLANG::JEquation).
bool isSeparator(const char c) const
Test for separator character.
const std::string & getComment() const
Get comment string.
bool isDivision(const char c) const
Test for division character.
bool isEndOfLine(const char c) const
Test for end of line character.
bool isRightBracket(const char c) const
Test for right bracket character.
bool isLeftBracket(const char c) const
Test for left bracket character.
bool isWhiteSpace(const char c) const
Test for white space character.
const char getDefaultDivision() const
Get default division character.
Facet class to specify parsing of a JLANG::JString object.
ostreambuf_iterator put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const
Put string.
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.
std::istreambuf_iterator< char, std::char_traits< char > > istreambuf_iterator
std::ostreambuf_iterator< char, std::char_traits< char > > ostreambuf_iterator
Auxiliary classes and methods for language specific functionality.
std::ostream & right_bracket(std::ostream &out)
Print right bracket.
std::ostream & left_bracket(std::ostream &out)
Print left bracket.
std::ostream & white_space(std::ostream &out)
Print white space.
std::ostream & operator<<(std::ostream &out, const JLANG::JEquationFacet &facet)
Parse facet.
JLANG::JEquationFacet setequation
Type definition of stream manipulator for equational I/O.
std::ostream & separator(std::ostream &out)
Print separator.
std::istream & operator>>(std::istream &in, const JLANG::JEquationFacet &facet)
Parse facet.
std::ostream & division(std::ostream &out)
Print division.
static const JLANG::JEndOfLine end_of_line
Print end of line.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for end of line.
const JEndOfLine & operator()(const unsigned int i) const
Constructor.
JEndOfLine()
Default constructor.
friend std::ostream & operator<<(std::ostream &out, const JEndOfLine &eol)
Print end of line.