[SR-Dev] git:andrei/fixups: fixups: fix param function using a param type mask

Andrei Pelinescu-Onciul andrei at iptel.org
Thu Nov 27 02:49:58 CET 2008


Module: sip-router
Branch: andrei/fixups
Commit: bf9d1c43796843bb25f33ab68d219806c5286506
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=bf9d1c43796843bb25f33ab68d219806c5286506

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Thu Nov 27 01:47:23 2008 +0100

fixups: fix param function using a param type mask

- added generic fix_param_type(types_mask, param) function which will
 try all the types specified in the mask in sequence, until if manages
 to convert the param to the corresponding type, or it finishes iterating
 through the types.
      (e.g. fix_param_types(FPARAM_AVP|FPARAM_SELECT|FPARAM_STR, param))
- changed FPARAM_* order such that more generic types are at the end
 (types having less chance of failing a conversion, like FPARAM_STR)

---

 sr_module.c |   22 ++++++++++++++++++++++
 sr_module.h |   29 ++++++++++++++++++++---------
 2 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/sr_module.c b/sr_module.c
index 114f0d2..497f8b7 100644
--- a/sr_module.c
+++ b/sr_module.c
@@ -38,6 +38,7 @@
  *  2006-02-07  added fix_flag (andrei)
  *  2008-02-29  store all the reponse callbacks in their own array (andrei)
  *  2008-11-17  support dual module interface: ser & kamailio (andrei)
+ *  2008-11-26  added fparam_free_contents() and fix_param_types (andrei)
  */
 
 
@@ -1174,6 +1175,27 @@ void fparam_free_contents(fparam_t* fp)
 }
 
 
+
+/** fix a param to one of the given types (mask).
+  *
+  * @param types - bitmap of the allowed types (e.g. FPARAM_INT|FPARAM_STR)
+  * @param param - value/result
+  * @return - 0 on success, -1 on error, 1 if param doesn't
+  *           match any of the types
+  */
+int fix_param_types(int types, void** param)
+{
+	int ret;
+	int t;
+	
+	for (t=types & ~(types-1); types; types&=(types-1), t=types & ~(types-1)){
+		if ((ret=fix_param(t, param))<=0) return ret;
+	}
+	return E_UNSPEC;
+}
+
+
+
 /*
  * Fixup variable string, the parameter can be
  * AVP, SELECT, or ordinary string. AVP and select
diff --git a/sr_module.h b/sr_module.h
index 1af0ea4..149ca6e 100644
--- a/sr_module.h
+++ b/sr_module.h
@@ -46,6 +46,7 @@
  *              dual module interface support: ser & kamailio (andrei)
  *  2008-11-18  prototypes for various fixed parameters numbers module
  *               functions (3, 4, 5 & 6) and variable parameters (andrei)
+ *  2008-11-26  added fparam_free_contents() and fix_param_types (andrei)
  */
 
 /*!
@@ -234,17 +235,23 @@ struct param_export_ {
 };
 
 
+
+/** allowed parameter types.
+  * the types that cannot be deduced from the string, should
+  * be at the end (e.g. FPARAM_STR), to allow fallback 
+  * (e.g. fix_param_types(FPARAM_AVP|FPARAM_SELECT|FPARAM_STR, param))
+  */
 enum {
 	FPARAM_UNSPEC = 0,
-	FPARAM_STRING = (1 << 0),
-	FPARAM_STR    = (1 << 1),
-	FPARAM_INT    = (1 << 2),
-	FPARAM_REGEX  = (1 << 3),
-	FPARAM_AVP    = (1 << 5),
-	FPARAM_SELECT = (1 << 6),
-	FPARAM_SUBST  = (1 << 7),
-	FPARAM_PVS    = (1 << 8),
-	FPARAM_PVE    = (1 << 9)
+	FPARAM_INT    = (1 << 0),
+	FPARAM_REGEX  = (1 << 1),
+	FPARAM_AVP    = (1 << 2),
+	FPARAM_SELECT = (1 << 3),
+	FPARAM_SUBST  = (1 << 4),
+	FPARAM_PVS    = (1 << 5),
+	FPARAM_PVE    = (1 << 6),
+	FPARAM_STRING = (1 << 7),
+	FPARAM_STR    = (1 << 8)
 };
 
 /*
@@ -419,6 +426,10 @@ int fix_flag( modparam_t type, void* val,
 int fix_param(int type, void** param);
 void fparam_free_contents(fparam_t* fp);
 
+/** fix a param to one of the given types (mask).
+  */
+int fix_param_types(int types, void** param);
+
 /*
  * Fixup variable string, the parameter can be
  * AVP, SELECT, or ordinary string. AVP and select




More information about the sr-dev mailing list