[openrtm-commit:00799] r2360 - in trunk/OpenRTM-aist/src/lib/coil: common tests/stringutil

openrtm @ openrtm.org openrtm @ openrtm.org
2012年 5月 22日 (火) 06:56:35 JST


Author: n-ando
Date: 2012-05-22 06:56:35 +0900 (Tue, 22 May 2012)
New Revision: 2360

Modified:
   trunk/OpenRTM-aist/src/lib/coil/common/stringutil.cpp
   trunk/OpenRTM-aist/src/lib/coil/common/stringutil.h
   trunk/OpenRTM-aist/src/lib/coil/tests/stringutil/stringutilTests.cpp
Log:
stringTo<bool> has been implemented.


Modified: trunk/OpenRTM-aist/src/lib/coil/common/stringutil.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/coil/common/stringutil.cpp	2012-05-08 10:45:59 UTC (rev 2359)
+++ trunk/OpenRTM-aist/src/lib/coil/common/stringutil.cpp	2012-05-21 21:56:35 UTC (rev 2360)
@@ -529,6 +529,35 @@
   
   /*!
    * @if jp
+   * @brief 与えられた文字列をboolに変換
+   * @else
+   * @brief Convert the given string to bool.
+   * @endif
+   */
+  template <>
+  bool stringTo<bool>(bool& val, const char* str)
+  {
+    if (str == 0) { return false; }
+    std::string boolstr(str);
+    coil::normalize(boolstr);
+    if (boolstr == "true" || boolstr == "1" ||
+        boolstr == "yes"  || boolstr == "on")
+      {
+        val = true;
+        return true;
+      }
+    else if (boolstr == "false" || boolstr == "0" ||
+             boolstr == "no"    || boolstr == "off")
+      {
+        val = false;
+        return true;
+      }
+    return false;
+  }
+
+  
+  /*!
+   * @if jp
    * @brief 与えられた文字列リストから重複を削除
    * @else
    * @brief Eliminate duplication from the given string list

Modified: trunk/OpenRTM-aist/src/lib/coil/common/stringutil.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/coil/common/stringutil.h	2012-05-08 10:45:59 UTC (rev 2359)
+++ trunk/OpenRTM-aist/src/lib/coil/common/stringutil.h	2012-05-21 21:56:35 UTC (rev 2360)
@@ -23,6 +23,7 @@
 #include <string>
 #include <vector>
 #include <sstream>
+#include <stdint.h>
 
 // Cygwin's gcc does not provide wstring type
 #if defined(Cygwin) && ( __GNUC__ < 4 )
@@ -632,6 +633,109 @@
 
   /*!
    * @if jp
+   * @brief 与えられた文字列をboolに変換
+   *
+   * 引数で与えられた文字列をboolに変換する。true/false を指定するため
+   * に以下の文字列が使用可能。大文字と小文字の区別はない。
+   *
+   * - true: 1, true, yes, on
+   * - true: 0, false, no, off
+   *
+   * @param val 変換先文字列
+   * @param str 変換元文字列
+   *
+   * @return true: 成功, false: 失敗
+   *
+   * @else
+   * @brief Convert the given string to bool
+   *
+   * Convert string given by the argument to bool. The following
+   * string can be used to specify true/false. Given string is not
+   * case-sensitive.
+   *
+   * - true: 1, true, yes, on
+   * - true: 0, false, no, off
+   *
+   * @param val String of conversion destination
+   * @param str String of conversion source
+   *
+   * @return true: successful, false: failed
+   *
+   * @endif
+   */
+  template <>
+  bool stringTo<bool>(bool& val, const char* str);
+
+  /*!
+   * @if jp
+   * @brief ポインタを16進数文字列に変換する
+   *
+   * 引数で与えられた文字列を16進数文字列に変換する。変換された文字列の
+   * 先頭には "0x" が付加される。
+   *
+   * @param ptr ポインタ
+   *
+   * @return 16進数文字列
+   *
+   * @else
+   * @brief Converting a pointer to hexadecimal string
+   *
+   * This function converts given string to hexadecimal string. "0x"
+   * will be added the head of the converted string.
+   *
+   * @param ptr A pointer to be converted
+   *
+   * @return Hexadecimal string
+   *
+   * @endif
+   */
+  template<class T>
+  std::string ptrToHex(T* n)
+  {
+    std::stringstream ss;
+    ss << std::hex << std::showbase;
+    ss << reinterpret_cast<uintptr_t>(n);
+    return ss.str();
+  };
+
+  /*!
+   * @if jp
+   * @brief 16進数文字列をポインタに変換する
+   *
+   * 引数で与えられた16進数文字列を文字列をに変換する。文字列の
+   * 先頭には "0x" が付加されているべきである。
+   *
+   * @param ptr ポインタ
+   * @param str 16進数文字列
+   * @return 変換が成功したら true、それ以外は false
+   *
+   * @else
+   * @brief Converting hexadecimal string to a pointer
+   *
+   * This function converts given hexadecimal string to a
+   * pointer. Hexadecimal string should have "0x" string in the head
+   * of string.
+   *
+   * @param ptr Pointer
+   * @param str Hexadeciaml string
+   * @return True will be returned when conversion successful
+   *
+   * @endif
+   */
+  template <class T>
+  bool hexToPtr(T*& ptr, const std::string str)
+  {
+    std::stringstream s;
+    if ((s << std::hex << str).fail()) { return false; }
+    uintptr_t intval;
+    if ((s >> intval).fail()) { return false; }
+    ptr = reinterpret_cast<T*>(intval);
+    if (ptr == NULL) { return false; }
+    return true;
+  }
+
+  /*!
+   * @if jp
    * @brief 与えられた文字列リストから重複を削除
    *
    * 引数で与えられた文字列リストから重複を削除したリストを作成する。

Modified: trunk/OpenRTM-aist/src/lib/coil/tests/stringutil/stringutilTests.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/coil/tests/stringutil/stringutilTests.cpp	2012-05-08 10:45:59 UTC (rev 2359)
+++ trunk/OpenRTM-aist/src/lib/coil/tests/stringutil/stringutilTests.cpp	2012-05-21 21:56:35 UTC (rev 2360)
@@ -24,6 +24,11 @@
 #include <cppunit/TestAssert.h>
 
 #include <coil/stringutil.h>
+      class A
+      {
+      public:
+        int intval;
+      };
 
 /*!
  * @class stringutilTests class
@@ -50,6 +55,9 @@
     CPPUNIT_TEST(test_isURL);
     CPPUNIT_TEST(test_otos);
     CPPUNIT_TEST(test_stringTo);
+    CPPUNIT_TEST(test_stringToBool);
+    CPPUNIT_TEST(test_ptrToHex);
+    CPPUNIT_TEST(test_hexToPtr);
     CPPUNIT_TEST(test_unique_sv);
     CPPUNIT_TEST(test_flatten);
     CPPUNIT_TEST(test_toArgv);
@@ -134,6 +142,82 @@
     void test_stringTo()
     {
     }
+    void test_stringToBool()
+    {
+      bool val;
+      if (coil::stringTo(val, "1")) { CPPUNIT_ASSERT_EQUAL(val, true); }
+      else                    { CPPUNIT_FAIL("conversion failed: 1"); }
+      if (coil::stringTo(val, "0")) { CPPUNIT_ASSERT_EQUAL(val, false); }
+      else                    { CPPUNIT_FAIL("conversion failed: 0"); }
+
+      if (coil::stringTo(val, "true"))  { CPPUNIT_ASSERT_EQUAL(val, true); }
+      else                        { CPPUNIT_FAIL("conversion failed: true"); }
+      if (coil::stringTo(val, "false")) { CPPUNIT_ASSERT_EQUAL(val, false); }
+      else                        { CPPUNIT_FAIL("conversion failed: false"); }
+
+      if (coil::stringTo(val, "TRUE"))  { CPPUNIT_ASSERT_EQUAL(val, true); }
+      else                        { CPPUNIT_FAIL("conversion failed: TRUE"); }
+      if (coil::stringTo(val, "FALSE")) { CPPUNIT_ASSERT_EQUAL(val, false); }
+      else                        { CPPUNIT_FAIL("conversion failed: FALSE"); }
+
+      if (coil::stringTo(val, "TrUe"))  { CPPUNIT_ASSERT_EQUAL(val, true); }
+      else                        { CPPUNIT_FAIL("conversion failed: TrUe"); }
+      if (coil::stringTo(val, "fAlsE")) { CPPUNIT_ASSERT_EQUAL(val, false); }
+      else                        { CPPUNIT_FAIL("conversion failed: fAlsE"); }
+
+      if (coil::stringTo(val, "yes"))  { CPPUNIT_ASSERT_EQUAL(val, true); }
+      else                       { CPPUNIT_FAIL("conversion failed: yes"); }
+      if (coil::stringTo(val, "no"))   { CPPUNIT_ASSERT_EQUAL(val, false); }
+      else                       { CPPUNIT_FAIL("conversion failed: no"); }
+
+      if (coil::stringTo(val, "YES"))  { CPPUNIT_ASSERT_EQUAL(val, true); }
+      else                       { CPPUNIT_FAIL("conversion failed: YES"); }
+      if (coil::stringTo(val, "NO"))   { CPPUNIT_ASSERT_EQUAL(val, false); }
+      else                       { CPPUNIT_FAIL("conversion failed: NO"); }
+
+      if (coil::stringTo(val, "on"))  { CPPUNIT_ASSERT_EQUAL(val, true); }
+      else                      { CPPUNIT_FAIL("conversion failed: on"); }
+      if (coil::stringTo(val, "off")) { CPPUNIT_ASSERT_EQUAL(val, false); }
+      else                      { CPPUNIT_FAIL("conversion failed: off"); }
+
+      if (coil::stringTo(val, "ON"))  { CPPUNIT_ASSERT_EQUAL(val, true); }
+      else                      { CPPUNIT_FAIL("conversion failed: ON"); }
+      if (coil::stringTo(val, "OFF")) { CPPUNIT_ASSERT_EQUAL(val, false); }
+      else                      { CPPUNIT_FAIL("conversion failed: OFF"); }
+
+      if (coil::stringTo(val, "hoge")) { CPPUNIT_FAIL("conversion failed: ON"); }
+      if (coil::stringTo(val, "muNya")) { CPPUNIT_FAIL("conversion failed: ON"); }
+      if (coil::stringTo(val, "12345")) { CPPUNIT_FAIL("conversion failed: ON"); }
+    }
+    void test_ptrToHex()
+    {
+      A* a = new A();
+      std::string ptrstr = coil::ptrToHex(a);
+      char cbuf[11];
+      sprintf(cbuf, "0x%x", reinterpret_cast<uintptr_t>(a));
+      CPPUNIT_ASSERT_MESSAGE(cbuf, (ptrstr == cbuf));
+      delete a;
+    }
+
+    void test_hexToPtr()
+    {
+      A* a0 = new A();
+      a0->intval = 98765;
+      std::string ptrstr = coil::ptrToHex(a0);
+      char cbuf[11];
+      sprintf(cbuf, "0x%x", reinterpret_cast<uintptr_t>(a0));
+      CPPUNIT_ASSERT_MESSAGE(cbuf, (ptrstr == cbuf));
+
+      A* a1;
+      CPPUNIT_ASSERT(coil::hexToPtr(a1, ptrstr));
+      CPPUNIT_ASSERT((a0 == a1));
+      CPPUNIT_ASSERT((a1->intval == 98765));
+
+      a1->intval = 12345;
+      CPPUNIT_ASSERT((a0->intval == 12345));
+
+      delete a0;
+    }
     void test_unique_sv()
     {
     }



More information about the openrtm-commit mailing list