| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckExtraSpaces.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 |
|
| 16 |
namespace kws { |
| 17 |
|
| 18 |
|
| 19 |
/** Check the number of space between the end of the declaration |
| 20 |
* and the end of the file */ |
| 21 |
bool Parser::CheckExtraSpaces(unsigned long max,bool checkEmptyLines) |
| 22 |
{ |
| 23 |
m_TestsDone[SPACES] = true; |
| 24 |
constexpr size_t length = 255; |
| 25 |
char* val = new char[length]; |
| 26 |
snprintf(val,length,"Spaces at the end of line = %ld max spaces",max); |
| 27 |
m_TestsDescription[SPACES] = val; |
| 28 |
delete [] val; |
| 29 |
|
| 30 |
bool hasError = false; |
| 31 |
auto posEndOfLine = static_cast<long int>(m_Buffer.find("\r\n", 0)); |
| 32 |
while(posEndOfLine != -1) |
| 33 |
{ |
| 34 |
bool checking = true; |
| 35 |
if(!checkEmptyLines) |
| 36 |
{ |
| 37 |
// Check if the line is empty |
| 38 |
long pos = posEndOfLine-1; |
| 39 |
bool empty = true; |
| 40 |
while(pos>0 && m_Buffer[pos]!='\n') |
| 41 |
{ |
| 42 |
if(m_Buffer[pos] != ' ') |
| 43 |
{ |
| 44 |
empty = false; |
| 45 |
break; |
| 46 |
} |
| 47 |
pos--; |
| 48 |
} |
| 49 |
|
| 50 |
if(empty) |
| 51 |
{ |
| 52 |
checking = false; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if(checking) |
| 57 |
{ |
| 58 |
// We try to find the word before that |
| 59 |
long i=posEndOfLine-1; |
| 60 |
unsigned long space = 0; |
| 61 |
while(i>=0) |
| 62 |
{ |
| 63 |
if(m_Buffer[i] == ' ') |
| 64 |
{ |
| 65 |
space++; |
| 66 |
if(space > max) |
| 67 |
{ |
| 68 |
Error error; |
| 69 |
error.line = this->GetLineNumber(posEndOfLine); |
| 70 |
error.line2 = error.line; |
| 71 |
error.number = SPACES; |
| 72 |
error.description = "Number of spaces before end of line exceed: "; |
| 73 |
constexpr size_t length = 21; |
| 74 |
char localval[length]; |
| 75 |
snprintf(localval,length,"%ld",space); |
| 76 |
error.description += localval; |
| 77 |
error.description += " (max="; |
| 78 |
snprintf(localval,length,"%ld",max); |
| 79 |
error.description += localval; |
| 80 |
error.description += ")"; |
| 81 |
m_ErrorList.push_back(error); |
| 82 |
hasError = true; |
| 83 |
break; // avoid multiple error line |
| 84 |
} |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
break; |
| 89 |
} |
| 90 |
i--; |
| 91 |
} |
| 92 |
} |
| 93 |
posEndOfLine = static_cast<long int>(m_Buffer.find("\r\n",posEndOfLine+1)); |
| 94 |
} |
| 95 |
|
| 96 |
return !hasError; |
| 97 |
} |
| 98 |
|
| 99 |
} // end namespace kws |