Re: [-] Re: non standard message_id breaks DLR

Angus M Wood <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Organization IBN
Message-ID <oprrscv6w3a92gwv@localhost>
On Thu, 3 Jul 2003 20:55:32 +0000 (UTC), Bruno Rodrigues 
<[email protected]> wrote:

> glibc already includes an regexp library. If you want to do this (and I
> would really appreciate some octstr_reg_match and octstr_reg_replace ;))
> you should create an abstraction layer in gwlib/ and compile it against
> glibc or against others - for *bsd and solaris?.

Ok, attached is a tentative patch against gwlib/octstr.c adding 
octstr_reg_matches(Octstr *haystack, Octstr *needle) where needle is
a regexp pattern. Returns 0 on hit, 1 on miss or -1 on invalid pattern.

See "man 7 regex" for regex syntax info.

If that sort of thing is satisfactory, I'm thinking returning a List
object from an 'octstr_reg_getmatches' would probably be useful, and
just the modified Octstr* from octstr_reg_replace?

Cheers,
	_Gus



-- 
Angus M Wood
Chief Architect
Inspired Broadcast Networks
http://www.inspiredbroadcast.net/
Out of Home Pay to Play Networked Entertainment
1-7 Livonia Street, London,  W1F 8AD, UK
Tel: +44 (0)20 7478 8280
Mob: +44 (0)7767 393039
Fax: +44 (0)20 7287 0131
test_octstr_regexp.c (application/octet-stream, 401 B)
/*
 * test_octstr_regexp.c - simple testing of octstr_regexp_match()
 */

#include "gwlib/gwlib.h"

int main(int argc, char *argv[]) {

	if (argc != 3) exit(-1);
	
	Octstr *os, *os2, *os3, *os4;

	gwlib_init();


	os = octstr_imm(argv[1]);
	os2 = octstr_imm(argv[2]);
	
	printf("Got [%d]\n",octstr_reg_matches(os,os2));


	octstr_destroy(os);
	octstr_destroy(os2);
	
	gwlib_shutdown();
	
	return 0;
}
regexp.patch (application/octet-stream, 1.5 KB)
? test/test_octstr_regexp.c
Index: gwlib/octstr.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/octstr.c,v
retrieving revision 1.145
diff -c -r1.145 octstr.c
*** gwlib/octstr.c	19 Jun 2003 12:55:40 -0000	1.145
--- gwlib/octstr.c	4 Jul 2003 12:13:12 -0000
***************
*** 16,21 ****
--- 16,23 ----
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
+ #include <regex.h>
+ 
  
  #include "gwlib.h"
  
***************
*** 927,932 ****
--- 929,975 ----
      return -1;    
  }
  
+ void print_regerror(int errorcode, regex_t *compiled)
+ {
+   size_t length = regerror(errorcode,compiled,NULL,0);
+   char *buffer = gw_malloc(length);
+   (void) regerror(errorcode,compiled, buffer, length);
+   debug("gwlib.octstr",0,"regexp compilation error: %s",buffer);
+ 
+ }
+ 
+ 
+ int octstr_reg_matches(Octstr *haystack, Octstr *needle)
+ {
+   char *errstr;
+   int errornum;
+   regex_t *r;
+   size_t len;
+     
+   gw_assert(haystack != NULL);
+   gw_assert(needle != NULL);
+   
+   r = gw_malloc(sizeof(regex_t));
+   
+   /* compile regexp */
+   errornum = regcomp(r,octstr_get_cstr(needle),REG_NOSUB | REG_EXTENDED);
+   if (errornum == 0) {
+     /* compile OK */
+     if (regexec(r,octstr_get_cstr(haystack),0,0,0) == 0) {
+       
+       return 0; /* hit */
+     }else{
+       return 1; /* miss */
+     }
+   }else {
+     print_regerror(errornum,r);
+ 
+     regfree(r);
+     gw_free(r);
+     
+     return -1;
+   }
+ }
  
  int octstr_print(FILE *f, Octstr *ostr)
  {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.