Jpp  15.0.4
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
20 namespace JLANG {}
21 namespace JPP { using namespace JLANG; }
22 
23 namespace 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,
34  public JEquationParameters
35  {
36  public:
37 
38  using JStringFacet::get;
39  using JStringFacet::put;
40 
41 
42  /**
43  * Default constructor.
44  */
46  JStringFacet(),
48  {}
49 
50 
51  /**
52  * Constructor.
53  *
54  * \param parameters equation parameters
55  */
57  JStringFacet(),
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  return i;
403  }
404 
405  private:
406 
407  JEquationFacet(const JEquationFacet&); // not defined
408  void operator=(const JEquationFacet&); // not defined
409  };
410 
411 
412  /**
413  * Auxiliary class for end of line.
414  */
415  struct JEndOfLine
416  {
417  /**
418  * Default constructor.
419  */
421  index(0)
422  {}
423 
424 
425  /**
426  * Constructor.
427  *
428  * \param i index
429  * \return this JEndOfLine
430  */
431  const JEndOfLine& operator()(const unsigned int i) const
432  {
433  index = i;
434 
435  return *this;
436  }
437 
438 
439  /**
440  * Print end of line.
441  *
442  * \param out output stream
443  * \param eol end of line
444  * \return output stream
445  */
446  friend inline std::ostream& operator<<(std::ostream& out, const JEndOfLine& eol)
447  {
448  using namespace std;
449 
450  if (has_facet<JEquationFacet>(out.getloc()))
451  out << use_facet<JEquationFacet>(out.getloc()).getPreferredEndOfLine(eol.index);
452  else
453  out << '\n';
454 
455  eol.index = 0;
456 
457  return out;
458  }
459 
460  private:
461  mutable unsigned int index;
462  };
463 
464 
465  /**
466  * Type definition of stream manipulator for equational I/O.
467  */
469 
470 
471  /**
472  * Parse facet.
473  *
474  * \param in input stream
475  * \param facet facet
476  * \return input stream
477  */
478  inline std::istream& operator>>(std::istream& in, const JLANG::JEquationFacet& facet)
479  {
480  using namespace std;
481 
482  in.imbue(locale(in.getloc(), facet.clone()));
483 
484  return in;
485  }
486 
487 
488  /**
489  * Parse facet.
490  *
491  * \param out output stream
492  * \param facet facet
493  * \return output stream
494  */
495  inline std::ostream& operator<<(std::ostream& out, const JLANG::JEquationFacet& facet)
496  {
497  using namespace std;
498 
499  out.imbue(locale(out.getloc(), facet.clone()));
500 
501  return out;
502  }
503 
504 
505  /**
506  * Print white space.
507  *
508  * \param out output stream
509  * \return output stream
510  */
511  inline std::ostream& white_space(std::ostream& out)
512  {
513  using namespace std;
514  using namespace JLANG;
515 
516  if (has_facet<JEquationFacet>(out.getloc()))
517  out << use_facet<JEquationFacet>(out.getloc()).getDefaultWhiteSpace();
518  else
519  out << ' ';
520 
521  return out;
522  }
523 
524 
525  /**
526  * Print division.
527  *
528  * \param out output stream
529  * \return output stream
530  */
531  inline std::ostream& division(std::ostream& out)
532  {
533  using namespace std;
534  using namespace JLANG;
535 
536  if (has_facet<JEquationFacet>(out.getloc()))
537  out << use_facet<JEquationFacet>(out.getloc()).getDefaultDivision();
538  else
539  out << '.';
540 
541  return out;
542  }
543 
544 
545  /**
546  * Print separator.
547  *
548  * \param out output stream
549  * \return output stream
550  */
551  inline std::ostream& separator(std::ostream& out)
552  {
553  using namespace std;
554  using namespace JLANG;
555 
556  if (has_facet<JEquationFacet>(out.getloc()))
557  out << use_facet<JEquationFacet>(out.getloc()).getDefaultSeparator();
558  else
559  out << '=';
560 
561  return out;
562  }
563 
564 
565  /**
566  * Print end of line.
567  */
569 
570 
571  /**
572  * Print left bracket.
573  *
574  * \param out output stream
575  * \return output stream
576  */
577  inline std::ostream& left_bracket(std::ostream& out)
578  {
579  using namespace std;
580  using namespace JLANG;
581 
582  if (has_facet<JEquationFacet>(out.getloc()))
583  out << use_facet<JEquationFacet>(out.getloc()).getLeftBracket();
584  else
585  out << '(';
586 
587  return out;
588  }
589 
590 
591  /**
592  * Print right bracket.
593  *
594  * \param out output stream
595  * \return output stream
596  */
597  inline std::ostream& right_bracket(std::ostream& out)
598  {
599  using namespace std;
600  using namespace JLANG;
601 
602  if (has_facet<JEquationFacet>(out.getloc()))
603  out << use_facet<JEquationFacet>(out.getloc()).getRightBracket();
604  else
605  out << ')';
606 
607  return out;
608  }
609 }
610 
611 #endif
virtual ostreambuf_iterator do_put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const override
Put string.
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.
unsigned int index
Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
const char getDefaultSeparator() const
Get default separator character.
const JEndOfLine & operator()(const unsigned int i) const
Constructor.
std::ostream & right_bracket(std::ostream &out)
Print right bracket.
bool isRightBracket(const char c) const
Test for right bracket character.
std::istreambuf_iterator< char, std::char_traits< char > > istreambuf_iterator
Definition: JStringFacet.hh:34
std::ostreambuf_iterator< char, std::char_traits< char > > ostreambuf_iterator
Definition: JStringFacet.hh:35
std::ostream & division(std::ostream &out)
Print division.
const std::string getPrefix(const std::string &prefix, const std::string &name) const
Get combined prefix for output.
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
std::ostream & white_space(std::ostream &out)
Print white space.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
Simple data structure to support I/O of equations (see class JLANG::JEquation).
ostreambuf_iterator put(ostreambuf_iterator out, const std::ios_base &format, const char c, const char buffer) const
Put character.
const char getDefaultWhiteSpace() const
Get default white space character.
const char getPreferredEndOfLine(const unsigned int index) const
Get preferred end of line character.
bool isEndOfLine(const char c) const
Test for end of line character.
const int n
Definition: JPolint.hh:660
JEquationFacet(const JEquationParameters &parameters)
Constructor.
std::ostream & left_bracket(std::ostream &out)
Print left bracket.
char getRightBracket() const
Get right bracket.
virtual ostreambuf_iterator do_put(ostreambuf_iterator out, const std::ios_base &format, const char c, const char buffer) const
Put character.
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.
std::ostream & separator(std::ostream &out)
Print separator.
std::istream & pop(std::istream &in) const
Pop white spaces.
friend std::ostream & operator<<(std::ostream &out, const JEndOfLine &eol)
Print end of line.
char getLeftBracket() const
Get left bracket.
JEndOfLine()
Default constructor.
return result
Definition: JPolint.hh:727
const char getDefaultDivision() const
Get default division character.
Auxiliary class for end of line.
bool isWhiteSpace(const char c) const
Test for white space character.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1618
std::vector< int > count
Definition: JAlgorithm.hh:180
JEquationFacet()
Default constructor.
JLANG::JEquationFacet setequation
Type definition of stream manipulator for equational I/O.
ostreambuf_iterator put(ostreambuf_iterator out, const std::ios_base &format, const char c, const std::string &buffer) const
Put string.
Definition: JStringFacet.hh:89
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.
Definition: JStringFacet.hh:70
Facet class to specify parsing of a JLANG::JString object.
Definition: JStringFacet.hh:26
bool isSeparator(const char c) const
Test for separator character.
static const JLANG::JEndOfLine end_of_line
Print end of line.
bool isDivision(const char c) const
Test for division character.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
void operator=(const JEquationFacet &)
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
virtual JEquationFacet * clone() const override
Clone this facet.
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.
bool isLeftBracket(const char c) const
Test for left bracket character.