forked from config4star/config4cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cpp
More file actions
203 lines (175 loc) · 4.64 KB
/
Configuration.cpp
File metadata and controls
203 lines (175 loc) · 4.64 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//-----------------------------------------------------------------------
// Copyright 2011 Ciaran McHale.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//----------------------------------------------------------------------
//--------
// #include's
//--------
#include <config4cpp/Configuration.h>
#include "ConfigurationImpl.h"
#include "MBChar.h"
#include <string.h>
#include <assert.h>
#include <stdlib.h>
namespace CONFIG4CPP_NAMESPACE {
Configuration::Configuration() { }
Configuration::~Configuration() { }
Configuration *
Configuration::create()
{
return new ConfigurationImpl();
}
void
Configuration::destroy()
{
delete this;
}
void
Configuration::mergeNames(
const char * scope,
const char * localName,
StringBuffer & fullyScopedName)
{
if (scope[0] == '\0') {
fullyScopedName = localName;
} else if (localName[0] == '\0') {
fullyScopedName = scope;
} else {
fullyScopedName.empty();
fullyScopedName << scope << "." << localName;
}
}
int
Configuration::mbstrlen(const char * str)
{
int count;
int status;
char byte;
const char * ptr;
wchar_t wChar;
MBChar ch;
mbstate_t mbtowcState;
memset(&mbtowcState, 0, sizeof(mbtowcState));
count = 0;
ptr = str;
while (*ptr != '\0') {
status = -1;
while (status == -1) {
byte = *ptr;
ptr ++;
if (byte == '\0' && !ch.isEmpty()) {
return -1; // invalid multi-byte string
}
if (byte == '\0') {
break;
}
if (!ch.add(byte)) {
return -1; // invalid multi-byte string
}
status = mbrtowc(&wChar, ch.c_str(), ch.length(), &mbtowcState);
if (status == -1 && ch.isFull()) {
return -1; // invalid multi-byte string
}
}
if (byte != '\0') {
count ++;
}
ch.reset();
}
return count;
}
//----------------------------------------------------------------------
// Function: patternMatch()
//
// Description: Returns true if the specified pattern matches the
// specified string.
//
// Note: The only wildcard supported is "*". It acts like the
// "*" wildcard in UNIX and DOS shells, that is, it
// matches zero or more characters.
//----------------------------------------------------------------------
bool
Configuration::patternMatch(const char * str, const char * pattern)
{
wchar_t * wStr;
int wStrLen;
wchar_t * wPattern;
int wPatternLen;
bool result;
int strLen;
int patternLen;
strLen = strlen(str);
wStr = new wchar_t[strLen + 1];
wStrLen = mbstowcs(wStr, str, strLen + 1);
assert(wStrLen != -1);
patternLen = strlen(pattern);
wPattern = new wchar_t[patternLen + 1];
wPatternLen = mbstowcs(wPattern, pattern, patternLen + 1);
assert(wPatternLen != -1);
result = patternMatchInternal(wStr, 0, wStrLen,
wPattern, 0, wPatternLen);
delete [] wStr;
delete [] wPattern;
return result;
}
bool
Configuration::patternMatchInternal(
const wchar_t * wStr,
int wStrIndex,
int wStrLen,
const wchar_t * wPattern,
int wPatternIndex,
int wPatternLen)
{
while (wPatternIndex < wPatternLen) {
if (wPattern[wPatternIndex] != '*') {
if (wPattern[wPatternIndex] != wStr[wStrIndex]) {
return false;
}
wPatternIndex++;
if (wStrIndex < wStrLen) {
wStrIndex++;
}
} else {
wPatternIndex++;
while (wPattern[wPatternIndex] == '*') {
wPatternIndex++;
}
if (wPatternIndex == wPatternLen) {
return true;
}
for (; wStrIndex < wStrLen; wStrIndex++) {
if (patternMatchInternal(wStr, wStrIndex,
wStrLen, wPattern, wPatternIndex,
wPatternLen))
{
return true;
}
}
}
}
if (wPattern[wPatternIndex] != wStr[wStrIndex]) {
return false;
}
return true;
}
}; // namespace CONFIG4CPP_NAMESPACE