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
|
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckBlackList.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>
namespace kws {
/** Check that there is no word in the class that matches a word in the black list */
bool Parser::CheckBlackList(const char* filename)
{
m_TestsDone[BLACKLIST] = true;
m_TestsDescription[BLACKLIST] = "No word should matche any words from the file: ";
m_TestsDescription[BLACKLIST] += filename;
bool hasError = false;
// If the black list is not read we read it
if(strcmp(m_BlackList.c_str(),filename))
{
// Read the file
std::ifstream file;
file.open(filename, std::ios::binary | std::ios::in);
if(!file.is_open())
{
std::cout << "Cannot open file: " << filename << std::endl;
return 0;
}
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;
m_BlackListBuffer = buf;
m_BlackListBuffer.resize(fileSize);
delete [] buf;
m_BlackList = filename;
file.close();
}
// Go through the list of words
size_t start = 0;
size_t space = m_BlackListBuffer.find(" ",start);
size_t eol = m_BlackListBuffer.find("\n",start);
size_t end = space;
if(eol != std::string::npos && eol < space)
{
if(eol == 0)
{
end = std::string::npos;
}
else
{
end = eol-1;
}
}
if(end == std::string::npos && eol != std::string::npos)
{
if(eol == 0)
{
end = std::string::npos;
}
else
{
end = eol-1;
}
}
while(end != std::string::npos && end>start)
{
std::string blackword = m_BlackListBuffer.substr(start,end-start);
start = end+1;
space = m_BlackListBuffer.find(" ",start);
eol = m_BlackListBuffer.find("\n",start);
end = space;
if(eol != std::string::npos && eol < space)
{
if(eol == 0)
{
end = std::string::npos;
}
else
{
end = eol-1;
}
}
if(end == std::string::npos && eol != std::string::npos)
{
if(eol == 0)
{
end = std::string::npos;
}
else
{
end = eol-1;
}
}
if(blackword.length() <= 1)
{
continue;
}
size_t pos = m_BufferNoComment.find(blackword,0);
while(pos != std::string::npos)
{
Error error;
error.line = this->GetLineNumber(pos,true);
error.line2 = error.line;
error.number = BLACKLIST;
error.description = "The word is in the black list";
m_ErrorList.push_back(error);
hasError = true;
pos = m_BufferNoComment.find(blackword,pos+1);
}
}
return !hasError;
}
} // end namespace kws
|