Browse code

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)

Andrei Pelinescu-Onciul authored on 27/11/2008 00:47:23
Showing 2 changed files
... ...
@@ -38,6 +38,7 @@
38 38
  *  2006-02-07  added fix_flag (andrei)
39 39
  *  2008-02-29  store all the reponse callbacks in their own array (andrei)
40 40
  *  2008-11-17  support dual module interface: ser & kamailio (andrei)
41
+ *  2008-11-26  added fparam_free_contents() and fix_param_types (andrei)
41 42
  */
42 43
 
43 44
 
... ...
@@ -1174,6 +1175,27 @@ void fparam_free_contents(fparam_t* fp)
1174 1174
 }
1175 1175
 
1176 1176
 
1177
+
1178
+/** fix a param to one of the given types (mask).
1179
+  *
1180
+  * @param types - bitmap of the allowed types (e.g. FPARAM_INT|FPARAM_STR)
1181
+  * @param param - value/result
1182
+  * @return - 0 on success, -1 on error, 1 if param doesn't
1183
+  *           match any of the types
1184
+  */
1185
+int fix_param_types(int types, void** param)
1186
+{
1187
+	int ret;
1188
+	int t;
1189
+	
1190
+	for (t=types & ~(types-1); types; types&=(types-1), t=types & ~(types-1)){
1191
+		if ((ret=fix_param(t, param))<=0) return ret;
1192
+	}
1193
+	return E_UNSPEC;
1194
+}
1195
+
1196
+
1197
+
1177 1198
 /*
1178 1199
  * Fixup variable string, the parameter can be
1179 1200
  * AVP, SELECT, or ordinary string. AVP and select
... ...
@@ -46,6 +46,7 @@
46 46
  *              dual module interface support: ser & kamailio (andrei)
47 47
  *  2008-11-18  prototypes for various fixed parameters numbers module
48 48
  *               functions (3, 4, 5 & 6) and variable parameters (andrei)
49
+ *  2008-11-26  added fparam_free_contents() and fix_param_types (andrei)
49 50
  */
50 51
 
51 52
 /*!
... ...
@@ -234,17 +235,23 @@ struct param_export_ {
234 234
 };
235 235
 
236 236
 
237
+
238
+/** allowed parameter types.
239
+  * the types that cannot be deduced from the string, should
240
+  * be at the end (e.g. FPARAM_STR), to allow fallback 
241
+  * (e.g. fix_param_types(FPARAM_AVP|FPARAM_SELECT|FPARAM_STR, param))
242
+  */
237 243
 enum {
238 244
 	FPARAM_UNSPEC = 0,
239
-	FPARAM_STRING = (1 << 0),
240
-	FPARAM_STR    = (1 << 1),
241
-	FPARAM_INT    = (1 << 2),
242
-	FPARAM_REGEX  = (1 << 3),
243
-	FPARAM_AVP    = (1 << 5),
244
-	FPARAM_SELECT = (1 << 6),
245
-	FPARAM_SUBST  = (1 << 7),
246
-	FPARAM_PVS    = (1 << 8),
247
-	FPARAM_PVE    = (1 << 9)
245
+	FPARAM_INT    = (1 << 0),
246
+	FPARAM_REGEX  = (1 << 1),
247
+	FPARAM_AVP    = (1 << 2),
248
+	FPARAM_SELECT = (1 << 3),
249
+	FPARAM_SUBST  = (1 << 4),
250
+	FPARAM_PVS    = (1 << 5),
251
+	FPARAM_PVE    = (1 << 6),
252
+	FPARAM_STRING = (1 << 7),
253
+	FPARAM_STR    = (1 << 8)
248 254
 };
249 255
 
250 256
 /*
... ...
@@ -419,6 +426,10 @@ int fix_flag( modparam_t type, void* val,
419 419
 int fix_param(int type, void** param);
420 420
 void fparam_free_contents(fparam_t* fp);
421 421
 
422
+/** fix a param to one of the given types (mask).
423
+  */
424
+int fix_param_types(int types, void** param);
425
+
422 426
 /*
423 427
  * Fixup variable string, the parameter can be
424 428
  * AVP, SELECT, or ordinary string. AVP and select