[Dhcp-agent-commits] dhcp-agent dhcp-stringbuffer.c,NONE,1.1 dhcp-stringbuffer.h,NONE,1.1 Makefile.a
Status: Alpha
Brought to you by:
actmodern
From: <act...@us...> - 2002-06-11 02:10:35
|
Update of /cvsroot/dhcp-agent/dhcp-agent In directory usw-pr-cvs1:/tmp/cvs-serv16798 Modified Files: Makefile.am Makefile.in TODO Added Files: dhcp-stringbuffer.c dhcp-stringbuffer.h Log Message: added stringbuffer object -- need to use this extensively in exception stack --- NEW FILE: dhcp-stringbuffer.c --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/dhcp-stringbuffer.c,v 1.1 2002/06/11 02:10:32 actmodern Exp $ * * Copyright 2001 Thamer Alharbash * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * */ #include <dhcp-agent.h> #include <dhcp-stringbuffer.h> /* create a new stringbuffer */ stringbuffer *make_stringbuffer(void) { stringbuffer *sb; sb = malloc(sizeof(stringbuffer)); sb->len = 0; sb->capacity = 0; sb->buf = malloc(sizeof(char) * 1); sb->buf[0] = 0; return sb; } /* destroy the stringbuffer */ void destroy_stringbuffer(stringbuffer *sb) { free(sb->buf); free(sb); } /* append string to stringbuffer */ void stringbuffer_append(stringbuffer *sb, char *s) { int len = strlen(s); sb->len += len; if(sb->capacity < sb->len) { sb->buf = realloc(sb->buf, sizeof(char) * (sb->len + 1)); sb->capacity = sb->len; } strcat(sb->buf, s); sb->buf[sb->len] = 0; } /* remove whitespace */ stringbuffer *stringbuffer_trim_whitespace(stringbuffer *sb) { char *ptr, *curptr; char *end, *begin; char *newbuf; if(sb->len == 0) /* empty string. */ return sb; /* create a new string */ newbuf = malloc(sizeof(sb->len + 1) * sizeof(char)); for(begin = &sb->buf[0]; *begin != '\0' && (*begin == ' ' || *begin == '\t'); begin++); if(*begin != '\0') { /* we do have whitespace in the beginning so find the end. */ for(end = &sb->buf[sb->len -1]; *end == ' ' || *end == '\t'; end--); if(end == begin) { /* all spaces! */ /* zip this string to nothing */ destroy_stringbuffer(sb); free(newbuf); return make_stringbuffer(); } else { /* there's something in between. * copy out. */ ptr = newbuf; for(curptr = begin;curptr != end;curptr++) { *ptr = *curptr; ptr++; } *ptr = 0; free(sb->buf); sb->buf = newbuf; } } else { /* zap beginning of string. since its all whitespace. */ free(newbuf); sb->buf[0] = 0; sb->len = 0; } return sb; } /* get the last occurance of a specific character. useful for slicing. */ char *stringbuffer_get_last_occurance(stringbuffer *sb, char c) { char *ptr, *ptrend = NULL; ptr = sb->buf; while((ptr = strchr(ptr, '\n')) != NULL) { ptrend = ptr; ptr++; } return ptrend; } /* remove the last newline character. */ void stringbuffer_trim_newline(stringbuffer *sb) { char *ptr; ptr = stringbuffer_get_last_occurance(sb, '\n'); if(ptr != NULL) *ptr = 0; ptr = stringbuffer_get_last_occurance(sb, '\r'); if(ptr != NULL) *ptr = 0; sb->len = strlen(sb->buf); return; } /* return the C string from the buffer. */ const char *stringbuffer_getstring(stringbuffer *sb) { return sb->buf; } --- NEW FILE: dhcp-stringbuffer.h --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/dhcp-stringbuffer.h,v 1.1 2002/06/11 02:10:32 actmodern Exp $ * * Copyright 2001 Thamer Alharbash * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * */ #ifndef DHCP_STRINGBUFFER_H #define DHCP_STRINGBUFFER_H /* string buffer object. */ typedef struct { int len; int capacity; char *buf; } stringbuffer; extern stringbuffer *make_stringbuffer(void); extern void destroy_stringbuffer(stringbuffer *sb); extern void stringbuffer_append(stringbuffer *sb, char *s); extern stringbuffer *stringbuffer_trim_whitespace(stringbuffer *sb); extern char *stringbuffer_get_last_occurance(stringbuffer *sb, char c); extern void stringbuffer_trim_newline(stringbuffer *sb); extern const char *stringbuffer_getstring(stringbuffer *sb); #endif /* DHCP_STRINGBUFFER_H */ Index: Makefile.am =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/Makefile.am,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** Makefile.am 25 May 2002 14:02:29 -0000 1.17 --- Makefile.am 11 Jun 2002 02:10:32 -0000 1.18 *************** *** 21,25 **** dhcp-client-states.c dhcp-options-strings.c dhcp-convert.c \ dhcp-sysconf.c dhcp-rtt.c dhcp-packet-build.c dhcp-icmp-discovery.c \ ! dhcp-arp-discovery.c dhcp-route.c dhcp-globconf.c dhcpclient_LDADD = @PCAP_LIB@ @DNET_LIB@ --- 21,25 ---- dhcp-client-states.c dhcp-options-strings.c dhcp-convert.c \ dhcp-sysconf.c dhcp-rtt.c dhcp-packet-build.c dhcp-icmp-discovery.c \ ! dhcp-arp-discovery.c dhcp-route.c dhcp-globconf.c dhcp-stringbuffer.c dhcpclient_LDADD = @PCAP_LIB@ @DNET_LIB@ Index: Makefile.in =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/Makefile.in,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** Makefile.in 25 May 2002 14:02:29 -0000 1.17 --- Makefile.in 11 Jun 2002 02:10:32 -0000 1.18 *************** *** 83,87 **** dhcpsniff_LDADD = @PCAP_LIB@ @DNET_LIB@ ! dhcpclient_SOURCES = dhcp-client.c dhcp-util.c dhcp-align.c dhcp-net.c dhcp-list.c dhcp-com.c dhcp-eth.c dhcp-ip.c dhcp-udp.c dhcp-arp.c dhcp-icmp.c dhcp-log.c dhcp-daemon.c dhcp-client-cache.c dhcp-cache-entry.c dhcp-client-control.c dhcp-interface.c dhcp-client-conf.c @DHCP_SNPRINTF@ dhcp-files.c dhcp-client-states.c dhcp-options-strings.c dhcp-convert.c dhcp-sysconf.c dhcp-rtt.c dhcp-packet-build.c dhcp-icmp-discovery.c dhcp-arp-discovery.c dhcp-route.c dhcp-globconf.c --- 83,87 ---- dhcpsniff_LDADD = @PCAP_LIB@ @DNET_LIB@ ! dhcpclient_SOURCES = dhcp-client.c dhcp-util.c dhcp-align.c dhcp-net.c dhcp-list.c dhcp-com.c dhcp-eth.c dhcp-ip.c dhcp-udp.c dhcp-arp.c dhcp-icmp.c dhcp-log.c dhcp-daemon.c dhcp-client-cache.c dhcp-cache-entry.c dhcp-client-control.c dhcp-interface.c dhcp-client-conf.c @DHCP_SNPRINTF@ dhcp-files.c dhcp-client-states.c dhcp-options-strings.c dhcp-convert.c dhcp-sysconf.c dhcp-rtt.c dhcp-packet-build.c dhcp-icmp-discovery.c dhcp-arp-discovery.c dhcp-route.c dhcp-globconf.c dhcp-stringbuffer.c *************** *** 117,121 **** dhcp-options-strings.o dhcp-convert.o dhcp-sysconf.o dhcp-rtt.o \ dhcp-packet-build.o dhcp-icmp-discovery.o dhcp-arp-discovery.o \ ! dhcp-route.o dhcp-globconf.o dhcpclient_DEPENDENCIES = dhcpclient_LDFLAGS = --- 117,121 ---- dhcp-options-strings.o dhcp-convert.o dhcp-sysconf.o dhcp-rtt.o \ dhcp-packet-build.o dhcp-icmp-discovery.o dhcp-arp-discovery.o \ ! dhcp-route.o dhcp-globconf.o dhcp-stringbuffer.o dhcpclient_DEPENDENCIES = dhcpclient_LDFLAGS = *************** *** 148,153 **** .deps/dhcp-options-strings.P .deps/dhcp-packet-build.P \ .deps/dhcp-print.P .deps/dhcp-route.P .deps/dhcp-rtt.P \ ! .deps/dhcp-sniff.P .deps/dhcp-sniffer-ohandlers.P .deps/dhcp-sysconf.P \ ! .deps/dhcp-udp.P .deps/dhcp-util.P SOURCES = $(dhcpsniff_SOURCES) $(dhcpclient_SOURCES) OBJECTS = $(dhcpsniff_OBJECTS) $(dhcpclient_OBJECTS) --- 148,154 ---- .deps/dhcp-options-strings.P .deps/dhcp-packet-build.P \ .deps/dhcp-print.P .deps/dhcp-route.P .deps/dhcp-rtt.P \ ! .deps/dhcp-sniff.P .deps/dhcp-sniffer-ohandlers.P \ ! .deps/dhcp-stringbuffer.P .deps/dhcp-sysconf.P .deps/dhcp-udp.P \ ! .deps/dhcp-util.P SOURCES = $(dhcpsniff_SOURCES) $(dhcpclient_SOURCES) OBJECTS = $(dhcpsniff_OBJECTS) $(dhcpclient_OBJECTS) Index: TODO =================================================================== RCS file: /cvsroot/dhcp-agent/dhcp-agent/TODO,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** TODO 11 Jun 2002 02:05:52 -0000 1.22 --- TODO 11 Jun 2002 02:10:32 -0000 1.23 *************** *** 88,89 **** --- 88,91 ---- -- dhcpsniff needs as brief mode as well as some fixes to its current output (timestamps aren't looking too good) + -- get stringbuffer used in places where strings are being manipulated + heavily. |