From 1127e07a2a6d6b3c0cbd342396e6c8af7ee54040 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Wed, 15 Oct 2014 13:05:40 +0200 Subject: added a directory scanner (Linux for now) --- .gitignore | 2 + include/util/FileUtils.hpp | 11 +++++ src/libutil/FileUtils.cpp | 79 +++++++++++++++++++++++++++++++ src/libutil/GNUmakefile | 3 +- src/libutil/Makefile.W32 | 6 ++- tests/utils/GNUmakefile | 5 +- tests/utils/Makefile.W32 | 5 +- tests/utils/test6.DATA/adir/afile2 | 0 tests/utils/test6.DATA/adir/afile3 | 0 tests/utils/test6.DATA/adir2/adir3/afile4 | 0 tests/utils/test6.DATA/adir2/adir3/afile5 | 0 tests/utils/test6.DATA/afile | 0 tests/utils/test6.MUST | 5 ++ tests/utils/test6.cpp | 20 ++++++++ 14 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 include/util/FileUtils.hpp create mode 100644 src/libutil/FileUtils.cpp create mode 100644 tests/utils/test6.DATA/adir/afile2 create mode 100644 tests/utils/test6.DATA/adir/afile3 create mode 100644 tests/utils/test6.DATA/adir2/adir3/afile4 create mode 100644 tests/utils/test6.DATA/adir2/adir3/afile5 create mode 100644 tests/utils/test6.DATA/afile create mode 100644 tests/utils/test6.MUST create mode 100755 tests/utils/test6.cpp diff --git a/.gitignore b/.gitignore index b570e71..c7aca13 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,8 @@ tests/*/test1 tests/*/test2 tests/*/test3 tests/*/test4 +tests/*/test5 +tests/*/test6 src/crawl/crawl makefiles/gmake/platform.mk.vars makefiles/gmake/platform.vars diff --git a/include/util/FileUtils.hpp b/include/util/FileUtils.hpp new file mode 100644 index 0000000..a9526ec --- /dev/null +++ b/include/util/FileUtils.hpp @@ -0,0 +1,11 @@ +#ifndef __UTIL_FILE_UTILS_H +#define __UTIL_FILE_UTILS_H + +#include +#include + +#include "util/UtilExportable.hpp" + +UTIL_DLL_VISIBLE std::vector directory_entries( const std::string &dir, bool absolute = false, bool recursive = false ); + +#endif 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 +#include +#include +#else +#define WIN32_MEAN_AND_LEAN +#include +#endif + +#include +#include +#include +#include + +using namespace std; + +#ifndef _WIN32 +static vector directory_entries_unix( const string &dir, bool absolute, bool recursive ) +{ + vector 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 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 directory_entries_win32( const string &/*dir*/, bool /*absolute*/, bool /* recursive */ ) +{ + vector files; + + return files; +} +#endif + +UTIL_DLL_VISIBLE vector 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 diff --git a/tests/utils/GNUmakefile b/tests/utils/GNUmakefile index af51da7..9e34155 100755 --- a/tests/utils/GNUmakefile +++ b/tests/utils/GNUmakefile @@ -17,7 +17,8 @@ TEST_CPP_BINS = \ test2$(EXE) \ test3$(EXE) \ test4$(EXE) \ - test5$(EXE) + test5$(EXE) \ + test6$(EXE) OBJS = @@ -36,3 +37,5 @@ local_test: @./exec_test test3 "Singleton" @./exec_test test4 "StringUtils split" @./exec_test test5 "StringUtils stringicasecmp" + @./exec_test test6 "FileUtils directory_entries" + diff --git a/tests/utils/Makefile.W32 b/tests/utils/Makefile.W32 index 55a681b..23fced4 100755 --- a/tests/utils/Makefile.W32 +++ b/tests/utils/Makefile.W32 @@ -23,7 +23,8 @@ TEST_CPP_BINS = \ test2.exe \ test3.exe \ test4.exe \ - test5.exe + test5.exe \ + test6.exe OBJS = @@ -34,6 +35,7 @@ test2.exe: test2.obj test3.exe: test3.obj test4.exe: test4.obj test5.exe: test5.obj +test6.exe: test6.obj local_all: @@ -48,3 +50,4 @@ local_test: @-exec_test test3 "Singleton" @-exec_test test4 "StringUtils split" @-exec_test test5 "StringUtils stringicasecmp" + @-exec_test test6 "FileUtils directory_entries" diff --git a/tests/utils/test6.DATA/adir/afile2 b/tests/utils/test6.DATA/adir/afile2 new file mode 100644 index 0000000..e69de29 diff --git a/tests/utils/test6.DATA/adir/afile3 b/tests/utils/test6.DATA/adir/afile3 new file mode 100644 index 0000000..e69de29 diff --git a/tests/utils/test6.DATA/adir2/adir3/afile4 b/tests/utils/test6.DATA/adir2/adir3/afile4 new file mode 100644 index 0000000..e69de29 diff --git a/tests/utils/test6.DATA/adir2/adir3/afile5 b/tests/utils/test6.DATA/adir2/adir3/afile5 new file mode 100644 index 0000000..e69de29 diff --git a/tests/utils/test6.DATA/afile b/tests/utils/test6.DATA/afile new file mode 100644 index 0000000..e69de29 diff --git a/tests/utils/test6.MUST b/tests/utils/test6.MUST new file mode 100644 index 0000000..45c0670 --- /dev/null +++ b/tests/utils/test6.MUST @@ -0,0 +1,5 @@ +test6.DATA/adir/afile2 +test6.DATA/adir/afile3 +test6.DATA/adir2/adir3/afile4 +test6.DATA/adir2/adir3/afile5 +test6.DATA/afile diff --git a/tests/utils/test6.cpp b/tests/utils/test6.cpp new file mode 100755 index 0000000..839f69b --- /dev/null +++ b/tests/utils/test6.cpp @@ -0,0 +1,20 @@ +#include "FileUtils.hpp" + +#include +#include + +using namespace std; + +int main( void ) +{ + vector entries = directory_entries( "test6.DATA", true, true ); + + sort( entries.begin( ), entries.end( ) ); + + vector::const_iterator it, end = entries.end( ); + for( it = entries.begin( ); it != end; it++ ) { + cout << (*it) << endl; + } + + return 0; +} -- cgit v1.2.3-54-g00ecf