id_t becomes a fully-fledged object, UUID's used for IDs, generic MIDI now owns bindi...
[ardour.git] / libs / pbd / id.cc
1 #include <ostream>
2 #include <iostream>
3
4 #include <string.h>
5 #include <uuid/uuid.h>
6
7 #include <pbd/id.h>
8
9 using namespace std;
10 using namespace PBD;
11
12 ID::ID ()
13 {
14         uuid_generate (id);
15 }
16
17 ID::ID (string str)
18 {
19         string_assign (str);
20 }
21
22 int
23 ID::string_assign (string str)
24 {
25         /* first check for old-style all-numeric ID's */
26
27         if (strcspn (str.c_str(), "0123456789") == 0) {
28                 /* all chars are numeric. just render the existing ID into the space in 
29                    which we would otherwise store a UUID.
30                 */
31
32                 memset (id, ' ', sizeof (id));
33                 snprintf ((char*) id, sizeof (id), str.c_str());
34
35         } else {
36
37                 /* OK, its UUID, probably */
38
39                 if (uuid_parse (str.c_str(), id)) {
40                         /* XXX error */
41                         return -1;
42                 }
43         }
44
45         return 0;
46 }
47
48 void
49 ID::print (char* buf) const
50 {
51         uuid_unparse (id, buf);
52 }
53
54 ID&
55 ID::operator= (string str)
56 {
57         string_assign (str);
58         return *this;
59 }
60
61 bool
62 ID::operator== (const ID& other) const
63 {
64         return memcmp (id, other.id, sizeof (id)) == 0;
65 }
66
67 ostream&
68 operator<< (ostream& ostr, const ID& id)
69 {
70         char buf[37];
71         id.print (buf);
72         ostr << buf;
73         return ostr;
74 }
75