-
Notifications
You must be signed in to change notification settings - Fork 3
/
MummyLineOrientedTextFileReader.cxx
403 lines (325 loc) · 10.3 KB
/
MummyLineOrientedTextFileReader.cxx
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//----------------------------------------------------------------------------
//
// $Id: MummyLineOrientedTextFileReader.cxx 470 2009-06-12 17:43:02Z hoffman $
//
// $Author: hoffman $
// $Date: 2009-06-12 13:43:02 -0400 (Fri, 12 Jun 2009) $
// $Revision: 470 $
//
// Copyright (C) 2007 Kitware, Inc.
//
//----------------------------------------------------------------------------
#include "MummyLineOrientedTextFileReader.h"
#include "MummyLog.h"
#include "MummySettings.h"
#include "gxsys/RegularExpression.hxx"
#include "gxsys/ios/fstream"
#include "gxsys/ios/sstream"
//----------------------------------------------------------------------------
MummyLineOrientedTextFileReader::MummyLineOrientedTextFileReader()
{
//this->FileName; // correctly constructed empty
//this->Lines; // correctly constructed empty
this->ExcludeMarkedLines = false;
//this->BeginExcludeRegex; // correctly constructed empty
//this->EndExcludeRegex; // correctly constructed empty
}
//----------------------------------------------------------------------------
MummyLineOrientedTextFileReader::~MummyLineOrientedTextFileReader()
{
}
//----------------------------------------------------------------------------
gxsys_stl::string MummyLineOrientedTextFileReader::GetFileName()
{
return this->FileName;
}
//----------------------------------------------------------------------------
void MummyLineOrientedTextFileReader::SetFileName(const char *filename)
{
if (filename)
{
this->FileName = filename;
}
else
{
this->FileName = "";
}
this->Update();
}
//----------------------------------------------------------------------------
bool MummyLineOrientedTextFileReader::GetExcludeMarkedLines()
{
return this->ExcludeMarkedLines;
}
//----------------------------------------------------------------------------
void MummyLineOrientedTextFileReader::SetExcludeMarkedLines(bool excludeMarkedLines)
{
this->ExcludeMarkedLines = excludeMarkedLines;
}
//----------------------------------------------------------------------------
gxsys_stl::string MummyLineOrientedTextFileReader::GetBeginExcludeRegex()
{
return this->BeginExcludeRegex;
}
//----------------------------------------------------------------------------
void MummyLineOrientedTextFileReader::SetBeginExcludeRegex(const gxsys_stl::string& beginExcludeRegex)
{
this->BeginExcludeRegex = beginExcludeRegex;
}
//----------------------------------------------------------------------------
gxsys_stl::string MummyLineOrientedTextFileReader::GetEndExcludeRegex()
{
return this->EndExcludeRegex;
}
//----------------------------------------------------------------------------
void MummyLineOrientedTextFileReader::SetEndExcludeRegex(const gxsys_stl::string& endExcludeRegex)
{
this->EndExcludeRegex = endExcludeRegex;
}
//----------------------------------------------------------------------------
void MummyLineOrientedTextFileReader::Update()
{
gxsys_ios::ifstream file(this->GetFileName().c_str());
this->Lines.clear();
if (file)
{
char line[4100];
bool trackBtxEtxLevel = !this->GetBeginExcludeRegex().empty() &&
!this->GetEndExcludeRegex().empty();
int btxEtxLevel = 0;
bool isLineComment = false;
gxsys::RegularExpression reLineComment;
reLineComment.compile("^[\\t ]*//");
gxsys::RegularExpression reBTX;
gxsys::RegularExpression reETX;
if (trackBtxEtxLevel)
{
reBTX.compile(this->GetBeginExcludeRegex().c_str());
reETX.compile(this->GetEndExcludeRegex().c_str());
}
//
// Old hard-coded values were eerily similar to BTX/ETX regexes found in
// the VTK source tree in Wrapping/vtkParse.l... ;)
//
//reBTX.compile("^[\\t ]*//BTX.*$");
//reETX.compile("^[\\t ]*//ETX.*$");
while (!file.eof())
{
line[0] = 0;
file.getline(line, 4099);
isLineComment = reLineComment.find(line);
if (trackBtxEtxLevel && reBTX.find(line))
{
btxEtxLevel++;
}
this->Lines.push_back(LineData(line, isLineComment, btxEtxLevel));
if (trackBtxEtxLevel && reETX.find(line))
{
btxEtxLevel--;
}
}
}
#if 0
// Dump it out with Trace to see what the settings and the output
// data look like... This is a Print-method-in-waiting...
//
gxsys_stl::ostringstream oss;
oss << "FileName: " << this->FileName << gxsys_stl::endl;
oss << "ExcludeMarkedLines: " << this->ExcludeMarkedLines << gxsys_stl::endl;
oss << "BeginExcludeRegex: " << this->BeginExcludeRegex << gxsys_stl::endl;
oss << "EndExcludeRegex: " << this->EndExcludeRegex << gxsys_stl::endl;
oss << "Lines:" << gxsys_stl::endl;
unsigned int i = 1;
gxsys_stl::vector<LineData>::iterator itLines;
for (itLines = this->Lines.begin(); itLines != this->Lines.end();
++itLines)
{
LineData line = *itLines;
// Comment?
//
if (line.IsLineComment)
{
oss << "C | ";
}
else
{
oss << " | ";
}
// BTX/ETX level:
//
if (line.BtxEtxLevel)
{
oss << line.BtxEtxLevel;
oss << " | ";
}
else
{
oss << " | ";
}
// Line number, i - 4 digits with leading zeroes:
//
char f = oss.fill();
gxsys_stl::streamsize w = oss.width();
oss.fill('0');
oss.width(4);
oss << i;
oss.fill(f);
oss.width(w);
// The line itself:
//
oss << ": " << line.Line << gxsys_stl::endl;
++i;
}
Trace(oss.str().c_str());
#endif
}
//----------------------------------------------------------------------------
unsigned int MummyLineOrientedTextFileReader::GetNumberOfLines()
{
return static_cast<unsigned int>(this->Lines.size());
}
//----------------------------------------------------------------------------
gxsys_stl::string MummyLineOrientedTextFileReader::GetLine(unsigned int lineNumber)
{
if (lineNumber<1 || lineNumber>this->GetNumberOfLines())
{
LogWarning(mw_NoSuchLineNumber, "lineNumber out of range in GetLine");
return "";
}
return this->Lines.at(lineNumber-1).Line;
}
//----------------------------------------------------------------------------
bool MummyLineOrientedTextFileReader::GetIsLineComment(unsigned int lineNumber)
{
if (lineNumber<1 || lineNumber>this->GetNumberOfLines())
{
LogWarning(mw_NoSuchLineNumber, "lineNumber out of range in GetIsLineComment");
return false;
}
return this->Lines.at(lineNumber-1).IsLineComment;
}
//----------------------------------------------------------------------------
int MummyLineOrientedTextFileReader::GetBtxEtxLevel(unsigned int lineNumber)
{
if (lineNumber<1 || lineNumber>this->GetNumberOfLines())
{
LogWarning(mw_NoSuchLineNumber, "lineNumber out of range in GetBtxEtxLevel");
return 0;
}
return this->Lines.at(lineNumber-1).BtxEtxLevel;
}
//----------------------------------------------------------------------------
bool MummyLineOrientedTextFileReader::IsLineExcluded(unsigned int lineNumber)
{
return this->GetExcludeMarkedLines() &&
this->GetBtxEtxLevel(lineNumber) > 0;
}
//----------------------------------------------------------------------------
void MummyLineOrientedTextFileReader::GetCommentBlockBefore(unsigned int lineNumber, gxsys_stl::vector<gxsys_stl::string>& block, unsigned int smallestAcceptableLineNumber)
{
if (lineNumber<2 || lineNumber>this->GetNumberOfLines())
{
LogWarning(mw_NoSuchLineNumber, "lineNumber out of range in GetCommentBlockBefore");
return;
}
// We seek the comment block before a class or method declaration in a
// header file. Like this line-numbered example from vtkObject.h:
//
// 52:
// 53: // Description:
// 54: // Create an object with Debug turned off, modified time initialized
// 55: // to zero, and reference counting on.
// 56: static vtkObject *New();
// 57:
//
// If given lineNumber==56 as input, we should compute begin==53 and end==55
// and return the comment block as a vector of line strings from
// begin to end inclusive...
unsigned int begin = 0;
unsigned int end = 0;
unsigned int i = lineNumber;
// Find the first comment before lineNumber, saving its index in 'end'.
// If no line comments occur before lineNumber, end will be 0.
//
while (0 == end && i>1)
{
i--;
if (GetIsLineComment(i) && 0==GetBtxEtxLevel(i))
{
end = i;
}
}
// Now find the first non-comment before end, saving the index of the comment
// line *after* it in 'begin'.
//
if (0 != end)
{
while (0 == begin && i>1)
{
i--;
if (!GetIsLineComment(i) && 0==GetBtxEtxLevel(i))
{
begin = i + 1;
}
}
// Didn't find a non-comment line before 'end'... Comment block must start
// right at line 1.
//
if (0 == begin)
{
begin = 1;
}
}
// If end==0 then there are no comment lines prior to lineNumber.
//
if (0 == end)
{
LogFileLineWarningMsg(this->FileName.c_str(), lineNumber, mw_UndocumentedEntity,
"No comment lines prior to line " << lineNumber << ". Undocumented class or method?");
}
else if (smallestAcceptableLineNumber > begin)
{
block.push_back(std::string("//Undocumented Block"));
}
else
{
gxsys_stl::string s;
gxsys::RegularExpression reBeginsWithWhiteSpace;
reBeginsWithWhiteSpace.compile("^([\\t ]*)[^\\t ].*");
size_t from = 0;
size_t to = 0;
for (i= begin; i<=end; ++i)
{
s = GetLine(i);
if (reBeginsWithWhiteSpace.find(s))
{
from = reBeginsWithWhiteSpace.match(1).size();
to = s.size() - from;
s = s.substr(from, to);
}
block.push_back(s);
}
}
}
//----------------------------------------------------------------------------
void MummyLineOrientedTextFileReader::GetFirstCommentBlock(gxsys_stl::vector<gxsys_stl::string>& block)
{
// Find the first non-comment line after the first comment line and use it
// to call GetCommentBlockBefore...
//
unsigned int i = 1;
unsigned int n = this->GetNumberOfLines();
bool foundFirstComment = false;
for (i= 1; i<=n; ++i)
{
if (this->GetIsLineComment(i) && 0==GetBtxEtxLevel(i))
{
foundFirstComment = true;
}
else if (foundFirstComment)
{
this->GetCommentBlockBefore(i+1, block, 1);
break;
}
}
}