[asterisk-commits] mmichelson: branch mmichelson/uuid r376818 - in /team/mmichelson/uuid: includ...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu Nov 29 09:39:38 CST 2012
Author: mmichelson
Date: Thu Nov 29 09:39:33 2012
New Revision: 376818
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=376818
Log:
Address some of David's feedback.
This adds an assumption that UUIDs will not be variable-length,
thus allowing the use of bare char* instead of ast_str for functions
to convert to and from a string. The length of a UUID is a constant
in the header file now.
Modified:
team/mmichelson/uuid/include/asterisk/uuid.h
team/mmichelson/uuid/main/uuid.c
team/mmichelson/uuid/tests/test_uuid.c
Modified: team/mmichelson/uuid/include/asterisk/uuid.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/uuid/include/asterisk/uuid.h?view=diff&rev=376818&r1=376817&r2=376818
==============================================================================
--- team/mmichelson/uuid/include/asterisk/uuid.h (original)
+++ team/mmichelson/uuid/include/asterisk/uuid.h Thu Nov 29 09:39:33 2012
@@ -23,6 +23,9 @@
#ifndef _ASTERISK_UUID_H
#define _ASTERISK_UUID_H
+/* Size of an RFC 4122 UUID string plus terminating null byte */
+#define AST_UUID_STR_LEN 37
+
struct ast_uuid;
/*!
@@ -45,9 +48,11 @@
* \brief Convert a UUID to a string
*
* \param uuid The UUID to convert to a string
- * \param str The string where the UUID will be stored
+ * \param[out] buf The buffer where the UUID string will be stored
+ * \param size The size of the buffer. Must be at least AST_UUID_STR_LEN.
+ * \returns The UUID string (a pointer to buf)
*/
-void ast_uuid_to_str(const struct ast_uuid *uuid, struct ast_str **str);
+char *ast_uuid_to_str(const struct ast_uuid *uuid, char *buf, size_t size);
/*!
* \brief Convert a string to a UUID
@@ -59,7 +64,7 @@
* \retval NULL Failed to convert
* \retval non-NULL The heap-allocated converted UUID
*/
-struct ast_uuid *ast_str_to_uuid(const struct ast_str *str);
+struct ast_uuid *ast_str_to_uuid(const char *str);
/*!
* \brief Make a copy of a UUID
Modified: team/mmichelson/uuid/main/uuid.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/uuid/main/uuid.c?view=diff&rev=376818&r1=376817&r2=376818
==============================================================================
--- team/mmichelson/uuid/main/uuid.c (original)
+++ team/mmichelson/uuid/main/uuid.c Thu Nov 29 09:39:33 2012
@@ -27,9 +27,6 @@
#include "asterisk/utils.h"
#include "asterisk/strings.h"
#include "asterisk/logger.h"
-
-/*! The length of a UUID string plus terminating null byte */
-#define UUID_STR_LEN 37
struct ast_uuid {
uuid_t uu;
@@ -103,24 +100,23 @@
return uuid;
}
-void ast_uuid_to_str(const struct ast_uuid *uuid, struct ast_str **str)
+char *ast_uuid_to_str(const struct ast_uuid *uuid, char *buf, size_t size)
{
- char uu_str[UUID_STR_LEN];
- uuid_unparse_lower(uuid->uu, uu_str);
-
- ast_str_set(str, 0, "%s", uu_str);
+ ast_assert(size >= AST_UUID_STR_LEN);
+ uuid_unparse_lower(uuid->uu, buf);
+ return buf;
}
-struct ast_uuid *ast_str_to_uuid(const struct ast_str *str)
+struct ast_uuid *ast_str_to_uuid(const char *str)
{
struct ast_uuid *uuid = ast_malloc(sizeof(*uuid));
int res;
if (!uuid) {
return NULL;
}
- res = uuid_parse(ast_str_buffer(str), uuid->uu);
+ res = uuid_parse(str, uuid->uu);
if (res) {
- ast_log(LOG_WARNING, "Unable to convert string %s into a UUID\n", ast_str_buffer(str));
+ ast_log(LOG_WARNING, "Unable to convert string %s into a UUID\n", str);
ast_free(uuid);
return NULL;
}
Modified: team/mmichelson/uuid/tests/test_uuid.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/uuid/tests/test_uuid.c?view=diff&rev=376818&r1=376817&r2=376818
==============================================================================
--- team/mmichelson/uuid/tests/test_uuid.c (original)
+++ team/mmichelson/uuid/tests/test_uuid.c Thu Nov 29 09:39:33 2012
@@ -35,7 +35,7 @@
struct ast_uuid *uuid1 = NULL;
struct ast_uuid *uuid2 = NULL;
struct ast_uuid *uuid3 = NULL;
- struct ast_str *uuid_str;
+ char uuid_str[AST_UUID_STR_LEN];
enum ast_test_result_state res = AST_TEST_FAIL;
switch (cmd) {
@@ -50,7 +50,6 @@
break;
}
- uuid_str = ast_str_alloca(64);
/* First, make sure that we can generate a UUID */
uuid1 = ast_uuid_generate();
if (!uuid1) {
@@ -65,23 +64,19 @@
}
/* Convert it to a string */
- ast_uuid_to_str(uuid1, &uuid_str);
+ ast_uuid_to_str(uuid1, uuid_str, sizeof(uuid_str));
- /* While it would be great to ensure the string is a specific length, we'll just
- * have to settle for making sure it's not zero-length in case we switch out UUID
- * libraries
- */
- if (ast_str_strlen(uuid_str) == 0) {
+ if (strlen(uuid_str) != (AST_UUID_STR_LEN - 1)) {
ast_test_status_update(test, "Failed to convert the UUID to a string\n");
goto end;
}
- ast_test_status_update(test, "Converted uuid to string, got %s\n", ast_str_buffer(uuid_str));
+ ast_test_status_update(test, "Converted uuid to string, got %s\n", uuid_str);
/* Now convert the string back to a UUID */
uuid2 = ast_str_to_uuid(uuid_str);
if (!uuid2) {
- ast_test_status_update(test, "Unable to convert string to UUID\n");
+ ast_test_status_update(test, "Unable to convert string %s to UUID\n", uuid_str);
goto end;
}
More information about the asterisk-commits
mailing list