[Asterisk-code-review] json.c/strings.c - Add a couple of utility functions (asterisk[master])

Kevin Harwell asteriskteam at digium.com
Fri Feb 8 14:51:12 CST 2019


Kevin Harwell has uploaded this change for review. ( https://gerrit.asterisk.org/10981


Change subject: json.c/strings.c - Add a couple of utility functions
......................................................................

json.c/strings.c - Add a couple of utility functions

Added 'ast_json_object_string_get' to the JSON wrapper in order to make is a
little easier to retrieve a string field from the JSON object.

Also added 'ast_strcmp_s' that does a NULL check on the compared strings prior
to calling strcmp.

Change-Id: I26f0a16d61537505eb41b4b05ef2e6d67fc2541b
---
M include/asterisk/json.h
M include/asterisk/strings.h
M main/json.c
M main/strings.c
4 files changed, 44 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/81/10981/1

diff --git a/include/asterisk/json.h b/include/asterisk/json.h
index 227afbb..e346b8d 100644
--- a/include/asterisk/json.h
+++ b/include/asterisk/json.h
@@ -562,6 +562,16 @@
 struct ast_json *ast_json_object_get(struct ast_json *object, const char *key);
 
 /*!
+ * \brief Get a string field from a JSON object.
+ *
+ * \param object JSON object.
+ * \param key Key of string field to look up.
+ * \return String value of given \a key.
+ * \return \c NULL on error, or key value is not a string.
+ */
+const char *ast_json_object_string_get(struct ast_json *object, const char *key);
+
+/*!
  * \brief Set a field in a JSON object.
  * \since 12.0.0
  *
diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h
index aaf2737..3332838 100644
--- a/include/asterisk/strings.h
+++ b/include/asterisk/strings.h
@@ -1298,6 +1298,17 @@
 char *ast_generate_random_string(char *buf, size_t size);
 
 /*!
+ * \brief Compare strings for equality checking for NULL.
+ *
+ * \param str1 The string to compare to str2
+ * \param str2 The string to compare to str1
+ *
+ * \return strcmp result if str1 and str2 are both not NULL, -1 if str2 is NULL,
+ *         1 if str1 is NULL, or -2 if both str1 and str2 are NULL.
+ */
+int ast_strcmp_s(const char *str1, const char *str2);
+
+/*!
  * \brief Compares 2 strings using realtime-style operators
  * \since 13.9.0
  *
diff --git a/main/json.c b/main/json.c
index 9a94767..94016cd 100644
--- a/main/json.c
+++ b/main/json.c
@@ -401,6 +401,12 @@
 	}
 	return (struct ast_json *)json_object_get((json_t *)object, key);
 }
+
+const char *ast_json_object_string_get(struct ast_json *object, const char *key)
+{
+	return ast_json_string_get(ast_json_object_get(object, key));
+}
+
 int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
 {
 	return json_object_set_new((json_t *)object, key, (json_t *)value);
diff --git a/main/strings.c b/main/strings.c
index a18bb48..6f0760f 100644
--- a/main/strings.c
+++ b/main/strings.c
@@ -236,6 +236,23 @@
 	return buf;
 }
 
+int ast_strcmp_s(const char *str1, const char *str2)
+{
+	if (str1 && str2) {
+		return strcmp(str1, str2);
+	}
+
+	if (!str1 && str2) {
+		return -1;
+	}
+
+	if (str1 && !str2) {
+		return 1;
+	}
+
+	return -2;
+}
+
 int ast_strings_match(const char *left, const char *op, const char *right)
 {
 	char *internal_op = (char *)op;

-- 
To view, visit https://gerrit.asterisk.org/10981
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I26f0a16d61537505eb41b4b05ef2e6d67fc2541b
Gerrit-Change-Number: 10981
Gerrit-PatchSet: 1
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190208/2c46fe7e/attachment-0001.html>


More information about the asterisk-code-review mailing list