home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / other / irc / include / dbuf.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-14  |  5.1 KB  |  160 lines

  1. /************************************************************************
  2.  *   IRC - Internet Relay Chat, include/dbuf.h
  3.  *   Copyright (C) 1990 Markku Savela
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 1, or (at your option)
  8.  *   any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #ifndef __dbuf_include__
  21. #define __dbuf_include__
  22.  
  23. #ifndef PROTO
  24. #ifdef __STDC__
  25. #    define PROTO(x)    x
  26. #else
  27. #    define PROTO(x)    ()
  28. #endif /* __STDC__ */
  29. #endif /* ! PROTO */
  30.  
  31. /*
  32. ** dbuf is a collection of functions which can be used to
  33. ** maintain a dynamic buffering of a byte stream.
  34. ** Functions allocate and release memory dynamically as
  35. ** required [Actually, there is nothing that prevents
  36. ** this package maintaining the buffer on disk, either]
  37. */
  38.  
  39. /*
  40. ** These structure definitions are only here to be used
  41. ** as a whole, *DO NOT EVER REFER TO THESE FIELDS INSIDE
  42. ** THE STRUCTURES*! It must be possible to change the internal
  43. ** implementation of this package without changing the
  44. ** interface.
  45. */
  46. #if !defined(_SEQUENT_)
  47. typedef struct dbuf
  48.     {
  49.     u_int    length;    /* Current number of bytes stored */
  50.     u_int    offset;    /* Offset to the first byte */
  51.     struct    dbufbuf *head;    /* First data buffer, if length > 0 */
  52.     } dbuf;
  53. #else
  54. typedef struct dbuf
  55.     {
  56.         uint   length; /* Current number of bytes stored */
  57.         uint   offset; /* Offset to the first byte */
  58.         struct  dbufbuf *head;  /* First data buffer, if length > 0 */
  59.     } dbuf;
  60. #endif
  61. /*
  62. ** And this 'dbufbuf' should never be referenced outside the
  63. ** implementation of 'dbuf'--would be "hidden" if C had such
  64. ** keyword...
  65. ** If it was possible, this would compile to be exactly 1 memory
  66. ** page in size. 2048 bytes seems to be the most common size, so
  67. ** as long as a pointer is 4 bytes, we get 2032 bytes for buffer
  68. ** data after we take away a bit for malloc to play with. -avalon
  69. */
  70. typedef struct dbufbuf
  71.     {
  72.     struct    dbufbuf    *next;    /* Next data buffer, NULL if this is last */
  73.     char    data[2032];    /* Actual data stored here */
  74.     } dbufbuf;
  75.  
  76. /*
  77. ** dbuf_put
  78. **    Append the number of bytes to the buffer, allocating more
  79. **    memory as needed. Bytes are copied into internal buffers
  80. **    from users buffer.
  81. **
  82. **    returns    > 0, if operation successfull
  83. **        < 0, if failed (due memory allocation problem)
  84. */
  85. int    dbuf_put PROTO((dbuf *, char *, int));
  86.                     /* Dynamic buffer header */
  87.                          /* Pointer to data to be stored */
  88.                          /* Number of bytes to store */
  89.  
  90. /*
  91. ** dbuf_get
  92. **    Remove number of bytes from the buffer, releasing dynamic
  93. **    memory, if applicaple. Bytes are copied from internal buffers
  94. **    to users buffer.
  95. **
  96. **    returns    the number of bytes actually copied to users buffer,
  97. **        if >= 0, any value less than the size of the users
  98. **        buffer indicates the dbuf became empty by this operation.
  99. **
  100. **        Return 0 indicates that buffer was already empty.
  101. **
  102. **        Negative return values indicate some unspecified
  103. **        error condition, rather fatal...
  104. */
  105. int    dbuf_get PROTO(( dbuf *, char *, int));
  106.                 /* Dynamic buffer header */
  107.                      /* Pointer to buffer to receive the data */
  108.                      /* Max amount of bytes that can be received */
  109.  
  110. /*
  111. ** dbuf_map, dbuf_delete
  112. **    These functions are meant to be used in pairs and offer
  113. **    a more efficient way of emptying the buffer than the
  114. **    normal 'dbuf_get' would allow--less copying needed.
  115. **
  116. **    map    returns a pointer to a largest contiguous section
  117. **        of bytes in front of the buffer, the length of the
  118. **        section is placed into the indicated "long int"
  119. **        variable. Returns NULL *and* zero length, if the
  120. **        buffer is empty.
  121. **
  122. **    delete    removes the specified number of bytes from the
  123. **        front of the buffer releasing any memory used for them.
  124. **
  125. **    Example use (ignoring empty condition here ;)
  126. **
  127. **        buf = dbuf_map(&dyn, &count);
  128. **        <process N bytes (N <= count) of data pointed by 'buf'>
  129. **        dbuf_delete(&dyn, N);
  130. **
  131. **    Note:     delete can be used alone, there is no real binding
  132. **        between map and delete functions...
  133. */
  134. char *dbuf_map PROTO((dbuf *, int *));
  135.                            /* Dynamic buffer header */
  136.                            /* Return number of bytes accessible */
  137.  
  138. int dbuf_delete PROTO((dbuf *, int));
  139.                     /* Dynamic buffer header */
  140.                     /* Number of bytes to delete */
  141.  
  142. /*
  143. ** DBufLength
  144. **    Return the current number of bytes stored into the buffer.
  145. **    (One should use this instead of referencing the internal
  146. **    length field explicitly...)
  147. */
  148. #define DBufLength(dyn) ((dyn)->length)
  149.  
  150. /*
  151. ** DBufClear
  152. **    Scratch the current content of the buffer. Release all
  153. **    allocated buffers and make it empty.
  154. */
  155. #define DBufClear(dyn)    dbuf_delete((dyn),DBufLength(dyn))
  156.  
  157. extern    int    dbuf_getmsg PROTO((dbuf *, char *, int));
  158.  
  159. #endif /* __dbuf_include__ */
  160.