From: Arvin Schnell Date: Thu, 10 Apr 2014 08:55:09 +0000 (+0200) Subject: - moved function to separate file X-Git-Tag: v0.2.2~3^2~4 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=dadb81007288b03cc40b5a41fdef09ef9e9cd6e4;p=thirdparty%2Fsnapper.git - moved function to separate file --- diff --git a/client/Makefile.am b/client/Makefile.am index 1820156b..ed05cc2d 100644 --- a/client/Makefile.am +++ b/client/Makefile.am @@ -13,6 +13,7 @@ snapper_SOURCES = \ types.cc types.h \ commands.cc commands.h \ cleanup.cc cleanup.h \ + misc.cc misc.h \ errors.cc errors.h snapper_LDADD = ../snapper/libsnapper.la utils/libutils.la ../dbus/libdbus.la diff --git a/client/misc.cc b/client/misc.cc new file mode 100644 index 00000000..a894bc17 --- /dev/null +++ b/client/misc.cc @@ -0,0 +1,156 @@ +/* + * Copyright (c) [2011-2014] Novell, Inc. + * + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, contact Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, you may + * find current contact information at www.novell.com. + */ + + +#include "config.h" + +#include +#include +#include + +#include + +#include "utils/text.h" + +#include "misc.h" + + +Snapshots::iterator +read_num(Snapper* snapper, const string& str) +{ + Snapshots& snapshots = snapper->getSnapshots(); + + istringstream s(str); + unsigned int num = 0; + s >> num; + + if (s.fail() || !s.eof()) + { + cerr << sformat(_("Invalid snapshot '%s'."), str.c_str()) << endl; + exit(EXIT_FAILURE); + } + + Snapshots::iterator snap = snapshots.find(num); + if (snap == snapshots.end()) + { + cerr << sformat(_("Snapshot '%u' not found."), num) << endl; + exit(EXIT_FAILURE); + } + + return snap; +} + + +unsigned int +read_num(const string& str) +{ + istringstream s(str); + unsigned int num = 0; + s >> num; + + if (s.fail() || !s.eof()) + { + cerr << sformat(_("Invalid snapshot '%s'."), str.c_str()) << endl; + exit(EXIT_FAILURE); + } + + return num; +} + + +pair +read_nums(const string& str, const string& delim) +{ + string::size_type pos = str.find(delim); + if (pos == string::npos) + { + cerr << _("Invalid snapshots.") << endl; + exit(EXIT_FAILURE); + } + + unsigned int num1 = read_num(str.substr(0, pos)); + unsigned int num2 = read_num(str.substr(pos + delim.size())); + + if (num1 == num2) + { + cerr << _("Identical snapshots.") << endl; + exit(EXIT_FAILURE); + } + + return pair(num1, num2); +} + + +map +read_userdata(const string& s, const map& old) +{ + map userdata = old; + + list tmp; + boost::split(tmp, s, boost::is_any_of(","), boost::token_compress_on); + if (tmp.empty()) + { + cerr << _("Invalid userdata.") << endl; + exit(EXIT_FAILURE); + } + + for (list::const_iterator it = tmp.begin(); it != tmp.end(); ++it) + { + string::size_type pos = it->find("="); + if (pos == string::npos) + { + cerr << _("Invalid userdata.") << endl; + exit(EXIT_FAILURE); + } + + string key = boost::trim_copy(it->substr(0, pos)); + string value = boost::trim_copy(it->substr(pos + 1)); + + if (key.empty()) + { + cerr << _("Invalid userdata.") << endl; + exit(EXIT_FAILURE); + } + + if (value.empty()) + userdata.erase(key); + else + userdata[key] = value; + } + + return userdata; +} + + +string +show_userdata(const map& userdata) +{ + string s; + + for (map::const_iterator it = userdata.begin(); it != userdata.end(); ++it) + { + if (!s.empty()) + s += ", "; + s += it->first + "=" + it->second; + } + + return s; +} diff --git a/client/misc.h b/client/misc.h new file mode 100644 index 00000000..ae89c950 --- /dev/null +++ b/client/misc.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) [2011-2014] Novell, Inc. + * + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, contact Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, you may + * find current contact information at www.novell.com. + */ + + +#include + +#include + + +using namespace snapper; +using namespace std; + + +Snapshots::iterator +read_num(Snapper* snapper, const string& str); + +unsigned int +read_num(const string& str); + +pair +read_nums(const string& str, const string& delim = ".."); + +map +read_userdata(const string& s, const map& old = map()); + +string +show_userdata(const map& userdata); + diff --git a/client/snapper.cc b/client/snapper.cc index b6e437b6..d65709ba 100644 --- a/client/snapper.cc +++ b/client/snapper.cc @@ -48,6 +48,7 @@ #include "commands.h" #include "cleanup.h" #include "errors.h" +#include "misc.h" using namespace snapper; @@ -82,129 +83,6 @@ string config_name = "root"; bool no_dbus = false; -Snapshots::iterator -read_num(Snapper* snapper, const string& str) -{ - Snapshots& snapshots = snapper->getSnapshots(); - - istringstream s(str); - unsigned int num = 0; - s >> num; - - if (s.fail() || !s.eof()) - { - cerr << sformat(_("Invalid snapshot '%s'."), str.c_str()) << endl; - exit(EXIT_FAILURE); - } - - Snapshots::iterator snap = snapshots.find(num); - if (snap == snapshots.end()) - { - cerr << sformat(_("Snapshot '%u' not found."), num) << endl; - exit(EXIT_FAILURE); - } - - return snap; -} - - -unsigned int -read_num(const string& str) -{ - istringstream s(str); - unsigned int num = 0; - s >> num; - - if (s.fail() || !s.eof()) - { - cerr << sformat(_("Invalid snapshot '%s'."), str.c_str()) << endl; - exit(EXIT_FAILURE); - } - - return num; -} - - -pair -read_nums(const string& str, const string& delim = "..") -{ - string::size_type pos = str.find(delim); - if (pos == string::npos) - { - cerr << _("Invalid snapshots.") << endl; - exit(EXIT_FAILURE); - } - - unsigned int num1 = read_num(str.substr(0, pos)); - unsigned int num2 = read_num(str.substr(pos + delim.size())); - - if (num1 == num2) - { - cerr << _("Identical snapshots.") << endl; - exit(EXIT_FAILURE); - } - - return pair(num1, num2); -} - - -map -read_userdata(const string& s, const map& old = map()) -{ - map userdata = old; - - list tmp; - boost::split(tmp, s, boost::is_any_of(","), boost::token_compress_on); - if (tmp.empty()) - { - cerr << _("Invalid userdata.") << endl; - exit(EXIT_FAILURE); - } - - for (list::const_iterator it = tmp.begin(); it != tmp.end(); ++it) - { - string::size_type pos = it->find("="); - if (pos == string::npos) - { - cerr << _("Invalid userdata.") << endl; - exit(EXIT_FAILURE); - } - - string key = boost::trim_copy(it->substr(0, pos)); - string value = boost::trim_copy(it->substr(pos + 1)); - - if (key.empty()) - { - cerr << _("Invalid userdata.") << endl; - exit(EXIT_FAILURE); - } - - if (value.empty()) - userdata.erase(key); - else - userdata[key] = value; - } - - return userdata; -} - - -string -show_userdata(const map& userdata) -{ - string s; - - for (map::const_iterator it = userdata.begin(); it != userdata.end(); ++it) - { - if (!s.empty()) - s += ", "; - s += it->first + "=" + it->second; - } - - return s; -} - - struct MyFiles : public Files { friend class MyComparison;