OpenVDB: Formats.h Source File

OpenVDB 7.0.0

Formats.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
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 {
22 OPENVDB_USE_VERSION_NAMESPACE
23 namespace OPENVDB_VERSION_NAME {
24 namespace util {
25
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
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
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
73
74
76 template<typename IntT>
77 class FormattedInt
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
117 template<typename IntT>
118 FormattedInt<IntT> formattedInt(IntT n) { return FormattedInt<IntT>(n); }
119
120 } // namespace util
121 } // namespace OPENVDB_VERSION_NAME
122 } // namespace openvdb
123
124 #endif // OPENVDB_UTIL_FORMATS_HAS_BEEN_INCLUDED
openvdb::v7_0::util::printBytes
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)
openvdb::v7_0::util::formattedInt
FormattedInt< IntT > formattedInt(IntT n)
Definition: Formats.h:118
version.h
Library and file format version numbers.
Platform.h
openvdb::v7_0::util::printTime
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)
openvdb::v7_0::util::FormattedInt
I/O manipulator that formats integer values with thousands separators.
Definition: Formats.h:77
OPENVDB_API
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:230
openvdb::v7_0::util::printNumber
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)
openvdb::v7_0::util::FormattedInt::sep
static char sep()
Definition: Formats.h:80
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
openvdb::v7_0::util::FormattedInt::FormattedInt
FormattedInt(IntT n)
Definition: Formats.h:82
openvdb::v7_0::util::operator<<
std::ostream & operator<<(std::ostream &os, const FormattedInt< IntT > &n)
Definition: Formats.h:114
openvdb::v7_0::util::FormattedInt::put
std::ostream & put(std::ostream &os) const
Definition: Formats.h:84
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:108
openvdb
Definition: Exceptions.h:13