FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppcheck/cli/filelister.cpp at main · codingman/cppcheck · GitHub
codingman
/
cppcheck
Public
forked from
cppcheck-opensource/cppcheck
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppcheck
/
cli
/
filelister.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
289 lines (244 loc) · 9.85 KB
Breadcrumbs
cppcheck
/
cli
/
filelister.cpp
Copy path
File metadata and controls
289 lines (244 loc) · 9.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2021 Cppcheck team.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#
include
"
filelister.h
"
#
include
"
path.h
"
#
include
"
pathmatch.h
"
#
include
"
utils.h
"
#
include
<
cstddef
>
#
include
<
cstring
>
//
fix NAME_MAX not found on macOS GCC8.1
#
include
<
climits
>
#
ifdef
_WIN32
//
/////////////////////////////////////////////////////////////////////////////
//
//// This code is WIN32 systems /////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
#
include
<
windows.h
>
#
ifndef
__BORLANDC__
#
include
<
shlwapi.h
>
#
endif
//
Here is the catch: cppcheck core is Ansi code (using char type).
//
When compiling Unicode targets WinAPI automatically uses *W Unicode versions
//
of called functions. Thus, we explicitly call *A versions of the functions.
static
BOOL
myIsDirectory
(
const
std::string& path)
{
#
ifdef
__BORLANDC__
return
(
GetFileAttributes
(path.
c_str
()) &
FILE_ATTRIBUTE_DIRECTORY
);
#
else
//
See http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx
return
PathIsDirectoryA
(path.
c_str
());
#
endif
}
static
HANDLE
myFindFirstFile
(
const
std::string& path,
LPWIN32_FIND_DATAA
findData)
{
HANDLE
hFind =
FindFirstFileA
(path.
c_str
(), findData);
return
hFind;
}
static
BOOL
myFileExists
(
const
std::string& path)
{
#
ifdef
__BORLANDC__
DWORD
fa =
GetFileAttributes
(path.
c_str
());
BOOL
result =
FALSE
;
if
(fa !=
INVALID_FILE_ATTRIBUTES
&& !(fa &
FILE_ATTRIBUTE_DIRECTORY
))
result =
TRUE
;
#
else
const
BOOL
result =
PathFileExistsA
(path.
c_str
()) && !
PathIsDirectoryA
(path.
c_str
());
#
endif
return
result;
}
std::string
FileLister::recursiveAddFiles
(std::map<std::string, std::
size_t
> &files,
const
std::string &path,
const
std::set<std::string> &extra,
const
PathMatch& ignored)
{
return
addFiles
(files, path, extra,
true
, ignored);
}
std::string
FileLister::addFiles
(std::map<std::string, std::
size_t
> &files,
const
std::string &path,
const
std::set<std::string> &extra,
bool
recursive,
const
PathMatch& ignored)
{
const
std::string cleanedPath =
Path::toNativeSeparators
(path);
//
basedir is the base directory which is used to form pathnames.
//
It always has a trailing backslash available for concatenation.
std::string basedir;
//
searchPattern is the search string passed into FindFirst and FindNext.
std::string searchPattern = cleanedPath;
//
The user wants to check all files in a dir
const
bool
checkAllFilesInDir = (
myIsDirectory
(cleanedPath) !=
FALSE
);
if
(checkAllFilesInDir) {
const
char
c = cleanedPath.
back
();
switch
(c) {
case
'
\\
'
:
searchPattern +=
'
*
'
;
basedir = cleanedPath;
break
;
case
'
*
'
:
basedir = cleanedPath.
substr
(
0
, cleanedPath.
length
() -
1
);
break
;
default
:
searchPattern +=
"
\\
*
"
;
if
(cleanedPath !=
"
.
"
)
basedir = cleanedPath +
'
\\
'
;
}
}
else
{
const
std::string::size_type pos = cleanedPath.
find_last_of
(
'
\\
'
);
if
(std::string::npos != pos) {
basedir = cleanedPath.
substr
(
0
, pos +
1
);
}
}
WIN32_FIND_DATAA
ffd;
HANDLE
hFind =
myFindFirstFile
(searchPattern, &ffd);
if
(
INVALID_HANDLE_VALUE
== hFind)
return
"
"
;
do
{
if
(ffd.
cFileName
[
0
] ==
'
.
'
|| ffd.
cFileName
[
0
] ==
'
\0
'
)
continue
;
const
char
* ansiFfd = ffd.
cFileName
;
if
(
std::strchr
(ansiFfd,
'
?
'
)) {
ansiFfd = ffd.
cAlternateFileName
;
}
const
std::string
fname
(basedir + ansiFfd);
if
((ffd.
dwFileAttributes
&
FILE_ATTRIBUTE_DIRECTORY
) ==
0
) {
//
File
if
((!checkAllFilesInDir ||
Path::acceptFile
(fname, extra)) && !ignored.
match
(fname)) {
const
std::string nativename =
Path::fromNativeSeparators
(fname);
//
Limitation: file sizes are assumed to fit in a 'size_t'
#
ifdef
_WIN64
files[nativename] = (
static_cast
<std::
size_t
>(ffd.
nFileSizeHigh
) <<
32
) | ffd.
nFileSizeLow
;
#
else
files[nativename] = ffd.
nFileSizeLow
;
#
endif
}
}
else
{
//
Directory
if
(recursive) {
if
(!ignored.
match
(fname)) {
std::string err =
FileLister::recursiveAddFiles
(files, fname, extra, ignored);
if
(!err.
empty
())
return
err;
}
}
}
}
while
(
FindNextFileA
(hFind, &ffd) !=
FALSE
);
FindClose
(hFind);
return
"
"
;
}
bool
FileLister::isDirectory
(
const
std::string &path)
{
return
(
myIsDirectory
(path) !=
FALSE
);
}
bool
FileLister::fileExists
(
const
std::string &path)
{
return
(
myFileExists
(path) !=
FALSE
);
}
#
else
//
/////////////////////////////////////////////////////////////////////////////
//
//// This code is POSIX-style systems ///////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////
#
if
defined(__CYGWIN__)
#
undef
__STRICT_ANSI__
#
endif
#
include
<
dirent.h
>
#
include
<
sys/stat.h
>
#
include
<
cerrno
>
#
ifndef
NAME_MAX
#
ifdef
MAXNAMLEN
#
define
NAME_MAX
MAXNAMLEN
#
endif
#
endif
static
std::string
addFiles2
(std::map<std::string, std::
size_t
> &files,
const
std::string &path,
const
std::set<std::string> &extra,
bool
recursive,
const
PathMatch& ignored
)
{
struct
stat
file_stat;
if
(
stat
(path.
c_str
(), &file_stat) != -
1
) {
if
((file_stat.
st_mode
&
S_IFMT
) ==
S_IFDIR
) {
DIR
* dir =
opendir
(path.
c_str
());
if
(!dir)
return
"
"
;
dirent * dir_result;
//
make sure we reserve enough space for the readdir_r() buffer;
//
according to POSIX:
//
The storage pointed to by entry shall be large enough for a
//
dirent with an array of char d_name members containing at
//
least {NAME_MAX}+1 elements.
//
on some platforms, d_name is not a static sized-array but
//
a pointer to space usually reserved right after the dirent
//
struct; the union here allows to reserve the space and to
//
provide a pointer to the right type that can be passed where
//
needed without casts
union
{
dirent entry;
char
buf[
sizeof
(*dir_result) + (
sizeof
(dir_result->
d_name
) >
1
?
0
:
NAME_MAX
+
1
)];
} dir_result_buffer;
UNUSED
(dir_result_buffer.
buf
);
//
do not trigger cppcheck itself on the "unused buf"
std::string new_path;
new_path.
reserve
(path.
length
() +
100
);
//
prealloc some memory to avoid constant new/deletes in loop
while
((
readdir_r
(dir, &dir_result_buffer.
entry
, &dir_result) ==
0
) && (dir_result !=
nullptr
)) {
if
((
std::strcmp
(dir_result->
d_name
,
"
.
"
) ==
0
) ||
(
std::strcmp
(dir_result->
d_name
,
"
..
"
) ==
0
))
continue
;
new_path = path +
'
/
'
+ dir_result->
d_name
;
#
if
defined(_DIRENT_HAVE_D_TYPE) || defined(_BSD_SOURCE)
bool
path_is_directory = (dir_result->
d_type
==
DT_DIR
|| (dir_result->
d_type
==
DT_UNKNOWN
&&
FileLister::isDirectory
(new_path)));
#
else
bool
path_is_directory =
FileLister::isDirectory
(new_path);
#
endif
if
(path_is_directory) {
if
(recursive && !ignored.
match
(new_path)) {
std::string err =
addFiles2
(files, new_path, extra, recursive, ignored);
if
(!err.
empty
())
return
err;
}
}
else
{
if
(
Path::acceptFile
(new_path, extra) && !ignored.
match
(new_path)) {
if
(
stat
(new_path.
c_str
(), &file_stat) != -
1
)
files[new_path] = file_stat.
st_size
;
else
return
"
Can't stat
"
+ new_path +
"
errno:
"
+
std::to_string
(errno);
}
}
}
closedir
(dir);
}
else
files[path] = file_stat.
st_size
;
}
return
"
"
;
}
std::string
FileLister::recursiveAddFiles
(std::map<std::string, std::
size_t
> &files,
const
std::string &path,
const
std::set<std::string> &extra,
const
PathMatch& ignored)
{
return
addFiles
(files, path, extra,
true
, ignored);
}
std::string
FileLister::addFiles
(std::map<std::string, std::
size_t
> &files,
const
std::string &path,
const
std::set<std::string> &extra,
bool
recursive,
const
PathMatch& ignored)
{
if
(!path.
empty
()) {
std::string corrected_path = path;
if
(
endsWith
(corrected_path,
'
/
'
))
corrected_path.
erase
(corrected_path.
end
() -
1
);
return
addFiles2
(files, corrected_path, extra, recursive, ignored);
}
return
"
"
;
}
bool
FileLister::isDirectory
(
const
std::string &path)
{
struct
stat
file_stat;
return
(
stat
(path.
c_str
(), &file_stat) != -
1
&& (file_stat.
st_mode
&
S_IFMT
) ==
S_IFDIR
);
}
bool
FileLister::fileExists
(
const
std::string &path)
{
struct
stat
file_stat;
return
(
stat
(path.
c_str
(), &file_stat) != -
1
&& (file_stat.
st_mode
&
S_IFMT
) ==
S_IFREG
);
}
#
endif
Back
|
FazBrowse Home
|
New Git URL