1: #
   2: 
   3: /*
   4: **  BUFFER MANIPULATION ROUTINES
   5: */
   6: 
   7: # include   "buf.h"
   8: 
   9: 
  10: /*
  11: **  BUFPUT -- put character onto buffer
  12: **
  13: **	The character 'c' is put onto the buffer 'bp'.  If the buffer
  14: **	need be extended it is.
  15: */
  16: 
  17: bufput(c, buffer)
  18: char        c;
  19: struct buf  **buffer;
  20: {
  21:     register struct buf *b;
  22:     register struct buf *a;
  23:     register struct buf **bp;
  24:     char            *bufalloc();
  25: 
  26:     bp = buffer;
  27:     b = *bp;
  28:     if (b == 0 || b->ptr >= &b->buffer[BUFSIZE])
  29:     {
  30:         /* allocate new buffer segment */
  31:         a = (struct buf *) bufalloc(sizeof(struct buf));
  32:         a->nextb = b;
  33:         a->ptr = a->buffer;
  34:         *bp = b = a;
  35:     }
  36: 
  37:     *b->ptr++ = c;
  38: }
  39: 
  40: 
  41: 
  42: /*
  43: **  BUFGET -- get character off of buffer
  44: **
  45: **	The buffer is popped and the character is returned.  If the
  46: **	segment is then empty, it is returned to the free list.
  47: */
  48: 
  49: bufget(buffer)
  50: struct buf  **buffer;
  51: {
  52:     register struct buf *b;
  53:     register char       c;
  54:     register struct buf **bp;
  55: 
  56:     bp = buffer;
  57:     b = *bp;
  58: 
  59:     if (b == 0 || b->ptr == b->buffer)
  60:     {
  61:         /* buffer is empty -- return end of file */
  62:         return (0);
  63:     }
  64: 
  65:     c = *--(b->ptr);
  66: 
  67:     /* check to see if we have emptied the (non-initial) segment */
  68:     if (b->ptr == b->buffer && b->nextb != 0)
  69:     {
  70:         /* deallocate segment */
  71:         *bp = b->nextb;
  72:         buffree(b);
  73:     }
  74: 
  75:     return (c);
  76: }
  77: 
  78: 
  79: 
  80: /*
  81: **  BUFPURGE -- return an entire buffer to the free list
  82: **
  83: **	The buffer is emptied and returned to the free list.  This
  84: **	routine should be called when the buffer is to no longer
  85: **	be used.
  86: */
  87: 
  88: bufpurge(buffer)
  89: struct buf  **buffer;
  90: {
  91:     register struct buf **bp;
  92:     register struct buf *a;
  93:     register struct buf *b;
  94: 
  95:     bp = buffer;
  96:     b = *bp;
  97:     *bp = 0;
  98: 
  99:     /* return the segments to the free list */
 100:     while (b != 0)
 101:     {
 102:         a = b->nextb;
 103:         buffree(b);
 104:         b = a;
 105:     }
 106: }
 107: 
 108: 
 109: 
 110: 
 111: /*
 112: **  BUFFLUSH -- flush a buffer
 113: **
 114: **	The named buffer is truncated to zero length.  However, the
 115: **	segments of the buffer are not returned to the system.
 116: */
 117: 
 118: bufflush(buffer)
 119: struct buf  **buffer;
 120: {
 121:     register struct buf *b;
 122:     register struct buf **bp;
 123: 
 124:     bp = buffer;
 125:     b = *bp;
 126:     if (b == 0)
 127:         return;
 128: 
 129:     /* return second and subsequent segments to the system */
 130:     bufpurge(&b->nextb);
 131: 
 132:     /* truncate this buffer to zero length */
 133:     b->ptr = b->buffer;
 134: }
 135: 
 136: 
 137: 
 138: /*
 139: **  BUFCRUNCH -- flatten a series of buffers to a string
 140: **
 141: **	The named buffer is flattenned to a conventional C string,
 142: **	null terminated.  The buffer is deallocated.  The string is
 143: **	allocated "somewhere" off in memory, and a pointer to it
 144: **	is returned.
 145: */
 146: 
 147: char    *Buf_flat;
 148: 
 149: char *bufcrunch(buffer)
 150: struct buf  **buffer;
 151: {
 152:     register char   *p;
 153:     char        *bufflatten();
 154: 
 155:     p = bufflatten(*buffer, 1);
 156:     *p = 0;
 157:     *buffer = 0;
 158:     return (Buf_flat);
 159: }
 160: 
 161: char *bufflatten(buf, length)
 162: struct buf  *buf;
 163: int     length;
 164: {
 165:     register struct buf *b;
 166:     register char       *p;
 167:     register char       *q;
 168:     char            *bufalloc();
 169: 
 170:     b = buf;
 171: 
 172:     /* see if we have advanced to beginning of buffer */
 173:     if (b != 0)
 174:     {
 175:         /* no, keep moving back */
 176:         p = bufflatten(b->nextb, length + (b->ptr - b->buffer));
 177:     }
 178:     else
 179:     {
 180:         /* yes, allocate the string */
 181:         Buf_flat = p = bufalloc(length);
 182:         return (p);
 183:     }
 184: 
 185:     /* copy buffer into string */
 186:     for (q = b->buffer; q < b->ptr; )
 187:         *p++ = *q++;
 188: 
 189:     /* deallocate the segment */
 190:     buffree(b);
 191: 
 192:     /* process next segment */
 193:     return (p);
 194: }
 195: 
 196: 
 197: 
 198: /*
 199: **  BUFALLOC -- allocate clear memory
 200: **
 201: **	This is similar to the system alloc routine except that
 202: **	it has no error return, and memory is guaranteed to be clear
 203: **	when you return.
 204: **
 205: **	It might be nice to rewrite this later to avoid the nasty
 206: **	memory fragmentation that alloc() tends toward.
 207: **
 208: **	The error processing might have to be modified if used anywhere
 209: **	other than INGRES.
 210: */
 211: 
 212: char *bufalloc(size)
 213: int size;
 214: {
 215:     register char   *p;
 216:     extern int  (*Exitfn)();    /* defined in syserr.c */
 217:     char        *malloc();
 218: 
 219:     p = malloc(size);
 220:     if (p == 0)
 221:     {
 222:         printf("Out of memory in macro processor\n");
 223:         (*Exitfn)(-1);
 224:     }
 225: 
 226:     bufclear(p, size);
 227: 
 228:     return (p);
 229: }
 230: 
 231: 
 232: 
 233: /*
 234: **  BUFCLEAR -- clear a block of core memory
 235: **
 236: **	Parameters:
 237: **		p -- pointer to area
 238: **		size -- size in bytes
 239: */
 240: 
 241: bufclear(p, size)
 242: char    *p;
 243: int size;
 244: {
 245:     register char   *a;
 246:     register char   *b;
 247: 
 248:     a = p;
 249: 
 250:     for (b = &a[size]; a < b; )
 251:         *a++ = 0;
 252: }
 253: 
 254: 
 255: 
 256: /*
 257: **  BUFFREE -- free memory
 258: */
 259: 
 260: buffree(ptr)
 261: char    *ptr;
 262: {
 263:     register char   *p;
 264: 
 265:     p = ptr;
 266: 
 267:     if (p == 0)
 268:         syserr("buffree: 0 ptr");
 269: 
 270:     free(p);
 271: }

Defined functions

bufalloc defined in line 212; used 10 times
bufclear defined in line 241; used 3 times
bufcrunch defined in line 149; used 12 times
bufflatten defined in line 161; used 3 times
bufflush defined in line 118; used 7 times
buffree defined in line 260; used 19 times
bufget defined in line 49; used 3 times
bufpurge defined in line 88; used 15 times
bufput defined in line 17; used 21 times

Defined variables

Buf_flat defined in line 147; used 2 times
Last modified: 1995-02-12
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2906
Valid CSS Valid XHTML 1.0 Strict