1/*
2 * Legal Notice
3 *
4 * This document and associated source code (the "Work") is a part of a
5 * benchmark specification maintained by the TPC.
6 *
7 * The TPC reserves all right, title, and interest to the Work as provided
8 * under U.S. and international laws, including without limitation all patent
9 * and trademark rights therein.
10 *
11 * No Warranty
12 *
13 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
14 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
15 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
16 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
17 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
18 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
19 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
20 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
21 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
22 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
23 * WITH REGARD TO THE WORK.
24 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
25 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
26 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
27 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
28 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
29 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
30 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
31 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * Contributors
34 * - Christopher Chan-Nui
35 */
36
37#include <iomanip>
38#include <sstream>
39#include <stdexcept>
40
41#include <cstdlib>
42#include <cerrno>
43#include <stdlib.h> // needed for strtoull which is not in the C++ headers
44
45#include "utilities/EGenStandardTypes.h"
46
47#ifdef WIN32
48#define strtoull _strtoui64
49#endif
50
51using std::strtod;
52using std::strtoul;
53
54namespace TPCE {
55
56// Converts an ASCII string into a 64 bit integer. Accepts a scaling factor of
57// 'K', 'M', or 'G' on values.
58// ptr - string to convert
59// Returns the integral 64 bit representation
60INT64 strtoint64(const char *ptr) {
61 INT64 val;
62 char *endp;
63 errno = 0;
64 val = strtoull(ptr, &endp, 0);
65 if (errno != 0) {
66 std::ostringstream strm;
67 strm << "Unable to parse integer '" << ptr << "'" << std::endl;
68 throw std::runtime_error(strm.str());
69 }
70 switch (*endp) {
71 case 'G':
72 val *= 1000 * 1000 * 1000;
73 break;
74 case 'M':
75 val *= 1000 * 1000;
76 break;
77 case 'K':
78 val *= 1000;
79 break;
80 case '\0':
81 endp--;
82 break;
83 default:
84 std::ostringstream strm;
85 strm << "Unable to parse invalid scale factor on integer '" << ptr << "'" << std::endl;
86 throw std::runtime_error(strm.str());
87 }
88 if (*++endp != '\0') {
89 std::ostringstream strm;
90 strm << "Unable to parse trailing characters on integer '" << ptr << "'" << std::endl;
91 throw std::runtime_error(strm.str());
92 }
93 return val;
94}
95
96// Converts an ASCII string into a double. Accepts a scaling factor of
97// 'K', 'M', or 'G' on values.
98// ptr - string to convert
99// Returns the double representation
100double strtodbl(const char *ptr) {
101 double val;
102 char *endp;
103 errno = 0;
104 val = strtod(ptr, &endp);
105 if (errno != 0) {
106 std::ostringstream strm;
107 strm << "Unable to parse floating point number '" << ptr << "'" << std::endl;
108 throw std::runtime_error(strm.str());
109 }
110 switch (*endp) {
111 case 'G':
112 val *= 1000.0 * 1000.0 * 1000.0;
113 break;
114 case 'M':
115 val *= 1000.0 * 1000.0;
116 break;
117 case 'K':
118 val *= 1000.0;
119 break;
120 case '\0':
121 endp--;
122 break;
123 default:
124 std::ostringstream strm;
125 strm << "Unable to parse invalid scale factor on floating point number '" << ptr << "'" << std::endl;
126 throw std::runtime_error(strm.str());
127 }
128 if (*++endp != '\0') {
129 std::ostringstream strm;
130 strm << "Unable to parse trailing characters on floating point number '" << ptr << "'" << std::endl;
131 throw std::runtime_error(strm.str());
132 }
133 return val;
134}
135
136// Converts an ASCII string in "HH:MM:SS" form into a INT64. HH: or HH:MM:
137// may be omitted.
138// ptr - string to convert
139// Returns the 64 bit integer representation
140INT64 timestrtoint64(const char *ptr) {
141 INT64 val;
142 char *endp;
143 errno = 0;
144 val = strtoul(ptr, &endp, 0);
145 if (*endp == ':') {
146 val = val * 60 + strtoul(endp + 1, &endp, 0);
147 if (*endp == ':') {
148 val = val * 60 + strtoul(endp + 1, &endp, 0);
149 }
150 }
151 if (errno != 0 || *endp != '\0') {
152 std::ostringstream strm;
153 strm << "Unable to parse time '" << ptr << "'" << std::endl;
154 throw std::runtime_error(strm.str());
155 }
156 return val;
157}
158
159// Converts a 64 bit integer into an HH:MM:SS string
160// val - integer to convert
161// Returns a string containing the formatted value.
162std::string int64totimestr(INT64 val) {
163 std::ostringstream strm;
164 int sec = static_cast<int>(val % 60);
165 val /= 60;
166 int min = static_cast<int>(val % 60);
167 int hrs = static_cast<int>(val / 60);
168
169 strm << std::setfill('0');
170 if (hrs) {
171 strm << std::setw(2) << hrs << ":";
172 }
173 strm << std::setw(2) << min << ":" << std::setw(2) << sec;
174
175 return strm.str();
176}
177
178} // namespace TPCE
179