HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Formats.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 //
31 /// @author Ken Museth
32 ///
33 /// @file Formats.h
34 ///
35 /// @brief Utility routines to output nicely-formatted numeric values
36 
37 
38 #ifndef OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
39 #define OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
40 
41 #include <iosfwd>
42 #include <sstream>
43 #include <string>
44 #include <openvdb/version.h>
45 #include <openvdb/Platform.h>
46 
47 
48 namespace openvdb {
50 namespace OPENVDB_VERSION_NAME {
51 namespace util {
52 
53 /// Output a byte count with the correct binary suffix (KB, MB, GB or TB).
54 /// @param os the output stream
55 /// @param bytes the byte count to be output
56 /// @param head a string to be output before the numeric text
57 /// @param tail a string to be output after the numeric text
58 /// @param exact if true, also output the unmodified count, e.g., "4.6 KB (4620 Bytes)"
59 /// @param width a fixed width for the numeric text
60 /// @param precision the number of digits after the decimal point
61 /// @return 0, 1, 2, 3 or 4, denoting the order of magnitude of the count.
62 OPENVDB_API int
63 printBytes(std::ostream& os, uint64_t bytes,
64  const std::string& head = "",
65  const std::string& tail = "\n",
66  bool exact = false, int width = 8, int precision = 3);
67 
68 /// Output a number with the correct SI suffix (thousand, million, billion or trillion)
69 /// @param os the output stream
70 /// @param number the number to be output
71 /// @param head a string to be output before the numeric text
72 /// @param tail a string to be output after the numeric text
73 /// @param exact if true, also output the unmodified count, e.g., "4.6 Thousand (4620)"
74 /// @param width a fixed width for the numeric text
75 /// @param precision the number of digits after the decimal point
76 /// @return 0, 1, 2, 3 or 4, denoting the order of magnitude of the number.
77 OPENVDB_API int
78 printNumber(std::ostream& os, uint64_t number,
79  const std::string& head = "",
80  const std::string& tail = "\n",
81  bool exact = true, int width = 8, int precision = 3);
82 
83 
84 ////////////////////////////////////////
85 
86 
87 /// @brief I/O manipulator that formats integer values with thousands separators
88 template<typename IntT>
90 {
91 public:
92  static char sep() { return ','; }
93 
94  FormattedInt(IntT n): mInt(n) {}
95 
96  std::ostream& put(std::ostream& os) const
97  {
98  // Convert the integer to a string.
99  std::ostringstream ostr;
100  ostr << mInt;
101  std::string s = ostr.str();
102  // Prefix the string with spaces if its length is not a multiple of three.
103  size_t padding = (s.size() % 3) ? 3 - (s.size() % 3) : 0;
104  s = std::string(padding, ' ') + s;
105  // Construct a new string in which groups of three digits are followed
106  // by a separator character.
107  ostr.str("");
108  for (size_t i = 0, N = s.size(); i < N; ) {
109  ostr << s[i];
110  ++i;
111  if (i >= padding && i % 3 == 0 && i < s.size()) {
112  ostr << sep();
113  }
114  }
115  // Remove any padding that was added and output the string.
116  s = ostr.str();
117  os << s.substr(padding, s.size());
118  return os;
119  }
120 
121 private:
122  IntT mInt;
123 };
124 
125 template<typename IntT>
126 std::ostream& operator<<(std::ostream& os, const FormattedInt<IntT>& n) { return n.put(os); }
127 
128 /// @return an I/O manipulator that formats the given integer value for output to a stream.
129 template<typename IntT>
131 
132 } // namespace util
133 } // namespace OPENVDB_VERSION_NAME
134 } // namespace openvdb
135 
136 #endif // OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
137 
138 // Copyright (c) 2012-2017 DreamWorks Animation LLC
139 // All rights reserved. This software is distributed under the
140 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
I/O manipulator that formats integer values with thousands separators.
Definition: Formats.h:89
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
png_uint_32 i
Definition: png.h:2877
GLint GLsizei width
Definition: glcorearb.h:102
GLdouble n
Definition: glcorearb.h:2007
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
OPENVDB_API int printNumber(std::ostream &os, uint64_t number, const std::string &head="", const std::string &tail="\n", bool exact=true, int width=8, int precision=3)
std::ostream & put(std::ostream &os) const
Definition: Formats.h:96
#define OPENVDB_VERSION_NAME
Definition: version.h:43
GLenum GLint GLint * precision
Definition: glcorearb.h:1924
GA_API const UT_StringHolder N
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:130
OPENVDB_API int printBytes(std::ostream &os, uint64_t bytes, const std::string &head="", const std::string &tail="\n", bool exact=false, int width=8, int precision=3)
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71