File: stringutil.cpp

package info (click to toggle)
powersave 0.14.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,764 kB
  • ctags: 999
  • sloc: sh: 11,357; cpp: 8,103; ansic: 2,631; makefile: 388
file content (175 lines) | stat: -rw-r--r-- 4,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/***************************************************************************
 *                                                                         *
 *                         Powersave Daemon                                *
 *                                                                         *
 *      Parts copyright 2003 John Knottenbelt, taken from dvbd-0.7.7       *
 *                      2005 Stefan Seyfried                               *
 *                                                                         *
 *               Author(s): John Knottenbelt                               *
 *                          Stefan Seyfried                                *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify it *
 * under the terms of the GNU General Public License as published by the   *
 * Free Software Foundation; either version 2 of the License, or (at you   *
 * option) any later version.                                              *
 *                                                                         *
 * This program is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
 * General Public License for more details.                                *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with this program; if not, write to the Free Software Foundation, Inc., *
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                  *
 *                                                                         *
 ***************************************************************************/

#include "config.h"
#include "stringutil.h"
#include <cctype>
#include <sstream>
#include <cstdio>

using namespace std;

string lowercase(const string & s)
{
	unsigned i,sizeofs;
	sizeofs = s.size();
	string l(sizeofs, 0);
	for (i = 0; i < sizeofs; i++)
		l[i] = tolower(s[i]);
	return l;
}

string toString(bool b)
{
	if (b)
		return "true";
	else
		return "false";
}

string toString(int n)
{
	ostringstream strstr;
	strstr << n ;
	return strstr.str();
}

string toString(unsigned n)
{
	char text[64];
	sprintf(text, "%u", n);
	return text;
}

string toString(double n)
{
	ostringstream output;
	output << n;
	return output.str();
}

double toDouble(const string & s)
{
	istringstream input(s);
	double n;

	if (input >> n)
		return n;
	else
		return 0.0;
}

bool toBool(const std::string & s)
{
	if (s == "true")
		return true;
	else
		return false;
}

unsigned long toUnsignedLong(const std::string & s)
{
	istringstream input(s);
	unsigned long n;

	if (input >> n)
		return n;
	else
		return 0;
}

int toPosInt(const std::string & s)
{
	istringstream input(s);
	int n;

	if (input >> n)
		return n;
	else
		return -1;
}

unsigned toUnsigned(const std::string & s)
{
	istringstream input(s);
	unsigned n;

	if (input >> n)
		return n;
	else
		return 0;
}

void splitString(const std::string & s, char delim, std::vector < std::string > &v)
{
	std::string::size_type pos = 0;
	while (true) {
		std::string::size_type next = s.find(delim, pos);
		v.push_back(s.substr(pos, next - pos));
		if (next == std::string::npos)
			break;
		pos = next + 1;
	}
}

std::string stripTrailingWS(const std::string & s)
{
	if (s.size() == 0)
		return "";

	int last = s.size() - 1;
	while (last >= 0 && isspace(s[last]))
		last--;

	return s.substr(0, last + 1);
}

std::string stripLeadingWS(const std::string & s)
{
	std::string::size_type leadingWS = s.find_first_not_of(" \t");
	if (leadingWS != std::string::npos)
		return s.substr(leadingWS, std::string::npos);
	else
		return s;
}

std::string stripLineComment(const std::string & s, char delim)
{
	std::string::size_type commentPos = s.find(delim);
	return s.substr(0, commentPos);
}

std::string stripApostrophe(const std::string & s)
{
	std::string::size_type apo = s.find_first_not_of("'\"");
	int last = s.size() - 1;
	if ((s[last] == '\'') || (s[last] == '\"'))
		last--;
	if (apo != std::string::npos)
		return s.substr(apo, last);
	else
		return s.substr(0, last);
}