diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2014-10-15 13:05:40 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2014-10-15 13:05:40 +0200 |
commit | 1127e07a2a6d6b3c0cbd342396e6c8af7ee54040 (patch) | |
tree | 1c21fe3da449094d0d7860fe88dc9d6ce26b183d /src | |
parent | a4c747472abc18e5fcb5a2e5caf7fe7ed1495027 (diff) | |
download | crawler-1127e07a2a6d6b3c0cbd342396e6c8af7ee54040.tar.gz crawler-1127e07a2a6d6b3c0cbd342396e6c8af7ee54040.tar.bz2 |
added a directory scanner (Linux for now)
Diffstat (limited to 'src')
-rw-r--r-- | src/libutil/FileUtils.cpp | 79 | ||||
-rwxr-xr-x | src/libutil/GNUmakefile | 3 | ||||
-rwxr-xr-x | src/libutil/Makefile.W32 | 6 |
3 files changed, 85 insertions, 3 deletions
diff --git a/src/libutil/FileUtils.cpp b/src/libutil/FileUtils.cpp new file mode 100644 index 0000000..64b1bb1 --- /dev/null +++ b/src/libutil/FileUtils.cpp @@ -0,0 +1,79 @@ +#include "util/FileUtils.hpp" + +#ifndef _WIN32 +#include <sys/types.h> +#include <dirent.h> +#include <limits.h> +#else +#define WIN32_MEAN_AND_LEAN +#include <windows.h> +#endif + +#include <sstream> +#include <stdexcept> +#include <cstring> +#include <cerrno> + +using namespace std; + +#ifndef _WIN32 +static vector<string> directory_entries_unix( const string &dir, bool absolute, bool recursive ) +{ + vector<string> files; + + DIR *d = opendir( dir.c_str( ) ); + if( d == NULL ) { + ostringstream ss; + ss << "opendir failed with '" << dir << "': " << strerror( errno ); + throw runtime_error( ss.str( ) ); + } + + struct dirent *entry = readdir( d ); + while( entry != NULL ) { + if( entry->d_type == DT_DIR ) { + if( strcmp( entry->d_name, "." ) == 0 || + strcmp( entry->d_name, ".." ) == 0 ) { + entry = readdir( d ); + continue; + } + if( recursive ) { + ostringstream ss; + ss << dir << "/" << entry->d_name; + vector<string> subfiles = directory_entries_unix( ss.str( ), absolute, recursive ); + files.insert( files.end( ), subfiles.begin( ), subfiles.end( ) ); + } + } else if( entry->d_type == DT_REG ) { + if( absolute ) { + ostringstream ss; + ss << dir << "/" << entry->d_name; + files.push_back( ss.str( ) ); + } else { + files.push_back( string( entry->d_name ) ); + } + } + entry = readdir( d ); + } + + closedir( d ); + + return files; +} +#endif + +#ifdef _WIN32 +static vector<string> directory_entries_win32( const string &/*dir*/, bool /*absolute*/, bool /* recursive */ ) +{ + vector<string> files; + + return files; +} +#endif + +UTIL_DLL_VISIBLE vector<string> directory_entries( const string &dir, bool absolute, bool recursive ) +{ +#ifdef _WIN32 + return directory_entries_win32( dir, absolute, recursive ); +#else + return directory_entries_unix( dir, absolute, recursive ); +#endif +} diff --git a/src/libutil/GNUmakefile b/src/libutil/GNUmakefile index 87cf711..b336953 100755 --- a/src/libutil/GNUmakefile +++ b/src/libutil/GNUmakefile @@ -21,7 +21,8 @@ DYNAMIC_LIB_MINOR = 0 DYNAMIC_LIB_PATCH = 0 CPP_OBJS = \ - StringUtils.o + StringUtils.o \ + FileUtils.o -include $(TOPDIR)/makefiles/gmake/sub.mk diff --git a/src/libutil/Makefile.W32 b/src/libutil/Makefile.W32 index dafcd1d..ec6dcdb 100755 --- a/src/libutil/Makefile.W32 +++ b/src/libutil/Makefile.W32 @@ -19,12 +19,14 @@ INCLUDE_LIBS = \ CPP_OBJS = \ StringUtils.obj \ win32\errormsg.obj \ - win32\stringutils.obj + win32\stringutils.obj \ + FileUtils.obj DYNAMIC_CPP_OBJS = \ StringUtils.dllobj \ win32\errormsg.dllobj \ - win32\stringutils.dllobj + win32\stringutils.dllobj \ + FileUtils.dllobj STATIC_LIB = \ utilstatic.lib |