3404ed437838a27c1b8c51f31d524dc3ab8d6c4e
[babeltrace.git] / include / babeltrace / uuid.h
1 #ifndef _BABELTRACE_UUID_H
2 #define _BABELTRACE_UUID_H
3
4 /*
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 *
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
15 */
16
17 #include <config.h>
18
19 /* Includes final \0. */
20 #define BABELTRACE_UUID_STR_LEN 37
21 #define BABELTRACE_UUID_LEN 16
22
23 #ifdef BABELTRACE_HAVE_LIBUUID
24 #include <uuid/uuid.h>
25
26 static inline
27 int babeltrace_uuid_generate(unsigned char *uuid_out)
28 {
29 uuid_generate(uuid_out);
30 return 0;
31 }
32
33 static inline
34 int babeltrace_uuid_unparse(const unsigned char *uuid_in, char *str_out)
35 {
36 uuid_unparse(uuid_in, str_out);
37 return 0;
38 }
39
40 static inline
41 int babeltrace_uuid_parse(const char *str_in, unsigned char *uuid_out)
42 {
43 return uuid_parse(str_in, uuid_out);
44 }
45
46 static inline
47 int babeltrace_uuid_compare(const unsigned char *uuid_a,
48 const unsigned char *uuid_b)
49 {
50 return uuid_compare(uuid_a, uuid_b);
51 }
52
53 #elif defined(BABELTRACE_HAVE_LIBC_UUID)
54 #include <uuid.h>
55 #include <stdint.h>
56 #include <string.h>
57 #include <stdlib.h>
58
59 static inline
60 int babeltrace_uuid_generate(unsigned char *uuid_out)
61 {
62 uint32_t status;
63
64 uuid_create((uuid_t *) uuid_out, &status);
65 if (status == uuid_s_ok)
66 return 0;
67 else
68 return -1;
69 }
70
71 static inline
72 int babeltrace_uuid_unparse(const unsigned char *uuid_in, char *str_out)
73 {
74 uint32_t status;
75 char *alloc_str;
76 int ret;
77
78 uuid_to_string((uuid_t *) uuid_in, &alloc_str, &status);
79 if (status == uuid_s_ok) {
80 strcpy(str_out, alloc_str);
81 ret = 0;
82 } else {
83 ret = -1;
84 }
85 free(alloc_str);
86 return ret;
87 }
88
89 static inline
90 int babeltrace_uuid_parse(const char *str_in, unsigned char *uuid_out)
91 {
92 uint32_t status;
93
94 uuid_from_string(str_in, (uuid_t *) uuid_out, &status);
95 if (status == uuid_s_ok)
96 return 0;
97 else
98 return -1;
99 }
100
101 static inline
102 int babeltrace_uuid_compare(const unsigned char *uuid_a,
103 const unsigned char *uuid_b)
104 {
105 uint32_t status;
106
107 uuid_compare((uuid_t *) uuid_a, (uuid_t *) uuid_b, &status);
108 if (status == uuid_s_ok)
109 return 0;
110 else
111 return -1;
112 }
113
114 #else
115 #error "Babeltrace needs to have a UUID generator configured."
116 #endif
117
118 #endif /* _BABELTRACE_UUID_H */
This page took 0.032017 seconds and 3 git commands to generate.