[dhcp-agent-commits] dhcp-agent/src Makefile.am,1.12,1.13 dhcp-libutil.h,1.21,1.22 dhcp-stringbuffer
Status: Alpha
Brought to you by:
actmodern
From: <act...@us...> - 2002-12-30 00:45:06
|
Update of /cvsroot/dhcp-agent/dhcp-agent/src In directory sc8-pr-cvs1:/tmp/cvs-serv12261/src Modified Files: Makefile.am dhcp-libutil.h dhcp-stringbuffer.c Log Message: stringbuffer able to do full string replacements Index: Makefile.am =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/Makefile.am,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** Makefile.am 22 Dec 2002 08:31:29 -0000 1.12 --- Makefile.am 30 Dec 2002 00:45:03 -0000 1.13 *************** *** 1,3 **** ! # $Header # # Main source Makefile.am --- 1,3 ---- ! # $Header$ # # Main source Makefile.am Index: dhcp-libutil.h =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-libutil.h,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** dhcp-libutil.h 29 Dec 2002 05:31:35 -0000 1.21 --- dhcp-libutil.h 30 Dec 2002 00:45:03 -0000 1.22 *************** *** 217,220 **** --- 217,221 ---- extern void stringbuffer_copy(stringbuffer_t *dest, stringbuffer_t *src); extern void stringbuffer_replace_c(stringbuffer_t *sb, char c, char replace); + extern void stringbuffer_replace(stringbuffer_t *sb, const char *string, const char *replace); extern void stringbuffer_align(stringbuffer_t *sb, int begin, int end); extern void stringbuffer_avprintf_align(stringbuffer_t *sb, int start, int end, const char *fmt, va_list ap); Index: dhcp-stringbuffer.c =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-stringbuffer.c,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** dhcp-stringbuffer.c 23 Dec 2002 00:51:25 -0000 1.9 --- dhcp-stringbuffer.c 30 Dec 2002 00:45:03 -0000 1.10 *************** *** 306,309 **** --- 306,369 ---- } + /* replace in stringbuffer occurances of c with replace */ + void stringbuffer_replace(stringbuffer_t *sb, const char *string, const char *replace) + { + char *ptr; + int i; + int str_len = strlen(string); + stringbuffer_t *sb_replace; + + if(string[0] == 0) + return; /* nothing to replace. */ + + sb_replace = stringbuffer_create(); + ptr = sb->buf; + + for(i = 0; i < sb->len; i++) { + + if((sb->len - i) < str_len) { + + /* copy in. */ + stringbuffer_copy(sb, sb_replace); + + /* append what's left. */ + stringbuffer_append(sb, &ptr[i]); + + /* free up. */ + stringbuffer_destroy(sb_replace); + + /* we're done. */ + return; + } + + if(ptr[i] == string[0]) { + + /* we know that we have at least enough to complete string. */ + if(!memcmp(&ptr[i], string, str_len)) { + + /* we have a match, replace. */ + stringbuffer_append(sb_replace, replace); + i += (str_len - 1); + continue; + } + } + + stringbuffer_append_c(sb_replace, ptr[i]); + } + + /* we're done: we should only get here if the last string to + be replaced ended the string itself. */ + + /* copy in. */ + stringbuffer_copy(sb, sb_replace); + + /* free up. */ + stringbuffer_destroy(sb_replace); + + /* we're done. */ + return; + + } + /* align a stringbuffer on begin and end columns. */ void stringbuffer_align(stringbuffer_t *sb, int begin, int end) |