| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckEmptyLines.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 succesive empty lines */ |
| 20 |
bool Parser::CheckEmptyLines(unsigned long max, bool checkEndOfFile) |
| 21 |
{ |
| 22 |
m_TestsDone[EMPTYLINES] = true; |
| 23 |
constexpr size_t length = 255; |
| 24 |
char* val = new char[length]; |
| 25 |
snprintf(val,length,"Empty lines = %ld max lines",max); |
| 26 |
m_TestsDescription[EMPTYLINES] = val; |
| 27 |
delete [] val; |
| 28 |
|
| 29 |
bool hasError = false; |
| 30 |
auto total = static_cast<unsigned long>(m_Buffer.size()); |
| 31 |
unsigned long i = 0; |
| 32 |
unsigned long j = 1; |
| 33 |
unsigned long empty = 0; |
| 34 |
while(i<total) |
| 35 |
{ |
| 36 |
// extract the line |
| 37 |
std::string line = m_Buffer.substr(i+1,m_Buffer.find("\n",i+1)-i-1); |
| 38 |
|
| 39 |
// if we have \n\r |
| 40 |
if(line.length() <= 1) |
| 41 |
{ |
| 42 |
empty++; |
| 43 |
} |
| 44 |
else |
| 45 |
{ |
| 46 |
empty = 0; |
| 47 |
} |
| 48 |
|
| 49 |
if(empty>max) |
| 50 |
{ |
| 51 |
bool valid = true; |
| 52 |
// Check if we are at the end of the file |
| 53 |
if(!checkEndOfFile) |
| 54 |
{ |
| 55 |
if(m_Buffer.find_first_not_of("\r\n ",i) == std::string::npos) |
| 56 |
{ |
| 57 |
valid = false; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if(valid) |
| 62 |
{ |
| 63 |
Error error; |
| 64 |
error.line = j; |
| 65 |
error.line2 = error.line; |
| 66 |
error.number = EMPTYLINES; |
| 67 |
error.description = "Empty lines exceed "; |
| 68 |
constexpr size_t length = 10; |
| 69 |
char* localval = new char[length]; |
| 70 |
snprintf(localval,length,"%ld",empty); |
| 71 |
error.description += localval; |
| 72 |
error.description += " (max="; |
| 73 |
delete [] localval; |
| 74 |
localval = new char[length]; |
| 75 |
snprintf(localval,length,"%ld",max); |
| 76 |
error.description += localval; |
| 77 |
error.description += ")"; |
| 78 |
delete [] localval; |
| 79 |
m_ErrorList.push_back(error); |
| 80 |
hasError = true; |
| 81 |
} |
| 82 |
} |
| 83 |
j++; |
| 84 |
i += static_cast<unsigned long>(line.length())+1; |
| 85 |
} |
| 86 |
return !hasError; |
| 87 |
} |
| 88 |
|
| 89 |
} // end namespace kws |