File: kwsCheckEndOfFileNewLine.cxx

package info (click to toggle)
kwstyle 1.0.1+git3224cf2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 45,404 kB
  • ctags: 145,645
  • sloc: cpp: 423,059; ansic: 9,347; xml: 588; sh: 102; php: 87; perl: 35; makefile: 7
file content (73 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download
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
/*=========================================================================

  Program:   KWStyle - Kitware Style Checker
  Module:    kwsCheckEndOfFileNewLine.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 if the end of the file has a new line */
bool Parser::CheckEndOfFileNewLine()
{
  m_TestsDone[EOF_NEW_LINE] = true;
  char* val = new char[255];
  sprintf(val,"The file should have only one new line");
  m_TestsDescription[EOF_NEW_LINE] = val;
  delete [] val;
  bool hasError = false;

  // Check if the last character is an end of line
  if(m_Buffer[m_Buffer.size()-1] != '\n')
    {
    Error error;
    error.line = this->GetLineNumber(m_Buffer.size()-1,false);
    error.line2 = error.line;
    error.number = EOF_NEW_LINE;
    error.description = "No new line at the end of file";
    m_ErrorList.push_back(error);
    hasError = true;
    }

  // Check the number empty lines at the end of the file
  if((m_Buffer[m_Buffer.size()-1] == ' ') || (m_Buffer[m_Buffer.size()-1] == '\n'))
    {
    long i = static_cast<long>(m_Buffer.size())-1;
    unsigned long numberOfEmptyLines = 0;
    while( ((m_Buffer[i] == '\n') ||  (m_Buffer[i] == ' ') || (m_Buffer[i] == '\r')) && (i>0))
      {
      if(m_Buffer[i] == '\n')
        {
        numberOfEmptyLines++;
        }
      i-=1;
      }

    if(numberOfEmptyLines>1)
      {
      // Maybe should be info and not error
      Error info;
      info.line2 = this->GetLineNumber(m_Buffer.size()-1,false)+1;
      info.line = info.line2-numberOfEmptyLines+2;
      info.number = EOF_NEW_LINE;
      info.description = "Number of empty lines at the end of files: ";
      char* localval = new char[10];
      sprintf(localval,"%ld",numberOfEmptyLines);
      info.description += localval;
      delete [] localval;
      m_ErrorList.push_back(info);
      }
    }

  return !hasError;
}

} // end namespace kws