[sr-dev] git:master: pv: two new functions- typeof(...) and not_empty(...)

Daniel-Constantin Mierla miconda at gmail.com
Sat Oct 6 09:57:36 CEST 2012


Module: sip-router
Branch: master
Commit: 007ca3c30b02e7a57cd229ea4b63573ef81f8c81
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=007ca3c30b02e7a57cd229ea4b63573ef81f8c81

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Sat Oct  6 09:52:37 2012 +0200

pv: two new functions- typeof(...) and not_empty(...)

- typeof(pvar, vtype) - test the type of the pseudo-variable
- not_empty(pvar) - test if the type is string and the value not empty

---

 modules_k/pv/README           |   67 +++++++++++++++++++++++++++++++---------
 modules_k/pv/doc/pv_admin.xml |   47 ++++++++++++++++++++++++++++-
 modules_k/pv/pv.c             |   67 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 165 insertions(+), 16 deletions(-)

diff --git a/modules_k/pv/README b/modules_k/pv/README
index a43da99..cbcb584 100644
--- a/modules_k/pv/README
+++ b/modules_k/pv/README
@@ -11,9 +11,9 @@ Daniel-Constantin Mierla
 
    <daniel at asipto.com>
 
-   Copyright © 2008-2011 Daniel-Constantin Mierla (asipto.com)
+   Copyright © 2008-2011 Daniel-Constantin Mierla (asipto.com)
 
-   Copyright © 2011 Juha Heinanen
+   Copyright © 2011 Juha Heinanen
      __________________________________________________________________
 
    Table of Contents
@@ -37,6 +37,8 @@ Daniel-Constantin Mierla
               4.1. pv_isset(pvar)
               4.2. pv_unset(pvar)
               4.3. is_int(pvar)
+              4.4. typeof(pvar, vtype)
+              4.5. not_empty(pvar)
 
         5. MI Commands
 
@@ -51,8 +53,10 @@ Daniel-Constantin Mierla
    1.4. pv_isset usage
    1.5. pv_unset usage
    1.6. is_int() usage
-   1.7. shv_set usage
-   1.8. shv_get usage
+   1.7. typeof() usage
+   1.8. not_empty() usage
+   1.9. shv_set usage
+   1.10. shv_get usage
 
 Chapter 1. Admin Guide
 
@@ -75,6 +79,8 @@ Chapter 1. Admin Guide
         4.1. pv_isset(pvar)
         4.2. pv_unset(pvar)
         4.3. is_int(pvar)
+        4.4. typeof(pvar, vtype)
+        4.5. not_empty(pvar)
 
    5. MI Commands
 
@@ -118,11 +124,11 @@ Chapter 1. Admin Guide
    _value_
      * _name_: shared variable name
      * _type_: type of the value
-          + “i”: integer value
-          + “s”: string value
+          + "i": integer value
+          + "s": string value
      * _value_: value to be set
 
-   Default value is “NULL”.
+   Default value is "NULL".
 
    Example 1.1. shvset parameter usage
 ...
@@ -139,11 +145,11 @@ modparam("pv", "shvset", "pstngw=s:sip:10.10.10.10")
    _value_
      * _name_: shared variable name
      * _type_: type of the value
-          + “i”: integer value
-          + “s”: string value
+          + "i": integer value
+          + "s": string value
      * _value_: value to be set
 
-   Default value is “NULL”.
+   Default value is "NULL".
 
    Example 1.2. varset parameter usage
 ...
@@ -167,6 +173,8 @@ modparam("pv","avp_aliases","email=s:email_addr;tmp=i:100")
    4.1. pv_isset(pvar)
    4.2. pv_unset(pvar)
    4.3. is_int(pvar)
+   4.4. typeof(pvar, vtype)
+   4.5. not_empty(pvar)
 
 4.1. pv_isset(pvar)
 
@@ -199,7 +207,7 @@ if(pv_isset("$avp("s:x")"))
 pv_unset("$avp("s:x")");
 ...
 
-4.3.  is_int(pvar)
+4.3. is_int(pvar)
 
    Function checks if pvar argument contains integer value and returns 1
    if it does and -1 otherwise.
@@ -213,6 +221,35 @@ if (is_int("$var(foo)")) {
 }
 ...
 
+4.4. typeof(pvar, vtype)
+
+   Returns true if the type of pseudo-variable matches the second
+   parameter. The second parameter can be: 'int' - type is integer; 'str'
+   - type is string; 'null' - type is null.
+
+   Function can be used from ANYROUTE.
+
+   Example 1.7. typeof() usage
+...
+if (trypeof("$var(foo)", "str")) {
+    xdbg("variable foo is a string\n");
+}
+...
+
+4.5. not_empty(pvar)
+
+   Returns true if the pseudo-variables has the type string and is not
+   empty value.
+
+   Function can be used from all kinds of routes.
+
+   Example 1.8. not_empty() usage
+...
+if (not_empty("$var(foo)")) {
+    append_hf("X-Foo: $var(foo)\r\n");
+}
+...
+
 5. MI Commands
 
    5.1. shv_set
