summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-10-15 13:05:40 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2014-10-15 13:05:40 +0200
commit1127e07a2a6d6b3c0cbd342396e6c8af7ee54040 (patch)
tree1c21fe3da449094d0d7860fe88dc9d6ce26b183d
parenta4c747472abc18e5fcb5a2e5caf7fe7ed1495027 (diff)
downloadcrawler-1127e07a2a6d6b3c0cbd342396e6c8af7ee54040.tar.gz
crawler-1127e07a2a6d6b3c0cbd342396e6c8af7ee54040.tar.bz2
added a directory scanner (Linux for now)
-rw-r--r--.gitignore2
-rw-r--r--include/util/FileUtils.hpp11
-rw-r--r--src/libutil/FileUtils.cpp79
-rwxr-xr-xsrc/libutil/GNUmakefile3
-rwxr-xr-xsrc/libutil/Makefile.W326
-rwxr-xr-xtests/utils/GNUmakefile5
-rwxr-xr-xtests/utils/Makefile.W325
-rw-r--r--tests/utils/test6.DATA/adir/afile20
-rw-r--r--tests/utils/test6.DATA/adir/afile30
-rw-r--r--tests/utils/test6.DATA/adir2/adir3/afile40
-rw-r--r--tests/utils/test6.DATA/adir2/adir3/afile50
-rw-r--r--tests/utils/test6.DATA/afile0
-rw-r--r--tests/utils/test6.MUST5
-rwxr-xr-xtests/utils/test6.cpp20
14 files changed, 131 insertions, 5 deletions
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 <string>
+#include <vector>
+
+#include "util/UtilExportable.hpp"
+
+UTIL_DLL_VISIBLE std::vector<std::string> 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 <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
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
--- /dev/null
+++ b/tests/utils/test6.DATA/adir/afile2
diff --git a/tests/utils/test6.DATA/adir/afile3 b/tests/utils/test6.DATA/adir/afile3
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/utils/test6.DATA/adir/afile3
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
--- /dev/null
+++ b/tests/utils/test6.DATA/adir2/adir3/afile4
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
--- /dev/null
+++ b/tests/utils/test6.DATA/adir2/adir3/afile5
diff --git a/tests/utils/test6.DATA/afile b/tests/utils/test6.DATA/afile
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/utils/test6.DATA/afile
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 <iostream>
+#include <algorithm>
+
+using namespace std;
+
+int main( void )
+{
+ vector<string> entries = directory_entries( "test6.DATA", true, true );
+
+ sort( entries.begin( ), entries.end( ) );
+
+ vector<string>::const_iterator it, end = entries.end( );
+ for( it = entries.begin( ); it != end; it++ ) {
+ cout << (*it) << endl;
+ }
+
+ return 0;
+}