Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwpl.git;a=commitdiff;h=3b7fafbd0e51aa291e3404882407bb951f665f93
commit 3b7fafbd0e51aa291e3404882407bb951f665f93 Author: James Buren <r...@frugalware.org> Date: Sun Sep 20 23:05:10 2009 -0500 utility.c * add new function * add assert to xmalloc diff --git a/src/utility.c b/src/utility.c index edee045..cc57e9d 100644 --- a/src/utility.c +++ b/src/utility.c @@ -1,13 +1,23 @@ //[of]:includes #include <stdlib.h> +#include <string.h> +#include <regex.h> +#include <iso646.h> #include <assert.h> #include "utility.h" //[cf] +//[of]:macros +#define REG_MATCH_FLAGS (REG_EXTENDED|REG_NOSUB) +#define REG_REPLACE_FLAGS (REG_EXTENDED) +//[cf] + //[of]:functions //[of]:xmalloc void *xmalloc(size_t num) { void *ptr; + assert((num > 0)); + ptr = malloc(num); if(!ptr) { @@ -17,4 +27,43 @@ void *xmalloc(size_t num) { return ptr; } //[cf] +//[of]:regreplace +bool regreplace(char **dest,const char *str,const char *pat,const char *sub) { + regex_t reg; + regmatch_t mat; + size_t n1, n2, n3, n4; + + assert((dest != NULL) and (str != NULL) and (pat != NULL) and (sub != NULL)); + + if(regcomp(®,pat,REG_REPLACE_FLAGS)) + return false; + + if(regexec(®,str,1,&mat,0)) { + regfree(®); + return false; + } + + n1 = mat.rm_so; + + n2 = strlen(sub); + + n3 = mat.rm_eo - mat.rm_so; + + n4 = strlen(str) - n3 + n2 + 1; + + (*dest) = new(char,n4); + + n3 += n1; + + strncpy(&(*dest)[0],str,n1); + + strncpy(&(*dest)[n1],sub,n2); + + strcpy(&(*dest)[n1+n2],&str[n3]); + + regfree(®); + + return true; +} +//[cf] //[cf] _______________________________________________ Frugalware-git mailing list Frugalware-git@frugalware.org http://frugalware.org/mailman/listinfo/frugalware-git