| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckBlackList.cxx |
| 5 |
|
| 6 |
Copyright (c) Kitware, Inc. All rights reserved. |
| 7 |
See Copyright.txt or http://www.kitware.com/Copyright.htm for details. |
| 8 |
|
| 9 |
This software is distributed WITHOUT ANY WARRANTY; without even |
| 10 |
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the above copyright notices for more information. |
| 12 |
|
| 13 |
=========================================================================*/ |
| 14 |
#include "kwsParser.h" |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
namespace kws { |
| 18 |
|
| 19 |
|
| 20 |
/** Check that there is no word in the class that matches a word in the black list */ |
| 21 |
bool Parser::CheckBlackList(const char* filename) |
| 22 |
{ |
| 23 |
m_TestsDone[BLACKLIST] = true; |
| 24 |
m_TestsDescription[BLACKLIST] = "No word should match any words from the file: "; |
| 25 |
m_TestsDescription[BLACKLIST] += filename; |
| 26 |
|
| 27 |
bool hasError = false; |
| 28 |
|
| 29 |
// If the black list is not read we read it |
| 30 |
if(strcmp(m_BlackList.c_str(),filename)) |
| 31 |
{ |
| 32 |
// Read the file |
| 33 |
std::ifstream file; |
| 34 |
file.open(filename, std::ios::binary | std::ios::in); |
| 35 |
if(!file.is_open()) |
| 36 |
{ |
| 37 |
std::cout << "Cannot open file: " << filename << std::endl; |
| 38 |
return false; |
| 39 |
} |
| 40 |
file.seekg(0,std::ios::end); |
| 41 |
unsigned long fileSize = file.tellg(); |
| 42 |
file.seekg(0,std::ios::beg); |
| 43 |
|
| 44 |
char* buf = new char[fileSize+1]; |
| 45 |
file.read(buf,fileSize); |
| 46 |
buf[fileSize] = 0; |
| 47 |
m_BlackListBuffer = buf; |
| 48 |
m_BlackListBuffer.resize(fileSize); |
| 49 |
delete [] buf; |
| 50 |
m_BlackList = filename; |
| 51 |
file.close(); |
| 52 |
} |
| 53 |
|
| 54 |
// Go through the list of words |
| 55 |
size_t start = 0; |
| 56 |
size_t space = m_BlackListBuffer.find(" ",start); |
| 57 |
size_t eol = m_BlackListBuffer.find("\n",start); |
| 58 |
|
| 59 |
size_t end = space; |
| 60 |
if(eol != std::string::npos && eol < space) |
| 61 |
{ |
| 62 |
if(eol == 0) |
| 63 |
{ |
| 64 |
end = std::string::npos; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
end = eol-1; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
if(end == std::string::npos && eol != std::string::npos) |
| 73 |
{ |
| 74 |
if(eol == 0) |
| 75 |
{ |
| 76 |
end = std::string::npos; |
| 77 |
} |
| 78 |
else |
| 79 |
{ |
| 80 |
end = eol-1; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
while(end != std::string::npos && end>start) |
| 85 |
{ |
| 86 |
std::string blackword = m_BlackListBuffer.substr(start,end-start); |
| 87 |
start = end+1; |
| 88 |
|
| 89 |
space = m_BlackListBuffer.find(" ",start); |
| 90 |
eol = m_BlackListBuffer.find("\n",start); |
| 91 |
|
| 92 |
end = space; |
| 93 |
if(eol != std::string::npos && eol < space) |
| 94 |
{ |
| 95 |
if(eol == 0) |
| 96 |
{ |
| 97 |
end = std::string::npos; |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
end = eol-1; |
| 102 |
} |
| 103 |
} |
| 104 |
if(end == std::string::npos && eol != std::string::npos) |
| 105 |
{ |
| 106 |
if(eol == 0) |
| 107 |
{ |
| 108 |
end = std::string::npos; |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
end = eol-1; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if(blackword.length() <= 1) |
| 117 |
{ |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
size_t pos = m_BufferNoComment.find(blackword,0); |
| 122 |
while(pos != std::string::npos) |
| 123 |
{ |
| 124 |
Error error; |
| 125 |
error.line = this->GetLineNumber(pos,true); |
| 126 |
error.line2 = error.line; |
| 127 |
error.number = BLACKLIST; |
| 128 |
error.description = "The word is in the black list"; |
| 129 |
m_ErrorList.push_back(error); |
| 130 |
hasError = true; |
| 131 |
pos = m_BufferNoComment.find(blackword,pos+1); |
| 132 |
} |
| 133 |
} |
| 134 |
return !hasError; |
| 135 |
} |
| 136 |
|
| 137 |
} // end namespace kws |