1 // Copyright 2012 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/units.hpp"
37 #include "utils/format/macros.hpp"
38 #include "utils/text/exceptions.hpp"
39 #include "utils/text/operations.ipp"
41 namespace units
= utils::units
;
44 /// Constructs a zero bytes quantity.
45 units::bytes::bytes(void) :
51 /// Constructs an arbitrary bytes quantity.
53 /// \param count_ The amount of bytes in the quantity.
54 units::bytes::bytes(const uint64_t count_
) :
60 /// Parses a string into a bytes quantity.
62 /// \param in_str The user-provided string to be converted.
64 /// \return The converted bytes quantity.
66 /// \throw std::runtime_error If the input string is empty or invalid.
68 units::bytes::parse(const std::string
& in_str
)
71 throw std::runtime_error("Bytes quantity cannot be empty");
74 std::string str
= in_str
;
76 const char unit
= str
[str
.length() - 1];
78 case 'T': case 't': multiplier
= TB
; break;
79 case 'G': case 'g': multiplier
= GB
; break;
80 case 'M': case 'm': multiplier
= MB
; break;
81 case 'K': case 'k': multiplier
= KB
; break;
82 default: multiplier
= 1;
85 str
.erase(str
.length() - 1);
89 throw std::runtime_error("Bytes quantity cannot be empty");
90 if (str
[0] == '.' || str
[str
.length() - 1] == '.') {
91 // The standard parser for float values accepts things like ".3" and
92 // "3.", which means that we would interpret ".3K" and "3.K" as valid
93 // quantities. I think this is ugly and should not be allowed, so
94 // special-case this condition and just error out.
95 throw std::runtime_error(F("Invalid bytes quantity '%s'") % in_str
);
100 count
= text::to_type
< double >(str
);
101 } catch (const text::value_error
& e
) {
102 throw std::runtime_error(F("Invalid bytes quantity '%s'") % in_str
);
105 return bytes(uint64_t(count
* multiplier
));
109 /// Formats a bytes quantity for user consumption.
111 /// \return A textual representation of the bytes quantiy.
113 units::bytes::format(void) const
116 return F("%.2sT") % (static_cast< float >(_count
) / TB
);
117 } else if (_count
>= GB
) {
118 return F("%.2sG") % (static_cast< float >(_count
) / GB
);
119 } else if (_count
>= MB
) {
120 return F("%.2sM") % (static_cast< float >(_count
) / MB
);
121 } else if (_count
>= KB
) {
122 return F("%.2sK") % (static_cast< float >(_count
) / KB
);
124 return F("%s") % _count
;
129 /// Implicit conversion to an integral representation.
130 units::bytes::operator uint64_t(void) const
136 /// Extracts a bytes quantity from a stream.
138 /// \param input The stream from which to read a single word representing the
140 /// \param rhs The variable into which to store the parsed value.
142 /// \return The input stream.
144 /// \post The bad bit of input is set to 1 if the parsing failed.
146 units::operator>>(std::istream
& input
, bytes
& rhs
)
150 if (input
.good() || input
.eof()) {
152 rhs
= bytes::parse(word
);
153 } catch (const std::runtime_error
& e
) {
154 input
.setstate(std::ios::badbit
);
161 /// Injects a bytes quantity into a stream.
163 /// \param output The stream into which to inject the bytes quantity as a
164 /// user-readable string.
165 /// \param rhs The bytes quantity to format.
167 /// \return The output stream.
169 units::operator<<(std::ostream
& output
, const bytes
& rhs
)
171 return (output
<< rhs
.format());