home *** CD-ROM | disk | FTP | other *** search
/ Programming with VisualAge for Java / IBMVJAVA.ISO / icswin95 / httpdw32.z / buffer.h < prev    next >
C/C++ Source or Header  |  1997-04-07  |  8KB  |  265 lines

  1. /************************************************************************
  2.  * Netscape Server API Compatibility Layer
  3.  * NSbuffer.h
  4.  *
  5.  ************************************************************************
  6.  */
  7.  
  8. /* Original Header Information:
  9.  ********************************
  10.  * Copyright (c) 1994, 1995.  Netscape Communications Corporation.  All
  11.  * rights reserved.
  12.  *
  13.  * Use of this software is governed by the terms of the license agreement for
  14.  * the Netscape Communications or Netscape Comemrce Server between the
  15.  * parties.
  16.  */
  17.  
  18.  
  19. /* ------------------------------------------------------------------------ */
  20.  
  21.  
  22. /* NETSCAPE SAYS:
  23.  * buffer.h: For performing buffered I/O on a file or socket descriptor.
  24.  *
  25.  * This is an abstraction to allow I/O to be performed regardless of the
  26.  * current system. That way, an integer file descriptor can be used under
  27.  * UNIX but a stdio FILE structure could be used on systems which don't
  28.  * support that or don't support it as efficiently.
  29.  *
  30.  * Two abstractions are defined: A file buffer, and a network buffer. A
  31.  * distinction is made so that mmap() can be used on files (but is not
  32.  * required). Also, the file buffer takes a file name as the object to
  33.  * open instead of a file descriptor. A lot of the network buffering
  34.  * is almost an exact duplicate of the non-mmap file buffering.
  35.  *
  36.  * If an error occurs, system-independent means to obtain an error string
  37.  * are also provided. However, if the underlying system is UNIX the error
  38.  * may not be accurate in a threaded environment.
  39.  *
  40.  * Rob McStool
  41.  *
  42.  */
  43.  
  44. /*******************************************************************
  45.  * Qualifications of the above:
  46.  *
  47.  * Right now, we don't touch sockets, only files.  This is because
  48.  * our API won't expose that level of detail (nor should it, IMNSHO),
  49.  * except through some ancillary socket(s) of sorts.  (Sure, I'd let
  50.  * an API writer handle a socket of type AF_INET; I also regularly
  51.  * distribute Uzis to nursery-school children.)
  52.  *
  53.  * Also, mmap() is strictly for optimization under Unix, which we
  54.  * may wish to dismiss as an annoying detail..for the first release,
  55.  * at least.
  56.  *******************************************************************
  57.  */
  58.  
  59. #ifndef NSBUFFER_H
  60. #define NSBUFFER_H
  61.  
  62.  
  63. /*
  64.  * We need certain system specific functions and symbols.
  65.  */
  66.  
  67. #include "base/file.h"
  68. /* It is in file.h that either FILE_UNIX or FILE_STDIO
  69.  * get defined (NOT both, ever!).  That's how we distinguish
  70.  * whether we are some flavor of Unix or not.
  71.  */
  72.  
  73.  
  74. #include "base/net.h"
  75.  
  76.  
  77. /* NETSCAPE SAYS:
  78.  * Requires that the macro MALLOC be set to a "safe" malloc that will
  79.  * exit if no memory is available. If not under MCC httpd, define MALLOC
  80.  * to be the real malloc and play with fire, or make your own function.
  81.  */
  82. /* I say:
  83.  * If you use malloc() responsibly, you're not "playing with fire".
  84.  * Test every pointer you get from standard library functions, as you
  85.  * should be doing.
  86.  */
  87.  
  88. #include "netsite.h"
  89.  
  90. #undef FILE_UNIX_MMAP  /* not used now, but kept around for later */
  91.  
  92. #ifdef FILE_UNIX_MMAP
  93. #include <sys/types.h>          /* caddr_t */
  94. #endif
  95.  
  96. #ifndef MIN
  97. #define MIN(a,b)  (((a) < (b)) ? (a) : (b))
  98. #endif
  99.  
  100. #define ISEOL(ch)   ( (LF == (ch))  || (CR == (ch)) )
  101.  
  102.  
  103. /*  Temporary define for FALSE  - dgk 4/14/96 */
  104.  
  105. #define FALSE 0
  106.  
  107. /* ------------------------------ Structures ------------------------------ */
  108.  
  109. #ifdef FILE_UNIX_MMAP
  110. typedef struct {
  111.     SYS_FILE fd;
  112.     caddr_t fp;
  113.     int len;
  114.  
  115.     char *inbuf;   /* for buffer_grab */
  116.     int cursize;
  117.  
  118.     int pos;
  119.     char *errmsg;
  120. } filebuf;
  121.  
  122. #else
  123.  
  124. typedef struct {
  125.     SYS_FILE fd;
  126.  
  127.     int pos, cursize, maxsize;
  128.     char *inbuf;
  129.     char *errmsg;
  130. } filebuf;
  131.  
  132. #endif
  133.  
  134. typedef struct {
  135.     SYS_NETFD sd;
  136.  
  137.     int pos, cursize, maxsize, rdtimeout;
  138.     char *inbuf;
  139.     char *errmsg;
  140. } netbuf;
  141.  
  142.  
  143. /* -------------------------------- Macros -------------------------------- */
  144.  
  145.  
  146. /*
  147.  * netbuf_getc gets a character from the given network buffer and returns
  148.  * it. (as an integer).
  149.  *
  150.  * It will return (int) IO_ERROR for an error and (int) IO_EOF for
  151.  * an error condition or EOF respectively.
  152.  */
  153.  
  154.  
  155. #define netbuf_getc(b) \
  156.  ((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : netbuf_next(b,1))
  157.  
  158. #ifdef FILE_UNIX_MMAP
  159. #define filebuf_getc(b) ((b)->pos == (b)->len ? IO_EOF : (b)->fp[(b)->pos++])
  160. #else
  161. #define filebuf_getc(b) \
  162.  ((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : filebuf_next(b,1))
  163. #endif
  164.  
  165.  
  166. /*
  167.  * buffer_error returns the last error that occurred with buffer. Don't use
  168.  * this unless you know an error occurred. Independent of network/file type.
  169.  */
  170.  
  171. #define buffer_error(b) ((b)->errmsg)
  172.  
  173. /*
  174.  * buffer_flush flushes any data after the current pos to the file
  175.  * descriptor fd. Regardless of buffer type.
  176.  */
  177. /* Actually, this will fail under Win32, because we still make the
  178.  * distinction between socket and file descriptors.  To be dealt
  179.  * with later.
  180.  */
  181.  
  182. #define buffer_flush(buf,fd) \
  183.     system_write(fd,&(buf)->inbuf[(buf)->pos], (buf)->cursize - (buf)->pos)
  184.  
  185.  
  186. /* ------------------------------ Prototypes ------------------------------ */
  187.  
  188.  
  189. /* NETSCAPE SAYS:
  190.  * buffer_open opens a new buffer reading the specified file, with an I/O
  191.  * buffer of size sz, and returns a new buffer structure which will hold
  192.  * the data.
  193.  *
  194.  * If FILE_UNIX_MMAP is defined, this may return NULL. If it does, check
  195.  * system_errmsg to get a message about the error.
  196.  */
  197.  
  198. filebuf *filebuf_open(SYS_FILE fd, int sz);
  199. netbuf *netbuf_open(SYS_NETFD sd, int sz);
  200.  
  201.  
  202. /*
  203.  * filebuf_open_nostat is a convenience function for mmap() buffer opens,
  204.  * if you happen to have the stat structure already.
  205.  */
  206.  
  207. #ifdef FILE_UNIX_MMAP
  208. #include <sys/stat.h>
  209. filebuf *filebuf_open_nostat(SYS_FILE fd, int sz, struct stat *finfo);
  210.  
  211. #else
  212. #define filebuf_open_nostat(fd,sz,finfo) filebuf_open(fd,sz)
  213. #endif
  214.  
  215. /*
  216.  * buffer_next loads size more bytes into the given buffer and returns the
  217.  * first one, or BUFFER_EOF on EOF and BUFFER_ERROR on error.
  218.  */
  219.  
  220. int filebuf_next(filebuf *buf, int advance);
  221. int netbuf_next(netbuf *buf, int advance);
  222.  
  223.  
  224. /*
  225.  * buffer_close deallocates a buffer and closes its associated files
  226.  * (does not close a network socket).
  227.  */
  228.  
  229. void filebuf_close(filebuf *buf);
  230. void netbuf_close(netbuf *buf);
  231.  
  232. /*
  233.  * buffer_grab will set the buffer's inbuf array to an array of sz bytes
  234.  * from the buffer's associated object. It returns the number of bytes
  235.  * actually read (between 1 and sz). It returns IO_EOF upon EOF or IO_ERROR
  236.  * upon error. The cursize entry of the structure will reflect the size
  237.  * of the iobuf array.
  238.  *
  239.  * The buffer will take care of allocation and deallocation of this array.
  240.  */
  241.  
  242. int filebuf_grab(filebuf *buf, int sz);
  243. int netbuf_grab(netbuf *buf, int sz);
  244.  
  245.  
  246. /*
  247.  * netbuf_buf2sd will send n bytes from the (probably previously read)
  248.  * buffer and send them to sd. If sd is -1, they are discarded. If n is
  249.  * -1, it will continue until EOF is recieved. Returns IO_ERROR on error
  250.  * and the number of bytes sent any other time.
  251.  */
  252. int netbuf_buf2sd(netbuf *buf, SYS_NETFD sd, int len);
  253.  
  254. /*
  255.  * filebuf_buf2sd assumes that nothing has been read from the filebuf,
  256.  * and just sends the file out to the given socket. Returns IO_ERROR on error
  257.  * and the number of bytes sent otherwise.
  258.  *
  259.  * Does not currently support you having read from the buffer previously. This
  260.  * can be changed transparently.
  261.  */
  262. int filebuf_buf2sd(filebuf *buf, SYS_NETFD sd);
  263.  
  264. #endif   /* NSBUFFER_H */
  265.