From 9b16fd7be49fff5752b6582a1b675b8bab470b5c Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Sun, 28 Feb 2010 20:41:16 +0200 Subject: [PATCH] Add Unix/Linux version of FileLister and do some cleanups. --- lib/filelister.cpp | 93 +++++++--------------------------------- lib/filelister.h | 2 +- lib/filelister_unix.cpp | 82 +++++++++++++++++++++++++++++++++++ lib/filelister_unix.h | 43 +++++++++++++++++++ lib/filelister_win32.cpp | 10 ++--- lib/filelister_win32.h | 2 - 6 files changed, 144 insertions(+), 88 deletions(-) create mode 100644 lib/filelister_unix.cpp create mode 100644 lib/filelister_unix.h diff --git a/lib/filelister.cpp b/lib/filelister.cpp index 94430ec3f..35f56d3dc 100644 --- a/lib/filelister.cpp +++ b/lib/filelister.cpp @@ -16,35 +16,36 @@ * along with this program. If not, see . */ -#include "filelister.h" -#include "fileLister_win32.h" #include #include #include #include #include #include +#include "filelister.h" #if defined(_WIN32) -#include -#ifndef __BORLANDC__ -#include -#endif +#include "fileLister_win32.h" #else // POSIX-style system -#include -#include +#include "filelister_unix.h" #endif +// We have one singleton FileLister. + static FileLister *fileLister; FileLister * getFileLister() { - if (fileLister == NULL) - { - fileLister = new FileListerWin32; - return fileLister; - } - return fileLister; + if (fileLister == NULL) + { +#if defined(_WIN32) + fileLister = new FileListerWin32; +#else // POSIX-style system + fileLister = new FileListerUnix; +#endif + return fileLister; + } + return fileLister; } std::string FileLister::simplifyPath(const char *originalPath) @@ -129,67 +130,3 @@ bool FileLister::acceptFile(const std::string &filename) 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 &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 -} diff --git a/lib/filelister.h b/lib/filelister.h index b9392578a..b8c8baecf 100644 --- a/lib/filelister.h +++ b/lib/filelister.h @@ -31,7 +31,7 @@ class FileLister public: virtual void recursiveAddFiles(std::vector &filenames, const std::string &path, bool recursive) = 0; 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); private: diff --git a/lib/filelister_unix.cpp b/lib/filelister_unix.cpp new file mode 100644 index 000000000..12e239fa9 --- /dev/null +++ b/lib/filelister_unix.cpp @@ -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 . + */ + +#include +#include +#include +#include +#include +#include + +#ifndef _WIN32 // POSIX-style system +#include +#include +#endif + +#ifndef _WIN32 + +#include "filelister.h" +#include "filelister_unix.h" + +/////////////////////////////////////////////////////////////////////////////// +////// This code is POSIX-style systems /////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +void FileLister::recursiveAddFiles(std::vector &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 +} diff --git a/lib/filelister_unix.h b/lib/filelister_unix.h new file mode 100644 index 000000000..bdd26cc6f --- /dev/null +++ b/lib/filelister_unix.h @@ -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 . + */ + +#ifndef FileListerUnixH +#define FileListerUnixH + +#include +#include +#include "filelister.h" + +/// @addtogroup Core +/// @{ + + +class FileListerUnix : public FileLister +{ +public: + virtual void recursiveAddFiles(std::vector &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 diff --git a/lib/filelister_win32.cpp b/lib/filelister_win32.cpp index c8bce5a40..22767d7ed 100644 --- a/lib/filelister_win32.cpp +++ b/lib/filelister_win32.cpp @@ -33,13 +33,6 @@ #endif - - - -/////////////////////////////////////////////////////////////////////////////// -////// This code is for Microsoft Windows ///////////////////////////////////// -/////////////////////////////////////////////////////////////////////////////// - #if defined(_WIN32) // Here is the catch: cppcheck core is Ansi code (using char type). @@ -193,6 +186,9 @@ void FileListerWin32::recursiveAddFiles(std::vector &filenames, con 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__ return bool(stricmp(fname1.c_str(), fname2.c_str()) == 0); #endif diff --git a/lib/filelister_win32.h b/lib/filelister_win32.h index 467ccca0d..df5b0cf5e 100644 --- a/lib/filelister_win32.h +++ b/lib/filelister_win32.h @@ -31,9 +31,7 @@ class FileListerWin32 : public FileLister { public: virtual void recursiveAddFiles(std::vector &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: };