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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
#include "MlfStream.h"
#include "Common.h"
#include "Error.h"
namespace TNet
{
//******************************************************************************
LabelContainer::
~LabelContainer()
{
while (!this->mLabelList.empty())
{
delete this->mLabelList.back();
this->mLabelList.pop_back();
}
}
//******************************************************************************
size_t
LabelContainer::
DirDepth(const std::string & rPath)
{
size_t depth = 0;
size_t length = rPath.length();
const char * s = rPath.c_str();
for (size_t i = 0; i < length; i++)
{
if (*s == '/' || *s == '\\')
{
depth++;
}
s++;
}
return depth;
}
//******************************************************************************
void
LabelContainer::
Insert(const std::string & rLabel,
std::streampos Pos)
{
LabelRecord ls;
size_t depth;
LabelRecord tmp_ls;
// we need to compute the depth of the label path if
// wildcard is used
// do we have a wildcard???
if (rLabel[0] == '*')
{
depth = this->DirDepth(rLabel);
}
else
{
depth = MAX_LABEL_DEPTH;
}
// perhaps we want to store the depth of the path in the label for the wildcards
// to work
this->mDepths.insert(depth);
// store the values
ls.mStreamPos = Pos;
ls.miLabelListLimit = mLabelList.end();
if (mLabelList.begin() != mLabelList.end()) {
ls.miLabelListLimit--;
}
// if no wildcard chars, then we try to store in hash, otherwise store in
// list
if (rLabel.find_first_of("*?%",1) == rLabel.npos)
{
if (!Find(rLabel, tmp_ls))
{
// look in the
this->mLabelMap[rLabel] = ls;
}
else {
;
//Warning("More general definition found when inserting " + rLabel + " ... label: " + MatchedPattern());
}
}
else
{
this->mLabelList.push_back(new std::pair<std::string,LabelRecord>(rLabel, ls));
}
}
//******************************************************************************
bool
LabelContainer::
FindInHash(const std::string & rLabel, LabelRecord & rLS)
{
bool found = false;
std::string str;
// current depth within the str
DepthType current_depth = MAX_LABEL_DEPTH;
// current search position within the str
size_t prev = rLabel.size() + 1;
// we will walk through the set depts bacwards so we begin at the end and move
// to the front...
std::set<DepthType>::reverse_iterator ri (this->mDepths.end());
std::set<DepthType>::reverse_iterator rlast (this->mDepths.begin());
LabelHashType::iterator lab;
// we perform the search until we run to the end of the set or we find something
while ((!found) && (ri != rlast))
{
// we don't need to do anything with the string if the depth is set to
// max label depth since it contains no *
if (*ri == MAX_LABEL_DEPTH)
{
found = ((lab=this->mLabelMap.find(rLabel)) != this->mLabelMap.end());
if (found) str = rLabel;
}
// we will crop the string and put * in the begining and try to search
else
{
// we know that we walk backwards in the depths, so we need to first find
// the last / and
if (current_depth == MAX_LABEL_DEPTH)
{
if (*ri > 0)
{
// we find the ri-th / from back
for (DepthType i=1; (i <= *ri) && (prev != rLabel.npos); i++)
{
prev = rLabel.find_last_of("/\\", prev-1);
}
}
else
{
prev = 0;
}
// check if finding succeeded (prev == str.npos => failure, see STL)
if (prev != rLabel.npos)
{
// construct the new string beign sought for
str.assign(rLabel, prev, rLabel.size());
str = '*' + str;
// now we try to find
found = ((lab=this->mLabelMap.find(str)) != this->mLabelMap.end());
// say, that current depth is *ri
current_depth = *ri;
}
else
{
prev = rLabel.size() + 1;
}
} // if (current_depth == MAX_LABEL_DEPTH)
else
{
// now we know at which / we are from the back, so we search forward now
// and we need to reach the ri-th /
while (current_depth > *ri)
{
// we try to find next /
if ((prev = rLabel.find_first_of("/\\", prev+1)) != rLabel.npos)
current_depth--;
else
return false;
}
// construct the new string beign sought for
str.assign(rLabel, prev, rLabel.size());
str = '*' + str;
// now we try to find
found = ((lab=this->mLabelMap.find(str)) != this->mLabelMap.end());
}
}
// move one element further (jump to next observed depth)
ri++;
} // while (run)
// some debug info
if (found)
{
rLS = lab->second;
this->mMatchedPattern = str;
}
return found;
}
//******************************************************************************
bool
LabelContainer::
FindInList(const std::string & rLabel, LabelRecord & rLS, bool limitSearch)
{
bool found = false;
std::string str;
LabelListType::iterator lab = mLabelList.begin();
LabelListType::iterator limit;
if (limitSearch && (rLS.miLabelListLimit != mLabelList.end()))
{
limit = rLS.miLabelListLimit;
limit++;
}
else
{
limit = this->mLabelList.end();
}
// we perform sequential search until we run to the end of the list or we find
// something
while ((!found) && (lab != limit))
{
if (ProcessMask(rLabel, (*lab)->first, str))
{
found = true;
}
else
{
lab++;
}
} // while (run)
// some debug info
if (found)
{
rLS = (*lab)->second;
this->mMatchedPattern = (*lab)->first;
this->mMatchedPatternMask = str;
}
return found;
}
//******************************************************************************
bool
LabelContainer::
Find(const std::string & rLabel, LabelRecord & rLS)
{
// try to find the label in the Hash
if (FindInHash(rLabel, rLS))
{
// we look in the list, but we limit the search.
FindInList(rLabel, rLS, true);
return true;
} //if (this->mLabelContainer.FindInHash(rLabel, label_stream))
else
{
// we didn't find it in the hash so we look in the list
return FindInList(rLabel, rLS);
}
}
} // namespace TNet
|