HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Formats.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 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 /// Output a time in milliseconds with the correct suffix (days, hours, minutes, seconds and milliseconds)
84 /// @param os the output stream
85 /// @param milliseconds the time to be output
86 /// @param head a string to be output before the time
87 /// @param tail a string to be output after the time
88 /// @param width a fixed width for the numeric text
89 /// @param precision the number of digits after the decimal point
90 /// @param verbose verbose level, 0 is compact format and 1 is long format
91 /// @return 0, 1, 2, 3, or 4 denoting the order of magnitude of the time.
92 OPENVDB_API int
93 printTime(std::ostream& os, double milliseconds,
94  const std::string& head = "",
95  const std::string& tail = "\n",
96  int width = 4, int precision = 1, int verbose = 0);
97 
98 
99 ////////////////////////////////////////
100 
101 
102 /// @brief I/O manipulator that formats integer values with thousands separators
103 template<typename IntT>
105 {
106 public:
107  static char sep() { return ','; }
108 
109  FormattedInt(IntT n): mInt(n) {}
110 
111  std::ostream& put(std::ostream& os) const
112  {
113  // Convert the integer to a string.
114  std::ostringstream ostr;
115  ostr << mInt;
116  std::string s = ostr.str();
117  // Prefix the string with spaces if its length is not a multiple of three.
118  size_t padding = (s.size() % 3) ? 3 - (s.size() % 3) : 0;
119  s = std::string(padding, ' ') + s;
120  // Construct a new string in which groups of three digits are followed
121  // by a separator character.
122  ostr.str("");
123  for (size_t i = 0, N = s.size(); i < N; ) {
124  ostr << s[i];
125  ++i;
126  if (i >= padding && i % 3 == 0 && i < s.size()) {
127  ostr << sep();
128  }
129  }
130  // Remove any padding that was added and output the string.
131  s = ostr.str();
132  os << s.substr(padding, s.size());
133  return os;
134  }
135 
136 private:
137  IntT mInt;
138 };
139 
140 template<typename IntT>
141 std::ostream& operator<<(std::ostream& os, const FormattedInt<IntT>& n) { return n.put(os); }
142 
143 /// @return an I/O manipulator that formats the given integer value for output to a stream.
144 template<typename IntT>
146 
147 } // namespace util
148 } // namespace OPENVDB_VERSION_NAME
149 } // namespace openvdb
150 
151 #endif // OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
152 
153 // Copyright (c) DreamWorks Animation LLC
154 // All rights reserved. This software is distributed under the
155 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
GLdouble s
Definition: glew.h:1390
OPENVDB_API int printTime(std::ostream &os, double milliseconds, const std::string &head="", const std::string &tail="\n", int width=4, int precision=1, int verbose=0)
I/O manipulator that formats integer values with thousands separators.
Definition: Formats.h:104
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1252
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:200
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:288
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:111
GLsizei n
Definition: glew.h:4040
GLsizei const GLchar *const * string
Definition: glew.h:1844
GA_API const UT_StringHolder N
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:145
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)
GLenum GLint GLint * precision
Definition: glew.h:3500
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:146