summaryrefslogtreecommitdiff
path: root/libs/pbd/id.cc
blob: 57ddb5b1282d2a54a3e40e1bfb41b2fda013d8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <ostream>
#include <iostream>

#include <string.h>
#include <uuid/uuid.h>

#include <pbd/id.h>

using namespace std;
using namespace PBD;

ID::ID ()
{
	uuid_generate (id);
}

ID::ID (string str)
{
	string_assign (str);
}

int
ID::string_assign (string str)
{
	/* first check for old-style all-numeric ID's */

	if (strcspn (str.c_str(), "0123456789") == 0) {
		/* all chars are numeric. just render the existing ID into the space in 
		   which we would otherwise store a UUID.
		*/

		memset (id, ' ', sizeof (id));
		snprintf ((char*) id, sizeof (id), str.c_str());

	} else {

		/* OK, its UUID, probably */

		if (uuid_parse (str.c_str(), id)) {
			/* XXX error */
			return -1;
		}
	}

	return 0;
}

void
ID::print (char* buf) const
{
	uuid_unparse (id, buf);
}

ID&
ID::operator= (string str)
{
	string_assign (str);
	return *this;
}

bool
ID::operator== (const ID& other) const
{
	return memcmp (id, other.id, sizeof (id)) == 0;
}

ostream&
operator<< (ostream& ostr, const ID& id)
{
	char buf[37];
	id.print (buf);
	ostr << buf;
	return ostr;
}