00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00026
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
00045
00046
00047 int agxbmore(agxbuf * xb, unsigned int ssz)
00048 {
00049 int cnt;
00050 int size;
00051 int nsize;
00052 unsigned char *nbuf;
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
00073
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
00085
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
00095
00096
00097 void agxbfree(agxbuf * xb)
00098 {
00099 if (xb->dyna)
00100 free(xb->buf);
00101 }
00102
00103
00104
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 }