2008-12-10 20:11:48 +01:00
|
|
|
|
/*
|
2008-11-03 19:52:39 +01:00
|
|
|
|
* c++check - c/c++ syntax checking
|
2008-11-06 20:40:49 +01:00
|
|
|
|
* Copyright (C) 2007-2008 Daniel Marjamäki and Reijo Tomperi
|
2008-11-03 19:52:39 +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>
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
#include <glob.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
2008-12-10 20:11:48 +01:00
|
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
2008-11-03 19:52:39 +01:00
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
bool FileLister::AcceptFile( const std::string &filename )
|
|
|
|
|
{
|
|
|
|
|
std::string::size_type dotLocation = filename.find_last_of ( '.' );
|
|
|
|
|
if ( dotLocation == std::string::npos )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
std::string extension = filename.substr( dotLocation );
|
|
|
|
|
|
2008-12-09 01:49:32 +01:00
|
|
|
|
if( extension == ".cpp" ||
|
2008-11-27 20:17:15 +01:00
|
|
|
|
extension == ".cxx" ||
|
2008-11-03 19:52:39 +01:00
|
|
|
|
extension == ".cc" ||
|
|
|
|
|
extension == ".c" )
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
////// This code is for __GNUC__ only /////////////////////////////////////////
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
// gcc / cygwin..
|
|
|
|
|
void FileLister::RecursiveAddFiles( std::vector<std::string> &filenames, const std::string &path, bool recursive )
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
oss << path;
|
|
|
|
|
if( recursive )
|
|
|
|
|
{
|
|
|
|
|
if ( path.length() > 0 && path[path.length()-1] != '/' )
|
|
|
|
|
oss << "/";
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2008-12-10 20:11:48 +01:00
|
|
|
|
////// This code is for Borland C++ and Visual C++ ////////////////////////////
|
2008-11-03 19:52:39 +01:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2008-12-10 20:11:48 +01:00
|
|
|
|
#if defined(__BORLANDC__) || defined(_MSC_VER)
|
2008-11-03 19:52:39 +01:00
|
|
|
|
|
|
|
|
|
void FileLister::RecursiveAddFiles( std::vector<std::string> &filenames, const std::string &path, bool recursive )
|
|
|
|
|
{
|
2008-12-07 01:56:24 +01:00
|
|
|
|
std::ostringstream bdir, oss;
|
2008-11-24 15:01:33 +01:00
|
|
|
|
oss << path;
|
|
|
|
|
if (recursive)
|
2008-11-03 19:52:39 +01:00
|
|
|
|
{
|
2008-12-10 20:11:48 +01:00
|
|
|
|
bdir << path;
|
2008-11-24 15:01:33 +01:00
|
|
|
|
if ( path.length() > 0 && path[path.length()-1] != '/' )
|
2008-12-10 20:11:48 +01:00
|
|
|
|
{
|
|
|
|
|
bdir << "/";
|
2008-11-24 15:01:33 +01:00
|
|
|
|
oss << "/";
|
2008-12-10 20:11:48 +01:00
|
|
|
|
}
|
2008-11-03 19:52:39 +01:00
|
|
|
|
|
2008-11-24 15:01:33 +01:00
|
|
|
|
oss << "*";
|
2008-11-03 19:52:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WIN32_FIND_DATA ffd;
|
2008-11-24 15:01:33 +01:00
|
|
|
|
HANDLE hFind = FindFirstFile(oss.str().c_str(), &ffd);
|
2008-12-10 20:11:48 +01:00
|
|
|
|
if ( INVALID_HANDLE_VALUE == hFind )
|
|
|
|
|
return;
|
2008-11-24 15:01:33 +01:00
|
|
|
|
|
|
|
|
|
do
|
2008-11-03 19:52:39 +01:00
|
|
|
|
{
|
2008-12-10 20:11:48 +01:00
|
|
|
|
std::ostringstream fname;
|
|
|
|
|
fname << bdir.str().c_str() << ffd.cFileName;
|
2008-11-24 15:01:33 +01:00
|
|
|
|
|
|
|
|
|
if ( ffd.cFileName[0] == '.' || ffd.cFileName[0] == '\0' )
|
|
|
|
|
continue;
|
|
|
|
|
|
2008-12-10 20:11:48 +01:00
|
|
|
|
if ( ( ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 )
|
|
|
|
|
{
|
2008-11-24 15:01:33 +01:00
|
|
|
|
// File
|
|
|
|
|
|
|
|
|
|
// If recursive is not used, accept all files given by user
|
|
|
|
|
if ( !recursive || FileLister::AcceptFile( ffd.cFileName ) )
|
2008-12-10 20:11:48 +01:00
|
|
|
|
filenames.push_back( fname.str() );
|
|
|
|
|
}
|
2008-11-24 15:01:33 +01:00
|
|
|
|
else if ( recursive )
|
2008-11-03 19:52:39 +01:00
|
|
|
|
{
|
2008-11-24 15:01:33 +01:00
|
|
|
|
// Directory
|
|
|
|
|
fname << "/";
|
2008-12-10 20:11:48 +01:00
|
|
|
|
FileLister::RecursiveAddFiles( filenames, fname.str().c_str(), recursive );
|
2008-11-03 19:52:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-11-24 15:01:33 +01:00
|
|
|
|
while (FindNextFile(hFind, &ffd) != FALSE);
|
2008-11-03 19:52:39 +01:00
|
|
|
|
|
2008-12-10 20:11:48 +01:00
|
|
|
|
FindClose(hFind);
|
2008-11-24 15:01:33 +01:00
|
|
|
|
}
|
2008-11-03 19:52:39 +01:00
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|