From 0ca0f1c64080faa9d82fa59f34f23089fcd1344a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A1n=20Kupec?= Date: Sat, 6 Mar 2010 09:27:31 +0100 Subject: [PATCH] PackageArgs added, preprocess() added. --- src/CMakeLists.txt | 2 + src/PackageArgs.cc | 96 ++++++++++++++++++++++++++++++++++++++++++ src/PackageArgs.h | 44 +++++++++++++++++++ tests/CMakeLists.txt | 2 + tests/PackageArgs_test.cc | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 src/PackageArgs.cc create mode 100644 src/PackageArgs.h create mode 100644 tests/PackageArgs_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aff8103..05de037 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,7 @@ SET (zypper_HEADERS install.h update.h solve-commit.h + PackageArgs.h Summary.h callbacks/keyring.h callbacks/media.h @@ -41,6 +42,7 @@ SET( zypper_SRCS install.cc update.cc solve-commit.cc + PackageArgs.cc Summary.cc callbacks/media.cc ${zypper_HEADERS} diff --git a/src/PackageArgs.cc b/src/PackageArgs.cc new file mode 100644 index 0000000..e51a610 --- /dev/null +++ b/src/PackageArgs.cc @@ -0,0 +1,96 @@ +/*---------------------------------------------------------------------------*\ + ____ _ _ __ _ __ ___ _ _ + |_ / || | '_ \ '_ \/ -_) '_| + /__|\_, | .__/ .__/\___|_| + |__/|_| |_| +\*---------------------------------------------------------------------------*/ + +/** \file PackageArgs.cc + * + */ + +#include +#include "zypp/base/Logger.h" + +#include "PackageArgs.h" +#include "Zypper.h" + + +using namespace std; + + +PackageArgs::PackageArgs() + : zypper(*Zypper::instance()) +{ + preprocess(zypper.arguments()); +} + +PackageArgs::PackageArgs(const vector & args) + : zypper(*Zypper::instance()) +{ + preprocess(args); +} + +void PackageArgs::preprocess(const vector & args) +{ + vector::size_type argc = args.size(); + + string tmp; + string arg; + bool op = false; + for(unsigned i = 0; i < argc; ++i) + { + tmp = args[i]; + + if (op) + { + arg += tmp; + op = false; + tmp.clear(); + } + // standalone operator + else if (tmp == "=" || tmp == "==" || tmp == "<" + || tmp == ">" || tmp == "<=" || tmp == ">=") + { + // not at the start or the end + if (i && i < argc - 1) + op = true; + } + // operator at the end of a random string, e.g. 'zypper=' + else if (tmp.find_last_of("=<>") == tmp.size() - 1 && i < argc - 1) + { + if (!arg.empty()) + _args.insert(arg); + arg = tmp; + op = true; + continue; + } + // operator at the start of a random string e.g. '>=3.2.1' + else if (i && tmp.find_first_of("=<>") == 0) + { + arg += tmp; + tmp.clear(); + op = false; + } + + if (op) + arg += tmp; + else + { + if (!arg.empty()) + _args.insert(arg); + arg = tmp; + } + } + + if (!arg.empty()) + _args.insert(arg); + + DBG << "args received: "; + copy(args.begin(), args.end(), ostream_iterator(DBG, " ")); + DBG << endl; + + DBG << "args compiled: "; + copy(_args.begin(), _args.end(), ostream_iterator(DBG, " ")); + DBG << endl; +} diff --git a/src/PackageArgs.h b/src/PackageArgs.h new file mode 100644 index 0000000..a164a61 --- /dev/null +++ b/src/PackageArgs.h @@ -0,0 +1,44 @@ +/*---------------------------------------------------------------------------*\ + ____ _ _ __ _ __ ___ _ _ + |_ / || | '_ \ '_ \/ -_) '_| + /__|\_, | .__/ .__/\___|_| + |__/|_| |_| +\*---------------------------------------------------------------------------*/ + +/** \file PackageArgs.h + * + */ + +#ifndef ZYPPER_PACKAGEARGS_H_ +#define ZYPPER_PACKAGEARGS_H_ + +#include +#include +#include + +#include "zypp/Capability.h" + +class Zypper; + +class PackageArgs +{ + typedef std::set StringSet; + +public: + PackageArgs(); + PackageArgs(const std::vector & args); + ~PackageArgs() {} + + StringSet asStringSet() const + { return _args; } + +protected: + void preprocess(const std::vector & args); + +private: + Zypper & zypper; + StringSet _args; +}; + + +#endif /* ZYPPER_PACKAGEARGS_H_ */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 48c3d57..b46b95f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,3 +11,5 @@ ADD_SUBDIRECTORY( utils ) ADD_CUSTOM_TARGET( ctest COMMAND ctest -a ) + +ADD_TESTS( PackageArgs ) \ No newline at end of file diff --git a/tests/PackageArgs_test.cc b/tests/PackageArgs_test.cc new file mode 100644 index 0000000..c887d28 --- /dev/null +++ b/tests/PackageArgs_test.cc @@ -0,0 +1,105 @@ +/*---------------------------------------------------------------------------*\ + ____ _ _ __ _ __ ___ _ _ + |_ / || | '_ \ '_ \/ -_) '_| + /__|\_, | .__/ .__/\___|_| + |__/|_| |_| +\*---------------------------------------------------------------------------*/ + +#include "TestSetup.h" +#include "PackageArgs.h" + +using namespace std; + +BOOST_AUTO_TEST_CASE(preprocess_test) +{ + { + vector rawargs; + rawargs.push_back("zypper"); + rawargs.push_back("libzypp"); + rawargs.push_back("satsolver-tools"); + + PackageArgs args(rawargs); + set sargs = args.asStringSet(); + + BOOST_CHECK((sargs.find("zypper") != sargs.end())); + BOOST_CHECK((sargs.find("libzypp") != sargs.end())); + BOOST_CHECK((sargs.find("satsolver-tools") != sargs.end())); + BOOST_CHECK_EQUAL(sargs.size(), 3); + } + + { + vector rawargs; + rawargs.push_back("zypper"); + rawargs.push_back("libzypp"); + rawargs.push_back(">="); + rawargs.push_back("6.30.0"); + + PackageArgs args(rawargs); + set sargs = args.asStringSet(); + + BOOST_CHECK((sargs.find("zypper") != sargs.end())); + BOOST_CHECK((sargs.find("libzypp>=6.30.0") != sargs.end())); + BOOST_CHECK_EQUAL(sargs.size(), 2); + } + + { + vector rawargs; + rawargs.push_back("vim"); + rawargs.push_back("zypper>1.4.0"); + rawargs.push_back("libzypp"); + rawargs.push_back("<"); + rawargs.push_back(">"); + rawargs.push_back("6.30.0"); + + PackageArgs args(rawargs); + set sargs = args.asStringSet(); + + BOOST_CHECK((sargs.find("vim") != sargs.end())); + BOOST_CHECK((sargs.find("zypper>1.4.0") != sargs.end())); + BOOST_CHECK((sargs.find("libzypp<>") != sargs.end())); + BOOST_CHECK((sargs.find("6.30.0") != sargs.end())); + BOOST_CHECK_EQUAL(sargs.size(), 4); + } + + { + vector rawargs; + rawargs.push_back("="); + rawargs.push_back("zypper=1.4.0"); + rawargs.push_back("libzypp"); + rawargs.push_back(">"); + rawargs.push_back("6.30.0"); + rawargs.push_back("=>"); + + PackageArgs args(rawargs); + set sargs = args.asStringSet(); + + BOOST_CHECK((sargs.find("=") != sargs.end())); + BOOST_CHECK((sargs.find("zypper=1.4.0") != sargs.end())); + BOOST_CHECK((sargs.find("libzypp>6.30.0") != sargs.end())); + BOOST_CHECK((sargs.find("=>") != sargs.end())); + BOOST_CHECK_EQUAL(sargs.size(), 4); + } + + { + vector rawargs; + rawargs.push_back("=zypper"); + rawargs.push_back("libzypp"); + rawargs.push_back(">6.30.0"); + rawargs.push_back("+vim"); + rawargs.push_back("git<="); + rawargs.push_back("1.6.4.2"); + rawargs.push_back("tree<="); + + PackageArgs args(rawargs); + set sargs = args.asStringSet(); + + BOOST_CHECK((sargs.find("=zypper") != sargs.end())); + BOOST_CHECK((sargs.find("libzypp>6.30.0") != sargs.end())); + BOOST_CHECK((sargs.find("+vim") != sargs.end())); + BOOST_CHECK((sargs.find("git<=1.6.4.2") != sargs.end())); + BOOST_CHECK((sargs.find("tree<=") != sargs.end())); + BOOST_CHECK_EQUAL(sargs.size(), 5); + } +} + +// vim: set ts=2 sts=8 sw=2 ai et: -- 2.7.4