dvbd / stringutil.cpp @ bfdb7446
History | View | Annotate | Download (2.7 KB)
1 |
/*
|
---|---|
2 |
Copyright 2003 John Knottenbelt
|
3 |
|
4 |
This program is free software; you can redistribute it and/or modify
|
5 |
it under the terms of the GNU General Public License as published by
|
6 |
the Free Software Foundation; either version 2 of the License, or
|
7 |
(at your option) any later version.
|
8 |
|
9 |
This program is distributed in the hope that it will be useful,
|
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
GNU General Public License for more details.
|
13 |
|
14 |
You should have received a copy of the GNU General Public License
|
15 |
along with this program; if not, write to the Free Software
|
16 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
|
17 |
*/
|
18 |
|
19 |
#include "config.h" |
20 |
#include "stringutil.h" |
21 |
#include <cctype> |
22 |
#include <sstream> |
23 |
#include <cstdio> |
24 |
|
25 |
using namespace std; |
26 |
|
27 |
string lowercase(const string &s) |
28 |
{ |
29 |
string l(s.size(), 0); |
30 |
for (unsigned i = 0; i < s.size(); i++) |
31 |
l[i] = tolower(s[i]); |
32 |
return l;
|
33 |
} |
34 |
|
35 |
string toString(int n) |
36 |
{ |
37 |
char text[64]; |
38 |
sprintf(text, "%d", n);
|
39 |
return text;
|
40 |
} |
41 |
|
42 |
string toString(unsigned n) |
43 |
{ |
44 |
char text[64]; |
45 |
sprintf(text, "%u", n);
|
46 |
return text;
|
47 |
} |
48 |
|
49 |
string toString(double n) |
50 |
{ |
51 |
ostringstream output; |
52 |
output << n; |
53 |
return output.str();
|
54 |
} |
55 |
|
56 |
double toDouble(const string &s) |
57 |
{ |
58 |
istringstream input(s); |
59 |
double n;
|
60 |
|
61 |
if (input >> n)
|
62 |
return n;
|
63 |
else
|
64 |
return 0.0; |
65 |
} |
66 |
|
67 |
unsigned long toUnsignedLong(const std::string &s) |
68 |
{ |
69 |
istringstream input(s); |
70 |
unsigned long n; |
71 |
|
72 |
if (input >> n)
|
73 |
return n;
|
74 |
else
|
75 |
return 0; |
76 |
} |
77 |
|
78 |
int toInt(const std::string &s) |
79 |
{ |
80 |
istringstream input(s); |
81 |
int n;
|
82 |
|
83 |
if (input >> n)
|
84 |
return n;
|
85 |
else
|
86 |
return 0; |
87 |
} |
88 |
|
89 |
unsigned toUnsigned(const std::string &s) |
90 |
{ |
91 |
istringstream input(s); |
92 |
unsigned n;
|
93 |
|
94 |
if (input >> n)
|
95 |
return n;
|
96 |
else
|
97 |
return 0; |
98 |
} |
99 |
|
100 |
void splitString(const std::string &s, char delim, std::vector<std::string> &v) |
101 |
{ |
102 |
std::string::size_type pos = 0; |
103 |
while (true) { |
104 |
std::string::size_type next = s.find(delim, pos);
|
105 |
v.push_back(s.substr(pos, next - pos)); |
106 |
if (next == std::string::npos) |
107 |
break;
|
108 |
pos = next + 1;
|
109 |
} |
110 |
} |
111 |
|
112 |
std::string stripTrailingWS(const std::string &s) |
113 |
{ |
114 |
if (s.size() == 0) |
115 |
return ""; |
116 |
|
117 |
int last = s.size() - 1; |
118 |
while (last > 0 && isspace(s[last])) |
119 |
last--; |
120 |
|
121 |
return s.substr(0, last + 1); |
122 |
} |
123 |
|
124 |
std::string stripLeadingWS(const std::string &s) |
125 |
{ |
126 |
std::string::size_type leadingWS = s.find_first_not_of(" \t"); |
127 |
if (leadingWS != std::string::npos) |
128 |
return s.substr(leadingWS, std::string::npos); |
129 |
else
|
130 |
return s;
|
131 |
} |
132 |
|
133 |
std::string stripLineComment(const std::string &s, char delim) |
134 |
{ |
135 |
std::string::size_type commentPos = s.find(delim);
|
136 |
return s.substr(0, commentPos); |
137 |
} |