[Frugalware-git] fwpl: utility.c * add new function * add assert to xmalloc

2009-09-20 Thread James Buren
Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=fwpl.git;a=commitdiff;h=3b7fafbd0e51aa291e3404882407bb951f665f93

commit 3b7fafbd0e51aa291e3404882407bb951f665f93
Author: James Buren 
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 
+#include 
+#include 
+#include 
#include 
#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


[Frugalware-git] fwpl: utility.c * add new function

2009-09-20 Thread James Buren
Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=fwpl.git;a=commitdiff;h=b0cdba2a2450a09381279dbd94b56e066ce469b6

commit b0cdba2a2450a09381279dbd94b56e066ce469b6
Author: James Buren 
Date:   Sun Sep 20 13:00:13 2009 -0500

utility.c
* add new function

diff --git a/src/utility.c b/src/utility.c
index 97a8115..638965e 100644
--- a/src/utility.c
+++ b/src/utility.c
@@ -22,6 +22,19 @@ void *xmalloc(size_t size) {
return ptr;
}

+void *xrealloc(void *ptr,size_t size) {
+  assert(ptr and size);
+
+  ptr = realloc(ptr,size);
+
+  if(!ptr) {
+eprintf("Memory reallocation failure of %u bytes.\n",size);
+abort();
+  }
+
+  return ptr;
+}
+
char *xstrdup(const char *str1) {
char *str;
size_t size;
___
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git