stringtools.hpp
Go to the documentation of this file.
1// $Id: stringtools.hpp 1068 2012-09-18 11:21:53Z perroe $
2
3// Copyright (c) 2011, Norwegian Computing Center
4// All rights reserved.
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7// • Redistributions of source code must retain the above copyright notice, this
8// list of conditions and the following disclaimer.
9// • Redistributions in binary form must reproduce the above copyright notice, this list of
10// conditions and the following disclaimer in the documentation and/or other materials
11// provided with the distribution.
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
13// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
15// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
17// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
20// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
22#ifndef NRLIB_STRINGTOOLS_HPP
23#define NRLIB_STRINGTOOLS_HPP
24
25#include <stdlib.h> // For atoi and atof
26
27#include <string>
28#include <iomanip>
29#include <vector>
30#include <sstream>
31#include <typeinfo>
32
33#include "../exception/exception.hpp"
34
35namespace NRLib {
36 std::vector<std::string> GetTokens(const std::string& s);
37
38 std::vector<std::string> GetQuotedTokens(const std::string& s);
39
42 template <typename T>
43 bool IsType(const std::string& s);
44
47 template <typename T>
48 T ParseType(const std::string& s);
49
52 template <>
53 std::string ParseType<std::string>(const std::string& s);
54
56 template <typename T>
57 std::string ToString(const T obj, int precision=-99999);
58
60 template <typename I>
61 I ParseAsciiArrayFast(std::string& s, I begin, size_t n);
62
65
68
71
74
79 const std::string& str);
80
83 const std::string& extension);
84
87 const std::string& extension);
88
91 const std::string & out,
92 const std::string & in);
93
96
97 bool IsNumber(const std::string & s);
98
100
102 inline std::string Whitespace() { return " \t\n\r\f\v"; }
103
104namespace NRLibPrivate {
105
106template <class A>
108{
109public:
110 static A ParseType(const char* s) {
111 return NRLib::ParseType<A>(s);
112 }
113};
114
115template <>
116class UnsafeParser<int>
117{
118public:
119 static int ParseType(const char* s) {
120 return atoi(s);
121 }
122};
123
124template <>
125class UnsafeParser<double>
126{
127public:
128 static double ParseType(const char* s) {
129 return atof(s);
130 }
131};
132
133template <>
134class UnsafeParser<float>
135{
136public:
137 static double ParseType(const char* s) {
138 return static_cast<float>(atof(s));
139 }
140};
141
142
143} // namespace NRLibPrivate
144
145} // namespace NRLib
146
147
149
150template <typename T>
152{
153 std::istringstream i(s);
154 T x;
155 char c;
156 if (!(i >> x) || i.get(c))
157 return false;
158 return true;
159}
160
161
162template <typename T>
164{
165 std::istringstream i(s);
166 T x;
167 char c;
168 if (!(i >> x))
169 throw Exception("Failed to convert \"" + s + "\" to " + typeid(T).name());
170 if (i.get(c))
171 throw Exception("Could not convert whole \"" + s + "\" to " + typeid(T).name());
172 return x;
173}
174
175
176template <typename T>
177std::string NRLib::ToString(const T obj, int precision)
178{
179 std::ostringstream o;
180 if (precision!=-99999) {
181 o << std::fixed << std::setprecision(precision);
182 }
183 if (!(o << obj)) {
184 throw Exception("Bad conversion.");
185 }
186 return o.str();
187}
188
189
190template <typename I>
192{
193 typedef typename std::iterator_traits<I>::value_type T;
194 std::string whitespace = " \n\r\f\t";
195
196 size_t pos = s.find_first_not_of(whitespace, 0);
197 size_t next_pos = s.find_first_of(whitespace, pos+1);
198 size_t i = 0;
199 while (i < n && pos != s.npos) {
200 if (next_pos != s.npos) {
201 s[next_pos] = '\0';
202 }
204 ++begin;
205 pos = s.find_first_not_of(whitespace, next_pos + 1);
206 next_pos = s.find_first_of(whitespace, pos + 1);
207 ++i;
208 }
209
210 if (i != n) {
211 throw Exception("Not enough elements parsed from string.");
212 }
213
214 return begin;
215}
216
217
218#endif // NRLIB_STRINGTOOLS_HPP
const char *const name
Definition: cJSON.h:258
const char *const string
Definition: cJSON.h:170
Definition: exception.hpp:34
static double ParseType(const char *s)
Definition: stringtools.hpp:128
static double ParseType(const char *s)
Definition: stringtools.hpp:137
static int ParseType(const char *s)
Definition: stringtools.hpp:119
Definition: stringtools.hpp:108
static A ParseType(const char *s)
Definition: stringtools.hpp:110
not_this_one begin(...)
Definition: exception.hpp:31
std::string GetPath(const std::string &filename)
Get the path from a full file name.
std::vector< std::string > GetTokens(const std::string &s)
std::vector< std::string > GetQuotedTokens(const std::string &s)
bool IsType(const std::string &s)
Definition: stringtools.hpp:151
std::string Uppercase(const std::string &text)
Return uppercase of input string.
I ParseAsciiArrayFast(std::string &s, I begin, size_t n)
Not safe. Replaces whitespace in s with \0.
Definition: stringtools.hpp:191
std::string GetStem(const std::string &filename)
Get the stem of the filename (filename without path and extension)
std::string Chomp(const std::string &s)
std::string AddExtension(const std::string &filename, const std::string &extension)
Add an extension to the filename.
T ParseType(const std::string &s)
Definition: stringtools.hpp:163
std::string GetExtension(const std::string &filename)
Get filename extension.
std::string Whitespace()
String with different kinds of whitespace characters.
Definition: stringtools.hpp:102
std::string ReplaceExtension(const std::string &filename, const std::string &extension)
Replace file extension.
bool IsNumber(const std::string &s)
std::string RemovePath(const std::string &filename)
Get file name only (no path) from full file name.
std::string PrependDir(const std::string &prefix, const std::string &str)
void Substitute(std::string &text, const std::string &out, const std::string &in)
In string text replace all occurences odf string "out" with string "in".
std::string ToString(const T obj, int precision=-99999)
Definition: stringtools.hpp:177
std::string extension(const std::string &fname)
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t x(y+z)