[asterisk-commits] file: branch file/bucket r395726 - /team/file/bucket/main/bucket.c
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Jul 30 06:55:59 CDT 2013
Author: file
Date: Tue Jul 30 06:55:57 2013
New Revision: 395726
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=395726
Log:
Reduce duplicated URI parsing code, and make it easier to change.
Modified:
team/file/bucket/main/bucket.c
Modified: team/file/bucket/main/bucket.c
URL: http://svnview.digium.com/svn/asterisk/team/file/bucket/main/bucket.c?view=diff&rev=395726&r1=395725&r2=395726
==============================================================================
--- team/file/bucket/main/bucket.c (original)
+++ team/file/bucket/main/bucket.c Tue Jul 30 06:55:57 2013
@@ -71,6 +71,48 @@
char name[0];
};
+/*!
+ * \brief Simple URI parser
+ *
+ * \param uri Full URI
+ * \param scheme The scheme in the URI
+ * \param name The name of the object in the URI
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ *
+ * \note The URI passed in will be modified
+ */
+static int bucket_uri_parse(char *uri, char **scheme, char **name)
+{
+ char *tmp;
+
+ /* If no URI has been specified this is invalid */
+ if (ast_strlen_zero(uri)) {
+ return -1;
+ }
+
+ *scheme = DEFAULT_UNSPECIFIED_SCHEME;
+ *name = NULL;
+
+ /* Assume the naming starts at the front until proven otherwise */
+ *name = uri;
+
+ /* Determine the scheme from the provided URI */
+ if ((tmp = strstr(uri, "://"))) {
+ *scheme = uri;
+ *tmp++ = '\0';
+ *name = tmp;
+ }
+
+ /* Determine the name */
+ if ((tmp = strrchr(*name, '/'))) {
+ *name = tmp + 1;
+ }
+
+ return 0;
+}
+
/*! \brief Callback function for creating a bucket */
static int bucket_wizard_create(const struct ast_sorcery *sorcery, void *data, void *object)
{
@@ -88,29 +130,19 @@
static void *bucket_wizard_retrieve(const struct ast_sorcery *sorcery, void *data, const char *type,
const char *id)
{
- char *full_uri, *scheme_name = DEFAULT_UNSPECIFIED_SCHEME, *tmp;
+ char *uri, *uri_scheme, *uri_name;
RAII_VAR(struct bucket_scheme *, scheme, NULL, ao2_cleanup);
- if (!(full_uri = ast_strdupa(id))) {
- return NULL;
- }
-
- /* Assume no scheme until proven otherwise */
- if ((tmp = strstr(full_uri, "://"))) {
- scheme_name = full_uri;
- *tmp++ = '\0';
-
- /* If only a scheme has been specified this URI is invalid */
- if (ast_strlen_zero(tmp)) {
- return NULL;
- }
- }
-
- if (ast_strlen_zero(scheme_name)) {
- return NULL;
- }
-
- scheme = ao2_find(schemes, scheme_name, OBJ_KEY);
+ if (!(uri = ast_strdupa(id))) {
+ return NULL;
+ }
+
+ if (bucket_uri_parse(uri, &uri_scheme, &uri_name) ||
+ ast_strlen_zero(uri_scheme) || ast_strlen_zero(uri_name)) {
+ return NULL;
+ }
+
+ scheme = ao2_find(schemes, uri_scheme, OBJ_KEY);
if (!scheme) {
return NULL;
}
@@ -156,29 +188,19 @@
static void *bucket_file_wizard_retrieve(const struct ast_sorcery *sorcery, void *data, const char *type,
const char *id)
{
- char *full_uri, *scheme_name = DEFAULT_UNSPECIFIED_SCHEME, *tmp;
+ char *uri, *uri_scheme, *uri_name;
RAII_VAR(struct bucket_scheme *, scheme, NULL, ao2_cleanup);
- if (!(full_uri = ast_strdupa(id))) {
- return NULL;
- }
-
- /* Assume no scheme until proven otherwise */
- if ((tmp = strstr(full_uri, "://"))) {
- scheme_name = full_uri;
- *tmp++ = '\0';
-
- /* If only a scheme has been specified this URI is invalid */
- if (ast_strlen_zero(tmp)) {
- return NULL;
- }
- }
-
- if (ast_strlen_zero(scheme_name)) {
- return NULL;
- }
-
- scheme = ao2_find(schemes, scheme_name, OBJ_KEY);
+ if (!(uri = ast_strdupa(id))) {
+ return NULL;
+ }
+
+ if (bucket_uri_parse(uri, &uri_scheme, &uri_name) ||
+ ast_strlen_zero(uri_scheme) || ast_strlen_zero(uri_name)) {
+ return NULL;
+ }
+
+ scheme = ao2_find(schemes, uri_scheme, OBJ_KEY);
if (!scheme) {
return NULL;
}
@@ -360,39 +382,24 @@
struct ast_bucket *ast_bucket_alloc(const char *uri)
{
- char *full_uri, *scheme = DEFAULT_UNSPECIFIED_SCHEME, *name, *tmp;
+ char *full_uri, *uri_scheme, *uri_name;
struct ast_bucket *bucket;
- if (ast_strlen_zero(uri) || !(full_uri = ast_strdupa(uri))) {
- return NULL;
- }
-
- /* Assume the naming starts at the front until proven otherwise */
- name = full_uri;
-
- /* Determine the scheme from the provided URI */
- if ((tmp = strstr(full_uri, "://"))) {
- scheme = full_uri;
- *tmp++ = '\0';
- name = tmp;
- }
-
- /* Determine the name of the bucket */
- if ((tmp = strrchr(name, '/'))) {
- name = tmp + 1;
- }
-
- /* If no scheme and name are available the URI is invalid */
- if (ast_strlen_zero(scheme) || ast_strlen_zero(name)) {
- return NULL;
- }
-
- if (!(bucket = ast_sorcery_alloc(bucket_sorcery, "bucket", name))) {
+ if (!(full_uri = ast_strdupa(uri))) {
+ return NULL;
+ }
+
+ if (bucket_uri_parse(full_uri, &uri_scheme, &uri_name) ||
+ ast_strlen_zero(uri_scheme) || ast_strlen_zero(uri_name)) {
+ return NULL;
+ }
+
+ if (!(bucket = ast_sorcery_alloc(bucket_sorcery, "bucket", uri_name))) {
return NULL;
}
ast_string_field_set(bucket, uri, uri);
- ast_string_field_set(bucket, scheme, scheme);
+ ast_string_field_set(bucket, scheme, uri_scheme);
return bucket;
}
@@ -559,39 +566,24 @@
struct ast_bucket_file *ast_bucket_file_alloc(const char *uri)
{
- char *full_uri, *scheme = DEFAULT_UNSPECIFIED_SCHEME, *name, *tmp;
+ char *full_uri, *uri_scheme, *uri_name;
struct ast_bucket_file *file;
- if (ast_strlen_zero(uri) || !(full_uri = ast_strdupa(uri))) {
- return NULL;
- }
-
- /* Assume the naming starts at the front until proven otherwise */
- name = full_uri;
-
- /* Determine the scheme from the provided URI */
- if ((tmp = strstr(full_uri, "://"))) {
- scheme = full_uri;
- *tmp++ = '\0';
- name = tmp;
- }
-
- /* Determine the name of the bucket */
- if ((tmp = strrchr(name, '/'))) {
- name = tmp + 1;
- }
-
- /* If no scheme and name are available the URI is invalid */
- if (ast_strlen_zero(scheme) || ast_strlen_zero(name)) {
- return NULL;
- }
-
- if (!(file = ast_sorcery_alloc(bucket_sorcery, "file", name))) {
+ if (!(full_uri = ast_strdupa(uri))) {
+ return NULL;
+ }
+
+ if (bucket_uri_parse(full_uri, &uri_scheme, &uri_name) ||
+ ast_strlen_zero(uri_scheme) || ast_strlen_zero(uri_name)) {
+ return NULL;
+ }
+
+ if (!(file = ast_sorcery_alloc(bucket_sorcery, "file", uri_name))) {
return NULL;
}
ast_string_field_set(file, uri, uri);
- ast_string_field_set(file, scheme, scheme);
+ ast_string_field_set(file, scheme, uri_scheme);
if (!tmpnam(file->path)) {
return NULL;
More information about the asterisk-commits
mailing list