summaryrefslogtreecommitdiff
path: root/src/base/StringHelper.cpp
blob: 6155859d6ede577a0fe01173bdde1256c375ffa8 (plain)
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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "StringHelper.h"

#include <stdlib.h>
#include <ctype.h>
#include <algorithm>
#include <cstdio>
#include <iterator>

using namespace std;

namespace avg {

bool isWhitespace(const string& s)
{
    return s.find_first_not_of(" \n\r\t") == s.npos;
}

void skipWhitespace(std::istream& is)
{
    string sWhitespace(" \n\r\t");
    bool bWhitespace;
    do {
        int i = is.peek();
        if (i == EOF) {
            bWhitespace = false;
        } else {
            bWhitespace = (sWhitespace.find(char(i)) != sWhitespace.npos);
        }
        if (bWhitespace) {
            is.ignore();
        }
    } while (bWhitespace);
}

void skipToken(std::istream& is, char token)
{
    skipWhitespace(is);
    int i = is.peek();
    if (i == token) {
        is.ignore();
    } else {
        is.setstate(ios::failbit);
    }
}

int stringToInt(const string& s)
{
    int i;
    fromString(s, i);
    return i;
}

float stringToFloat(const string& s)
{
    float d;
    fromString(s, d);
    return d;
}

bool stringToBool(const string& s)
{
    // avg usually wants xml attributes in lowercase, but python only
    // sees 'True' as true, so we'll accept that too. Also, python 2.3
    // has 1 as true, so that has to be ok too.
    if (s == "True" || s == "true" || s == "1") {
        return true;
    }
    if (s == "False" || s == "false" || s == "0") {
        return false;
    }
    throw (Exception(AVG_ERR_TYPE, string("Could not convert ")+s+" to bool."));
}

std::string removeStartEndSpaces(const string& s)
{
    string sResult = s;
    while (sResult.size() > 0 && (sResult[0] == ' ' || sResult[0] == '\n' 
            || sResult[0] == '\r' || sResult[0] == '\t')) 
    {
        sResult.erase(0, 1);
    }
    if (sResult.size() == 0) {
        return sResult;
    }
    char c = sResult[sResult.length()-1];
    while (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
        sResult.erase(sResult.length()-1, 1);
        c = sResult[sResult.length()-1];
    }
    return sResult;
}

string toLowerCase(const string& s)
{
    string sResult;
    for (unsigned i=0; i<s.length(); ++i) {
        sResult.push_back(::tolower(s[i]));
    }
    return sResult;
}

bool equalIgnoreCase(const string& s1, const string& s2)
{
    if (s1.length() != s2.length()) {
        return false;
    }
    string sUpper1;
    string sUpper2;
    transform(s1.begin(), s1.end(), std::back_inserter(sUpper1), (int(*)(int)) toupper);
    transform(s2.begin(), s2.end(), std::back_inserter(sUpper2), (int(*)(int)) toupper);
    return sUpper1 == sUpper2;
}

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

}