Debian Codegraph @

kwsCheckComma.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckComma.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 #include <string.h> // strlen
16
17 namespace kws {
18
19 /** Check the number of spaces after the if keyword */
20 bool Parser::CheckComma( unsigned int before, unsigned int after )
21 {
22 m_TestsDone[COMMA] = true;
23 m_TestsDescription[COMMA] =
24 "Number of spaces for , should be:";
25 constexpr size_t length = 200;
26 char* val = new char[length];
27 snprintf(val,length," before = %d",before);
28 m_TestsDescription[COMMA] += val;
29 snprintf(val,length," : after = %d",after);
30 m_TestsDescription[COMMA] += val;
31 delete [] val;
32
33 bool hasErrors = false;
34
35 long int prevOperatorPos = 0;
36 size_t operatorPos = m_BufferNoComment.find(",", prevOperatorPos);
37 while(operatorPos != std::string::npos)
38 {
39 // Check number of space before
40 unsigned int bfr = 0;
41 auto i = static_cast<long int>(operatorPos);
42 while( i>0 && m_BufferNoComment[i-1] == ' ' )
43 {
44 ++bfr;
45 --i;
46 }
47
48 // Check number of space after
49 unsigned int aft = 0;
50 i = static_cast<long int>( operatorPos+1 );
51 while( i<(long int)m_BufferNoComment.size()
52 && m_BufferNoComment[i] == ' ' )
53 {
54 aft++;
55 i++;
56 }
57 if( i<(long int)m_BufferNoComment.size()
58 && ( m_BufferNoComment[i] == '\n' ||
59 m_BufferNoComment[i] == '\r' ||
60 m_BufferNoComment[i] == '\\' ) )
61 {
62 aft = after;
63 }
64
65 if( bfr != before || aft != after )
66 {
67 if( !this->IsBetweenQuote(operatorPos,false) )
68 {
69 Error error;
70 error.line = this->GetLineNumber(operatorPos,true);
71 error.line2 = error.line;
72 error.number = COMMA;
73 error.description = "Spaces before , = ";
74 constexpr size_t length = 200;
75 char* errorVal = new char[length];
76 snprintf(errorVal,length,"%d",bfr);
77 error.description += errorVal;
78 error.description += " v.s ";
79 snprintf(errorVal,length,"%d",before);
80 error.description += errorVal;
81 error.description += " : Spaces after , = ";
82 snprintf(errorVal,length,"%d",aft);
83 error.description += errorVal;
84 error.description += " v.s ";
85 snprintf(errorVal,length,"%d",after);
86 error.description += errorVal;
87 m_ErrorList.push_back(error);
88 delete [] errorVal;
89 hasErrors = true;
90 }
91 }
92
93 prevOperatorPos = static_cast<long int>(operatorPos)+1;
94 if( prevOperatorPos < (long int)( m_BufferNoComment.size() )-1 )
95 {
96 operatorPos = m_BufferNoComment.find(",", prevOperatorPos);
97 }
98 else
99 {
100 operatorPos = std::string::npos;
101 }
102
103 }
104
105 return !hasErrors;
106 }
107
108
109 } // end namespace kws