/misc/src/release/graphviz-2.18-1/src/graphviz-2.18/lib/graph/agxbuf.c

Go to the documentation of this file.
00001 /* $Id: agxbuf.c,v 1.3 2006/01/04 20:04:51 erg Exp $ $Revision: 1.3 $ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /**********************************************************
00005 *      This software is part of the graphviz package      *
00006 *                http://www.graphviz.org/                 *
00007 *                                                         *
00008 *            Copyright (c) 1994-2004 AT&T Corp.           *
00009 *                and is licensed under the                *
00010 *            Common Public License, Version 1.0           *
00011 *                      by AT&T Corp.                      *
00012 *                                                         *
00013 *        Information and Software Systems Research        *
00014 *              AT&T Research, Florham Park NJ             *
00015 **********************************************************/
00016 
00017 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <agxbuf.h>
00022 
00023 #define N_GNEW(n,t)      (t*)malloc((n)*sizeof(t))
00024 
00025 /* agxbinit:
00026  * Assume if init is non-null, hint = sizeof(init[])
00027  */
00028 void agxbinit(agxbuf * xb, unsigned int hint, unsigned char *init)
00029 {
00030     if (init) {
00031         xb->buf = init;
00032         xb->dyna = 0;
00033     } else {
00034         if (hint == 0)
00035             hint = BUFSIZ;
00036         xb->dyna = 1;
00037         xb->buf = N_GNEW(hint, unsigned char);
00038     }
00039     xb->eptr = xb->buf + hint;
00040     xb->ptr = xb->buf;
00041     *xb->ptr = '\0';
00042 }
00043 
00044 /* agxbmore;
00045  * Expand buffer to hold at least ssz more bytes.
00046  */
00047 int agxbmore(agxbuf * xb, unsigned int ssz)
00048 {
00049     int cnt;                    /* current no. of characters in buffer */
00050     int size;                   /* current buffer size */
00051     int nsize;                  /* new buffer size */
00052     unsigned char *nbuf;        /* new buffer */
00053 
00054     size = xb->eptr - xb->buf;
00055     nsize = 2 * size;
00056     if (size + ssz > nsize)
00057         nsize = size + ssz;
00058     cnt = xb->ptr - xb->buf;
00059     if (xb->dyna) {
00060         nbuf = realloc(xb->buf, nsize);
00061     } else {
00062         nbuf = N_GNEW(nsize, unsigned char);
00063         memcpy(nbuf, xb->buf, cnt);
00064         xb->dyna = 1;
00065     }
00066     xb->buf = nbuf;
00067     xb->ptr = xb->buf + cnt;
00068     xb->eptr = xb->buf + nsize;
00069     return 0;
00070 }
00071 
00072 /* agxbput_n:
00073  * Append string s of length n onto xb
00074  */
00075 int agxbput_n(agxbuf * xb, char *s, unsigned int ssz)
00076 {
00077     if (xb->ptr + ssz > xb->eptr)
00078         agxbmore(xb, ssz);
00079     memcpy(xb->ptr, s, ssz);
00080     xb->ptr += ssz;
00081     return ssz;
00082 }
00083 
00084 /* agxbput:
00085  * Append string s into xb
00086  */
00087 int agxbput(agxbuf * xb, char *s)
00088 {
00089     unsigned int ssz = strlen(s);
00090 
00091     return agxbput_n(xb, s, ssz);
00092 }
00093 
00094 /* agxbfree:
00095  * Free any malloced resources.
00096  */
00097 void agxbfree(agxbuf * xb)
00098 {
00099     if (xb->dyna)
00100         free(xb->buf);
00101 }
00102 
00103 /* agxbpop:
00104  * Removes last character added, if any.
00105  */
00106 int agxbpop(agxbuf * xb)
00107 {
00108     int c;
00109     if (xb->ptr > xb->buf) {
00110         c = *xb->ptr--;
00111         return c;
00112     } else
00113         return -1;
00114 
00115 }

Generated on Mon Mar 31 19:03:26 2008 for Graphviz by  doxygen 1.5.1