Sfoglia il codice sorgente

- 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 21 anni fa
parent
commit
4e8f053ec0
3 ha cambiato i file con 24 aggiunte e 7 eliminazioni
  1. 1 1
      Makefile.defs
  2. 19 4
      re.c
  3. 4 2
      re.h

+ 1 - 1
Makefile.defs

@@ -50,7 +50,7 @@ MAIN_NAME=ser
 VERSION = 0
 PATCHLEVEL = 8
 SUBLEVEL =   99
-EXTRAVERSION = -dev14
+EXTRAVERSION = -dev15
 
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")

+ 19 - 4
re.c

@@ -30,6 +30,7 @@
  * History:
  * --------
  *   2003-08-04  created by andrei
+ *   2004-11-12  minor api extension, added *count (andrei)
  */
 
 
@@ -386,8 +387,12 @@ error:
 
 
 /* WARNING: input must be 0 terminated! */
+/* returns: 0 if no match or error, or subst result; if count!=0
+ *           it will be set to 0 (no match), the number of matches
+ *           or -1 (error).
+ */
 struct replace_lst* subst_run(struct subst_expr* se, const char* input,
-								struct sip_msg* msg)
+								struct sip_msg* msg, int* count)
 {
 	struct replace_lst *head;
 	struct replace_lst **crt;
@@ -396,10 +401,12 @@ struct replace_lst* subst_run(struct subst_expr* se, const char* input,
 	regmatch_t* pmatch;
 	int nmatch;
 	int eflags;
+	int cnt;
 	
 	
 	/* init */
 	head=0;
+	cnt=0;
 	crt=&head;
 	p=input;
 	nmatch=se->max_pmatch+1;
@@ -439,21 +446,28 @@ struct replace_lst* subst_run(struct subst_expr* se, const char* input,
 			}
 			crt=&((*crt)->next);
 			p+=pmatch[0].rm_eo;
+			cnt++;
 		}
 	}while((r==0) && se->replace_all);
 	pkg_free(pmatch);
+	if (count)*count=cnt;
 	return head;
 error:
 	if (head) replace_lst_free(head);
 	if (pmatch) pkg_free(pmatch);
+	if (count) *count=-1;
 	return 0;
 }
 
 
 
 /* returns the substitution result in a str, input must be 0 term
- *  0 on no match or malloc error */ 
-str* subst_str(const char *input, struct sip_msg* msg, struct subst_expr* se)
+ *  0 on no match or malloc error
+ *  if count is non zero it will be set to the number of matches, or -1
+ *   if error 
+ */ 
+str* subst_str(const char *input, struct sip_msg* msg, struct subst_expr* se,
+				int* count)
 {
 	str* res;
 	struct replace_lst *lst;
@@ -468,7 +482,7 @@ str* subst_str(const char *input, struct sip_msg* msg, struct subst_expr* se)
 	/* compute the len */
 	len=strlen(input);
 	end=input+len;
-	lst=subst_run(se, input, msg);
+	lst=subst_run(se, input, msg, count);
 	if (lst==0){
 		DBG("subst_str: no match\n");
 		return 0;
@@ -510,5 +524,6 @@ error:
 		if (res->s) pkg_free(res->s);
 		pkg_free(res);
 	}
+	if (count) *count=-1;
 	return 0;
 }

+ 4 - 2
re.h

@@ -30,6 +30,7 @@
  * History:
  * --------
  *   2003-08-04  created by andrei
+ *   2004-11-12  minor api extension, added *count (andrei)
  */
 
 #ifndef _re_h
@@ -74,8 +75,9 @@ void subst_expr_free(struct subst_expr* se);
 void replace_lst_free(struct replace_lst* l);
 struct subst_expr*  subst_parser(str* subst);
 struct replace_lst* subst_run( struct subst_expr* se, const char* input, 
-		                       struct sip_msg* msg);
-str* subst_str(const char* input, struct sip_msg* msg, struct subst_expr* se);
+		                       struct sip_msg* msg, int *count);
+str* subst_str(const char* input, struct sip_msg* msg,
+				struct subst_expr* se, int* count);