@@ -225,8 +262,8 @@ if (is_int("$var(foo)")) {
    Parameters:
      * _name_: shared variable name
      * _type_: type of the value
-          + “int”: integer value
-          + “str”: string value
+          + "int": integer value
+          + "str": string value
      * _value_: value to be set
 
    MI FIFO Command Format:
@@ -236,7 +273,7 @@ if (is_int("$var(foo)")) {
                 _value_
                 _empty_line_
 
-   Example 1.7. shv_set usage
+   Example 1.9. shv_set usage
 ...
 $ kamctl fifo shv_set debug int 0
 ...
@@ -254,7 +291,7 @@ $ kamctl fifo shv_set debug int 0
                 _name_
                 _empty_line_
 
-   Example 1.8. shv_get usage
+   Example 1.10. shv_get usage
 ...
 $ kamctl fifo shv_get debug
 $ kamctl fifo shv_get
diff --git a/modules_k/pv/doc/pv_admin.xml b/modules_k/pv/doc/pv_admin.xml
index d826703..51f6c40 100644
--- a/modules_k/pv/doc/pv_admin.xml
+++ b/modules_k/pv/doc/pv_admin.xml
@@ -206,7 +206,7 @@ pv_unset("$avp("s:x")");
 			<para>
 			Function checks if pvar argument contains integer value
 			and returns 1 if it does and -1 otherwise.
-	    	        </para>
+			</para>
 			<para>
 			Function can be used from all kinds of routes.
 			</para>
@@ -221,6 +221,51 @@ if (is_int("$var(foo)")) {
 				</programlisting>
 			</example>
 		</section>
+		<section>
+			<title>
+				<function moreinfo="none">typeof(pvar, vtype)</function>
+			</title>
+			<para>
+			Returns true if the type of pseudo-variable matches the second
+			parameter. The second parameter can be: 'int' - type is integer;
+			'str' - type is string; 'null' - type is null.
+			</para>
+			<para>
+			Function can be used from ANYROUTE.
+			</para>
+			<example>
+				<title><function>typeof()</function> usage</title>
+				<programlisting format="linespecific">
+...
+if (trypeof("$var(foo)", "str")) {
+    xdbg("variable foo is a string\n");
+}
+...
+				</programlisting>
+			</example>
+		</section>
+		<section>
+			<title>
+				<function moreinfo="none">not_empty(pvar)</function>
+			</title>
+			<para>
+			Returns true if the pseudo-variables has the type string and
+			is not empty value.
+			</para>
+			<para>
+			Function can be used from all kinds of routes.
+			</para>
+			<example>
+				<title><function>not_empty()</function> usage</title>
+				<programlisting format="linespecific">
+...
+if (not_empty("$var(foo)")) {
+    append_hf("X-Foo: $var(foo)\r\n");
+}
+...
+				</programlisting>
+			</example>
+		</section>
 	</section>
 
 	<section>
diff --git a/modules_k/pv/pv.c b/modules_k/pv/pv.c
index 6bd0feb..41fcfa6 100644
--- a/modules_k/pv/pv.c
+++ b/modules_k/pv/pv.c
@@ -437,6 +437,8 @@ static void mod_destroy(void);
 static int pv_isset(struct sip_msg* msg, char* pvid, char *foo);
 static int pv_unset(struct sip_msg* msg, char* pvid, char *foo);
 static int is_int(struct sip_msg* msg, char* pvar, char* s2);
+static int pv_typeof(sip_msg_t *msg, char *pv, char *t);
+static int pv_not_empty(sip_msg_t *msg, char *pv, char *s2);
 
 static cmd_export_t cmds[]={
 	{"pv_isset",  (cmd_function)pv_isset,  1, fixup_pvar_null, 0, 
@@ -449,6 +451,12 @@ static cmd_export_t cmds[]={
 #endif
 	{"is_int", (cmd_function)is_int, 1, fixup_pvar_null, fixup_free_pvar_null,
 		ANY_ROUTE},
+	{"typeof", (cmd_function)pv_typeof,       2, fixup_pvar_none,
+		fixup_free_pvar_none,
+		ANY_ROUTE},
+	{"not_empty", (cmd_function)pv_not_empty, 1, fixup_pvar_null,
+		fixup_free_pvar_null,
+		ANY_ROUTE},
 
 	{0,0,0,0,0,0}
 };
@@ -536,6 +544,65 @@ static int add_avp_aliases(modparam_t type, void* val)
 }
 
 /**
+ * match the type of the variable value
+ */
+static int pv_typeof(sip_msg_t *msg, char *pv, char *t)
+{
+	pv_value_t val;
+
+	if (pv==NULL || t==NULL)
+		return -1;
+	if(pv_get_spec_value(msg, (pv_spec_t*)pv, &val) != 0)
+		return -1;
+
+	switch(t[0]) {
+		case 'i':
+		case 'I':
+			if(val.flags & PV_TYPE_INT)
+				return 1;
+			return -1;
+		case 'n':
+		case 'N':
+			if(val.flags & PV_VAL_NULL)
+				return 1;
+			return -1;
+		case 's':
+		case 'S':
+			if(!(val.flags & PV_VAL_STR))
+				return -1;
+			if(val.flags & PV_TYPE_INT)
+				return -1;
+			return 1;
+		default:
+			return -1;
+	}
+}
+
+/**
+ * return true if the type is string and value not empty
+ */
+static int pv_not_empty(sip_msg_t *msg, char *pv, char *s2)
+{
+	pv_value_t val;
+
+	if (pv==NULL)
+		return -1;
+
+	if(pv_get_spec_value(msg, (pv_spec_t*)pv, &val) != 0)
+		return -1;
+
+	if(!(val.flags & PV_VAL_STR))
+		return -1;
+	if(val.flags & PV_TYPE_INT)
+		return -1;
+
+	if(val.rs.len>0)
+		return 1;
+
+	return -1;
+}
+
+/**
  * Copyright (C) 2011 Juha Heinanen
  *
  * Checks if pvar argument contains int value




More information about the sr-dev mailing list