2010-03-11 20:58:59 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2010-03-11 20:58:59 +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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <cctype>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2010-07-26 21:13:34 +02:00
|
|
|
#include "filelister.h"
|
|
|
|
#include "filelister_win32.h"
|
|
|
|
#include "path.h"
|
|
|
|
|
2010-03-11 20:58:59 +01:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#ifndef __BORLANDC__
|
|
|
|
#include <shlwapi.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2010-10-16 23:32:31 +02:00
|
|
|
// 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.
|
|
|
|
|
2010-03-11 20:58:59 +01:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL MyIsDirectory(std::string path)
|
|
|
|
{
|
|
|
|
WCHAR * unicodeCleanPath = new WCHAR[path.size() + 1];
|
|
|
|
TransformAnsiToUcs2(path.c_str(), unicodeCleanPath,
|
|
|
|
(path.size() * sizeof(WCHAR)) + 1);
|
|
|
|
// See http://msdn.microsoft.com/en-us/library/bb773621(VS.85).aspx
|
|
|
|
BOOL res = PathIsDirectory(unicodeCleanPath);
|
|
|
|
delete [] unicodeCleanPath;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HANDLE MyFindFirstFile(std::string path, LPWIN32_FIND_DATA findData)
|
|
|
|
{
|
|
|
|
WCHAR * unicodeOss = new wchar_t[path.size() + 1];
|
|
|
|
TransformAnsiToUcs2(path.c_str(), unicodeOss, (path.size() + 1) * sizeof(WCHAR));
|
|
|
|
HANDLE hFind = FindFirstFile(unicodeOss, findData);
|
|
|
|
delete [] unicodeOss;
|
|
|
|
return hFind;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // defined(UNICODE)
|
|
|
|
|
|
|
|
static BOOL MyIsDirectory(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
|
2011-01-07 12:27:12 +01:00
|
|
|
return PathIsDirectory(path.c_str());
|
2010-03-11 20:58:59 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static HANDLE MyFindFirstFile(std::string path, LPWIN32_FIND_DATA findData)
|
|
|
|
{
|
|
|
|
HANDLE hFind = FindFirstFile(path.c_str(), findData);
|
|
|
|
return hFind;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(UNICODE)
|
|
|
|
|
2010-11-09 19:11:43 +01:00
|
|
|
void FileListerWin32::recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path)
|
2010-03-11 20:58:59 +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;
|
|
|
|
|
2010-07-26 21:13:34 +02:00
|
|
|
std::string cleanedPath = Path::toNativeSeparators(path);
|
2010-03-11 20:58:59 +01:00
|
|
|
|
|
|
|
oss << cleanedPath;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (MyIsDirectory(cleanedPath.c_str()))
|
2010-03-11 20:58:59 +01:00
|
|
|
{
|
|
|
|
char c = cleanedPath[ cleanedPath.size()-1 ];
|
2010-04-02 07:30:58 +02:00
|
|
|
switch (c)
|
2010-03-11 20:58:59 +01:00
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
oss << '*';
|
|
|
|
bdir << cleanedPath;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
bdir << cleanedPath.substr(0, cleanedPath.length() - 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
oss << "\\*";
|
2010-11-08 18:53:48 +01:00
|
|
|
if (cleanedPath != ".")
|
|
|
|
bdir << cleanedPath << '\\';
|
2010-03-11 20:58:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string::size_type pos;
|
|
|
|
pos = cleanedPath.find_last_of('\\');
|
2010-04-02 07:30:58 +02:00
|
|
|
if (std::string::npos != pos)
|
2010-03-11 20:58:59 +01:00
|
|
|
{
|
|
|
|
bdir << cleanedPath.substr(0, pos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WIN32_FIND_DATA ffd;
|
|
|
|
HANDLE hFind = MyFindFirstFile(oss.str(), &ffd);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (INVALID_HANDLE_VALUE == hFind)
|
2010-03-11 20:58:59 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0')
|
2010-03-11 20:58:59 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
#if defined(UNICODE)
|
|
|
|
char * ansiFfd = new char[wcslen(ffd.cFileName) + 1];
|
|
|
|
TransformUcs2ToAnsi(ffd.cFileName, ansiFfd, wcslen(ffd.cFileName) + 1);
|
2010-10-16 21:39:58 +02:00
|
|
|
#else // defined(UNICODE)
|
|
|
|
const char * ansiFfd = &ffd.cFileName[0];
|
|
|
|
if (strchr(ansiFfd,'?'))
|
2010-10-16 12:40:21 +02:00
|
|
|
{
|
2010-10-16 21:39:58 +02:00
|
|
|
ansiFfd = &ffd.cAlternateFileName[0];
|
2010-10-16 12:40:21 +02:00
|
|
|
}
|
2010-03-11 20:58:59 +01:00
|
|
|
#endif // defined(UNICODE)
|
|
|
|
|
|
|
|
std::ostringstream fname;
|
|
|
|
fname << bdir.str().c_str() << ansiFfd;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
2010-03-11 20:58:59 +01:00
|
|
|
{
|
|
|
|
// File
|
|
|
|
|
|
|
|
// If recursive is not used, accept all files given by user
|
2010-11-09 19:32:11 +01:00
|
|
|
if (sameFileName(path,ansiFfd) || FileLister::acceptFile(ansiFfd))
|
2010-07-26 21:13:34 +02:00
|
|
|
{
|
|
|
|
const std::string nativename = Path::fromNativeSeparators(fname.str());
|
|
|
|
filenames.push_back(nativename);
|
|
|
|
}
|
2010-03-11 20:58:59 +01:00
|
|
|
}
|
2010-11-09 19:32:11 +01:00
|
|
|
else
|
2010-03-11 20:58:59 +01:00
|
|
|
{
|
|
|
|
// Directory
|
2010-11-09 19:32:11 +01:00
|
|
|
getFileLister()->recursiveAddFiles(filenames, fname.str());
|
2010-03-11 20:58:59 +01:00
|
|
|
}
|
|
|
|
#if defined(UNICODE)
|
|
|
|
delete [] ansiFfd;
|
|
|
|
#endif // defined(UNICODE)
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
while (FindNextFile(hFind, &ffd) != FALSE);
|
2010-03-11 20:58:59 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (INVALID_HANDLE_VALUE != hFind)
|
2010-03-11 20:58:59 +01:00
|
|
|
{
|
|
|
|
FindClose(hFind);
|
|
|
|
hFind = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileListerWin32::sameFileName(const std::string &fname1, const std::string &fname2)
|
|
|
|
{
|
2010-02-28 19:41:16 +01:00
|
|
|
#ifdef __GNUC__
|
|
|
|
return bool(strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
|
|
|
|
#endif
|
2010-03-11 20:58:59 +01:00
|
|
|
#ifdef __BORLANDC__
|
|
|
|
return bool(stricmp(fname1.c_str(), fname2.c_str()) == 0);
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return bool(_stricmp(fname1.c_str(), fname2.c_str()) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
2010-03-16 21:06:22 +01:00
|
|
|
|
|
|
|
#endif // _WIN32
|