[BACK]Return to buf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Diff for /src/usr.bin/make/buf.c between version 1.10 and 1.11

version 1.10, 1999/12/16 16:41:41 version 1.11, 1999/12/16 16:46:38
Line 83 
Line 83 
 #include    "make.h"  #include    "make.h"
 #include    "buf.h"  #include    "buf.h"
   
 #ifndef max  /* BufExpand --
 #define max(a,b)  ((a) > (b) ? (a) : (b))  
 #endif  
   
 /*  
  * BufExpand --  
  *      Expand the given buffer to hold the given number of additional   *      Expand the given buffer to hold the given number of additional
  *      chars.   *      chars.  Makes sure there's room for an extra '\0' char at
  *      Makes sure there's room for an extra NULL char at the end of the   *      the end of the buffer to terminate the string.  */
  *      buffer in case it holds a string.  #define BufExpand(bp,nb)                                \
  */  do {                                                    \
 #define BufExpand(bp,nb)                                        \      size_t   occupied = (bp)->inPtr - (bp)->buffer;     \
 do {                                                            \      size_t   size = (bp)->endPtr - (bp)->buffer;        \
     char  *newBuf;                                              \                                                          \
     size_t   newSize = (bp)->size;                              \      do {                                                \
                                                                 \          size *= 2 ;                                     \
     do {                                                        \      } while (size - occupied < (nb)+1+BUF_MARGIN);      \
         newSize *= 2 ;                                          \      (bp)->buffer = (bp)->inPtr = (bp)->endPtr =         \
         (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);    \          erealloc((bp)->buffer, size);                   \
     } while ((bp)->left < (nb)+1+BUF_MARGIN);                   \      (bp)->inPtr += occupied;                            \
     newBuf = erealloc((bp)->buffer, newSize);           \      (bp)->endPtr += size;                               \
     (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer);        \  } while (0);
     (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);      \  
     (bp)->buffer = newBuf;                                      \  
     (bp)->size = newSize;                                       \  
 } while (0)  
   
 #define BUF_DEF_SIZE    256     /* Default buffer size */  #define BUF_DEF_SIZE    256     /* Default buffer size */
 #define BUF_MARGIN      256     /* Make sure we are comfortable */  #define BUF_MARGIN      256     /* Make sure we are comfortable */
Line 121 
Line 112 
 {  {
     BufExpand(bp, 1);      BufExpand(bp, 1);
 }  }
   
 /*-  
  *-----------------------------------------------------------------------  
  * Buf_AddChars --  
  *      Add a number of chars to the buffer.  
  *  
  * Side Effects:  
  *      Guess what?  
  *  
  *-----------------------------------------------------------------------  
  */  
 void  void
 Buf_AddChars(bp, numBytes, bytesPtr)  Buf_AddChars(bp, numBytes, bytesPtr)
     Buffer      bp;      Buffer      bp;
Line 139 
Line 120 
     const char  *bytesPtr;      const char  *bytesPtr;
 {  {
   
     if (bp->left < numBytes+1)      if (bp->endPtr - bp->inPtr < numBytes+1)
         BufExpand(bp, numBytes);          BufExpand(bp, numBytes);
   
     memcpy(bp->inPtr, bytesPtr, numBytes);      memcpy(bp->inPtr, bytesPtr, numBytes);
     bp->inPtr += numBytes;      bp->inPtr += numBytes;
     bp->left -= numBytes;  
 }  }
   
 /*-  
  *-----------------------------------------------------------------------  
  * Buf_Reset -  
  *      Throw away all chars in a buffer.  
  *  
  * Side Effects:  
  *      The chars are discarded.  
  *  
  *-----------------------------------------------------------------------  
  */  
 void  
 Buf_Reset(bp)  
     Buffer      bp;  
 {  
   
     bp->inPtr = bp->outPtr = bp->buffer;  
     bp->left = bp->size;  
 }  
   
 /*-  
  *-----------------------------------------------------------------------  
  * Buf_Init --  
  *      Initialize a buffer. If no initial size is given, a reasonable  
  *      default is used.  
  *  
  * Results:  
  *      A buffer to be given to other functions in this library.  
  *  
  * Side Effects:  
  *      The buffer is created, the space allocated and pointers  
  *      initialized.  
  *  
  *-----------------------------------------------------------------------  
  */  
 Buffer  Buffer
 Buf_Init(size)  Buf_Init(size)
     size_t      size;   /* Initial size for the buffer */      size_t      size;   /* Initial size for the buffer */
Line 192 
Line 138 
     if (size == 0) {      if (size == 0) {
         size = BUF_DEF_SIZE;          size = BUF_DEF_SIZE;
     }      }
     bp->left = bp->size = size;      bp->inPtr = bp->endPtr = bp->buffer = emalloc(size);
     bp->buffer = emalloc(size);      bp->endPtr += size;
     bp->inPtr = bp->outPtr = bp->buffer;  
   
     return (bp);      return (bp);
 }  }
   
 /*-  
  *-----------------------------------------------------------------------  
  * Buf_Destroy --  
  *      Nuke a buffer and all its resources.  
  *  
  * Side Effects:  
  *      The buffer is freed.  
  *  
  *-----------------------------------------------------------------------  
  */  
 void  void
 Buf_Destroy(buf, freeData)  Buf_Destroy(buf, freeData)
     Buffer  buf;        /* Buffer to destroy */      Buffer  buf;        /* Buffer to destroy */
Line 220 
Line 155 
     }      }
     free ((char *)buf);      free ((char *)buf);
 }  }
   
 /*-  
  *-----------------------------------------------------------------------  
  * Buf_ReplaceLastChar --  
  *     Replace the last char in a buffer.  
  *  
  * Side Effects:  
  *     If the buffer was empty intially, then a new byte will be added.  
  *     Otherwise, the last byte is overwritten.  
  *  
  *-----------------------------------------------------------------------  
  */  
 void  void
 Buf_ReplaceLastChar(buf, byte)  Buf_ReplaceLastChar(buf, byte)
     Buffer      buf;    /* buffer to augment */      Buffer      buf;    /* buffer to augment */
     char        byte;   /* byte to be written */      char        byte;   /* byte to be written */
 {  {
     if (buf->inPtr == buf->outPtr)      if (buf->inPtr == buf->buffer)
         Buf_AddChar(buf, byte);          Buf_AddChar(buf, byte);
     else      else
         *(buf->inPtr - 1) = byte;          *(buf->inPtr - 1) = byte;

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.11