Statistics
| Branch: | Tag: | Revision:

dvbd / src / stringutil.cpp @ 59be6a47

History | View | Annotate | Download (2.88 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(bool b)
36
{
37
  if (b) return "true";
38
  else return "false";
39
}
40

    
41
string toString(int n)
42
{
43
    char text[64];
44
    sprintf(text, "%d", n);
45
    return text;
46
}
47

    
48
string toString(unsigned n)
49
{
50
    char text[64];
51
    sprintf(text, "%u", n);
52
    return text;
53
}
54

    
55
string toString(double n)
56
{
57
    ostringstream output;
58
    output << n;
59
    return output.str();
60
}
61

    
62
double toDouble(const string &s)
63
{
64
    istringstream input(s);
65
    double n;
66
    
67
    if (input >> n)
68
        return n;
69
    else
70
        return 0.0;
71
}
72

    
73
bool toBool(const std::string &s)
74
{
75
  if (s == "true")
76
    return true;
77
  else
78
    return false;
79
}
80

    
81
unsigned long toUnsignedLong(const std::string &s)
82
{
83
    istringstream input(s);
84
    unsigned long n;
85
    
86
    if (input >> n)
87
        return n;
88
    else
89
        return 0;
90
}
91

    
92
int toInt(const std::string &s)
93
{
94
    istringstream input(s);
95
    int n;
96
    
97
    if (input >> n)
98
        return n;
99
    else
100
        return 0;
101
}
102

    
103
unsigned toUnsigned(const std::string &s)
104
{
105
    istringstream input(s);
106
    unsigned n;
107
    
108
    if (input >> n)
109
        return n;
110
    else
111
        return 0;
112
}
113

    
114
void splitString(const std::string &s, char delim, std::vector<std::string> &v)
115
{
116
  std::string::size_type pos = 0;
117
  while (true) {
118
    std::string::size_type next = s.find(delim, pos);
119
    v.push_back(s.substr(pos, next - pos));
120
    if (next == std::string::npos)
121
      break;
122
    pos = next + 1;
123
  }
124
}
125

    
126
std::string stripTrailingWS(const std::string &s)
127
{
128
  if (s.size() == 0)
129
    return "";
130

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

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

    
138
std::string stripLeadingWS(const std::string &s)
139
{
140
  std::string::size_type leadingWS = s.find_first_not_of(" \t");
141
  if (leadingWS != std::string::npos)
142
    return s.substr(leadingWS, std::string::npos);
143
  else
144
    return s;
145
}
146

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