diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2012-08-10 14:44:17 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2012-08-10 14:44:17 +0200 |
commit | 4a6b24a567142317a24a98d2ab998f5093a581cc (patch) | |
tree | 522107a269c040e69b933fc9f69fe330703562b5 /src | |
parent | 9fe78b708868dcb5bdbfc88ed96dee18c6f1f6b3 (diff) | |
download | crawler-4a6b24a567142317a24a98d2ab998f5093a581cc.tar.gz crawler-4a6b24a567142317a24a98d2ab998f5093a581cc.tar.bz2 |
first porting attempts to Windows:
nmake support from Wolframe
module loader adapted
tests for typeinfo and template trickery
Diffstat (limited to 'src')
-rwxr-xr-x[-rw-r--r--] | src/GNUmakefile | 1 | ||||
-rwxr-xr-x[-rw-r--r--] | src/Logger.hpp | 28 | ||||
-rwxr-xr-x | src/Makefile.W32 | 55 | ||||
-rwxr-xr-x[-rw-r--r--] | src/ModuleLoader.hpp | 30 | ||||
-rwxr-xr-x[-rw-r--r--] | src/RewindInputStream.hpp | 3 | ||||
-rwxr-xr-x[-rw-r--r--] | src/TypeInfo.hpp | 14 | ||||
-rwxr-xr-x[-rw-r--r--] | src/crawlingwolf.cpp | 33 |
7 files changed, 161 insertions, 3 deletions
diff --git a/src/GNUmakefile b/src/GNUmakefile index 11bc63f..f30d750 100644..100755 --- a/src/GNUmakefile +++ b/src/GNUmakefile @@ -9,6 +9,7 @@ INCLUDE_CPPFLAGS = \ INCLUDE_LDFLAGS = \ INCLUDE_DIRS = \ + -I. INCLUDE_LIBS = \ diff --git a/src/Logger.hpp b/src/Logger.hpp index b62b799..93513d0 100644..100755 --- a/src/Logger.hpp +++ b/src/Logger.hpp @@ -9,7 +9,12 @@ #include <cstdio> #include <cstring> +#ifndef _WIN32 #include <sys/time.h> +#else +#define WIN32_MEAN_AND_LEAN +#include <windows.h> +#endif using namespace std; @@ -26,6 +31,8 @@ enum LogLevel { logDEBUG4 }; +#ifndef _WIN32 + static inline string timestamp( ) { time_t t; @@ -46,6 +53,27 @@ static inline string timestamp( ) return result; } +#else + +static inline string timestamp( ) +{ + enum { LEN = 255 }; + char buf[LEN]; + + if( GetTimeFormat( LOCALE_USER_DEFAULT, 0, 0, "HH':'mm':'ss", buf, LEN ) == 0 ) { + return "<timestamp error>"; + } + + static DWORD first = GetTickCount( ); + + char result[LEN] = { 0 }; + _snprintf( result, LEN, "%s.%03ld", buf, (long)( GetTickCount( ) - first ) % 1000 ); + + return result; +} + +#endif + template<typename T> class Log { diff --git a/src/Makefile.W32 b/src/Makefile.W32 new file mode 100755 index 0000000..ddedac3 --- /dev/null +++ b/src/Makefile.W32 @@ -0,0 +1,55 @@ +TOPDIR = .. + +#SUBDIRS = modules + +!INCLUDE $(TOPDIR)\makefiles\nmake\platform.mk + +INCLUDE_CXXFLAGS = \ + /D_WIN32_WINNT=0x504 + +INCLUDE_DIRS = \ + /I. + +INCLUDE_LDFLAGS = \ + +INCLUDE_LIBS = \ + +LOCAL_STATIC_LIB_OBJS = \ + URL.obj \ + MIMEType.obj + +LOCAL_STATIC_LIB = \ + crawlingwolf.lib + +CPP_OBJS = \ + $(LOCAL_STATIC_LIB_OBJS) + +CPP_BINS = \ + crawlingwolf.exe + +all: $(CPP_OBJS) $(CPP_BINS) + +!INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk + +crawlingwolf.exe: crawlingwolf.obj $(CPP_OBJS) + +$(LOCAL_STATIC_LIB): $(LOCAL_STATIC_LIB_OBJS) + $(LINK) /lib /nologo /out:$@ $(STATIC_LDFLAGS) $? + +local_all: $(LOCAL_STATIC_LIB) + +local_clean: + @-erase $(LOCAL_STATIC_LIB) 2>NUL + @-erase $(CPP_OBJS) 2>NUL + @-erase test.bat 2>NUL + +local_distclean: + +local_test: + +copy_prereq: + +run: copy_prereq + @-echo echo Running Crawlingwolf... > test.bat + @-echo crawlingwolf.exe >> test.bat + @-test.bat diff --git a/src/ModuleLoader.hpp b/src/ModuleLoader.hpp index 8ecccc3..e7aae37 100644..100755 --- a/src/ModuleLoader.hpp +++ b/src/ModuleLoader.hpp @@ -7,7 +7,12 @@ #include <stdexcept> #include <typeinfo> +#ifndef _WIN32 #include <dlfcn.h> +#else +#define WIN32_MEAN_AND_LEAN +#include <windows.h> +#endif #include "ModuleRegistry.hpp" @@ -16,7 +21,11 @@ template< typename Interface, typename CtorParams = NullType > struct Module { +#ifndef _WIN32 void *handle; +#else + HMODULE handle; +#endif ModuleRegistry< Interface, CtorParams > *registry; }; @@ -38,16 +47,33 @@ class BaseModuleLoader { Module< Interface, CtorParams> m; for( std::vector<string>::const_iterator it = files.begin( ); it != files.end( ); it++ ) { +#ifndef _WIN32 m.handle = dlopen( it->c_str( ), RTLD_NOW ); +#else + m.handle = LoadLibrary( it->c_str( ) ); +#endif if( !m.handle ) { +#ifndef _WIN32 throw std::runtime_error( dlerror( ) ); +#else + // TODO: error message here + throw std::runtime_error( "Module load error" ); +#endif } std::string registryName = "registry_" + demangle( typeid( Interface ) ); +#ifndef _WIN32 m.registry = static_cast< ModuleRegistry< Interface, CtorParams > *>( dlsym( m.handle, registryName.c_str( ) ) ); +#else + m.registry = ( ModuleRegistry< Interface, CtorParams > *)( GetProcAddress( m.handle, registryName.c_str( ) ) ); +#endif if( !m.registry ) { +#ifndef _WIN32 dlclose( m.handle ); +#else + (void)FreeLibrary( m.handle ); +#endif throw std::runtime_error( "missing module registry" ); } @@ -59,7 +85,11 @@ class BaseModuleLoader { { for( typename mapType::iterator it = m_modules.begin( ); it != m_modules.end( ); it++ ) { if( (*it).second.handle ) { +#ifndef _WIN32 dlclose( (*it).second.handle ); +#else + (void)FreeLibrary( (*it).second.handle ); +#endif (*it).second.handle = 0; } } diff --git a/src/RewindInputStream.hpp b/src/RewindInputStream.hpp index 9ca66b7..2b7e743 100644..100755 --- a/src/RewindInputStream.hpp +++ b/src/RewindInputStream.hpp @@ -15,8 +15,9 @@ class RewindInputStream : public std::istream { virtual void rewind( ) = 0; protected: + RewindInputStream( const URL &url ) - : m_baseUrl( url ) + : m_baseUrl( url ), std::istream( 0 ) { } diff --git a/src/TypeInfo.hpp b/src/TypeInfo.hpp index dc718ff..1e049ca 100644..100755 --- a/src/TypeInfo.hpp +++ b/src/TypeInfo.hpp @@ -40,7 +40,19 @@ std::string demangle( const std::type_info &info ) #else -#error "C++ demangling not ported!" +#ifdef _WIN32 + +std::string demangle( const std::type_info &info ) +{ + return info.name( ); +} + +#else + +#error "type.name() demangling not implemented!" + +#endif // _WIN32 + #endif // defined( __GNUG__ ) && defined( __GLIBCXX__ ) diff --git a/src/crawlingwolf.cpp b/src/crawlingwolf.cpp index 9a3a027..d5a7ba9 100644..100755 --- a/src/crawlingwolf.cpp +++ b/src/crawlingwolf.cpp @@ -15,22 +15,48 @@ #include <vector> #include <list> +#ifndef _WIN32 #include <signal.h> +#else +#define WIN32_MEAN_AND_LEAN +#endif using namespace std; static bool term = false; +#ifndef _WIN32 + static void terminate_func( int sig ) { (void)sig; term = true; } +#else + +BOOL WINAPI termHandler( DWORD ctrlType ) +{ + switch( ctrlType ){ + case CTRL_C_EVENT: + case CTRL_BREAK_EVENT: + case CTRL_CLOSE_EVENT: + case CTRL_LOGOFF_EVENT: + case CTRL_SHUTDOWN_EVENT: + term = true; + return TRUE; + default: + return FALSE; + } +} + +#endif + int main( void ) { FILELog::reportingLevel( ) = logINFO; - + +#ifndef _WIN32 struct sigaction sa; memset( &sa, 0, sizeof( struct sigaction ) ); sa.sa_handler = terminate_func; @@ -38,6 +64,11 @@ int main( void ) if( sigaction( SIGINT, &sa, NULL ) < 0 ) { cerr << "Unable to install termianation signal handler" << endl; } +#else + SetConsoleCtrlHandler( termHandler, TRUE ); +#endif + + LOG( logNOTICE ) << "Loading modules"; vector<string> normalizerModules; normalizerModules.push_back( "./modules/urlnormalizer/simpleurl/mod_urlnormalizer_simple.so" ); |