Use PBD string conversion functions in PBD::ID instead of snprintf
authorTim Mayberry <mojofunk@gmail.com>
Fri, 14 Aug 2015 09:56:53 +0000 (19:56 +1000)
committerTim Mayberry <mojofunk@gmail.com>
Tue, 18 Apr 2017 21:49:58 +0000 (07:49 +1000)
Keep ID::print in place for now and replace usage in subsequent commit to
minimize changes

libs/pbd/id.cc
libs/pbd/pbd/id.h

index 9a610c7418e4135f6980653375e4d42a40746642..6898da2d123177fba4c1d8ee49fcc761ea5644e2 100644 (file)
@@ -26,6 +26,8 @@
 #include <inttypes.h>
 
 #include "pbd/id.h"
+#include "pbd/string_convert.h"
+
 #include <string>
 
 using namespace std;
@@ -63,10 +65,10 @@ ID::reset ()
        _id = _counter++;
 }
 
-int
+bool
 ID::string_assign (string str)
 {
-       return sscanf (str.c_str(), "%" PRIu64, &_id) != 0;
+       return string_to_uint64 (str, _id);
 }
 
 void
@@ -75,17 +77,16 @@ ID::print (char* buf, uint32_t bufsize) const
        snprintf (buf, bufsize, "%" PRIu64, _id);
 }
 
-string ID::to_s() const
+std::string
+ID::to_s () const
 {
-    char buf[32]; // see print()
-    print(buf, sizeof (buf));
-    return string(buf);
+       return to_string (_id);
 }
 
 bool
 ID::operator== (const string& str) const
 {
-       return to_s() == str;
+       return to_string (_id) == str;
 }
 
 ID&
@@ -105,11 +106,9 @@ ID::operator= (const ID& other)
 }
 
 ostream&
-operator<< (ostream& ostr, const ID& _id)
+operator<< (ostream& ostr, const ID& id)
 {
-       char buf[32];
-       _id.print (buf, sizeof (buf));
-       ostr << buf;
+       ostr << id.to_s();
        return ostr;
 }
 
index 5316e7c20e32ccfc940dbb2d7d3d79644c6ec3bc..ca62b10dc231540062ba93acccb4cf54178e1a46 100644 (file)
@@ -56,7 +56,8 @@ class LIBPBD_API ID {
        }
 
        void print (char* buf, uint32_t bufsize) const;
-        std::string to_s() const;
+
+       std::string to_s () const;
 
        static uint64_t counter() { return _counter; }
        static void init_counter (uint64_t val) { _counter = val; }
@@ -64,7 +65,7 @@ class LIBPBD_API ID {
 
   private:
        uint64_t _id;
-       int string_assign (std::string);
+       bool string_assign (std::string);
 
        static Glib::Threads::Mutex* counter_lock;
        static uint64_t _counter;