[Asterisk-code-review] func_min and func_max: Two new dialplan functions (asterisk[master])

N A asteriskteam at digium.com
Sun May 16 10:24:12 CDT 2021


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15892 )


Change subject: func_min and func_max: Two new dialplan functions
......................................................................

func_min and func_max: Two new dialplan functions

Introduces two new dialplan functions, MIN and MAX,
which can be used to calculate the minimum or
maximum of up to two integers.

ASTERISK-29431

Change-Id: I2bda9269d18f9d54833c85e48e41fce0e0ce4d8d
---
A doc/CHANGES-staging/func_min_max.txt
A funcs/func_max.c
A funcs/func_min.c
3 files changed, 200 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/92/15892/1

diff --git a/doc/CHANGES-staging/func_min_max.txt b/doc/CHANGES-staging/func_min_max.txt
new file mode 100644
index 0000000..cc4e6e0
--- /dev/null
+++ b/doc/CHANGES-staging/func_min_max.txt
@@ -0,0 +1,4 @@
+Subject: func_min and func_max
+
+Introduce two new functions, MIN and MAX, which can be used to
+obtain the minimum or maximum of up to two integers.
diff --git a/funcs/func_max.c b/funcs/func_max.c
new file mode 100644
index 0000000..ab717ae
--- /dev/null
+++ b/funcs/func_max.c
@@ -0,0 +1,98 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2021, Digium, Inc.
+ *
+ * Naveen Albert <asterisk at phreaknet.org>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Maximum number
+ *
+ * \author Naveen Albert <asterisk at phreaknet.org>
+ *
+ * \ingroup functions
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
+#include "asterisk/app.h"
+
+/*** DOCUMENTATION
+	<function name="MAX" language="en_US">
+		<synopsis>
+			Returns the maximum of two integers.
+		</synopsis>
+		<syntax>
+			<parameter name="num1" />
+			<parameter name="num2" />
+		</syntax>
+		<description>
+			<para>Returns the maximum of two integers <replaceable>num1</replaceable> and <replaceable>num2</replaceable>.</para>
+			<para>Example:  Set(max=${MAX(4,7)});
+			Sets the max variable equal to 7.</para>
+		</description>
+	</function>
+ ***/
+static int acf_max_exec(struct ast_channel *chan, const char *cmd,
+			 char *parse, char *buffer, size_t buflen)
+{
+	int int1, response_int, int2;
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(num1);
+			     AST_APP_ARG(num2);
+	);
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (ast_strlen_zero(args.num1) || sscanf(args.num1, "%30d", &int1) != 1)
+		int1 = 0;
+
+	if (ast_strlen_zero(args.num2) || sscanf(args.num2, "%30d", &int2) != 1)
+		int2 = int1;
+	
+	response_int = (int1 < int2) ? int2 : int1;
+	ast_debug(1, "%d is the maximum of [%d,%d]\n", response_int, int1, int2);
+	snprintf(buffer, buflen, "%d", response_int);
+
+	return 0;
+}
+
+static struct ast_custom_function acf_max = {
+	.name = "MAX",
+	.read = acf_max_exec,
+	.read_max = 12,
+};
+
+static int unload_module(void)
+{
+	ast_custom_function_unregister(&acf_max);
+
+	return 0;
+}
+
+static int load_module(void)
+{
+	return ast_custom_function_register(&acf_max);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Maximum number dialplan function");
\ No newline at end of file
diff --git a/funcs/func_min.c b/funcs/func_min.c
new file mode 100644
index 0000000..e0714f7
--- /dev/null
+++ b/funcs/func_min.c
@@ -0,0 +1,98 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2021, Digium, Inc.
+ *
+ * Naveen Albert <asterisk at phreaknet.org>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Minimum number
+ *
+ * \author Naveen Albert <asterisk at phreaknet.org>
+ *
+ * \ingroup functions
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
+#include "asterisk/app.h"
+
+/*** DOCUMENTATION
+	<function name="MIN" language="en_US">
+		<synopsis>
+			Returns the minimum of two integers.
+		</synopsis>
+		<syntax>
+			<parameter name="num1" />
+			<parameter name="num2" />
+		</syntax>
+		<description>
+			<para>Returns the minimum of two integers <replaceable>num1</replaceable> and <replaceable>num2</replaceable>.</para>
+			<para>Example:  Set(min=${MIN(7,4)});
+			Sets the min variable equal to 4.</para>
+		</description>
+	</function>
+ ***/
+static int acf_min_exec(struct ast_channel *chan, const char *cmd,
+			 char *parse, char *buffer, size_t buflen)
+{
+	int int1, response_int, int2;
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(num1);
+			     AST_APP_ARG(num2);
+	);
+
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (ast_strlen_zero(args.num1) || sscanf(args.num1, "%30d", &int1) != 1)
+		int1 = 0;
+
+	if (ast_strlen_zero(args.num2) || sscanf(args.num2, "%30d", &int2) != 1)
+		int2 = int1;
+	
+	response_int = (int1 > int2) ? int2 : int1;
+	ast_debug(1, "%d is the minimum of [%d,%d]\n", response_int, int1, int2);
+	snprintf(buffer, buflen, "%d", response_int);
+
+	return 0;
+}
+
+static struct ast_custom_function acf_min = {
+	.name = "MIN",
+	.read = acf_min_exec,
+	.read_max = 12,
+};
+
+static int unload_module(void)
+{
+	ast_custom_function_unregister(&acf_min);
+
+	return 0;
+}
+
+static int load_module(void)
+{
+	return ast_custom_function_register(&acf_min);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Minimum number dialplan function");
\ No newline at end of file

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I2bda9269d18f9d54833c85e48e41fce0e0ce4d8d
Gerrit-Change-Number: 15892
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210516/908e0609/attachment-0001.html>


More information about the asterisk-code-review mailing list