HPCToolkit
StrUtil.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //****************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 // Author:
59 // Nathan Tallent
60 //
61 //****************************************************************************
62 
63 //************************** System Include Files ****************************
64 
65 #include <iostream>
66 #include <fstream>
67 
68 #include <string>
69 using std::string;
70 
71 #include <cstdlib>
72 #include <cstring>
73 
74 #include <errno.h>
75 
76 #define __STDC_FORMAT_MACROS
77 #include <stdint.h>
78 
79 //*************************** User Include Files *****************************
80 
81 #include <include/gcc-attr.h>
82 
83 #include "StrUtil.hpp"
84 #include "diagnostics.h"
85 
86 //************************** Forward Declarations ****************************
87 
88 //****************************************************************************
89 
90 //****************************************************************************
91 // StrUtil
92 //****************************************************************************
93 
94 namespace StrUtil {
95 
96 // --------------------------------------------------------------------------
97 //
98 // --------------------------------------------------------------------------
99 
100 void
101 tokenize_char(const std::string& tokenstr, const char* delim,
102  std::vector<std::string>& tokenvec)
103 {
104  const size_t sz = tokenstr.size();
105  for (size_t begp = 0, endp = 0; begp < sz; begp = endp+1) {
106  begp = tokenstr.find_first_not_of(delim, begp);
107  if (begp == string::npos) {
108  break;
109  }
110 
111  endp = tokenstr.find_first_of(delim, begp);
112  if (endp == string::npos) {
113  endp = sz;
114  }
115  string x = tokenstr.substr(begp, endp - begp); // [begin, end)
116  tokenvec.push_back(x);
117  }
118 }
119 
120 
121 void
122 tokenize_str(const std::string& tokenstr, const char* delim,
123  std::vector<std::string>& tokenvec)
124 {
125  const int delimsz = strlen(delim);
126  const size_t sz = tokenstr.size();
127 
128  for (size_t begp = 0, endp = 0; begp < sz; begp = endp + delimsz) {
129  endp = tokenstr.find(delim, begp);
130  if (endp == string::npos) {
131  endp = sz;
132  }
133  string x = tokenstr.substr(begp, endp - begp); // [begin, end)
134  tokenvec.push_back(x);
135  }
136 }
137 
138 
139 // --------------------------------------------------------------------------
140 //
141 // --------------------------------------------------------------------------
142 
143 string
144 join(const std::vector<string>& tokenvec, const char* delim,
145  size_t begIdx, size_t endIdx)
146 {
147  string result;
148 
149  // N.B.: [begIdx, endIdx)
150  for (size_t i = begIdx; i < endIdx; ++i) {
151  result += tokenvec[i];
152  if (i + 1 < endIdx) {
153  result += delim;
154  }
155  }
156 
157  return result;
158 }
159 
160 // --------------------------------------------------------------------------
161 //
162 // --------------------------------------------------------------------------
163 
164 long
165 toLong(const char* str, unsigned* endidx)
166 {
167  long value = 0;
168  DIAG_Assert((str && str[0] != '\0'), "StrUtil::toLong: empty string!");
169 
170  errno = 0;
171  char* endptr = NULL;
172  value = strtol(str, &endptr, 0);
173  if (endidx) {
174  *endidx = (endptr - str) / sizeof(char);
175  }
176  if (errno || (!endidx && endptr && strlen(endptr) > 0)) {
177  string msg = "[StrUtil::toLong] Cannot convert `" + string(str)
178  + "' to integral (long) value";
179  if (errno) { // not always set
180  msg += string(" (") + strerror(errno) + string(")");
181  }
182  DIAG_Throw(msg);
183  }
184  return value;
185 }
186 
187 
188 uint64_t
189 toUInt64(const char* str, unsigned* endidx)
190 {
191  uint64_t value = 0;
192  DIAG_Assert((str && str[0] != '\0'), "StrUtil::toUInt64: empty string!");
193 
194  errno = 0;
195  char* endptr = NULL;
196  value = strtoull(str, &endptr, 0);
197  if (endidx) {
198  *endidx = (endptr - str) / sizeof(char);
199  }
200  if (errno || (!endidx && endptr && strlen(endptr) > 0)) {
201  string msg = "[StrUtil::toUInt64] Cannot convert `" + string(str)
202  + "' to integral (uint64_t) value";
203  if (errno) { // not always set
204  msg += string(" (") + strerror(errno) + string(")");
205  }
206  DIAG_Throw(msg);
207  }
208  return value;
209 }
210 
211 
212 double
213 toDbl(const char* str, unsigned* endidx)
214 {
215  double value = 0;
216  DIAG_Assert((str && str[0] != '\0'), "StrUtil::toDbl: empty string!");
217 
218  errno = 0;
219  char* endptr = NULL;
220  value = strtod(str, &endptr);
221  if (endidx) {
222  *endidx = (endptr - str) / sizeof(char);
223  }
224  if (errno || (!endidx && endptr && strlen(endptr) > 0)) {
225  string msg = "[StrUtil::toDbl] Cannot convert `" + string(str)
226  + "' to real (double) value.";
227  if (errno) { // not always set
228  msg += string(" (") + strerror(errno) + string(")");
229  }
230  DIAG_Throw(msg);
231  }
232  return value;
233 }
234 
235 
236 // --------------------------------------------------------------------------
237 //
238 // --------------------------------------------------------------------------
239 
240 static char buf[32];
241 
242 string
243 toStr(const int x, int base)
244 {
245  const char* format = NULL;
246 
247  switch (base) {
248  case 10:
249  format = "%d";
250  break;
251 
252  default:
254  }
255 
256  sprintf(buf, format, x);
257  return string(buf);
258 }
259 
260 
261 string
262 toStr(const unsigned x, int base)
263 {
264  const char* format = NULL;
265 
266  switch (base) {
267  case 10:
268  //int numSz = (x == 0) ? 1 : (int) log10((double)l); // no log16...
269  //stringSize = 2 + numSz;
270  format = "%u";
271  break;
272 
273  case 16:
274  //int numSz = (x == 0) ? 1 : (int) log10((double)l);
275  //stringSize = 4 + numSz;
276  format = "%#x";
277  break;
278 
279  default:
281  }
282 
283  sprintf(buf, format, x);
284  return string(buf);
285 }
286 
287 
288 string
289 toStr(const int64_t x, int base)
290 {
291  const char* format = NULL;
292 
293  switch (base) {
294  case 10:
295  format = "%" PRId64;
296  break;
297 
298  case 16:
299  format = "%#" PRIx64;
300  break;
301 
302  default:
304  }
305 
306  sprintf(buf, format, x);
307  return string(buf);
308 }
309 
310 
311 string
312 toStr(const uint64_t x, int base)
313 {
314  const char* format = NULL;
315 
316  switch (base) {
317  case 10:
318  format = "%" PRIu64;
319  break;
320 
321  case 16:
322  format = "%#" PRIx64;
323  break;
324 
325  default:
327  }
328 
329  sprintf(buf, format, x);
330  return string(buf);
331 }
332 
333 
334 string
335 toStr(const void* x, int GCC_ATTR_UNUSED base)
336 {
337  sprintf(buf, "%p", x);
338  return string(buf);
339 }
340 
341 
342 string
343 toStr(const double x, const char* format)
344 {
345  //static char buf[19]; // 0xhhhhhhhhhhhhhhhh format
346  sprintf(buf, format, x);
347  return string(buf);
348 }
349 
350 
351 //****************************************************************************
352 
353 } // end of StrUtil namespace
double toDbl(const char *str, unsigned *endidx)
Definition: StrUtil.cpp:213
uint64_t toUInt64(const char *str, unsigned *endidx)
Definition: StrUtil.cpp:189
string toStr(const int x, int base)
Definition: StrUtil.cpp:243
const char * DIAG_Unimplemented
Definition: fmt.c:108
long toLong(const char *str, unsigned *endidx)
Definition: StrUtil.cpp:165
void tokenize_str(const std::string &tokenstr, const char *delim, std::vector< std::string > &tokenvec)
Definition: StrUtil.cpp:122
string join(const std::vector< string > &tokenvec, const char *delim, size_t begIdx, size_t endIdx)
Definition: StrUtil.cpp:144
void tokenize_char(const std::string &tokenstr, const char *delim, std::vector< std::string > &tokenvec)
Definition: StrUtil.cpp:101
#define NULL
Definition: ElfHelper.cpp:85
#define DIAG_Die(...)
Definition: diagnostics.h:267
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80