From 4efe0d3388dbfee1ae2c12d16801dffe67a23a92 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Tue, 8 Nov 2011 15:24:20 +0100 Subject: [PATCH] CXX uses colorgcc, ArgParse::findKeyinArgMap bugfix, new test/test_ArgParse.hpp --- .gitignore | 3 + build/CMakeLists.txt | 4 +- include/Common.hpp | 1 - other/mysqlclient_main.cpp | 129 +++++++++++++++++++++++++++++++++++++ src/ArgParse.cpp | 23 ++++--- test/CMakeLists.txt | 14 ++-- test/run_test.sh | 16 ++--- test/test_ArgParse.hpp | 73 +++++++++++++++++++++ 8 files changed, 240 insertions(+), 23 deletions(-) create mode 100644 other/mysqlclient_main.cpp create mode 100644 test/test_ArgParse.hpp diff --git a/.gitignore b/.gitignore index 479fb67..9a7ee35 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ test.out */lcov2.info */core *.gcno +*.gcda *.kdev_include_paths test/generated_main.cpp test/test @@ -18,3 +19,5 @@ leak.log leak.log.core.* gdb.out html/* +test/testCppUtils +test/testCppUtils.out diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index 560b23c..ac6ba0b 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required (VERSION 2.6) project (CPP_UTILS_LIB) +set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++") + set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow " "-ggdb -fprofile-arcs -ftest-coverage") add_definitions( ${CXX_FLAGS} ) @@ -9,4 +11,4 @@ include_directories (../include) aux_source_directory(../src CPP_UTILS_LIB_SOURCES) add_library (CppUtils SHARED ${CPP_UTILS_LIB_SOURCES}) -target_link_libraries(CppUtils pthread rt gcov) +target_link_libraries(CppUtils pthread rt gcov mysqlclient) diff --git a/include/Common.hpp b/include/Common.hpp index acb04f8..668251a 100644 --- a/include/Common.hpp +++ b/include/Common.hpp @@ -112,5 +112,4 @@ inline void StrToT( T &t, const std::string s ) ss >> t; } - #endif // COMMON_HPP diff --git a/other/mysqlclient_main.cpp b/other/mysqlclient_main.cpp new file mode 100644 index 0000000..6522671 --- /dev/null +++ b/other/mysqlclient_main.cpp @@ -0,0 +1,129 @@ +// g++ mysqlclient_main.cpp src/Logger.cpp src/MysqlClient.cpp src/ArgParse.cpp -I./include -lmysqlclient + + +#include "Logger.hpp" +#include "Common.hpp" + +#include "ArgParse.hpp" +#include "MysqlClient.hpp" +#include + +#include +#include +#include +#include + +void setUpArgs(ArgParse &argParse) +{ + argParse.addArgument("--host", + "Hostname/IP", + ArgParse::STRING ); + argParse.addArgument("-u, --user", + "Username", + ArgParse::STRING ); + argParse.addArgument("-db, --database", + "Database", + ArgParse::STRING ); + argParse.addArgument("-p, --password", + "Password", + ArgParse::STRING ); + argParse.addArgument("-port", + "Port", + ArgParse::INT ); + argParse.addArgument("-s, --unix-socket", + "Unix socket", + ArgParse::STRING ); + argParse.addArgument("-f, --client-flags", + "Client flags", + ArgParse::INT ); +} + + +bool getArgs( int argc, char* argv[], + ArgParse &argParse, + std::string &host, + std::string &user, + std::string &db, + std::string &pass, + std::string &unixsocket, + int &port, + int &clientflags ) +{ + try { + argParse.parseArgs(argc, argv); + } catch (std::runtime_error e) { + std::cerr << e.what() << std::endl << std::endl; + return false; + } + + argParse.argAsString("--host", host); + argParse.argAsString("-u, --user", user); + argParse.argAsString("-db, --database", db); + argParse.argAsString("-p, --password", pass); + argParse.argAsInt("-port", port); + argParse.argAsString("-s, --unix-socket", unixsocket); + argParse.argAsInt("-f, --client-flags", clientflags); + + return true; +} + + +void printResults(std::list &results) +{ + LOG ( Logger::DEBUG, std::string("Got query result number of rows: "). + append(TToStr(results.size())).c_str() ); + + for (std::list::const_iterator it = results.begin(); + it != results.end(); + ++it ) { + LOG ( Logger::DEBUG, (*it).c_str() ); + } +} + + +int main(int argc, char* argv[] ) +{ + Logger::createInstance(); + Logger::init(std::cout); + Logger::setLogLevel(Logger::FINEST); + + ArgParse argParse("Simple MySQL client", + "Report bugs to: denes.matetelki@gmail.com"); + + setUpArgs(argParse); + + std::string host, user, db, pass, unixsocket; + int port, clientflags; + if ( !getArgs(argc, argv, + argParse, + host, user, db, pass, unixsocket, + port, clientflags ) || + argParse.foundArg("-h, --help") ) + { + std::cout << argParse.usage() << std::endl; + return 1; + } + + init_client_errs(); + + MysqlClient mysqlClient ( + argParse.foundArg("--host") ? host.c_str() : NULL, + argParse.foundArg("-u, --user") ? user.c_str() : NULL, + argParse.foundArg("-p, --password") ? pass.c_str() : NULL, + argParse.foundArg("-db, --database") ? db .c_str() : NULL, + argParse.foundArg("-port") ? port : 0, + argParse.foundArg("-s, --unix-socket") ? unixsocket.c_str() : NULL, + argParse.foundArg("-f, --client-flags") ? clientflags : 0 ); + + std::list results; + if ( !mysqlClient.querty("SELECT * FROM seats", results) ) { + LOG ( Logger::ERR, "Could not execute query." ); + } else { + printResults(results); + } + + finish_client_errs(); + + Logger::destroy(); + return 0; +} diff --git a/src/ArgParse.cpp b/src/ArgParse.cpp index 0c586f1..adea27b 100644 --- a/src/ArgParse.cpp +++ b/src/ArgParse.cpp @@ -1,5 +1,6 @@ #include "ArgParse.hpp" +#include "Common.hpp" #include #include @@ -381,17 +382,23 @@ ArgParse::argCompare::operator()(const std::string a,const std::string b) const std::map::iterator ArgParse::findKeyinArgMap(const std::string param) { - ArgMap::iterator it; - for( it = m_params.begin(); it != m_params.end(); ++it) { + for( ArgMap::iterator it = m_params.begin(); it != m_params.end(); ++it) { - // if it's the short param at the beginning - if ( (*it).first.find(param) == 0 ) - return it; + std::string first; + std::string second; - // or is it the long after the comma? size_t commaPos = (*it).first.find(","); - if ( commaPos != std::string::npos && - (*it).first.find( param, commaPos+1 ) != std::string::npos ) + if ( commaPos == std::string::npos ) { + first = (*it).first; + } else { + first = (*it).first.substr(0, commaPos); + second = (*it).first.substr(commaPos+1); + } + + first.erase(std::remove_if(first.begin(), first.end(), isspace), first.end()); + second.erase(std::remove_if(second.begin(), second.end(), isspace), second.end()); + + if ( param == first || param == second ) return it; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 32f41e6..47ee911 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,6 @@ + +set(CMAKE_CXX_COMPILER "/usr/lib/colorgcc/bin/g++") + set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow " "-ggdb -fprofile-arcs -ftest-coverage --std=c++0x " ) @@ -15,10 +18,11 @@ if(CXXTEST_FOUND) generated_main.cpp Fixture.hpp - test_Singelton_call_once.hpp -# test_Singleton.hpp -# test_Singleton_meyers.hpp -# test_Singleton_DCLP.hpp + test_ArgParse.hpp +# test_Singelton_call_once.hpp +# test_Singleton.hpp +# test_Singleton_meyers.hpp +# test_Singleton_DCLP.hpp # test_Mutex.hpp # test_ScopedLock.hpp # test_ConditionalVariable.hpp @@ -35,4 +39,4 @@ endif() add_custom_target( test COMMAND ./run_test.sh ./testCppUtils DEPENDS testCppUtils -) \ No newline at end of file +) diff --git a/test/run_test.sh b/test/run_test.sh index d225319..1d4b474 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -23,14 +23,14 @@ post="\E[00;00m" ulimit -c unlimited -# test=$1 -test=./test - -# if [ $# -ne 1 ] -# then -# echo "Usage: `basename $0` " -# exit 1 -# fi +if [ $# -ne 1 ] +then + echo "Usage: `basename $0` " + exit 1 +fi + +test=$1 +# test=./test if [ ! -e $test ]; then echo -e "The parameter binary: $test does not exists" diff --git a/test/test_ArgParse.hpp b/test/test_ArgParse.hpp new file mode 100644 index 0000000..a3e8984 --- /dev/null +++ b/test/test_ArgParse.hpp @@ -0,0 +1,73 @@ +#include + +#include "Common.hpp" +#include "Fixture.hpp" + + +#define private public +#include "ArgParse.hpp" + +#include + +class TestArgParseSuite : public CxxTest::TestSuite +{ + + +public: + + void testBasic( void ) + { + TEST_HEADER; + + ArgParse argParse("intro", "outro"); + + TS_ASSERT_EQUALS( argParse.usage(), std::string( + "intro\n\n" + "usage: [OPTION]\n\n" + "Options:\n" + "-h, --help Prints this help message\n\n" + "outro\n") ); + } + + void testfindKeyinArgMap ( void ) + { + TEST_HEADER; + + ArgParse argParse("intro", "outro"); + + argParse.addArgument("-s, --unix-rocket", + "Unix rocket", + ArgParse::STRING ); + + TS_ASSERT_DIFFERS ( argParse.findKeyinArgMap("-s"), argParse.m_params.end() ); + TS_ASSERT_DIFFERS ( argParse.findKeyinArgMap("--unix-rocket"), argParse.m_params.end() ); + + TS_ASSERT_EQUALS( argParse.findKeyinArgMap("-u"), argParse.m_params.end() ); + TS_ASSERT_EQUALS( argParse.findKeyinArgMap("-r"), argParse.m_params.end() ); + } + + void asdtestAddArgs( void ) + { + TEST_HEADER; + + ArgParse argParse("intro", "outro"); + + argParse.addArgument("-s, --unix-socket", + "Unix socket", + ArgParse::STRING ); + argParse.addArgument("-u, --user", + "Username", + ArgParse::STRING ); + + int argc = 3; + char *argv[] = { (char*)"test", + (char*)"-u", + (char*)"username" }; + + argParse.parseArgs(argc, argv); + + TS_ASSERT_EQUALS( argParse.isArg("-s, --unix-socket"), false ); + TS_ASSERT_EQUALS( argParse.isArg("-u, --user"), true ); + } + +};