Debian Codegraph @

kwsCheckEndOfFileNewLine.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckEndOfFileNewLine.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 /** Check if the end of the file has a new line */
19 bool Parser::CheckEndOfFileNewLine()
20 {
21 m_TestsDone[EOF_NEW_LINE] = true;
22 constexpr size_t length = 255;
23 char* val = new char[length];
24 snprintf(val,length,"The file should have only one new line");
25 m_TestsDescription[EOF_NEW_LINE] = val;
26 delete [] val;
27 bool hasError = false;
28
29 // Check if the last character is an end of line
30 if(m_Buffer[m_Buffer.size()-1] != '\n')
31 {
32 Error error;
33 error.line = this->GetLineNumber(m_Buffer.size()-1,false);
34 error.line2 = error.line;
35 error.number = EOF_NEW_LINE;
36 error.description = "No new line at the end of file";
37 m_ErrorList.push_back(error);
38 hasError = true;
39 }
40
41 // Check the number empty lines at the end of the file
42 if((m_Buffer[m_Buffer.size()-1] == ' ') || (m_Buffer[m_Buffer.size()-1] == '\n'))
43 {
44 long i = static_cast<long>(m_Buffer.size())-1;
45 unsigned long numberOfEmptyLines = 0;
46 while( ((m_Buffer[i] == '\n') || (m_Buffer[i] == ' ') || (m_Buffer[i] == '\r')) && (i>0))
47 {
48 if(m_Buffer[i] == '\n')
49 {
50 numberOfEmptyLines++;
51 }
52 i-=1;
53 }
54
55 if(numberOfEmptyLines>1)
56 {
57 // Maybe should be info and not error
58 Error info;
59 info.line2 = this->GetLineNumber(m_Buffer.size()-1,false)+1;
60 info.line = info.line2-numberOfEmptyLines+2;
61 info.number = EOF_NEW_LINE;
62 info.description = "Number of empty lines at the end of files: ";
63 constexpr size_t length = 10;
64 char* localval = new char[length];
65 snprintf(localval,length,"%ld",numberOfEmptyLines);
66 info.description += localval;
67 delete [] localval;
68 m_ErrorList.push_back(info);
69 }
70 }
71
72 return !hasError;
73 }
74
75 } // end namespace kws