xref: /minix3/external/bsd/kyua-cli/dist/utils/units.cpp (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
111be35a1SLionel Sambuc // Copyright 2012 Google Inc.
211be35a1SLionel Sambuc // All rights reserved.
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
511be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
611be35a1SLionel Sambuc // met:
711be35a1SLionel Sambuc //
811be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
911be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
1011be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
1111be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
1211be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
1311be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
1411be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
1511be35a1SLionel Sambuc //   without specific prior written permission.
1611be35a1SLionel Sambuc //
1711be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include "utils/units.hpp"
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc extern "C" {
3211be35a1SLionel Sambuc #include <stdint.h>
3311be35a1SLionel Sambuc }
3411be35a1SLionel Sambuc 
3511be35a1SLionel Sambuc #include <stdexcept>
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc #include "utils/format/macros.hpp"
3811be35a1SLionel Sambuc #include "utils/text/exceptions.hpp"
3911be35a1SLionel Sambuc #include "utils/text/operations.ipp"
4011be35a1SLionel Sambuc 
4111be35a1SLionel Sambuc namespace units = utils::units;
4211be35a1SLionel Sambuc 
4311be35a1SLionel Sambuc 
4411be35a1SLionel Sambuc /// Constructs a zero bytes quantity.
bytes(void)4511be35a1SLionel Sambuc units::bytes::bytes(void) :
4611be35a1SLionel Sambuc     _count(0)
4711be35a1SLionel Sambuc {
4811be35a1SLionel Sambuc }
4911be35a1SLionel Sambuc 
5011be35a1SLionel Sambuc 
5111be35a1SLionel Sambuc /// Constructs an arbitrary bytes quantity.
5211be35a1SLionel Sambuc ///
5311be35a1SLionel Sambuc /// \param count_ The amount of bytes in the quantity.
bytes(const uint64_t count_)5411be35a1SLionel Sambuc units::bytes::bytes(const uint64_t count_) :
5511be35a1SLionel Sambuc     _count(count_)
5611be35a1SLionel Sambuc {
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc 
5911be35a1SLionel Sambuc 
6011be35a1SLionel Sambuc /// Parses a string into a bytes quantity.
6111be35a1SLionel Sambuc ///
6211be35a1SLionel Sambuc /// \param in_str The user-provided string to be converted.
6311be35a1SLionel Sambuc ///
6411be35a1SLionel Sambuc /// \return The converted bytes quantity.
6511be35a1SLionel Sambuc ///
6611be35a1SLionel Sambuc /// \throw std::runtime_error If the input string is empty or invalid.
6711be35a1SLionel Sambuc units::bytes
parse(const std::string & in_str)6811be35a1SLionel Sambuc units::bytes::parse(const std::string& in_str)
6911be35a1SLionel Sambuc {
7011be35a1SLionel Sambuc     if (in_str.empty())
7111be35a1SLionel Sambuc         throw std::runtime_error("Bytes quantity cannot be empty");
7211be35a1SLionel Sambuc 
7311be35a1SLionel Sambuc     uint64_t multiplier;
7411be35a1SLionel Sambuc     std::string str = in_str;
7511be35a1SLionel Sambuc     {
7611be35a1SLionel Sambuc         const char unit = str[str.length() - 1];
7711be35a1SLionel Sambuc         switch (unit) {
7811be35a1SLionel Sambuc         case 'T': case 't': multiplier = TB; break;
7911be35a1SLionel Sambuc         case 'G': case 'g': multiplier = GB; break;
8011be35a1SLionel Sambuc         case 'M': case 'm': multiplier = MB; break;
8111be35a1SLionel Sambuc         case 'K': case 'k': multiplier = KB; break;
8211be35a1SLionel Sambuc         default: multiplier = 1;
8311be35a1SLionel Sambuc         }
8411be35a1SLionel Sambuc         if (multiplier != 1)
8511be35a1SLionel Sambuc             str.erase(str.length() - 1);
8611be35a1SLionel Sambuc     }
8711be35a1SLionel Sambuc 
8811be35a1SLionel Sambuc     if (str.empty())
8911be35a1SLionel Sambuc         throw std::runtime_error("Bytes quantity cannot be empty");
9011be35a1SLionel Sambuc     if (str[0] == '.' || str[str.length() - 1] == '.') {
9111be35a1SLionel Sambuc         // The standard parser for float values accepts things like ".3" and
9211be35a1SLionel Sambuc         // "3.", which means that we would interpret ".3K" and "3.K" as valid
9311be35a1SLionel Sambuc         // quantities.  I think this is ugly and should not be allowed, so
9411be35a1SLionel Sambuc         // special-case this condition and just error out.
9511be35a1SLionel Sambuc         throw std::runtime_error(F("Invalid bytes quantity '%s'") % in_str);
9611be35a1SLionel Sambuc     }
9711be35a1SLionel Sambuc 
9811be35a1SLionel Sambuc     double count;
9911be35a1SLionel Sambuc     try {
10011be35a1SLionel Sambuc         count = text::to_type< double >(str);
10111be35a1SLionel Sambuc     } catch (const text::value_error& e) {
10211be35a1SLionel Sambuc         throw std::runtime_error(F("Invalid bytes quantity '%s'") % in_str);
10311be35a1SLionel Sambuc     }
10411be35a1SLionel Sambuc 
10511be35a1SLionel Sambuc     return bytes(uint64_t(count * multiplier));
10611be35a1SLionel Sambuc }
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc 
10911be35a1SLionel Sambuc /// Formats a bytes quantity for user consumption.
11011be35a1SLionel Sambuc ///
11111be35a1SLionel Sambuc /// \return A textual representation of the bytes quantiy.
11211be35a1SLionel Sambuc std::string
format(void) const11311be35a1SLionel Sambuc units::bytes::format(void) const
11411be35a1SLionel Sambuc {
11511be35a1SLionel Sambuc     if (_count >= TB) {
11611be35a1SLionel Sambuc         return F("%.2sT") % (static_cast< float >(_count) / TB);
11711be35a1SLionel Sambuc     } else if (_count >= GB) {
11811be35a1SLionel Sambuc         return F("%.2sG") % (static_cast< float >(_count) / GB);
11911be35a1SLionel Sambuc     } else if (_count >= MB) {
12011be35a1SLionel Sambuc         return F("%.2sM") % (static_cast< float >(_count) / MB);
12111be35a1SLionel Sambuc     } else if (_count >= KB) {
12211be35a1SLionel Sambuc         return F("%.2sK") % (static_cast< float >(_count) / KB);
12311be35a1SLionel Sambuc     } else {
12411be35a1SLionel Sambuc         return F("%s") % _count;
12511be35a1SLionel Sambuc     }
12611be35a1SLionel Sambuc }
12711be35a1SLionel Sambuc 
12811be35a1SLionel Sambuc 
12911be35a1SLionel Sambuc /// Implicit conversion to an integral representation.
operator uint64_t(void) const13011be35a1SLionel Sambuc units::bytes::operator uint64_t(void) const
13111be35a1SLionel Sambuc {
13211be35a1SLionel Sambuc     return _count;
13311be35a1SLionel Sambuc }
13411be35a1SLionel Sambuc 
13511be35a1SLionel Sambuc 
13611be35a1SLionel Sambuc /// Extracts a bytes quantity from a stream.
13711be35a1SLionel Sambuc ///
13811be35a1SLionel Sambuc /// \param input The stream from which to read a single word representing the
13911be35a1SLionel Sambuc ///     bytes quantity.
14011be35a1SLionel Sambuc /// \param rhs The variable into which to store the parsed value.
14111be35a1SLionel Sambuc ///
14211be35a1SLionel Sambuc /// \return The input stream.
14311be35a1SLionel Sambuc ///
14411be35a1SLionel Sambuc /// \post The bad bit of input is set to 1 if the parsing failed.
14511be35a1SLionel Sambuc std::istream&
operator >>(std::istream & input,bytes & rhs)146*84d9c625SLionel Sambuc units::operator>>(std::istream& input, bytes& rhs)
14711be35a1SLionel Sambuc {
14811be35a1SLionel Sambuc     std::string word;
14911be35a1SLionel Sambuc     input >> word;
15011be35a1SLionel Sambuc     if (input.good() || input.eof()) {
15111be35a1SLionel Sambuc         try {
152*84d9c625SLionel Sambuc             rhs = bytes::parse(word);
15311be35a1SLionel Sambuc         } catch (const std::runtime_error& e) {
15411be35a1SLionel Sambuc             input.setstate(std::ios::badbit);
15511be35a1SLionel Sambuc         }
15611be35a1SLionel Sambuc     }
15711be35a1SLionel Sambuc     return input;
15811be35a1SLionel Sambuc }
15911be35a1SLionel Sambuc 
16011be35a1SLionel Sambuc 
16111be35a1SLionel Sambuc /// Injects a bytes quantity into a stream.
16211be35a1SLionel Sambuc ///
16311be35a1SLionel Sambuc /// \param output The stream into which to inject the bytes quantity as a
16411be35a1SLionel Sambuc ///     user-readable string.
16511be35a1SLionel Sambuc /// \param rhs The bytes quantity to format.
16611be35a1SLionel Sambuc ///
16711be35a1SLionel Sambuc /// \return The output stream.
16811be35a1SLionel Sambuc std::ostream&
operator <<(std::ostream & output,const bytes & rhs)169*84d9c625SLionel Sambuc units::operator<<(std::ostream& output, const bytes& rhs)
17011be35a1SLionel Sambuc {
17111be35a1SLionel Sambuc     return (output << rhs.format());
17211be35a1SLionel Sambuc }
173