forked from dlidstrom/Duplo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SourceFile.cpp
executable file
·230 lines (181 loc) · 6.4 KB
/
SourceFile.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SourceFile.h"
#include "TextFile.h"
#include "StringUtil.h"
#include <algorithm>
#include <assert.h>
unsigned int SourceFile::m_minChars = 3;
bool SourceFile::m_ignorePrepStuff = false;
SourceFile::SourceFile(const std::string& fileName ) :
m_fileName(fileName),
m_FileType(FileType::GetFileType(fileName))
{
TextFile listOfFiles(m_fileName.c_str());
std::vector<std::string> lines;
listOfFiles.readLines(lines, false);
//Get lines that the file has.
m_linesOfFile = lines.size( );
m_sourceLines.reserve( m_linesOfFile );
int openBlockComments = 0;
int index = 0;
for( auto & line : lines ){
int lineSize = static_cast<int>(line.size() );
std::string tmp;
tmp.reserve( lineSize );
// Remove block comments
if (FileType::FILETYPE_C == m_FileType ||
FileType::FILETYPE_CPP == m_FileType ||
FileType::FILETYPE_CXX == m_FileType ||
FileType::FILETYPE_H == m_FileType ||
FileType::FILETYPE_HPP == m_FileType ||
FileType::FILETYPE_JAVA == m_FileType ||
FileType::FILETYPE_CS == m_FileType ||
FileType::FILETYPE_QML == m_FileType){
for( int j=0 ; j< lineSize ; j++ ) {
if( j < ( lineSize - 1 ) && line.substr( j, 2 ) == "/*" ) {
openBlockComments++;
}
if(openBlockComments <= 0) {
tmp.push_back(line[j]);
}
if( j > 0 && j < ( lineSize ) && line.substr( j - 1, 2 ) == "*/" ) {
openBlockComments--;
}
}
}
if (FileType::FILETYPE_VB == m_FileType) {
tmp = line;
}
AddToLines( tmp , index );
index++;
}
}
void SourceFile::AddToLines( const std::string & tmp ,int index )
{
std::string cleaned;
getCleanLine(tmp, cleaned);
if(isSourceLine(cleaned)){
//m_sourceLines.push_back(new SourceLine(cleaned, index));
m_sourceLines.emplace_back( cleaned, index );
}
}
void SourceFile::getCleanLine(const std::string& line, std::string& cleanedLine){
// Remove single line comments
int lineSize = (int)line.size();
cleanedLine.reserve( lineSize );
for( int i=0;i< lineSize; i++ ) {
switch (m_FileType)
{
case FileType::FILETYPE_C :
case FileType::FILETYPE_CPP :
case FileType::FILETYPE_CXX :
case FileType::FILETYPE_H :
case FileType::FILETYPE_HPP :
case FileType::FILETYPE_JAVA:
case FileType::FILETYPE_CS :
case FileType::FILETYPE_QML :
if( ( i < lineSize -1 ) && line.substr( i, 2 ) == "//" ) {
//if(i < lineSize-1 && line[i] == '/' && line[i+1] == '/'){
return;
}
break;
case FileType::FILETYPE_VB :
if(i < lineSize-1 && line[i] == '\''){
return;
}
break;
default:
break;
}
cleanedLine.push_back(line[i]);
}
}
bool SourceFile::isSourceLine(const std::string& line){
std::string tmp = StringUtil::trim(line);
// filter min size lines
if (tmp.size() < m_minChars)
{
return false;
}
std::transform(tmp.begin(), tmp.end(), tmp.begin(), tolower);
if(m_ignorePrepStuff){
switch (m_FileType)
{
case FileType::FILETYPE_C :
case FileType::FILETYPE_CPP :
case FileType::FILETYPE_CXX :
case FileType::FILETYPE_H :
case FileType::FILETYPE_HPP :
case FileType::FILETYPE_JAVA:
case FileType::FILETYPE_QML:
if(tmp[0] == '#')
{
return false;
}
break;
case FileType::FILETYPE_CS :
{
if(tmp[0] == '#')
{
return false;
}
// look for other markers to avoid
const std::string PreProc_CS[] = { "using", "private", "protected", "public" };
for (int i=0; i<4; i++ ) {
if (tmp.find(PreProc_CS[i].c_str(), 0, PreProc_CS[i].length())!=std::string::npos)
return false;
}
}
break;
case FileType::FILETYPE_VB :
{
// look for preprocessor marker in start of string
const std::string PreProc_VB = "imports";
return std::string::npos == tmp.find(PreProc_VB.c_str(), 0, PreProc_VB.length());
}
break;
default:
break;
}
}
bool bRet = (tmp.size() >= m_minChars);
assert(bRet);
return bRet && std::find_if(tmp.begin(), tmp.end(), isalpha)!=tmp.end();
}
int SourceFile::getNumOfLinesOfCode() const
{
return static_cast<int>( m_sourceLines.size() );
}
const SourceLine& SourceFile::getLine(const int index) const
{
return m_sourceLines[index];
}
const std::string& SourceFile::getFilename () const {
return m_fileName;
}
int SourceFile::getNumOfLinesOfFile( )
{
return m_linesOfFile;
}
void SourceFile::setMinChars( unsigned int a_min_chars )
{
m_minChars = a_min_chars;
}
void SourceFile::setIgnorePreprocessor( bool a_ignore )
{
m_ignorePrepStuff = a_ignore;
}