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
|
|
|
|
* Copyright (C) 2007-2009 Daniel Marjamäki, Reijo Tomperi, Nicolas Le Cam,
|
|
|
|
* Leandro Penz, Kimmo Varis
|
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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filelister.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
2009-01-23 21:55:06 +01:00
|
|
|
#include <cstring>
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <string>
|
2009-01-04 19:19:57 +01:00
|
|
|
#include <cctype>
|
|
|
|
#include <algorithm>
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-04 19:46:45 +01:00
|
|
|
#if defined(__GNUC__) && !defined(__MINGW32__)
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <glob.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-01-04 19:46:45 +01:00
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
|
2008-12-18 22:28:57 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
std::string FileLister::simplifyPath(const char *originalPath)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
std::string subPath = "";
|
|
|
|
std::vector<std::string> pathParts;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (; *originalPath; ++originalPath)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (*originalPath == '/')
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (subPath.length() > 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
pathParts.push_back(subPath);
|
2008-12-18 22:28:57 +01:00
|
|
|
subPath = "";
|
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
pathParts.push_back("/");
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
else
|
2009-01-05 16:49:57 +01:00
|
|
|
subPath.append(1, *originalPath);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (subPath.length() > 0)
|
|
|
|
pathParts.push_back(subPath);
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
if (pathParts[i] == ".." && i > 1)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
pathParts.erase(pathParts.begin() + i);
|
|
|
|
pathParts.erase(pathParts.begin() + i - 1);
|
|
|
|
pathParts.erase(pathParts.begin() + i - 2);
|
2008-12-18 22:28:57 +01:00
|
|
|
i = 0;
|
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (i > 0 && pathParts[i] == ".")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
pathParts.erase(pathParts.begin() + i);
|
2008-12-18 22:28:57 +01:00
|
|
|
i = 0;
|
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (pathParts[i] == "/" && i > 0 && pathParts[i-1] == "/")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
pathParts.erase(pathParts.begin() + i - 1);
|
2008-12-18 22:28:57 +01:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
2009-01-05 16:49:57 +01:00
|
|
|
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
oss << pathParts[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
bool FileLister::AcceptFile(const std::string &filename)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
std::string::size_type dotLocation = filename.find_last_of('.');
|
|
|
|
if (dotLocation == std::string::npos)
|
2008-12-18 22:28:57 +01:00
|
|
|
return false;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
std::string extension = filename.substr(dotLocation);
|
|
|
|
std::transform(extension.begin(), extension.end(), extension.begin(), static_cast < int(*)(int) > (std::tolower));
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (extension == ".cpp" ||
|
2008-12-18 22:28:57 +01:00
|
|
|
extension == ".cxx" ||
|
|
|
|
extension == ".cc" ||
|
2009-01-16 21:46:41 +01:00
|
|
|
extension == ".c" ||
|
|
|
|
extension == ".c++")
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
////// This code is for __GNUC__ only /////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-01-04 19:46:45 +01:00
|
|
|
#if defined(__GNUC__) && !defined(__MINGW32__)
|
2008-12-18 22:28:57 +01:00
|
|
|
// gcc / cygwin..
|
2009-01-05 16:49:57 +01:00
|
|
|
void FileLister::RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << path;
|
2009-01-05 16:49:57 +01:00
|
|
|
if (path.length() > 0 && path[path.length()-1] == '/')
|
2008-12-18 22:28:57 +01:00
|
|
|
oss << "*";
|
|
|
|
|
|
|
|
glob_t glob_results;
|
2009-01-05 16:49:57 +01:00
|
|
|
glob(oss.str().c_str(), GLOB_MARK, 0, &glob_results);
|
|
|
|
for (unsigned int i = 0; i < glob_results.gl_pathc; i++)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
std::string filename = glob_results.gl_pathv[i];
|
2009-01-05 16:49:57 +01:00
|
|
|
if (filename == "." || filename == ".." || filename.length() == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (filename[filename.length()-1] != '/')
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// File
|
|
|
|
|
|
|
|
// If recursive is not used, accept all files given by user
|
2009-01-05 16:49:57 +01:00
|
|
|
if (!recursive || FileLister::AcceptFile(filename))
|
|
|
|
filenames.push_back(filename);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (recursive)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Directory
|
2009-01-05 16:49:57 +01:00
|
|
|
FileLister::RecursiveAddFiles(filenames, filename, recursive);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
globfree(&glob_results);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
////// This code is for Borland C++ and Visual C++ ////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-01-04 19:46:45 +01:00
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void FileLister::RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
std::ostringstream bdir, oss;
|
2009-01-19 00:51:31 +01:00
|
|
|
std::string cleanedPath = path;
|
|
|
|
|
|
|
|
std::replace(cleanedPath.begin(), cleanedPath.end(), '\\', '/');
|
|
|
|
oss << cleanedPath;
|
|
|
|
|
|
|
|
if (cleanedPath.length() > 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
2009-01-17 20:34:11 +01:00
|
|
|
// Windows doesn't recognize "." as current folder by default
|
2009-01-19 00:51:31 +01:00
|
|
|
if (cleanedPath == ".")
|
2009-01-17 20:34:11 +01:00
|
|
|
{
|
|
|
|
oss << "/*";
|
|
|
|
}
|
2009-01-19 00:51:31 +01:00
|
|
|
else if (cleanedPath[cleanedPath.length() - 1] == '/')
|
2009-01-17 20:34:11 +01:00
|
|
|
{
|
2009-01-19 00:51:31 +01:00
|
|
|
bdir << cleanedPath;
|
2009-01-17 20:34:11 +01:00
|
|
|
oss << "*";
|
|
|
|
}
|
2009-01-19 00:51:31 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bdir << cleanedPath.substr(0, cleanedPath.rfind('/') + 1);
|
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WIN32_FIND_DATA ffd;
|
|
|
|
HANDLE hFind = FindFirstFile(oss.str().c_str(), &ffd);
|
2009-01-05 16:49:57 +01:00
|
|
|
if (INVALID_HANDLE_VALUE == hFind)
|
2008-12-18 22:28:57 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::ostringstream fname;
|
|
|
|
fname << bdir.str().c_str() << ffd.cFileName;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if (ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0')
|
2008-12-18 22:28:57 +01:00
|
|
|
continue;
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// File
|
|
|
|
|
|
|
|
// If recursive is not used, accept all files given by user
|
2009-01-05 16:49:57 +01:00
|
|
|
if (!recursive || FileLister::AcceptFile(ffd.cFileName))
|
|
|
|
filenames.push_back(fname.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2009-01-05 16:49:57 +01:00
|
|
|
else if (recursive)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Directory
|
|
|
|
fname << "/";
|
2009-01-05 16:49:57 +01:00
|
|
|
FileLister::RecursiveAddFiles(filenames, fname.str().c_str(), recursive);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (FindNextFile(hFind, &ffd) != FALSE);
|
|
|
|
|
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-23 21:25:13 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2008-12-18 22:28:57 +01:00
|
|
|
|
2009-01-23 21:25:13 +01:00
|
|
|
bool FileLister::SameFileName(const char fname1[], const char fname2[])
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
return bool(strcmp(fname1, fname2) == 0);
|
|
|
|
#endif
|
|
|
|
#ifdef __GNUC__
|
|
|
|
return bool(strcasecmp(fname1, fname2) == 0);
|
|
|
|
#endif
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
return bool(stricmp(fname1, fname2) == 0);
|
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return bool(_stricmp(fname1, fname2) == 0);
|
|
|
|
#endif
|
|
|
|
}
|