-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwsCheckHeader.cxx
404 lines (359 loc) · 10.2 KB
/
kwsCheckHeader.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
404
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckHeader.cxx
Copyright (c) Kitware, Inc. All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "kwsParser.h"
#include <string.h> // strlen
namespace kws {
/** Check header given a template filename
* The template should have '<NA>\n' tag to avoid checking the rest of the line
* or <NA> to skip a word.
* The header should also be at the beginning of the file */
bool Parser::CheckHeader(const char* filename, bool considerSpaceEOL,bool useCVS)
{
m_TestsDone[HEADER] = true;
char* val = new char[255];
sprintf(val,"The header should respect the template");
m_TestsDescription[HEADER] = val;
delete [] val;
bool hasError = false;
if(!filename)
{
std::cout << "CheckHeader(): Please specify an header file or a directory containing headers" << std::endl;
return false;
}
std::vector<std::string> fileNames;
// if there is no space in the name (i.e on linux) we remove the
// '"' if any.
std::string headerFilename = filename;
if(headerFilename.find(' ') == std::string::npos)
{
headerFilename = "";
for(unsigned long j=0;j<strlen(filename);j++)
{
if(filename[j] != '\"')
{
headerFilename+=filename[j];
}
}
}
// Remove quotes if any on linux
#ifndef WIN32
if(headerFilename[0] == '"')
{
headerFilename = headerFilename.substr(1,headerFilename.size()-2);
}
#endif
// Check if we have a directory or header
kwssys::Directory directory;
if(directory.Load(headerFilename.c_str()))
{
std::string dirname = headerFilename;
if(dirname[dirname.size()-1] != '/')
{
dirname += "/";
}
for(unsigned int i=0;i<directory.GetNumberOfFiles();i++)
{
std::string file = directory.GetFile(i);
std::string fullpath = dirname+file;
if(file!=".." && file!="." && file!="CVS"&& file!= ".svn")
{
fileNames.push_back(dirname+file);
}
}
}
else
{
fileNames.push_back(headerFilename);
}
// Create a temporary vector of errors
std::vector<ErrorVectorType> tempErrors;
std::vector<std::string>::const_iterator itFilename = fileNames.begin();
while(itFilename != fileNames.end())
{
hasError = false;
// Read the header file
std::ifstream file;
file.open((*itFilename).c_str(), std::ios::binary | std::ios::in);
if(!file.is_open())
{
std::cout << "Cannot open header file: " << (*itFilename).c_str() << std::endl;
return false;
}
file.seekg(0,std::ios::end);
unsigned long fileSize = file.tellg();
file.seekg(0,std::ios::beg);
char* buf = new char[fileSize+1];
file.read(buf,fileSize);
buf[fileSize] = 0;
std::string buffer(buf);
buffer.resize(fileSize);
file.close();
delete [] buf;
this->ConvertBufferToWindowsFileType(buffer);
ErrorVectorType tempErrorVector;
// Check the file char by char
std::string::const_iterator ith = buffer.begin();
std::string::const_iterator it = m_Buffer.begin();
unsigned int pos = 0;
unsigned int posh = 0;
int line = -1;
while((ith != buffer.end()) && (it != m_Buffer.end()))
{
// if we have cvs
if((*ith == '$') && useCVS)
{
ith++;
posh++;
it++;
pos++;
while(((*ith) != '$') && (ith != buffer.end()))
{
ith++;
posh++;
}
while(((*it) != '$') && (it != m_Buffer.end()))
{
pos++;
it++;
}
//continue;
}
if((*it) != (*ith))
{
// Check if we have a <NA> tag
if((*ith) == '<')
{
long int pos2 = buffer.find("<NA>",posh);
long int pos3 = buffer.find("<NA>\n",posh);
// We skip the line
if(pos3 == static_cast<long int>(posh))
{
while(((*ith) != '\n') && (ith != buffer.end()))
{
ith++;
posh++;
}
while(((*it) != '\n') && (it != m_Buffer.end()))
{
pos++;
it++;
}
continue;
}
// if we have the tag we skip the word
else if(pos2 == static_cast<long int>(posh))
{
while(((*ith) != ' ') && (ith != buffer.end()))
{
ith++;
posh++;
}
while(((*it) != ' ') && (it != m_Buffer.end()))
{
pos++;
it++;
}
continue;
}
}
// if we should not check the spaces at the end of line
else if( (!considerSpaceEOL)
&& ((*ith == ' ') || (*it == ' '))
)
{
// search if we are effectively at the end of the line
bool isAtEnd = true;
if(*it == ' ')
{
std::string::const_iterator ittemp = it;
while((ittemp != m_Buffer.end()) && ((*ittemp) != '\n'))
{
if(*ittemp != ' ')
{
isAtEnd = false;
break;
}
ittemp++;
}
}
else if(*ith == ' ')
{
std::string::const_iterator ittemp = ith;
while((ittemp != buffer.end()) && ((*ittemp) != '\n'))
{
if(*ittemp != ' ')
{
isAtEnd = false;
break;
}
ittemp++;
}
}
// If we are at the end we skip the line
if(!isAtEnd)
{
while((ith != buffer.end()) && ((*ith) != '\n'))
{
ith++;
posh++;
}
while((it != m_Buffer.end()) && ((*it) != '\n'))
{
pos++;
it++;
}
continue;
}
}
// Report the error
//hasError = true;
// We report the wrong word and the line
int l = this->GetLineNumber(pos);
if(l != line)
{
line = l;
// Find the word
long int poshw = buffer.find(' ',posh);
long int poshw2 = buffer.find('\n',posh);
std::string wordh = "";
if(poshw!= -1 && poshw < poshw2)
{
wordh = buffer.substr(posh,poshw-posh);
}
else if (poshw2 != -1)
{
wordh = buffer.substr(posh,poshw2-posh-1);
}
// Find the word
long int posw = m_Buffer.find(' ',pos);
long int posw2 = m_Buffer.find('\n',pos);
std::string word = "";
if(posw!=-1 && posw < posw2)
{
word = m_Buffer.substr(pos,posw-pos);
}
else if (poshw2 != -1)
{
word = m_Buffer.substr(pos,posw2-pos-1);
}
if(word == wordh)
{
wordh = "wrong ident";
}
if(word == " ")
{
word = "[space]";
}
if(wordh == " ")
{
wordh = "[space]";
}
if(word == "\r")
{
word = "[end of line]";
}
if(wordh == "\r")
{
wordh = "[end of line]";
}
if(word[0] == 0)
{
word = "[no char]";
}
if(wordh[0] == 0)
{
wordh = "[no char]";
}
Error error;
error.line = line;
error.line2 = error.line;
error.number = HEADER;
error.description = "Header mismatch: ";
error.description += word;
error.description += " (";
error.description += wordh;
error.description += ") : ";
error.description += headerFilename.c_str();
tempErrorVector.push_back(error);
hasError = true;
// We skip that line
while((ith != buffer.end()) && ((*ith) != '\n'))
{
ith++;
posh++;
}
while((it != m_Buffer.end()) && ((*it) != '\n'))
{
pos++;
it++;
}
}
}
if(ith != buffer.end())
{
posh++;
ith++;
}
if(it != m_Buffer.end())
{
pos++;
it++;
}
}
if(it == m_Buffer.end())
{
Error error;
error.line = 1;
error.line2 = error.line;
error.number = HEADER;
error.description = "The header is incomplete";
tempErrorVector.push_back(error);
hasError = true;
}
// If we don't have any errors we return the current errors
if(!hasError)
{
m_HeaderFilename = *itFilename;
ErrorVectorType::const_iterator itErr = tempErrorVector.begin();
while(itErr != tempErrorVector.end())
{
m_ErrorList.push_back(*itErr);
itErr++;
}
return true;
}
tempErrors.push_back(tempErrorVector);
itFilename++;
} // end loop of filenames
// We check the less errors we can have
unsigned int header=0;
int minErrors = 9999999;
unsigned int i=0;
std::vector<ErrorVectorType>::const_iterator itErrV = tempErrors.begin();
while(itErrV != tempErrors.end())
{
if((int)(*itErrV).size() < minErrors)
{
header = i;
minErrors = (*itErrV).size();
}
i++;
itErrV++;
}
ErrorVectorType::const_iterator itErr = tempErrors[header].begin();
while(itErr != tempErrors[header].end())
{
m_ErrorList.push_back(*itErr);
itErr++;
}
m_HeaderFilename = fileNames[header];
return !hasError;
}
} // end namespace kws