~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
 
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
3
 *  Author: Pierre-Luc Bacon <pierre-luc.bacon@savoirfairelinux.com>
4
4
 *
5
5
 *  This program is free software; you can redistribute it and/or modify
33
33
 
34
34
namespace sfl {
35
35
 
36
 
Pattern::Pattern(const std::string& pattern, const std::string& options) :
 
36
Pattern::Pattern(const std::string& pattern, bool matchGlobally) :
37
37
    pattern_(pattern),
38
38
    subject_(),
39
39
    re_(NULL),
40
40
    ovector_(),
 
41
    offset_{0, 0},
41
42
    count_(0),
42
 
    options_(0),
43
 
    optionsDescription_(options)
44
 
{
45
 
    // Set offsets
46
 
    offset_[0] = offset_[1] = 0;
47
 
 
48
 
    for (unsigned int i = 0; i < options.length(); i++) {
49
 
        switch (options.at(i)) {
50
 
            case 'i':
51
 
                options_ |= PCRE_CASELESS;
52
 
                break;
53
 
 
54
 
            case 'm':
55
 
                options_ |= PCRE_MULTILINE;
56
 
                break;
57
 
 
58
 
            case 's':
59
 
                options_ |= PCRE_DOTALL;
60
 
                break;
61
 
 
62
 
            case 'x':
63
 
                options_ |= PCRE_EXTENDED;
64
 
                break;
65
 
        }
66
 
    }
67
 
 
68
 
    // Compile the pattern.
69
 
    compile();
70
 
}
71
 
 
72
 
Pattern::~Pattern()
73
 
{
74
 
    if (re_ != NULL)
75
 
        pcre_free(re_);
76
 
}
77
 
 
78
 
void Pattern::compile()
 
43
    matchGlobally_(matchGlobally)
79
44
{
80
45
    // Compile the pattern
81
46
    int offset;
103
68
    ovector_.resize((captureCount + 1) * 3);
104
69
}
105
70
 
106
 
unsigned int Pattern::getCaptureGroupCount()
107
 
{
108
 
    int captureCount = 0;
109
 
    pcre_fullinfo(re_, NULL, PCRE_INFO_CAPTURECOUNT, &captureCount);
110
 
    return captureCount;
111
 
}
112
 
 
113
 
std::vector<std::string> Pattern::groups()
114
 
{
115
 
    const char ** stringList;
116
 
 
117
 
    pcre_get_substring_list(subject_.c_str(), &ovector_[0], count_, &stringList);
118
 
    std::vector<std::string> matchedSubstrings;
119
 
    for (int i = 1; stringList[i] != NULL; i++)
120
 
        matchedSubstrings.push_back(stringList[i]);
121
 
 
122
 
    pcre_free_substring_list(stringList);
123
 
 
124
 
    return matchedSubstrings;
125
 
}
126
 
 
127
 
std::string Pattern::group(int groupNumber)
128
 
{
129
 
    const char * stringPtr;
130
 
    int rc = pcre_get_substring(subject_.substr(offset_[0]).c_str(), &ovector_[0],
131
 
                                count_, groupNumber, &stringPtr);
132
 
 
133
 
    if (rc < 0) {
134
 
        switch (rc) {
135
 
            case PCRE_ERROR_NOSUBSTRING:
136
 
                throw std::out_of_range("Invalid group reference.");
137
 
 
138
 
            case PCRE_ERROR_NOMEMORY:
139
 
                throw MatchError("Memory exhausted.");
140
 
 
141
 
            default:
142
 
                throw MatchError("Failed to get named substring.");
143
 
        }
144
 
    }
145
 
    std::string matchedStr(stringPtr);
146
 
 
147
 
    pcre_free_substring(stringPtr);
148
 
 
149
 
    return matchedStr;
150
 
}
151
 
 
152
 
std::string Pattern::group(const std::string& groupName)
 
71
Pattern::~Pattern()
 
72
{
 
73
    if (re_ != NULL)
 
74
        pcre_free(re_);
 
75
}
 
76
 
 
77
 
 
78
std::string Pattern::group(const char *groupName)
153
79
{
154
80
    const char * stringPtr = NULL;
155
81
    int rc = pcre_get_named_substring(re_, subject_.substr(offset_[0]).c_str(),
156
 
                                      &ovector_[0], count_, groupName.c_str(),
 
82
                                      &ovector_[0], count_, groupName,
157
83
                                      &stringPtr);
158
84
 
159
85
    if (rc < 0) {
176
102
    return matchedStr;
177
103
}
178
104
 
179
 
void Pattern::start(const std::string& groupName) const
180
 
{
181
 
    int index = pcre_get_stringnumber(re_, groupName.c_str());
182
 
    start(index);
183
 
}
184
 
 
185
 
size_t Pattern::start(unsigned int groupNumber) const
186
 
{
187
 
    if (groupNumber <= (unsigned int) count_)
188
 
        return ovector_[(groupNumber + 1) * 2];
189
 
    else
190
 
        throw std::out_of_range("Invalid group reference.");
191
 
    return 0;
192
 
}
193
 
 
194
105
size_t Pattern::start() const
195
106
{
196
107
    return ovector_[0] + offset_[0];
197
108
}
198
109
 
199
 
void Pattern::end(const std::string& groupName) const
200
 
{
201
 
    int index = pcre_get_stringnumber(re_, groupName.c_str());
202
 
    end(index);
203
 
}
204
 
 
205
 
size_t Pattern::end(unsigned int groupNumber) const
206
 
{
207
 
    if (groupNumber <= (unsigned int) count_)
208
 
        return ovector_[((groupNumber + 1) * 2) + 1 ] - 1;
209
 
    else
210
 
        throw std::out_of_range("Invalid group reference.");
211
 
    return 0;
212
 
}
213
 
 
214
110
size_t Pattern::end() const
215
111
{
216
112
    return (ovector_[1] - 1) + offset_[0];
218
114
 
219
115
bool Pattern::matches()
220
116
{
221
 
    return matches(subject_);
222
 
    return true;
223
 
}
224
 
 
225
 
bool Pattern::matches(const std::string& subject)
226
 
{
227
117
    // Try to find a match for this pattern
228
 
    int rc = pcre_exec(re_, NULL, subject.substr(offset_[1]).c_str(),
229
 
                       subject.length() - offset_[1], 0, options_, &ovector_[0],
 
118
    int rc = pcre_exec(re_, NULL, subject_.substr(offset_[1]).c_str(),
 
119
                       subject_.length() - offset_[1], 0, 0, &ovector_[0],
230
120
                       ovector_.size());
231
121
 
232
122
    // Matching failed.
236
126
    }
237
127
 
238
128
    // Handle the case if matching should be done globally
239
 
    if (optionsDescription_.find("g") != std::string::npos) {
 
129
    if (matchGlobally_) {
240
130
        offset_[0] = offset_[1];
241
131
        // New offset is old offset + end of relative offset
242
132
        offset_[1] =  ovector_[1] + offset_[0];