Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPrint.hh
Go to the documentation of this file.
1 #ifndef __JEEP__JPRINT__
2 #define __JEEP__JPRINT__
3 
4 #include <ostream>
5 #include <sstream>
6 #include <iomanip>
7 
8 #include "Jeep/JColor.hh"
9 
10 
11 /**
12  * \file
13  * I/O formatting auxiliaries.
14  * \author mdejong
15  */
16 namespace JEEP {}
17 namespace JPP { using namespace JEEP; }
18 
19 namespace JEEP {
20 
21  /**
22  * Get index for user I/O manipulation.
23  *
24  * \return index
25  */
26  inline int getIndex()
27  {
28  static const int index = std::ios_base::xalloc();
29 
30  return index;
31  }
32 
33 
34  /**
35  * Print options.
36  */
38  SHORT_PRINT = 1, //!< short print
39  MEDIUM_PRINT = 2, //!< medium print
40  LONG_PRINT = 3 //!< long print
41  };
42 
43 
44  /**
45  * Get output stream for conversion to std::string or C-string.
46  *
47  * Note that the ostream is emptied before use.
48  *
49  * \return ostream
50  */
51  inline std::ostream& getOstream()
52  {
53  static std::ostringstream buffer;
54 
55  buffer.str("");
56 
57  return buffer;
58  }
59 }
60 
61 
62 /**
63  * Get print option.
64  *
65  * \param out output stream
66  * \return print option
67  */
68 inline int getPrintOption(std::ostream& out)
69 {
70  return out.iword(JEEP::getIndex());
71 }
72 
73 
74 /**
75  * Set print option.
76  *
77  * \param out output stream
78  * \param option print option
79  */
80 inline void setPrintOption(std::ostream& out, const int option)
81 {
82  out.iword(JEEP::getIndex()) = option;
83 }
84 
85 
86 /**
87  * Get short print option.
88  *
89  * \param out output stream
90  * \return true if short print option is on; else false
91  */
92 inline bool getShortprint(std::ostream& out)
93 {
94  return getPrintOption(out) == JEEP::SHORT_PRINT;
95 }
96 
97 
98 /**
99  * Set short print option.
100  *
101  * \param out output stream
102  */
103 inline void setShortprint(std::ostream& out)
104 {
105  return setPrintOption(out, JEEP::SHORT_PRINT);
106 }
107 
108 
109 /**
110  * Get medium print option.
111  *
112  * \param out output stream
113  * \return true if medium print option is on; else false
114  */
115 inline bool getMediumprint(std::ostream& out)
116 {
117  return getPrintOption(out) == JEEP::MEDIUM_PRINT;
118 }
119 
120 
121 /**
122  * Set medium print option.
123  *
124  * \param out output stream
125  */
126 inline void setMediumprint(std::ostream& out)
127 {
128  return setPrintOption(out, JEEP::MEDIUM_PRINT);
129 }
130 
131 
132 /**
133  * Get long print option.
134  *
135  * \param out output stream
136  * \return true if long print option is on; else false
137  */
138 inline bool getLongprint(std::ostream& out)
139 {
140  return getPrintOption(out) == JEEP::LONG_PRINT;
141 }
142 
143 
144 /**
145  * Set long print option.
146  *
147  * \param out output stream
148  */
149 inline void setLongprint(std::ostream& out)
150 {
151  return setPrintOption(out, JEEP::LONG_PRINT);
152 }
153 
154 
155 /**
156  * Set short printing.
157  *
158  * \param out output stream
159  * \return output stream
160  */
161 inline std::ostream& shortprint(std::ostream& out)
162 {
163  setShortprint(out);
164 
165  return out;
166 }
167 
168 
169 /**
170  * Set medium printing.
171  *
172  * \param out output stream
173  * \return output stream
174  */
175 inline std::ostream& mediumprint(std::ostream& out)
176 {
177  setMediumprint(out);
178 
179  return out;
180 }
181 
182 
183 /**
184  * Set long printing.
185  *
186  * \param out output stream
187  * \return output stream
188  */
189 inline std::ostream& longprint(std::ostream& out)
190 {
191  setLongprint(out);
192 
193  return out;
194 }
195 
196 
197 /**
198  * Print newline character.
199  *
200  * \param out output stream
201  * \return output stream
202  */
203 inline std::ostream& newline(std::ostream& out)
204 {
205  return out << '\n';
206 }
207 
208 
209 /**
210  * Print white space character.
211  *
212  * \param out output stream
213  * \return output stream
214  */
215 inline std::ostream& whitespace(std::ostream& out)
216 {
217  return out << ' ';
218 }
219 
220 
221 /**
222  * Print tab character.
223  *
224  * \param out output stream
225  * \return output stream
226  */
227 inline std::ostream& tab(std::ostream& out)
228 {
229  return out << '\t';
230 }
231 
232 
233 /**
234  * Rewind character.
235  *
236  * \param out output stream
237  * \return output stream
238  */
239 inline std::ostream& rewind(std::ostream& out)
240 {
241  return (out << '\r').flush();
242 }
243 
244 
245 /**
246  * Auxiliary data structure for alignment of data.
247  */
248 struct WIDTH {
249  /**
250  * Constructor.
251  *
252  * \param width width
253  */
254  WIDTH(const int width)
255  {
256  this->width = width;
257  }
258 
259  /**
260  * Format specifier.
261  *
262  * \param out output stream
263  * \param format format
264  * \return output stream
265  */
266  friend inline std::ostream& operator<<(std::ostream& out, const WIDTH& format)
267  {
268  using namespace std;
269 
270  return out << setw(format.width);
271  }
272 
273  int width;
274 };
275 
276 
277 /**
278  * Auxiliary data structure for alignment of data.
279  */
280 struct LEFT :
281  public WIDTH
282 {
283  /**
284  * Constructor.
285  *
286  * \param width width
287  */
288  LEFT(const int width) :
289  WIDTH(width)
290  {}
291 
292  /**
293  * Format specifier.
294  *
295  * \param out output stream
296  * \param format format
297  * \return output stream
298  */
299  friend inline std::ostream& operator<<(std::ostream& out, const LEFT& format)
300  {
301  using namespace std;
302 
303  return out << setw(format.width) << left;
304  }
305 };
306 
307 
308 /**
309  * Auxiliary data structure for alignment of data.
310  */
311 struct RIGHT :
312  public WIDTH
313 {
314  /**
315  * Constructor.
316  *
317  * \param width width
318  */
319  RIGHT(const int width) :
320  WIDTH(width)
321  {}
322 
323  /**
324  * Format specifier.
325  *
326  * \param out output stream
327  * \param format format
328  * \return output stream
329  */
330  friend inline std::ostream& operator<<(std::ostream& out, const RIGHT& format)
331  {
332  using namespace std;
333 
334  return out << setw(format.width) << right;
335  }
336 };
337 
338 
339 /**
340  * Auxiliary data structure for sequence of same character.
341  */
342 struct FILL :
343  public WIDTH
344 {
345  /**
346  * Constructor.
347  *
348  * \param width width
349  * \param fill fill character
350  */
351  FILL(const int width,
352  const char fill = ' ') :
353  WIDTH(width)
354  {
355  this->fill = fill;
356  }
357 
358  /**
359  * Format specifier.
360  *
361  * \param out output stream
362  * \param format format
363  * \return output stream
364  */
365  friend inline std::ostream& operator<<(std::ostream& out, const FILL& format)
366  {
367  using namespace std;
368 
369  const char c = out.fill();
370 
371  return out << setfill(format.fill) << setw(format.width) << format.fill << setfill(c);
372  }
373 
374  char fill;
375 };
376 
377 
378 /**
379  * Auxiliary data structure for alignment of data.
380  */
381 struct CENTER :
382  public WIDTH
383 {
384 protected:
385  /**
386  * Auxiliary class for format center.
387  */
388  struct JCenter :
389  public WIDTH
390  {
391  /**
392  * Constructor.
393  *
394  * \param out output stream
395  * \param format format center
396  */
397  JCenter(std::ostream& out, const WIDTH& format) :
398  WIDTH(format),
399  out (out)
400  {}
401 
402 
403  /**
404  * Write value to output stream.
405  *
406  * \param value value
407  * \return this JCenter
408  */
409  template<class T>
410  std::ostream& operator<<(const T& value)
411  {
412  using namespace std;
413 
414  ostringstream os;
415 
416  os.copyfmt(out);
417 
418  os << value;
419 
420  const int w = this->width - os.str().size();
421  const char c = this->out.fill();
422 
423  if (w > 0)
424  return this->out << FILL(w/2) << os.str() << FILL((w+1)/2) << setfill(c);
425  else
426  return this->out << os.str();
427  }
428 
429  private:
430  std::ostream& out;
431  };
432 
433 public:
434  /**
435  * Constructor.
436  *
437  * \param width width
438  */
439  CENTER(const int width) :
440  WIDTH(width)
441  {}
442 
443 
444  /**
445  * Format specifier.
446  *
447  * \param out output stream
448  * \param format format
449  * \return output stream
450  */
451  friend inline JCenter operator<<(std::ostream& out, const CENTER& format)
452  {
453  return JCenter(out, format);
454  }
455 };
456 
457 
458 /**
459  * Auxiliary data structure for floating point format specification.
460  */
461 struct FIXED :
462  public WIDTH
463 {
464  /**
465  * Constructor.
466  *
467  * \param width width
468  * \param precision precision
469  */
470  FIXED(const int width,
471  const int precision) :
472  WIDTH(width)
473  {
474  this->precision = precision;
475  }
476 
477  /**
478  * Format specifier.
479  *
480  * \param out output stream
481  * \param format format
482  * \return output stream
483  */
484  friend inline std::ostream& operator<<(std::ostream& out, const FIXED& format)
485  {
486  using namespace std;
487 
488  return out << fixed << right << setw(format.width) << setprecision(format.precision);
489  }
490 
492 };
493 
494 
495 /**
496  * Auxiliary data structure for floating point format specification.
497  */
498 struct SCIENTIFIC :
499  public WIDTH
500 {
501  /**
502  * Constructor.
503  *
504  * \param width width
505  * \param precision precision
506  */
507  SCIENTIFIC(const int width,
508  const int precision) :
509  WIDTH(width)
510  {
511  this->precision = precision;
512  }
513 
514  /**
515  * Format specifier.
516  *
517  * \param out output stream
518  * \param format format
519  * \return output stream
520  */
521  friend inline std::ostream& operator<<(std::ostream& out, const SCIENTIFIC& format)
522  {
523  using namespace std;
524 
525  return out << scientific << right << setw(format.width) << setprecision(format.precision);
526  }
527 
529 };
530 
531 
532 /**
533  * Auxiliary class to temporarily modify format specifications.
534  */
535 struct JFlags {
536  /**
537  * Constructor.
538  *
539  * \param out output stream
540  */
541  JFlags(std::ostream& out) :
542  __out(out)
543  {
544  __flags = out.flags();
545  }
546 
547 
548  /**
549  * Constructor.
550  *
551  * \param out output stream
552  * \param flags format flags
553  */
554  JFlags(std::ostream& out, std::ios::fmtflags flags) :
555  __out(out)
556  {
557  __flags = out.flags();
558 
559  out.setf(flags);
560  }
561 
562 
563  /**
564  * Constructor.
565  *
566  * \param out output stream
567  * \param flags format flags
568  * \param mask format mask
569  */
570  JFlags(std::ostream& out, std::ios::fmtflags flags, std::ios::fmtflags mask) :
571  __out(out)
572  {
573  __flags = out.flags();
574 
575  out.setf(flags, mask);
576  }
577 
578 
579  /**
580  * Destructor.
581  *
582  * This destructor restores the old format specifications.
583  */
585  {
586  __out.flags(__flags);
587  }
588 
589 protected:
590  std::ostream& __out;
591  std::ios::fmtflags __flags;
592  std::ios::fmtflags __mask;
593 };
594 
595 
596 /**
597  * Make string.
598  *
599  * \param A std::ostream compatible construct
600  * \return std::string
601  */
602 #define MAKE_STRING(A) (static_cast<std::ostringstream&>(JEEP::getOstream() << A)).str()
603 
604 
605 /**
606  * Make C-string.
607  *
608  * \param A std::ostream compatible construct
609  * \return C-string
610  */
611 #define MAKE_CSTRING(A) MAKE_STRING(A).c_str()
612 
613 #endif
RIGHT(const int width)
Constructor.
Definition: JPrint.hh:319
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JPrint.hh:239
Auxiliary data structure for alignment of data.
Definition: JPrint.hh:248
JPrintOption_t
Print options.
Definition: JPrint.hh:37
friend std::ostream & operator<<(std::ostream &out, const WIDTH &format)
Format specifier.
Definition: JPrint.hh:266
void setMediumprint(std::ostream &out)
Set medium print option.
Definition: JPrint.hh:126
int width
Definition: JPrint.hh:273
friend std::ostream & operator<<(std::ostream &out, const SCIENTIFIC &format)
Format specifier.
Definition: JPrint.hh:521
friend std::ostream & operator<<(std::ostream &out, const RIGHT &format)
Format specifier.
Definition: JPrint.hh:330
void setLongprint(std::ostream &out)
Set long print option.
Definition: JPrint.hh:149
void setShortprint(std::ostream &out)
Set short print option.
Definition: JPrint.hh:103
int precision
Definition: JPrint.hh:528
WIDTH(const int width)
Constructor.
Definition: JPrint.hh:254
std::ios::fmtflags __mask
Definition: JPrint.hh:592
std::ostream & whitespace(std::ostream &out)
Print white space character.
Definition: JPrint.hh:215
long print
Definition: JPrint.hh:40
void setPrintOption(std::ostream &out, const int option)
Set print option.
Definition: JPrint.hh:80
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:461
friend std::ostream & operator<<(std::ostream &out, const FIXED &format)
Format specifier.
Definition: JPrint.hh:484
bool getLongprint(std::ostream &out)
Get long print option.
Definition: JPrint.hh:138
Auxiliary data structure for alignment of data.
Definition: JPrint.hh:381
std::ostream & newline(std::ostream &out)
Print newline character.
Definition: JPrint.hh:203
char fill
Definition: JPrint.hh:374
Auxiliary class for format center.
Definition: JPrint.hh:388
int precision
Definition: JPrint.hh:491
I/O coloring auxiliaries.
short print
Definition: JPrint.hh:38
LEFT(const int width)
Constructor.
Definition: JPrint.hh:288
std::ostream & mediumprint(std::ostream &out)
Set medium printing.
Definition: JPrint.hh:175
int getIndex()
Get index for user I/O manipulation.
Definition: JPrint.hh:26
std::ostream & getOstream()
Get output stream for conversion to std::string or C-string.
Definition: JPrint.hh:51
std::ostream & __out
Definition: JPrint.hh:590
bool getMediumprint(std::ostream &out)
Get medium print option.
Definition: JPrint.hh:115
JFlags(std::ostream &out, std::ios::fmtflags flags, std::ios::fmtflags mask)
Constructor.
Definition: JPrint.hh:570
Auxiliary data structure for sequence of same character.
Definition: JPrint.hh:342
friend std::ostream & operator<<(std::ostream &out, const LEFT &format)
Format specifier.
Definition: JPrint.hh:299
std::ios::fmtflags __flags
Definition: JPrint.hh:591
JFlags(std::ostream &out, std::ios::fmtflags flags)
Constructor.
Definition: JPrint.hh:554
std::ostream & longprint(std::ostream &out)
Set long printing.
Definition: JPrint.hh:189
~JFlags()
Destructor.
Definition: JPrint.hh:584
SCIENTIFIC(const int width, const int precision)
Constructor.
Definition: JPrint.hh:507
JCenter(std::ostream &out, const WIDTH &format)
Constructor.
Definition: JPrint.hh:397
std::ostream & tab(std::ostream &out)
Print tab character.
Definition: JPrint.hh:227
FILL(const int width, const char fill= ' ')
Constructor.
Definition: JPrint.hh:351
int getPrintOption(std::ostream &out)
Get print option.
Definition: JPrint.hh:68
friend std::ostream & operator<<(std::ostream &out, const FILL &format)
Format specifier.
Definition: JPrint.hh:365
medium print
Definition: JPrint.hh:39
std::ostream & out
Definition: JPrint.hh:430
bool getShortprint(std::ostream &out)
Get short print option.
Definition: JPrint.hh:92
JFlags(std::ostream &out)
Constructor.
Definition: JPrint.hh:541
CENTER(const int width)
Constructor.
Definition: JPrint.hh:439
std::ostream & shortprint(std::ostream &out)
Set short printing.
Definition: JPrint.hh:161
FIXED(const int width, const int precision)
Constructor.
Definition: JPrint.hh:470
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:498
std::ostream & operator<<(const T &value)
Write value to output stream.
Definition: JPrint.hh:410
Auxiliary class to temporarily modify format specifications.
Definition: JPrint.hh:535
friend JCenter operator<<(std::ostream &out, const CENTER &format)
Format specifier.
Definition: JPrint.hh:451