LOOS  v2.3.2
Fmt.cpp
1 /*
2  Fmt.cpp
3 
4  Formatter based heavily on example from Stroustrup's C++ book...
5 */
6 
7 
8 
9 #include <Fmt.hpp>
10 
11 
12 namespace loos {
13  Fmt& Fmt::scientific() { fmt = std::ios_base::scientific; return(*this); }
14  Fmt& Fmt::fixed() { fmt = std::ios_base::fixed; return(*this); }
15  Fmt& Fmt::general() { fmt = ~(std::ios_base::fixed|std::ios_base::scientific); return(*this); }
16 
17  Fmt& Fmt::precision(const uint p) { prc = p; return(*this); }
18  Fmt& Fmt::width(const uint w) { wdth = w; return(*this); }
19  Fmt& Fmt::fill(const char c) { fil = c; return(*this); }
20 
21  Fmt& Fmt::trailingZeros(bool b) { trl = b; return(*this); }
22  Fmt& Fmt::plus(bool b) { pos = b; return(*this); }
23 
24  Fmt& Fmt::left(void) { ali = LEFT; return(*this); }
25  Fmt& Fmt::right(void) { ali = RIGHT; return(*this); }
26  Fmt& Fmt::internal(void) { ali = INTERNAL; return(*this); }
27 
28 
30  BoundFmt Fmt::operator()(double d) const { return(BoundFmt(*this, d)); }
31 
33  std::ostream& operator<<(std::ostream& os, const BoundFmt& bf) {
34  std::ostringstream s;
35 
36  s.precision(bf.f.prc);
37  s.setf(bf.f.fmt, std::ios_base::floatfield);
38  s.width(bf.f.wdth);
39  s.fill(bf.f.fil);
40 
41  switch(bf.f.ali) {
42  case Fmt::LEFT:
43  s.setf(std::ios_base::left, std::ios_base::adjustfield); break;
44  case Fmt::RIGHT:
45  s.setf(std::ios_base::right, std::ios_base::adjustfield); break;
46  default:
47  s.setf(std::ios_base::internal, std::ios_base::adjustfield); break;
48  }
49 
50  if (bf.f.trl)
51  s.setf(std::ios_base::showpoint);
52  else
53  s.unsetf(std::ios_base::showpoint);
54 
55  if (bf.f.pos)
56  s.setf(std::ios_base::showpos);
57  else
58  s.unsetf(std::ios_base::showpos);
59 
60  s << bf.val;
61 
62  std::string ss(s.str());
63  if (ss.size() > bf.f.wdth)
64  ss = ss.substr(ss.size() - bf.f.wdth, bf.f.wdth);
65 
66  return(os << ss);
67  }
68 
69 
70 }
Fmt & fixed(void)
Output in fixed-point.
Definition: Fmt.cpp:14
Fmt & plus(bool b=true)
Prepend plus sign?
Definition: Fmt.cpp:22
Fmt & fill(const char)
Set the fill character.
Definition: Fmt.cpp:19
Internal helper class to bind formatting state.
Definition: Fmt.hpp:80
Fmt & trailingZeros(bool b=true)
Determines whether or not trailing zeros are shown.
Definition: Fmt.cpp:21
Fmt & precision(const uint)
Set the precision.
Definition: Fmt.cpp:17
BoundFmt operator()(double d) const
Returns the bound formatter.
Definition: Fmt.cpp:30
Fmt & width(const uint)
Set the output field width.
Definition: Fmt.cpp:18
Fmt & scientific(void)
Output in scientific format.
Definition: Fmt.cpp:13
Output formatter class, adapted from Stroustrup's book.
Definition: Fmt.hpp:25
Namespace for most things not already encapsulated within a class.
Fmt & right(void)
Align right.
Definition: Fmt.cpp:25
Fmt & left(void)
Align left.
Definition: Fmt.cpp:24
Fmt & general(void)
Output normally (i.e. general)
Definition: Fmt.cpp:15
Fmt & internal(void)
Align "internal" (see C++ ref)
Definition: Fmt.cpp:26