aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Stephen P. Bennett <spb@exherbo.org> 2006-01-23 02:17:05 +0000
committerAvatar Stephen P. Bennett <spb@exherbo.org> 2006-01-23 02:17:05 +0000
commitdcbdd26b9b38a691d45e74fe5add94cc4e835e80 (patch)
treecdfb9bbd0ef2e62adba8754d4e32b5aba574e54b
parentc46326e48c46b88cb6a2692a7e325efbfe5b602d (diff)
downloadpaludis-dcbdd26b9b38a691d45e74fe5add94cc4e835e80.tar.gz
paludis-dcbdd26b9b38a691d45e74fe5add94cc4e835e80.tar.xz
Added destringify routine(s)
-rw-r--r--paludis/destringify.cc29
-rw-r--r--paludis/destringify.hh111
-rw-r--r--paludis/destringify_TEST.cc95
-rw-r--r--paludis/files.m41
4 files changed, 236 insertions, 0 deletions
diff --git a/paludis/destringify.cc b/paludis/destringify.cc
new file mode 100644
index 0000000..6389461
--- /dev/null
+++ b/paludis/destringify.cc
@@ -0,0 +1,29 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Paludis 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, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+#include "destringify.hh"
+
+using namespace paludis;
+
+DestringifyError::DestringifyError(const std::string & str) throw () :
+ Exception("Couldn't destringify " + str + "." )
+{
+}
diff --git a/paludis/destringify.hh b/paludis/destringify.hh
new file mode 100644
index 0000000..e29147c
--- /dev/null
+++ b/paludis/destringify.hh
@@ -0,0 +1,111 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Paludis 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, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_DESTRINGIFY_HH
+#define PALUDIS_GUARD_PALUDIS_DESTRINGIFY_HH 1
+
+#include <string>
+#include <sstream>
+#include <paludis/exception.hh>
+
+/** \file
+ * Destringify functions.
+ */
+
+namespace paludis
+{
+ /**
+ * Default exception type thrown by destringify when it fails.
+ */
+ class DestringifyError : public Exception
+ {
+ public:
+ /**
+ * Constructor.
+ */
+ DestringifyError(const std::string & str) throw ();
+ };
+
+ namespace destringify_internals
+ {
+ template <typename Type_, typename Exception_>
+ struct Destringifier
+ {
+ static Type_ do_destringify(const std::string & s)
+ {
+ std::istringstream ss(s);
+ Type_ t;
+ ss >> t;
+ if (!ss.eof() || ss.bad())
+ throw Exception_(s);
+ return t;
+ }
+ };
+
+ template <typename Exception_>
+ struct Destringifier<std::string, Exception_>
+ {
+ static std::string do_destringify(const std::string & s)
+ {
+ return s;
+ }
+ };
+
+ template <typename Exception_>
+ struct Destringifier<bool, Exception_>
+ {
+ static bool do_destringify(const std::string & s)
+ {
+ int i;
+ try
+ {
+ i = Destringifier<int, Exception_>::do_destringify(s);
+ return i > 0;
+ }
+ catch(DestringifyError &)
+ {
+ bool b;
+ std::istringstream ss(s);
+ ss >> std::boolalpha >> b;
+ if(!ss.eof() || ss.bad())
+ throw Exception_(s);
+ return b;
+ }
+ }
+ };
+ }
+
+ /**
+ * Extract a value of some type from a string.
+ */
+ template <typename Type_, typename Exception_>
+ Type_ destringify(const std::string & s)
+ {
+ return destringify_internals::Destringifier<Type_, Exception_>::do_destringify(s);
+ }
+
+ template <typename Type_>
+ Type_ destringify(const std::string & s)
+ {
+ return destringify<Type_, DestringifyError>(s);
+ }
+}
+
+#endif
diff --git a/paludis/destringify_TEST.cc b/paludis/destringify_TEST.cc
new file mode 100644
index 0000000..8e9c771
--- /dev/null
+++ b/paludis/destringify_TEST.cc
@@ -0,0 +1,95 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Stephen Bennett <spb@gentoo.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Paludis 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, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "destringify.hh"
+#include <test/test_framework.hh>
+#include <test/test_runner.hh>
+#include <string>
+
+using namespace test;
+using namespace paludis;
+
+/** \file
+ * Test cases for destringify.hh
+ *
+ * \ingroup Test
+ */
+
+namespace test_cases
+{
+ /** \test
+ * Test destringify for integers.
+ *
+ * \ingroup Test
+ */
+ struct DestringifyIntTests : TestCase
+ {
+ DestringifyIntTests() : TestCase("destringify int") { }
+
+ void run()
+ {
+ TEST_CHECK_EQUAL(destringify<int>("0"), 0);
+ TEST_CHECK_EQUAL(destringify<int>("1"), 1);
+ TEST_CHECK_EQUAL(destringify<int>("99"), 99);
+ TEST_CHECK_EQUAL(destringify<int>("-99"), -99);
+ TEST_CHECK_EQUAL(destringify<int>(" 12345"), 12345);
+ }
+ } test_case_destringify_int;
+
+ struct DestringifyFloatTests : TestCase
+ {
+ DestringifyFloatTests() : TestCase("destringify float") { }
+
+ void run()
+ {
+ TEST_CHECK_EQUAL(destringify<float>("0"), 0.f);
+ TEST_CHECK_EQUAL(destringify<float>("0.0"), 0.f);
+ TEST_CHECK_EQUAL(destringify<float>("0.1"), 0.1f);
+ TEST_CHECK_EQUAL(destringify<float>("-1.54"), -1.54f);
+ }
+ } test_case_destringify_float;
+
+ struct DestringifyStringTests : TestCase
+ {
+ DestringifyStringTests() : TestCase("destringify string") { }
+
+ void run()
+ {
+ TEST_CHECK_EQUAL(destringify<std::string>("asdf"), "asdf");
+ TEST_CHECK_EQUAL(destringify<std::string>(" a f e b "), " a f e b ");
+ }
+ } test_case_destringify_string;
+
+ struct DestringifyBoolTests : TestCase
+ {
+ DestringifyBoolTests() : TestCase("destringify bool") { }
+
+ void run()
+ {
+ TEST_CHECK( destringify<bool>("true"));
+ TEST_CHECK( destringify<bool>("1"));
+ TEST_CHECK( destringify<bool>("5"));
+ TEST_CHECK(!destringify<bool>("false"));
+ TEST_CHECK(!destringify<bool>("0"));
+ TEST_CHECK(!destringify<bool>("-1"));
+ }
+ } test_case_destringify_bool;
+}
+
diff --git a/paludis/files.m4 b/paludis/files.m4
index ad46036..ea6a803 100644
--- a/paludis/files.m4
+++ b/paludis/files.m4
@@ -79,3 +79,4 @@ add(`version_operator', `hh', `cc', `test')
add(`version_spec', `hh', `cc', `test')
add(`virtual_constructor', `hh', `cc', `test')
add(`visitor', `hh', `cc', `test')
+add(`destringify', `hh', `cc', `test')