2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
|
2008-12-18 22:28:57 +01:00
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
|
|
|
|
2009-01-23 21:55:06 +01:00
|
|
|
#include <cstring>
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <string>
|
2011-03-20 15:21:45 +01:00
|
|
|
#include <sstream>
|
2010-02-28 19:41:16 +01:00
|
|
|
#include "filelister.h"
|
2011-03-20 14:25:11 +01:00
|
|
|
#include "path.h"
|
2010-03-11 20:58:59 +01:00
|
|
|
|
2011-03-20 14:25:11 +01:00
|
|
|
|
|
|
|
#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. So we must convert data given to WinAPI functions from
|
|
|
|
// ANSI to Unicode. Likewise we must convert data we get from WinAPI from
|
|
|
|
// Unicode to ANSI.
|
|
|
|
|
|
|
|
// Note that qmake creates VS project files that define UNICODE but don't
|
|
|
|
// define _UNICODE! Which means e.g. TCHAR macros don't work properly.
|
|
|
|
|
|
|
|
#if defined(UNICODE)
|
|
|
|
|
|
|
|
static bool TransformUcs2ToAnsi(LPCWSTR psUcs, LPSTR psAnsi, int nAnsi)
|
|
|
|
{
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, psUcs, -1, psAnsi, nAnsi, NULL, NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool TransformAnsiToUcs2(LPCSTR psAnsi, LPWSTR psUcs, UINT nUcs)
|
|
|
|
{
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, psAnsi, -1, psUcs, nUcs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-08 11:50:54 +02:00
|
|
|
static BOOL MyIsDirectory(const std::string& path)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
|
|
|
WCHAR * unicodeCleanPath = new WCHAR[path.size() + 1];
|
2012-04-08 11:50:54 +02:00
|
|
|
TransformAnsiToUcs2(path.c_str(), unicodeCleanPath, path.size() + 1);
|
2011-03-20 14:25:11 +01:00
|
|
|
// See http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx
|
|
|
|
BOOL res = PathIsDirectory(unicodeCleanPath);
|
|
|
|
delete [] unicodeCleanPath;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-04-08 11:50:54 +02:00
|
|
|
static HANDLE MyFindFirstFile(const std::string& path, LPWIN32_FIND_DATA findData)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
|
|
|
WCHAR * unicodeOss = new wchar_t[path.size() + 1];
|
2012-04-08 11:50:54 +02:00
|
|
|
TransformAnsiToUcs2(path.c_str(), unicodeOss, path.size() + 1);
|
2011-03-20 14:25:11 +01:00
|
|
|
HANDLE hFind = FindFirstFile(unicodeOss, findData);
|
|
|
|
delete [] unicodeOss;
|
|
|
|
return hFind;
|
|
|
|
}
|
|
|
|
|
2012-04-08 11:50:54 +02:00
|
|
|
static BOOL MyFileExists(const std::string& path)
|
2011-08-06 23:11:53 +02:00
|
|
|
{
|
|
|
|
WCHAR * unicodeOss = new wchar_t[path.size() + 1];
|
2012-04-08 11:50:54 +02:00
|
|
|
TransformAnsiToUcs2(path.c_str(), unicodeOss, path.size() + 1);
|
2011-08-06 23:11:53 +02:00
|
|
|
BOOL result = PathFileExists(unicodeOss);
|
|
|
|
delete [] unicodeOss;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-03-20 14:25:11 +01:00
|
|
|
#else // defined(UNICODE)
|
|
|
|
|
2012-04-08 11:50:54 +02:00
|
|
|
static BOOL MyIsDirectory(const std::string& path)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
return (GetFileAttributes(path.c_str()) & FILE_ATTRIBUTE_DIRECTORY);
|
|
|
|
#else
|
|
|
|
// See http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx
|
|
|
|
return PathIsDirectory(path.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-04-08 11:50:54 +02:00
|
|
|
static HANDLE MyFindFirstFile(const std::string& path, LPWIN32_FIND_DATA findData)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
|
|
|
HANDLE hFind = FindFirstFile(path.c_str(), findData);
|
|
|
|
return hFind;
|
|
|
|
}
|
|
|
|
|
2012-04-08 11:50:54 +02:00
|
|
|
static BOOL MyFileExists(const std::string& path)
|
2011-08-06 23:11:53 +02:00
|
|
|
{
|
2011-10-16 07:52:54 +02:00
|
|
|
#ifdef __BORLANDC__
|
|
|
|
DWORD fa = GetFileAttributes(path.c_str());
|
|
|
|
BOOL result = FALSE;
|
|
|
|
if (fa != INVALID_FILE_ATTRIBUTES && !(fa & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
result = TRUE;
|
|
|
|
#else
|
2011-08-06 23:11:53 +02:00
|
|
|
BOOL result = PathFileExists(path.c_str());
|
2011-10-16 07:52:54 +02:00
|
|
|
#endif
|
2011-08-06 23:11:53 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-03-20 14:25:11 +01:00
|
|
|
#endif // defined(UNICODE)
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
|
|
|
// oss is the search string passed into FindFirst and FindNext.
|
|
|
|
// bdir is the base directory which is used to form pathnames.
|
|
|
|
// It always has a trailing backslash available for concatenation.
|
|
|
|
std::ostringstream bdir, oss;
|
|
|
|
|
|
|
|
std::string cleanedPath = Path::toNativeSeparators(path);
|
|
|
|
|
|
|
|
oss << cleanedPath;
|
|
|
|
|
2011-12-08 21:28:34 +01:00
|
|
|
if (MyIsDirectory(cleanedPath)) {
|
2011-03-20 14:25:11 +01:00
|
|
|
char c = cleanedPath[ cleanedPath.size()-1 ];
|
2011-10-13 20:53:06 +02:00
|
|
|
switch (c) {
|
2011-03-20 14:25:11 +01:00
|
|
|
case '\\':
|
|
|
|
oss << '*';
|
|
|
|
bdir << cleanedPath;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
bdir << cleanedPath.substr(0, cleanedPath.length() - 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
oss << "\\*";
|
|
|
|
if (cleanedPath != ".")
|
|
|
|
bdir << cleanedPath << '\\';
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-12-08 21:28:34 +01:00
|
|
|
std::string::size_type pos = cleanedPath.find_last_of('\\');
|
2011-10-13 20:53:06 +02:00
|
|
|
if (std::string::npos != pos) {
|
2011-03-20 14:25:11 +01:00
|
|
|
bdir << cleanedPath.substr(0, pos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WIN32_FIND_DATA ffd;
|
|
|
|
HANDLE hFind = MyFindFirstFile(oss.str(), &ffd);
|
|
|
|
if (INVALID_HANDLE_VALUE == hFind)
|
|
|
|
return;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
do {
|
2011-03-20 14:25:11 +01:00
|
|
|
if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#if defined(UNICODE)
|
2012-07-08 23:39:46 +02:00
|
|
|
std::size_t length = wcslen(ffd.cFileName);
|
2012-04-08 11:50:54 +02:00
|
|
|
char * ansiFfd = new char[length + 1];
|
|
|
|
TransformUcs2ToAnsi(ffd.cFileName, ansiFfd, length + 1);
|
2011-03-20 14:25:11 +01:00
|
|
|
#else // defined(UNICODE)
|
|
|
|
const char * ansiFfd = &ffd.cFileName[0];
|
2011-10-13 20:53:06 +02:00
|
|
|
if (strchr(ansiFfd,'?')) {
|
2011-03-20 14:25:11 +01:00
|
|
|
ansiFfd = &ffd.cAlternateFileName[0];
|
|
|
|
}
|
|
|
|
#endif // defined(UNICODE)
|
|
|
|
|
|
|
|
std::ostringstream fname;
|
2011-12-08 21:28:34 +01:00
|
|
|
fname << bdir.str() << ansiFfd;
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
|
2011-03-20 14:25:11 +01:00
|
|
|
// File
|
|
|
|
|
|
|
|
// If recursive is not used, accept all files given by user
|
2012-04-08 11:50:54 +02:00
|
|
|
if (Path::sameFileName(path, ansiFfd) || Path::acceptFile(ansiFfd)) {
|
2011-03-20 14:25:11 +01:00
|
|
|
const std::string nativename = Path::fromNativeSeparators(fname.str());
|
2012-02-19 17:22:59 +01:00
|
|
|
// Limitation: file sizes are assumed to fit in a 'size_t'
|
2012-04-08 11:50:54 +02:00
|
|
|
#ifdef _WIN64
|
2012-07-08 23:39:46 +02:00
|
|
|
files[nativename] = (static_cast<std::size_t>(ffd.nFileSizeHigh) << 32) | ffd.nFileSizeLow;
|
2012-04-08 11:50:54 +02:00
|
|
|
#else
|
|
|
|
files[nativename] = ffd.nFileSizeLow;
|
|
|
|
#endif
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-03-20 14:25:11 +01:00
|
|
|
// Directory
|
2012-02-19 17:22:59 +01:00
|
|
|
FileLister::recursiveAddFiles(files, fname.str());
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
|
|
|
#if defined(UNICODE)
|
|
|
|
delete [] ansiFfd;
|
|
|
|
#endif // defined(UNICODE)
|
2011-10-13 20:53:06 +02:00
|
|
|
} while (FindNextFile(hFind, &ffd) != FALSE);
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (INVALID_HANDLE_VALUE != hFind) {
|
2011-03-20 14:25:11 +01:00
|
|
|
FindClose(hFind);
|
|
|
|
hFind = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileLister::isDirectory(const std::string &path)
|
|
|
|
{
|
|
|
|
return (MyIsDirectory(path) != FALSE);
|
|
|
|
}
|
|
|
|
|
2011-08-06 15:47:57 +02:00
|
|
|
bool FileLister::fileExists(const std::string &path)
|
|
|
|
{
|
2011-12-08 21:28:34 +01:00
|
|
|
return (MyFileExists(path) == TRUE);
|
2011-08-06 15:47:57 +02:00
|
|
|
}
|
2011-03-20 14:25:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
////// This code is POSIX-style systems ///////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <glob.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
2011-04-19 13:52:32 +02:00
|
|
|
#include <sys/stat.h>
|
2011-03-20 14:25:11 +01:00
|
|
|
|
2011-10-29 19:30:33 +02:00
|
|
|
// Get absolute path. Returns empty string if path does not exist or other error.
|
|
|
|
std::string FileLister::getAbsolutePath(const std::string& path)
|
|
|
|
{
|
|
|
|
std::string absolute_path;
|
|
|
|
|
|
|
|
#ifdef PATH_MAX
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
if (realpath(path.c_str(), buf) != NULL)
|
|
|
|
absolute_path = buf;
|
|
|
|
#else
|
|
|
|
char *dynamic_buf;
|
|
|
|
if ((dynamic_buf = realpath(path.c_str(), NULL)) != NULL) {
|
|
|
|
absolute_path = dynamic_buf;
|
|
|
|
free(dynamic_buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return absolute_path;
|
|
|
|
}
|
|
|
|
|
2012-02-19 17:22:59 +01:00
|
|
|
void FileLister::recursiveAddFiles2(std::set<std::string> &seen_paths,
|
2012-07-08 23:39:46 +02:00
|
|
|
std::map<std::string, std::size_t> &files,
|
2011-03-22 00:59:53 +01:00
|
|
|
const std::string &path)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << path;
|
|
|
|
if (path.length() > 0 && path[path.length()-1] == '/')
|
|
|
|
oss << "*";
|
|
|
|
|
|
|
|
glob_t glob_results;
|
|
|
|
glob(oss.str().c_str(), GLOB_MARK, 0, &glob_results);
|
2011-10-13 20:53:06 +02:00
|
|
|
for (unsigned int i = 0; i < glob_results.gl_pathc; i++) {
|
2011-03-20 14:25:11 +01:00
|
|
|
const std::string filename = glob_results.gl_pathv[i];
|
|
|
|
if (filename == "." || filename == ".." || filename.length() == 0)
|
|
|
|
continue;
|
|
|
|
|
2011-10-29 19:40:50 +02:00
|
|
|
// Determine absolute path. Empty filename if path does not exist
|
|
|
|
const std::string absolute_path = getAbsolutePath(filename);
|
|
|
|
if (absolute_path.empty())
|
|
|
|
continue;
|
|
|
|
|
2011-10-29 19:57:12 +02:00
|
|
|
// Did we already process this entry?
|
|
|
|
if (seen_paths.find(absolute_path) != seen_paths.end())
|
|
|
|
continue;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (filename[filename.length()-1] != '/') {
|
2011-03-20 14:25:11 +01:00
|
|
|
// File
|
|
|
|
|
2012-01-06 17:31:10 +01:00
|
|
|
if (Path::sameFileName(path,filename) || Path::acceptFile(filename)) {
|
2011-10-29 19:57:12 +02:00
|
|
|
seen_paths.insert(absolute_path);
|
|
|
|
|
2011-04-19 13:52:32 +02:00
|
|
|
struct stat sb;
|
2011-10-29 19:30:33 +02:00
|
|
|
if (stat(absolute_path.c_str(), &sb) == 0) {
|
2012-02-19 17:22:59 +01:00
|
|
|
// Limitation: file sizes are assumed to fit in a 'size_t'
|
2012-07-08 23:39:46 +02:00
|
|
|
files[filename] = static_cast<std::size_t>(sb.st_size);
|
2012-02-19 17:22:59 +01:00
|
|
|
} else
|
|
|
|
files[filename] = 0;
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-03-20 14:25:11 +01:00
|
|
|
// Directory
|
2011-10-29 19:40:50 +02:00
|
|
|
|
2011-10-29 19:57:12 +02:00
|
|
|
seen_paths.insert(absolute_path);
|
2012-02-19 17:22:59 +01:00
|
|
|
recursiveAddFiles2(seen_paths, files, filename);
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
globfree(&glob_results);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
void FileLister::recursiveAddFiles(std::map<std::string, std::size_t> &files, const std::string &path)
|
2011-03-20 14:25:11 +01:00
|
|
|
{
|
2011-10-29 19:57:12 +02:00
|
|
|
std::set<std::string> seen_paths;
|
2012-02-19 17:22:59 +01:00
|
|
|
recursiveAddFiles2(seen_paths, files, path);
|
2011-03-20 14:25:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileLister::isDirectory(const std::string &path)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
glob_t glob_results;
|
|
|
|
glob(path.c_str(), GLOB_MARK, 0, &glob_results);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (glob_results.gl_pathc == 1) {
|
2011-03-20 14:25:11 +01:00
|
|
|
const std::string glob_path = glob_results.gl_pathv[0];
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!glob_path.empty() && glob_path[glob_path.size() - 1] == '/') {
|
2011-03-20 14:25:11 +01:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
globfree(&glob_results);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-06 15:47:57 +02:00
|
|
|
bool FileLister::fileExists(const std::string &path)
|
|
|
|
{
|
|
|
|
struct stat statinfo;
|
|
|
|
int result = stat(path.c_str(), &statinfo);
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (result < 0) { // Todo: should check errno == ENOENT?
|
2011-08-06 15:47:57 +02:00
|
|
|
// File not found
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if file is regular file
|
|
|
|
if ((statinfo.st_mode & S_IFMT) == S_IFREG)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-20 14:25:11 +01:00
|
|
|
#endif
|