Add Unix/Linux version of FileLister and do some cleanups.
This commit is contained in:
parent
27d06855b4
commit
9b16fd7be4
|
@ -16,32 +16,33 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "filelister.h"
|
|
||||||
#include "fileLister_win32.h"
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include "filelister.h"
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <windows.h>
|
#include "fileLister_win32.h"
|
||||||
#ifndef __BORLANDC__
|
|
||||||
#include <shlwapi.h>
|
|
||||||
#endif
|
|
||||||
#else // POSIX-style system
|
#else // POSIX-style system
|
||||||
#include <glob.h>
|
#include "filelister_unix.h"
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// We have one singleton FileLister.
|
||||||
|
|
||||||
static FileLister *fileLister;
|
static FileLister *fileLister;
|
||||||
|
|
||||||
FileLister * getFileLister()
|
FileLister * getFileLister()
|
||||||
{
|
{
|
||||||
if (fileLister == NULL)
|
if (fileLister == NULL)
|
||||||
{
|
{
|
||||||
|
#if defined(_WIN32)
|
||||||
fileLister = new FileListerWin32;
|
fileLister = new FileListerWin32;
|
||||||
|
#else // POSIX-style system
|
||||||
|
fileLister = new FileListerUnix;
|
||||||
|
#endif
|
||||||
return fileLister;
|
return fileLister;
|
||||||
}
|
}
|
||||||
return fileLister;
|
return fileLister;
|
||||||
|
@ -129,67 +130,3 @@ bool FileLister::acceptFile(const std::string &filename)
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
////// This code is for Microsoft Windows /////////////////////////////////////
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
|
|
||||||
#else // other than _WIN32
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
////// This code is POSIX-style systems ///////////////////////////////////////
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive)
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
for (unsigned int i = 0; i < glob_results.gl_pathc; i++)
|
|
||||||
{
|
|
||||||
std::string filename = glob_results.gl_pathv[i];
|
|
||||||
if (filename == "." || filename == ".." || filename.length() == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (filename[filename.length()-1] != '/')
|
|
||||||
{
|
|
||||||
// File
|
|
||||||
|
|
||||||
// If recursive is not used, accept all files given by user
|
|
||||||
if (!recursive || FileLister::acceptFile(filename))
|
|
||||||
filenames.push_back(filename);
|
|
||||||
}
|
|
||||||
else if (recursive)
|
|
||||||
{
|
|
||||||
// Directory
|
|
||||||
FileLister::recursiveAddFiles(filenames, filename, recursive);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
globfree(&glob_results);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
bool FileLister::sameFileName(const std::string &fname1, const std::string &fname2)
|
|
||||||
{
|
|
||||||
#if defined(__linux__) || defined(__sun)
|
|
||||||
return bool(fname1 == fname2);
|
|
||||||
#endif
|
|
||||||
#ifdef __GNUC__
|
|
||||||
return bool(strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
|
|
||||||
#endif
|
|
||||||
#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
|
|
||||||
}
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ class FileLister
|
||||||
public:
|
public:
|
||||||
virtual void recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive) = 0;
|
virtual void recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive) = 0;
|
||||||
virtual std::string simplifyPath(const char *originalPath);
|
virtual std::string simplifyPath(const char *originalPath);
|
||||||
virtual bool sameFileName(const std::string &fname1, const std::string &fname2);
|
virtual bool sameFileName(const std::string &fname1, const std::string &fname2) = 0;
|
||||||
virtual bool acceptFile(const std::string &filename);
|
virtual bool acceptFile(const std::string &filename);
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
/*
|
||||||
|
* Cppcheck - A tool for static C/C++ code analysis
|
||||||
|
* Copyright (C) 2007-2009 Daniel Marjamäki and 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 <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
|
#include <cctype>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#ifndef _WIN32 // POSIX-style system
|
||||||
|
#include <glob.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
|
||||||
|
#include "filelister.h"
|
||||||
|
#include "filelister_unix.h"
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
////// This code is POSIX-style systems ///////////////////////////////////////
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void FileLister::recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
for (unsigned int i = 0; i < glob_results.gl_pathc; i++)
|
||||||
|
{
|
||||||
|
std::string filename = glob_results.gl_pathv[i];
|
||||||
|
if (filename == "." || filename == ".." || filename.length() == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (filename[filename.length()-1] != '/')
|
||||||
|
{
|
||||||
|
// File
|
||||||
|
|
||||||
|
// If recursive is not used, accept all files given by user
|
||||||
|
if (!recursive || FileLister::acceptFile(filename))
|
||||||
|
filenames.push_back(filename);
|
||||||
|
}
|
||||||
|
else if (recursive)
|
||||||
|
{
|
||||||
|
// Directory
|
||||||
|
FileLister::recursiveAddFiles(filenames, filename, recursive);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
globfree(&glob_results);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
bool FileListerUnix::sameFileName(const std::string &fname1, const std::string &fname2)
|
||||||
|
{
|
||||||
|
#if defined(__linux__) || defined(__sun)
|
||||||
|
return bool(fname1 == fname2);
|
||||||
|
#endif
|
||||||
|
#ifdef __GNUC__
|
||||||
|
return bool(strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
|
||||||
|
#endif
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* Cppcheck - A tool for static C/C++ code analysis
|
||||||
|
* Copyright (C) 2007-2009 Daniel Marjamäki and 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FileListerUnixH
|
||||||
|
#define FileListerUnixH
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "filelister.h"
|
||||||
|
|
||||||
|
/// @addtogroup Core
|
||||||
|
/// @{
|
||||||
|
|
||||||
|
|
||||||
|
class FileListerUnix : public FileLister
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
|
||||||
|
// virtual static std::string simplifyPath(const char *originalPath);
|
||||||
|
virtual bool sameFileName(const std::string &fname1, const std::string &fname2);
|
||||||
|
// virtual static bool acceptFile(const std::string &filename);
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @}
|
||||||
|
|
||||||
|
#endif // #ifndef FileListerUnixH
|
|
@ -33,13 +33,6 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
////// This code is for Microsoft Windows /////////////////////////////////////
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
// Here is the catch: cppcheck core is Ansi code (using char type).
|
// Here is the catch: cppcheck core is Ansi code (using char type).
|
||||||
|
@ -193,6 +186,9 @@ void FileListerWin32::recursiveAddFiles(std::vector<std::string> &filenames, con
|
||||||
|
|
||||||
bool FileListerWin32::sameFileName(const std::string &fname1, const std::string &fname2)
|
bool FileListerWin32::sameFileName(const std::string &fname1, const std::string &fname2)
|
||||||
{
|
{
|
||||||
|
#ifdef __GNUC__
|
||||||
|
return bool(strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
|
||||||
|
#endif
|
||||||
#ifdef __BORLANDC__
|
#ifdef __BORLANDC__
|
||||||
return bool(stricmp(fname1.c_str(), fname2.c_str()) == 0);
|
return bool(stricmp(fname1.c_str(), fname2.c_str()) == 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -31,9 +31,7 @@ class FileListerWin32 : public FileLister
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
|
virtual void recursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
|
||||||
// virtual static std::string simplifyPath(const char *originalPath);
|
|
||||||
virtual bool sameFileName(const std::string &fname1, const std::string &fname2);
|
virtual bool sameFileName(const std::string &fname1, const std::string &fname2);
|
||||||
// virtual static bool acceptFile(const std::string &filename);
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue