home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1999 March B
/
SCO_CASTOR4RRT.iso
/
nsfast
/
root.9
/
usr
/
ns-home
/
nsapi
/
include
/
base
/
buffer.h
/
buffer
Wrap
Text File
|
1998-08-19
|
8KB
|
271 lines
/*
* Copyright (c) 1994, 1995. Netscape Communications Corporation. All
* rights reserved.
*
* Use of this software is governed by the terms of the license agreement for
* the Netscape FastTrack or Netscape Enterprise Server between the
* parties.
*/
/* ------------------------------------------------------------------------ */
/*
* buffer.h: For performing buffered I/O on a file or socket descriptor.
*
* This is an abstraction to allow I/O to be performed regardless of the
* current system. That way, an integer file descriptor can be used under
* UNIX but a stdio FILE structure could be used on systems which don't
* support that or don't support it as efficiently.
*
* Two abstractions are defined: A file buffer, and a network buffer. A
* distinction is made so that mmap() can be used on files (but is not
* required). Also, the file buffer takes a file name as the object to
* open instead of a file descriptor. A lot of the network buffering
* is almost an exact duplicate of the non-mmap file buffering.
*
* If an error occurs, system-independent means to obtain an error string
* are also provided. However, if the underlying system is UNIX the error
* may not be accurate in a threaded environment.
*
* Rob McCool
*
*/
#ifndef BUFFER_H
#define BUFFER_H
/*
* We need certain system specific functions and symbols.
*/
#include "file.h"
#include "net.h"
/*
* Requires that the macro MALLOC be set to a "safe" malloc that will
* exit if no memory is available. If not under MCC httpd, define MALLOC
* to be the real malloc and play with fire, or make your own function.
*/
#include "../netsite.h"
#ifdef FILE_UNIX_MMAP
#include <sys/types.h> /* caddr_t */
#endif
/* ------------------------------ Structures ------------------------------ */
#ifdef FILE_MMAP
typedef struct {
SYS_FILE fd;
#ifdef FILE_UNIX_MMAP
caddr_t fp;
#else /* FILE_WIN32_MMAP */
HANDLE fdmap;
char *fp;
#endif /* FILE_UNIX_MMAP */
int len;
unsigned char *inbuf; /* for buffer_grab */
int cursize;
int pos;
char *errmsg;
} filebuffer;
#else
typedef struct {
SYS_FILE fd;
int pos, cursize, maxsize;
unsigned char *inbuf;
char *errmsg;
} filebuffer;
#endif
/* C++ streamio defines a filebuf class. */
/* XXXrobm Temporarily allow this typedef for NT compiles. */
#if !defined(__cplusplus) || !defined(XP_UNIX)
typedef filebuffer filebuf;
#endif
typedef struct {
SYS_NETFD sd;
int pos, cursize, maxsize, rdtimeout;
#ifdef XP_WIN32
CHAR address[64];
#endif /* XP_WIN32 */
unsigned char *inbuf;
char *errmsg;
} netbuf;
/* -------------------------------- Macros -------------------------------- */
/*
* netbuf_getc gets a character from the given network buffer and returns
* it. (as an integer).
*
* It will return (int) IO_ERROR for an error and (int) IO_EOF for
* an error condition or EOF respectively.
*/
#define netbuf_getc(b) \
((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : netbuf_next(b,1))
#ifdef FILE_MMAP
#define filebuf_getc(b) ((b)->pos == (b)->len ? IO_EOF : (b)->fp[(b)->pos++])
#else
#define filebuf_getc(b) \
((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : filebuf_next(b,1))
#endif
#ifdef XP_WIN32
#define pipebuf_getc(b) \
((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : pipebuf_next(b,1))
#endif /* XP_WIN32 */
/*
* buffer_error returns the last error that occurred with buffer. Don't use
* this unless you know an error occurred. Independent of network/file type.
*/
#define buffer_error(b) ((b)->errmsg)
/* ------------------------------ Prototypes ------------------------------ */
/*
* buffer_open opens a new buffer reading the specified file, with an I/O
* buffer of size sz, and returns a new buffer structure which will hold
* the data.
*
* If FILE_UNIX_MMAP is defined, this may return NULL. If it does, check
* system_errmsg to get a message about the error.
*/
NSAPI_PUBLIC filebuffer *filebuf_open(SYS_FILE fd, int sz);
NSAPI_PUBLIC netbuf *netbuf_open(SYS_NETFD sd, int sz);
/*
* filebuf_open_nostat is a convenience function for mmap() buffer opens,
* if you happen to have the stat structure already.
*/
/*
* filebuf_create is a convenience function if the file is already open
* or mmap'd. It creates a new filebuf for use with the mmap'd file.
* If mmap_ptr is NULL, or MMAP is not supported on this system, it
* creates a buffer with buffer size bufsz.
*/
filebuffer *filebuf_create(SYS_FILE fd, caddr_t mmap_ptr, int mmap_len,
int bufsz);
/*
* filebuf_close_buffer is provided to cleanup a filebuf without closing
* the underlying file. If clean_mmap is 1, and the file is memory mapped,
* the file will be unmapped. If clean_mmap is 0, the file will not
* be unmapped.
*/
void filebuf_close_buffer(filebuffer *buf, int clean_mmap);
#ifdef FILE_MMAP
#include <sys/stat.h>
NSAPI_PUBLIC
filebuffer *filebuf_open_nostat(SYS_FILE fd, int sz, struct stat *finfo);
#else /* !MMAP */
#define filebuf_open_nostat(fd,sz,finfo) filebuf_open(fd,sz)
#endif
#ifdef XP_WIN32
NSAPI_PUBLIC
filebuffer *pipebuf_open(SYS_FILE fd, int sz, struct stat *finfo);
#endif /* XP_WIN32 */
/*
* buffer_next loads size more bytes into the given buffer and returns the
* first one, or BUFFER_EOF on EOF and BUFFER_ERROR on error.
*/
NSAPI_PUBLIC int filebuf_next(filebuffer *buf, int advance);
NSAPI_PUBLIC int netbuf_next(netbuf *buf, int advance);
#ifdef XP_WIN32
NSAPI_PUBLIC int pipebuf_next(filebuffer *buf, int advance);
#endif /* XP_WIN32 */
/*
* buffer_close deallocates a buffer and closes its associated files
* (does not close a network socket).
*/
NSAPI_PUBLIC void filebuf_close(filebuffer *buf);
NSAPI_PUBLIC void netbuf_close(netbuf *buf);
#ifdef XP_WIN32
NSAPI_PUBLIC void pipebuf_close(filebuffer *buf);
#endif /* XP_WIN32 */
/*
* buffer_grab will set the buffer's inbuf array to an array of sz bytes
* from the buffer's associated object. It returns the number of bytes
* actually read (between 1 and sz). It returns IO_EOF upon EOF or IO_ERROR
* upon error. The cursize entry of the structure will reflect the size
* of the iobuf array.
*
* The buffer will take care of allocation and deallocation of this array.
*/
NSAPI_PUBLIC int filebuf_grab(filebuffer *buf, int sz);
NSAPI_PUBLIC int netbuf_grab(netbuf *buf, int sz);
#ifdef XP_WIN32
NSAPI_PUBLIC int pipebuf_grab(filebuffer *buf, int sz);
#endif /* XP_WIN32 */
/*
* netbuf_buf2sd will send n bytes from the (probably previously read)
* buffer and send them to sd. If sd is -1, they are discarded. If n is
* -1, it will continue until EOF is recieved. Returns IO_ERROR on error
* and the number of bytes sent any other time.
*/
NSAPI_PUBLIC int netbuf_buf2sd(netbuf *buf, SYS_NETFD sd, int len);
/*
* filebuf_buf2sd assumes that nothing has been read from the filebuf,
* and just sends the file out to the given socket. Returns IO_ERROR on error
* and the number of bytes sent otherwise.
*
* Does not currently support you having read from the buffer previously. This
* can be changed transparently.
*/
NSAPI_PUBLIC int filebuf_buf2sd(filebuffer *buf, SYS_NETFD sd);
#ifdef XP_WIN32
/*
* NT pipe version of filebuf_buf2sd.
*/
NSAPI_PUBLIC int pipebuf_buf2sd(filebuffer *buf, SYS_NETFD sd, int len);
/*
* NT pipe version of netbuf_buf2sd.
*/
NSAPI_PUBLIC int pipebuf_netbuf2sd(netbuf *buf, SYS_FILE sd, int len);
#endif /* XP_WIN32 */
#endif