HPCToolkit
StrUtil.hpp
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 #ifndef support_StrUtil_hpp
64 #define support_StrUtil_hpp
65 
66 //************************** System Include Files ****************************
67 
68 #include <iostream>
69 #include <fstream>
70 #include <string>
71 #include <vector>
72 
73 #include <inttypes.h>
74 
75 //*************************** User Include Files *****************************
76 
77 //************************** Forward Declarations ****************************
78 
79 //****************************************************************************
80 // StrUtil
81 //****************************************************************************
82 
83 namespace StrUtil {
84 
85 // --------------------------------------------------------------------------
86 // tokenize_char: Given a string of tokens 'tokenstr' delimited by any of the
87 // *individual characters* in 'delim', extract and place each token
88 // string in 'tokenvec'.
89 //
90 // tokenize_str: Same as tokenize_char, except that the delimiter
91 // itself is one string (rather than several possible single characters).
92 //
93 // --------------------------------------------------------------------------
94 
95 void
96 tokenize_char(const std::string& tokenstr, const char* delim,
97  std::vector<std::string>& tokenvec);
98 
99 void
100 tokenize_str(const std::string& tokenstr, const char* delim,
101  std::vector<std::string>& tokenvec);
102 
103 
104 // --------------------------------------------------------------------------
105 // join: Given a vector of tokens 'tokenvec' and a delimiter 'delim',
106 // form a string by placing delim in between every element of
107 // tokenvec[begIdx ... endIdx).
108 // --------------------------------------------------------------------------
109 
110 std::string
111 join(const std::vector<std::string>& tokenvec, const char* delim,
112  size_t begIdx, size_t endIdx);
113 
114 
115 // --------------------------------------------------------------------------
116 // string -> numerical types
117 //
118 // Comments [FIXME]: see strtol. If 'endidx' is non-NULL, it is set
119 // to the index (relative to 'str') of the first non-numerical
120 // character. (Thus, the entire string is valid if endidx ==
121 // length(str).) If 'endidx' is NULL, an error is raised if any
122 // 'junk' appears after the proposed string.
123 //
124 // --------------------------------------------------------------------------
125 
126 long
127 toLong(const char* str, unsigned* endidx = NULL);
128 
129 inline long
130 toLong(const std::string& str, unsigned* endidx = NULL)
131 {
132  return toLong(str.c_str(), endidx);
133 }
134 
135 
136 uint64_t
137 toUInt64(const char* str, unsigned* endidx = NULL);
138 
139 inline uint64_t
140 toUInt64(const std::string& str, unsigned* endidx = NULL)
141 {
142  return toUInt64(str.c_str(), endidx);
143 }
144 
145 
146 double
147 toDbl(const char* str, unsigned* endidx = NULL);
148 
149 inline double
150 toDbl(const std::string& str, unsigned* endidx = NULL)
151 {
152  return toDbl(str.c_str(), endidx);
153 }
154 
155 
156 // --------------------------------------------------------------------------
157 // numerical types -> string
158 // base: one of 10, 16
159 // --------------------------------------------------------------------------
160 
161 std::string
162 toStr(const int x, int base = 10);
163 
164 std::string
165 toStr(const unsigned x, int base = 10);
166 
167 std::string
168 toStr(const int64_t x, int base = 10);
169 
170 std::string
171 toStr(const uint64_t x, int base = 10);
172 
173 std::string
174 toStr(const void* x, int base = 16);
175 
176 std::string
177 toStr(const double x, const char* format = "%.3f");
178 
179 
180 } // end of StrUtil namespace
181 
182 
183 #endif // support_StrUtil_hpp
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
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