~ubuntu-branches/ubuntu/raring/sflphone/raring

« back to all changes in this revision

Viewing changes to daemon/src/sip/pattern.cpp

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
 
3
 *  Author: Pierre-Luc Bacon <pierre-luc.bacon@savoirfairelinux.com>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 3 of the License, or
 
8
 *  (at your option) any later version.
 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 *  Additional permission under GNU GPL version 3 section 7:
 
19
 *
 
20
 *  If you modify this program, or any covered work, by linking or
 
21
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
22
 *  modified version of that library), containing parts covered by the
 
23
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 
24
 *  grants you additional permission to convey the resulting work.
 
25
 *  Corresponding Source for a non-source form of such a combination
 
26
 *  shall include the source code for the parts of OpenSSL used as well
 
27
 *  as that of the covered work.
 
28
 */
 
29
 
 
30
#include "pattern.h"
 
31
#include <sstream>
 
32
#include <cstdio>
 
33
 
 
34
namespace sfl {
 
35
 
 
36
Pattern::Pattern(const std::string& pattern, const std::string& options) :
 
37
    pattern_(pattern),
 
38
    subject_(),
 
39
    re_(NULL),
 
40
    ovector_(NULL),
 
41
    ovectorSize_(0),
 
42
    count_(0),
 
43
    options_(0),
 
44
    optionsDescription_(options)
 
45
{
 
46
    // Set offsets
 
47
    offset_[0] = offset_[1] = 0;
 
48
 
 
49
    for (unsigned int i = 0; i < options.length(); i++) {
 
50
        switch (options.at(i)) {
 
51
            case 'i':
 
52
                options_ |= PCRE_CASELESS;
 
53
                break;
 
54
 
 
55
            case 'm':
 
56
                options_ |= PCRE_MULTILINE;
 
57
                break;
 
58
 
 
59
            case 's':
 
60
                options_ |= PCRE_DOTALL;
 
61
                break;
 
62
 
 
63
            case 'x':
 
64
                options_ |= PCRE_EXTENDED;
 
65
                break;
 
66
        }
 
67
    }
 
68
 
 
69
    // Compile the pattern.
 
70
    compile();
 
71
}
 
72
 
 
73
Pattern::~Pattern()
 
74
{
 
75
    if (re_ != NULL)
 
76
        pcre_free(re_);
 
77
 
 
78
    delete[] ovector_;
 
79
}
 
80
 
 
81
void Pattern::compile()
 
82
{
 
83
    // Compile the pattern
 
84
    int offset;
 
85
    const char * error;
 
86
 
 
87
    re_ = pcre_compile(pattern_.c_str(), 0, &error, &offset, NULL);
 
88
 
 
89
    if (re_ == NULL) {
 
90
        std::string offsetStr;
 
91
        std::stringstream ss;
 
92
        ss << offset;
 
93
        offsetStr = ss.str();
 
94
 
 
95
        std::string msg("PCRE compiling failed at offset " + offsetStr);
 
96
 
 
97
        throw CompileError(msg);
 
98
    }
 
99
 
 
100
    // Allocate an appropriate amount
 
101
    // of memory for the output vector.
 
102
    int captureCount;
 
103
 
 
104
    pcre_fullinfo(re_, NULL, PCRE_INFO_CAPTURECOUNT, &captureCount);
 
105
 
 
106
    delete[] ovector_;
 
107
 
 
108
    ovector_ = new int[(captureCount + 1) * 3];
 
109
 
 
110
    ovectorSize_ = (captureCount + 1) * 3;
 
111
}
 
112
 
 
113
unsigned int Pattern::getCaptureGroupCount()
 
114
{
 
115
    int captureCount;
 
116
    pcre_fullinfo(re_, NULL, PCRE_INFO_CAPTURECOUNT, &captureCount);
 
117
    return captureCount;
 
118
}
 
119
 
 
120
std::vector<std::string> Pattern::groups()
 
121
{
 
122
    const char ** stringList;
 
123
 
 
124
    pcre_get_substring_list(subject_.c_str(),
 
125
                            ovector_,
 
126
                            count_,
 
127
                            &stringList);
 
128
 
 
129
    std::vector<std::string> matchedSubstrings;
 
130
 
 
131
    for (int i = 1; stringList[i] != NULL; i++)
 
132
        matchedSubstrings.push_back(stringList[i]);
 
133
 
 
134
    pcre_free_substring_list(stringList);
 
135
 
 
136
    return matchedSubstrings;
 
137
}
 
138
 
 
139
std::string Pattern::group(int groupNumber)
 
140
{
 
141
    const char * stringPtr;
 
142
 
 
143
    int rc = pcre_get_substring(subject_.substr(offset_[0]).c_str(), ovector_,
 
144
                                count_, groupNumber, &stringPtr);
 
145
 
 
146
    if (rc < 0) {
 
147
        switch (rc) {
 
148
            case PCRE_ERROR_NOSUBSTRING:
 
149
                throw std::out_of_range("Invalid group reference.");
 
150
 
 
151
            case PCRE_ERROR_NOMEMORY:
 
152
                throw MatchError("Memory exhausted.");
 
153
 
 
154
            default:
 
155
                throw MatchError("Failed to get named substring.");
 
156
        }
 
157
    }
 
158
 
 
159
    std::string matchedStr(stringPtr);
 
160
 
 
161
    pcre_free_substring(stringPtr);
 
162
 
 
163
    return matchedStr;
 
164
}
 
165
 
 
166
std::string Pattern::group(const std::string& groupName)
 
167
{
 
168
    const char * stringPtr = NULL;
 
169
    int rc = pcre_get_named_substring(re_, subject_.substr(offset_[0]).c_str(),
 
170
                                      ovector_, count_, groupName.c_str(),
 
171
                                      &stringPtr);
 
172
 
 
173
    if (rc < 0) {
 
174
        switch (rc) {
 
175
            case PCRE_ERROR_NOSUBSTRING:
 
176
                break;
 
177
 
 
178
            case PCRE_ERROR_NOMEMORY:
 
179
                throw MatchError("Memory exhausted.");
 
180
 
 
181
            default:
 
182
                throw MatchError("Failed to get named substring.");
 
183
        }
 
184
    }
 
185
 
 
186
    std::string matchedStr;
 
187
 
 
188
    if (stringPtr) {
 
189
        matchedStr = stringPtr;
 
190
        pcre_free_substring(stringPtr);
 
191
    }
 
192
 
 
193
    return matchedStr;
 
194
}
 
195
 
 
196
void Pattern::start(const std::string& groupName) const
 
197
{
 
198
    int index = pcre_get_stringnumber(re_, groupName.c_str());
 
199
    start(index);
 
200
}
 
201
 
 
202
size_t Pattern::start(unsigned int groupNumber) const
 
203
{
 
204
    if (groupNumber <= (unsigned int) count_)
 
205
        return ovector_[(groupNumber + 1) * 2];
 
206
    else
 
207
        throw std::out_of_range("Invalid group reference.");
 
208
}
 
209
 
 
210
size_t Pattern::start() const
 
211
{
 
212
    return ovector_[0] + offset_[0];
 
213
}
 
214
 
 
215
void Pattern::end(const std::string& groupName) const
 
216
{
 
217
    int index = pcre_get_stringnumber(re_, groupName.c_str());
 
218
    end(index);
 
219
}
 
220
 
 
221
size_t Pattern::end(unsigned int groupNumber) const
 
222
{
 
223
    if (groupNumber <= (unsigned int) count_)
 
224
        return ovector_[((groupNumber + 1) * 2) + 1 ] - 1;
 
225
    else
 
226
        throw std::out_of_range("Invalid group reference.");
 
227
}
 
228
 
 
229
size_t Pattern::end() const
 
230
{
 
231
    return (ovector_[1] - 1) + offset_[0];
 
232
}
 
233
 
 
234
bool Pattern::matches()
 
235
{
 
236
    return matches(subject_);
 
237
}
 
238
 
 
239
bool Pattern::matches(const std::string& subject)
 
240
{
 
241
    // Try to find a match for this pattern
 
242
    int rc = pcre_exec(re_, NULL, subject.substr(offset_[1]).c_str(),
 
243
                       subject.length() - offset_[1], 0, options_, ovector_,
 
244
                       ovectorSize_);
 
245
 
 
246
    // Matching failed.
 
247
    if (rc < 0) {
 
248
        offset_[0] = offset_[1] = 0;
 
249
        return false;
 
250
    }
 
251
 
 
252
    // Handle the case if matching should be done globally
 
253
    if (optionsDescription_.find("g") != std::string::npos) {
 
254
        offset_[0] = offset_[1];
 
255
        // New offset is old offset + end of relative offset
 
256
        offset_[1] =  ovector_[1] + offset_[0];
 
257
    }
 
258
 
 
259
    // Matching succeded but not enough space.
 
260
    // @TODO figure out something more clever to do in this case.
 
261
    if (rc == 0)
 
262
        throw MatchError("No space to store all substrings.");
 
263
 
 
264
    // Matching succeeded. Keep the number of substrings for
 
265
    // subsequent calls to group().
 
266
    count_ = rc;
 
267
 
 
268
    return true;
 
269
}
 
270
 
 
271
std::vector<std::string> Pattern::split()
 
272
{
 
273
    size_t tokenEnd = -1;
 
274
    size_t tokenStart = 0;
 
275
 
 
276
    std::vector<std::string> substringSplitted;
 
277
 
 
278
    while (matches()) {
 
279
        tokenStart = start();
 
280
        substringSplitted.push_back(subject_.substr(tokenEnd + 1,
 
281
                                    tokenStart - tokenEnd - 1));
 
282
        tokenEnd = end();
 
283
    }
 
284
 
 
285
    substringSplitted.push_back(subject_.substr(tokenEnd + 1,
 
286
                                                tokenStart - tokenEnd - 1));
 
287
    return substringSplitted;
 
288
}
 
289
}