| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckLineLength.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 character per line */ |
| 20 |
bool Parser::CheckLineLength(unsigned long max,bool checkHeader, bool doErrorCheck) |
| 21 |
{ |
| 22 |
m_TestsDone[LINE_LENGTH] = true; |
| 23 |
constexpr size_t length = 255; |
| 24 |
char* val = new char[length]; |
| 25 |
snprintf(val,length,"Line Length = %ld max chars",max); |
| 26 |
m_TestsDescription[LINE_LENGTH] = val; |
| 27 |
delete [] val; |
| 28 |
|
| 29 |
m_Positions.clear(); |
| 30 |
bool hasError = false; |
| 31 |
|
| 32 |
size_t fileSize = 0; |
| 33 |
|
| 34 |
// If we do not want to check the header |
| 35 |
if(!checkHeader) |
| 36 |
{ |
| 37 |
if (!m_HeaderFilename.empty()) { |
| 38 |
std::ifstream file; |
| 39 |
file.open(m_HeaderFilename.c_str(), std::ios::binary | std::ios::in); |
| 40 |
if(!file.is_open()) |
| 41 |
{ |
| 42 |
std::cout << "CheckLineLength() - Cannot open file: " |
| 43 |
<< m_HeaderFilename << std::endl; |
| 44 |
} |
| 45 |
else |
| 46 |
{ |
| 47 |
file.seekg(0,std::ios::end); |
| 48 |
fileSize = file.tellg(); |
| 49 |
file.close(); |
| 50 |
} |
| 51 |
} else { |
| 52 |
// we look at the first '*/' in the file which indicated the end of the current header |
| 53 |
// This assume that there is an header at some point |
| 54 |
size_t endHeader = m_Buffer.find("*/",0); |
| 55 |
if(endHeader != std::string::npos) |
| 56 |
{ |
| 57 |
fileSize = endHeader; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
size_t cc; |
| 63 |
const char* inch = m_Buffer.c_str(); |
| 64 |
size_t inStrSize = m_Buffer.size(); |
| 65 |
long int line_start = 0; |
| 66 |
long int line_end = 0; |
| 67 |
long int line_count = 1; |
| 68 |
m_Positions.push_back(0); |
| 69 |
for ( cc = 0; cc < inStrSize; ++ cc ) |
| 70 |
{ |
| 71 |
if ( *inch == '\n' ) |
| 72 |
{ |
| 73 |
m_Positions.push_back(cc); |
| 74 |
line_end = static_cast<long int>(cc); |
| 75 |
long int line_length = line_end - line_start-1; |
| 76 |
if(doErrorCheck && (line_length > (long int)max && cc>fileSize)) |
| 77 |
{ |
| 78 |
Error error; |
| 79 |
error.line = line_count; |
| 80 |
error.line2 = error.line; |
| 81 |
error.number = LINE_LENGTH; |
| 82 |
error.description = "Line length exceed "; |
| 83 |
constexpr size_t length = 21; |
| 84 |
char localval[length]; |
| 85 |
snprintf(localval,length,"%ld",line_length); |
| 86 |
error.description += localval; |
| 87 |
error.description += " (max="; |
| 88 |
snprintf(localval,length,"%ld",max); |
| 89 |
error.description += localval; |
| 90 |
error.description += ")"; |
| 91 |
m_ErrorList.push_back(error); |
| 92 |
hasError = true; |
| 93 |
} |
| 94 |
line_start = static_cast<long int>(cc) + 1; |
| 95 |
line_count ++; |
| 96 |
} |
| 97 |
inch ++; |
| 98 |
} |
| 99 |
m_Positions.push_back(cc); |
| 100 |
|
| 101 |
return !hasError; |
| 102 |
} |
| 103 |
|
| 104 |
} // end namespace kws |