-
Notifications
You must be signed in to change notification settings - Fork 30
/
kwsCheckEmptyLines.cxx
89 lines (77 loc) · 2.37 KB
/
kwsCheckEmptyLines.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
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckEmptyLines.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"
namespace kws {
/** Check the number of succesive empty lines */
bool Parser::CheckEmptyLines(unsigned long max, bool checkEndOfFile)
{
m_TestsDone[EMPTYLINES] = true;
constexpr size_t length = 255;
char* val = new char[length];
snprintf(val,length,"Empty lines = %ld max lines",max);
m_TestsDescription[EMPTYLINES] = val;
delete [] val;
bool hasError = false;
auto total = static_cast<unsigned long>(m_Buffer.size());
unsigned long i = 0;
unsigned long j = 1;
unsigned long empty = 0;
while(i<total)
{
// extract the line
std::string line = m_Buffer.substr(i+1,m_Buffer.find("\n",i+1)-i-1);
// if we have \n\r
if(line.length() <= 1)
{
empty++;
}
else
{
empty = 0;
}
if(empty>max)
{
bool valid = true;
// Check if we are at the end of the file
if(!checkEndOfFile)
{
if(m_Buffer.find_first_not_of("\r\n ",i) == std::string::npos)
{
valid = false;
}
}
if(valid)
{
Error error;
error.line = j;
error.line2 = error.line;
error.number = EMPTYLINES;
error.description = "Empty lines exceed ";
constexpr size_t length = 10;
char* localval = new char[length];
snprintf(localval,length,"%ld",empty);
error.description += localval;
error.description += " (max=";
delete [] localval;
localval = new char[length];
snprintf(localval,length,"%ld",max);
error.description += localval;
error.description += ")";
delete [] localval;
m_ErrorList.push_back(error);
hasError = true;
}
}
j++;
i += static_cast<unsigned long>(line.length())+1;
}
return !hasError;
}
} // end namespace kws