Browse code

- re api minor extension (you can distinguish now between an internal error and a no match) - subst_user refurbished

For more info see http://mail.iptel.org/pipermail/serdev/2004-July/002271.html
(whole thread).

Andrei Pelinescu-Onciul authored on 12/11/2004 16:58:58
Showing 3 changed files
... ...
@@ -50,7 +50,7 @@ MAIN_NAME=ser
50 50
 VERSION = 0
51 51
 PATCHLEVEL = 8
52 52
 SUBLEVEL =   99
53
-EXTRAVERSION = -dev14
53
+EXTRAVERSION = -dev15
54 54
 
55 55
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
56 56
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
... ...
@@ -30,6 +30,7 @@
30 30
  * History:
31 31
  * --------
32 32
  *   2003-08-04  created by andrei
33
+ *   2004-11-12  minor api extension, added *count (andrei)
33 34
  */
34 35
 
35 36
 
... ...
@@ -386,8 +387,12 @@ error:
386 387
 
387 388
 
388 389
 /* WARNING: input must be 0 terminated! */
390
+/* returns: 0 if no match or error, or subst result; if count!=0
391
+ *           it will be set to 0 (no match), the number of matches
392
+ *           or -1 (error).
393
+ */
389 394
 struct replace_lst* subst_run(struct subst_expr* se, const char* input,
390
-								struct sip_msg* msg)
395
+								struct sip_msg* msg, int* count)
391 396
 {
392 397
 	struct replace_lst *head;
393 398
 	struct replace_lst **crt;
... ...
@@ -396,10 +401,12 @@ struct replace_lst* subst_run(struct subst_expr* se, const char* input,
396 401
 	regmatch_t* pmatch;
397 402
 	int nmatch;
398 403
 	int eflags;
404
+	int cnt;
399 405
 	
400 406
 	
401 407
 	/* init */
402 408
 	head=0;
409
+	cnt=0;
403 410
 	crt=&head;
404 411
 	p=input;
405 412
 	nmatch=se->max_pmatch+1;
... ...
@@ -439,21 +446,28 @@ struct replace_lst* subst_run(struct subst_expr* se, const char* input,
439 446
 			}
440 447
 			crt=&((*crt)->next);
441 448
 			p+=pmatch[0].rm_eo;
449
+			cnt++;
442 450
 		}
443 451
 	}while((r==0) && se->replace_all);
444 452
 	pkg_free(pmatch);
453
+	if (count)*count=cnt;
445 454
 	return head;
446 455
 error:
447 456
 	if (head) replace_lst_free(head);
448 457
 	if (pmatch) pkg_free(pmatch);
458
+	if (count) *count=-1;
449 459
 	return 0;
450 460
 }
451 461
 
452 462
 
453 463
 
454 464
 /* returns the substitution result in a str, input must be 0 term
455
- *  0 on no match or malloc error */ 
456
-str* subst_str(const char *input, struct sip_msg* msg, struct subst_expr* se)
465
+ *  0 on no match or malloc error
466
+ *  if count is non zero it will be set to the number of matches, or -1
467
+ *   if error 
468
+ */ 
469
+str* subst_str(const char *input, struct sip_msg* msg, struct subst_expr* se,
470
+				int* count)
457 471
 {
458 472
 	str* res;
459 473
 	struct replace_lst *lst;
... ...
@@ -468,7 +482,7 @@ str* subst_str(const char *input, struct sip_msg* msg, struct subst_expr* se)
468 482
 	/* compute the len */
469 483
 	len=strlen(input);
470 484
 	end=input+len;
471
-	lst=subst_run(se, input, msg);
485
+	lst=subst_run(se, input, msg, count);
472 486
 	if (lst==0){
473 487
 		DBG("subst_str: no match\n");
474 488
 		return 0;
... ...
@@ -510,5 +524,6 @@ error:
510 524
 		if (res->s) pkg_free(res->s);
511 525
 		pkg_free(res);
512 526
 	}
527
+	if (count) *count=-1;
513 528
 	return 0;
514 529
 }
... ...
@@ -30,6 +30,7 @@
30 30
  * History:
31 31
  * --------
32 32
  *   2003-08-04  created by andrei
33
+ *   2004-11-12  minor api extension, added *count (andrei)
33 34
  */
34 35
 
35 36
 #ifndef _re_h
... ...
@@ -74,8 +75,9 @@ void subst_expr_free(struct subst_expr* se);
74 75
 void replace_lst_free(struct replace_lst* l);
75 76
 struct subst_expr*  subst_parser(str* subst);
76 77
 struct replace_lst* subst_run( struct subst_expr* se, const char* input, 
77
-		                       struct sip_msg* msg);
78
-str* subst_str(const char* input, struct sip_msg* msg, struct subst_expr* se);
78
+		                       struct sip_msg* msg, int *count);
79
+str* subst_str(const char* input, struct sip_msg* msg,
80
+				struct subst_expr* se, int* count);
79 81
 
80 82
 
81 83