HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Formats.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
4 /// @author Ken Museth
5 ///
6 /// @file Formats.h
7 ///
8 /// @brief Utility routines to output nicely-formatted numeric values
9 
10 
11 #ifndef OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
12 #define OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
13 
14 #include <iosfwd>
15 #include <sstream>
16 #include <string>
17 #include <openvdb/version.h>
18 #include <openvdb/Platform.h>
19 
20 
21 namespace openvdb {
23 namespace OPENVDB_VERSION_NAME {
24 namespace util {
25 
26 /// Output a byte count with the correct binary suffix (KB, MB, GB or TB).
27 /// @param os the output stream
28 /// @param bytes the byte count to be output
29 /// @param head a string to be output before the numeric text
30 /// @param tail a string to be output after the numeric text
31 /// @param exact if true, also output the unmodified count, e.g., "4.6 KB (4620 Bytes)"
32 /// @param width a fixed width for the numeric text
33 /// @param precision the number of digits after the decimal point
34 /// @return 0, 1, 2, 3 or 4, denoting the order of magnitude of the count.
35 OPENVDB_API int
36 printBytes(std::ostream& os, uint64_t bytes,
37  const std::string& head = "",
38  const std::string& tail = "\n",
39  bool exact = false, int width = 8, int precision = 3);
40 
41 /// Output a number with the correct SI suffix (thousand, million, billion or trillion)
42 /// @param os the output stream
43 /// @param number the number to be output
44 /// @param head a string to be output before the numeric text
45 /// @param tail a string to be output after the numeric text
46 /// @param exact if true, also output the unmodified count, e.g., "4.6 Thousand (4620)"
47 /// @param width a fixed width for the numeric text
48 /// @param precision the number of digits after the decimal point
49 /// @return 0, 1, 2, 3 or 4, denoting the order of magnitude of the number.
50 OPENVDB_API int
51 printNumber(std::ostream& os, uint64_t number,
52  const std::string& head = "",
53  const std::string& tail = "\n",
54  bool exact = true, int width = 8, int precision = 3);
55 
56 /// Output a time in milliseconds with the correct suffix (days, hours, minutes, seconds and milliseconds)
57 /// @param os the output stream
58 /// @param milliseconds the time to be output
59 /// @param head a string to be output before the time
60 /// @param tail a string to be output after the time
61 /// @param width a fixed width for the numeric text
62 /// @param precision the number of digits after the decimal point
63 /// @param verbose verbose level, 0 is compact format and 1 is long format
64 /// @return 0, 1, 2, 3, or 4 denoting the order of magnitude of the time.
65 OPENVDB_API int
66 printTime(std::ostream& os, double milliseconds,
67  const std::string& head = "",
68  const std::string& tail = "\n",
69  int width = 4, int precision = 1, int verbose = 0);
70 
71 
72 ////////////////////////////////////////
73 
74 
75 /// @brief I/O manipulator that formats integer values with thousands separators
76 template<typename IntT>
78 {
79 public:
80  static char sep() { return ','; }
81 
82  FormattedInt(IntT n): mInt(n) {}
83 
84  std::ostream& put(std::ostream& os) const
85  {
86  // Convert the integer to a string.
87  std::ostringstream ostr;
88  ostr << mInt;
89  std::string s = ostr.str();
90  // Prefix the string with spaces if its length is not a multiple of three.
91  size_t padding = (s.size() % 3) ? 3 - (s.size() % 3) : 0;
92  s = std::string(padding, ' ') + s;
93  // Construct a new string in which groups of three digits are followed
94  // by a separator character.
95  ostr.str("");
96  for (size_t i = 0, N = s.size(); i < N; ) {
97  ostr << s[i];
98  ++i;
99  if (i >= padding && i % 3 == 0 && i < s.size()) {
100  ostr << sep();
101  }
102  }
103  // Remove any padding that was added and output the string.
104  s = ostr.str();
105  os << s.substr(padding, s.size());
106  return os;
107  }
108 
109 private:
110  IntT mInt;
111 };
112 
113 template<typename IntT>
114 std::ostream& operator<<(std::ostream& os, const FormattedInt<IntT>& n) { return n.put(os); }
115 
116 /// @return an I/O manipulator that formats the given integer value for output to a stream.
117 template<typename IntT>
119 
120 } // namespace util
121 } // namespace OPENVDB_VERSION_NAME
122 } // namespace openvdb
123 
124 #endif // OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
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:77
UT_StringArray JOINTS head
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLdouble s
Definition: glad.h:3009
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:239
GLdouble n
Definition: glcorearb.h:2008
#define OPENVDB_API
Definition: Platform.h:274
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:84
GLenum GLint GLint * precision
Definition: glcorearb.h:1925
Library and file format version numbers.
GA_API const UT_StringHolder N
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:118
GLint GLsizei width
Definition: glcorearb.h:103
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_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:119
Definition: format.h:2459