Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_uuid.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework Adaptive UUID Support.
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 
15 #include "afw_internal.h"
16 
17 
18 
19 /* Generate a new UUID. */
20 AFW_DEFINE(void)
22  afw_uuid_t *uuid, afw_xctx_t *xctx)
23 {
24  apr_uuid_get(uuid);
25 }
26 
27 
28 /* Create a UUID. */
29 AFW_DEFINE(const afw_uuid_t *)
31  const afw_pool_t *p, afw_xctx_t *xctx)
32 {
33  afw_uuid_t * uuid;
34 
35  uuid = afw_pool_malloc_type(p, afw_uuid_t, xctx);
36  apr_uuid_get(uuid);
37  return uuid;
38 }
39 
40 
41 /* Create a UUID as a standard format UUID string value. */
42 AFW_DEFINE(const afw_value_t *)
44  const afw_pool_t *p, afw_xctx_t *xctx)
45 {
46  afw_uuid_t uuid;
47  afw_value_string_t *string;
48 
49  string = afw_value_allocate_string(p, xctx);
50  string->internal.len = APR_UUID_FORMATTED_LENGTH + 1;
51  string->internal.s = afw_pool_malloc(p, string->internal.len,
52  xctx);
53  apr_uuid_get(&uuid);
54  apr_uuid_format((char *)string->internal.s, &uuid);
55 
56  return (const afw_value_t *)string;
57 }
58 
59 
60 /* Create a UUID as a standard format UUID string. */
61 AFW_DEFINE(const afw_utf8_t *)
63  const afw_pool_t *p, afw_xctx_t *xctx)
64 {
65  afw_uuid_t uuid;
66  char *buffer;
67 
68  buffer = afw_pool_malloc(p, APR_UUID_FORMATTED_LENGTH + 1,
69  xctx);
70  apr_uuid_get(&uuid);
71  apr_uuid_format(buffer, &uuid);
72 
73  return afw_utf8_create(buffer, AFW_UTF8_Z_LEN, p, xctx);
74 }
75 
76 
77 /* Convert uuid to a standard format UUID string. */
78 AFW_DEFINE(const afw_utf8_t *)
80  const afw_uuid_t *uuid,
81  const afw_pool_t *p, afw_xctx_t *xctx)
82 {
83  char *buffer;
84 
85  buffer = afw_pool_malloc(p, APR_UUID_FORMATTED_LENGTH + 1,
86  xctx);
87  apr_uuid_format(buffer, uuid);
88 
89  return afw_utf8_create(buffer, AFW_UTF8_Z_LEN, p, xctx);
90 }
91 
92 
93 /* Parse a standard format UUID string to a uuid. */
94 AFW_DEFINE(void)
96  afw_uuid_t *uuid,
97  const afw_utf8_t *s,
98  afw_xctx_t *xctx)
99 {
100  apr_status_t rv = 0;
101  char uuid_str[APR_UUID_FORMATTED_LENGTH + 1];
102 
103  if (s->len != APR_UUID_FORMATTED_LENGTH) goto error;
104  memcpy(uuid_str, s->s, s->len);
105  uuid_str[s->len] = 0;
106  rv = apr_uuid_parse(uuid, uuid_str);
107  if (rv != APR_SUCCESS) goto error;
108  return;
109 
110 error:
111  AFW_THROW_ERROR_RV_Z(general, apr, rv, "Invalid uuid string.", xctx);
112 }
113 
114 
115 /* Convert a standard format UUID string to uuid. */
116 AFW_DEFINE(const afw_uuid_t *)
118  const afw_utf8_t *s,
119  const afw_pool_t *p, afw_xctx_t *xctx)
120 {
121  afw_uuid_t *uuid;
122  char uuid_str[APR_UUID_FORMATTED_LENGTH + 1];
123  apr_status_t rv = 0;
124 
125  if (s->len > APR_UUID_FORMATTED_LENGTH) goto error;
126  memcpy(uuid_str, s->s, s->len);
127  uuid_str[s->len] = 0;
128  uuid = afw_pool_malloc_type(p, afw_uuid_t, xctx);
129  rv = apr_uuid_parse(uuid, uuid_str);
130  if (rv != APR_SUCCESS) goto error;
131 
132  return uuid;
133 
134 error:
135  AFW_THROW_ERROR_RV_Z(general, apr, rv, "Invalid uuid string.", xctx);
136 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
afw_value_allocate_string(const afw_pool_t *p, afw_xctx_t *xctx)
Allocate function for unmanaged data type string value.
#define AFW_UTF8_Z_LEN
String is NUL (0) terminate.
Definition: afw_common.h:266
#define AFW_THROW_ERROR_RV_Z(code, rv_source_id, rv, message_z, xctx)
Macro used to set error and rv in xctx and throw it.
Definition: afw_error.h:301
#define afw_pool_malloc(instance, size, xctx)
Call method malloc of interface afw_pool.
#define afw_pool_malloc_type(instance, type, xctx)
Macro to allocate uncleared memory to hold type in pool.
Definition: afw_pool.h:182
#define afw_utf8_create(s, len, p, xctx)
Create utf-8 string without copy unless necessary in pool specified.
Definition: afw_utf8.h:239
afw_uuid_from_utf8(const afw_utf8_t *s, const afw_pool_t *p, afw_xctx_t *xctx)
Convert standard format UUID utf-8 string to uuid.
Definition: afw_uuid.c:117
afw_uuid_parse(afw_uuid_t *uuid, const afw_utf8_t *s, afw_xctx_t *xctx)
Parse a standard format UUID string to a uuid.
Definition: afw_uuid.c:95
afw_uuid_create(const afw_pool_t *p, afw_xctx_t *xctx)
Create a UUID.
Definition: afw_uuid.c:30
afw_uuid_create_string(const afw_pool_t *p, afw_xctx_t *xctx)
Create a UUID as a standard format UUID utf-8 string value.
Definition: afw_uuid.c:43
afw_uuid_create_utf8(const afw_pool_t *p, afw_xctx_t *xctx)
Create a UUID as a standard format UUID utf-8 string.
Definition: afw_uuid.c:62
afw_uuid_generate(afw_uuid_t *uuid, afw_xctx_t *xctx)
Generate a new UUID.
Definition: afw_uuid.c:21
afw_uuid_to_utf8(const afw_uuid_t *uuid, const afw_pool_t *p, afw_xctx_t *xctx)
Convert uuid to a standard format UUID utf-8 string.
Definition: afw_uuid.c:79
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_value public struct.
struct for data type string values.
Interface afw_xctx public struct.