STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/common/utils.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2013 Timo Bingmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
15 #include <stxxl/bits/namespace.h>
16 
17 #include <sstream>
18 #include <iomanip>
19 
21 
22 //! Parse a string like "343KB" or " 44 GiB " into the corresponding size in
23 //! bytes.
24 bool parse_SI_IEC_size(const std::string& str, uint64& size, char def_unit)
25 {
26  char* endptr;
27  size = strtoul(str.c_str(), &endptr, 10);
28  if (!endptr) return false; // parse failed, no number
29 
30  while (endptr[0] == ' ') ++endptr; // skip over spaces
31 
32  // multiply with base ^ power
33  unsigned int base = 1000;
34  unsigned int power = 0;
35 
36  if (endptr[0] == 'k' || endptr[0] == 'K')
37  power = 1, ++endptr;
38  else if (endptr[0] == 'm' || endptr[0] == 'M')
39  power = 2, ++endptr;
40  else if (endptr[0] == 'g' || endptr[0] == 'G')
41  power = 3, ++endptr;
42  else if (endptr[0] == 't' || endptr[0] == 'T')
43  power = 4, ++endptr;
44  else if (endptr[0] == 'p' || endptr[0] == 'P')
45  power = 5, ++endptr;
46 
47  // switch to power of two (only if power was set above)
48  if ((endptr[0] == 'i' || endptr[0] == 'I') && power != 0)
49  base = 1024, ++endptr;
50 
51  // byte indicator
52  if (endptr[0] == 'b' || endptr[0] == 'B') {
53  ++endptr;
54  }
55  else if (power == 0)
56  {
57  // no explicit power indicator, and no 'b' or 'B' -> apply default unit
58  switch (def_unit)
59  {
60  default: break;
61  case 'k': power = 1, base = 1000;
62  break;
63  case 'm': power = 2, base = 1000;
64  break;
65  case 'g': power = 3, base = 1000;
66  break;
67  case 't': power = 4, base = 1000;
68  break;
69  case 'p': power = 5, base = 1000;
70  break;
71  case 'K': power = 1, base = 1024;
72  break;
73  case 'M': power = 2, base = 1024;
74  break;
75  case 'G': power = 3, base = 1024;
76  break;
77  case 'T': power = 4, base = 1024;
78  break;
79  case 'P': power = 5, base = 1024;
80  break;
81  }
82  }
83 
84  // skip over spaces
85  while (endptr[0] == ' ') ++endptr;
86 
87  // multiply size
88  for (unsigned int p = 0; p < power; ++p)
89  size *= base;
90 
91  return (endptr[0] == 0);
92 }
93 
94 std::string format_SI_size(uint64 number)
95 {
96  // may not overflow, std::numeric_limits<uint64>::max() == 16 EiB
97  double multiplier = 1000.0;
98  static const char* SIendings[] = { "", "k", "M", "G", "T", "P", "E" };
99  unsigned int scale = 0;
100  double number_d = (double)number;
101  while (number_d >= multiplier) {
102  number_d /= multiplier;
103  ++scale;
104  }
105  std::ostringstream out;
106  out << std::fixed << std::setprecision(3) << number_d
107  << ' ' << SIendings[scale];
108  return out.str();
109 }
110 
111 std::string format_IEC_size(uint64 number)
112 {
113  // may not overflow, std::numeric_limits<uint64>::max() == 16 EiB
114  double multiplier = 1024.0;
115  static const char* IECendings[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
116  unsigned int scale = 0;
117  double number_d = (double)number;
118  while (number_d >= multiplier) {
119  number_d /= multiplier;
120  ++scale;
121  }
122  std::ostringstream out;
123  out << std::fixed << std::setprecision(3) << number_d
124  << ' ' << IECendings[scale];
125  return out.str();
126 }
127 
unsigned long long int uint64
Definition: types.h:39
std::string format_SI_size(uint64 number)
Format a byte size using SI (K, M, G, T) suffixes (powers of ten). Returns &quot;123 M&quot; or similar...
Definition: utils.cpp:94
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
bool parse_SI_IEC_size(const std::string &str, uint64 &size, char def_unit=0)
Parse a string like &quot;343KB&quot; or &quot;44 GiB&quot; into the corresponding size in bytes. Returns the number of b...
Definition: utils.cpp:24
std::string format_IEC_size(uint64 number)
Format a byte size using IEC (Ki, Mi, Gi, Ti) suffixes (powers of two). Returns &quot;123 Ki&quot; or similar...
Definition: utils.cpp:111
#define STXXL_END_NAMESPACE
Definition: namespace.h:17