Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / apps / JAWS / clients / WebSTONE / src / nsapi-includes / base / buffer.h
blob21389b62b84e239320d01be07d12b744de2399bf
1 /*
2 * Copyright (c) 1994, 1995. Netscape Communications Corporation. All
3 * rights reserved.
5 * Use of this software is governed by the terms of the license agreement for
6 * the Netscape Communications or Netscape Comemrce Server between the
7 * parties.
8 */
11 /* ------------------------------------------------------------------------ */
15 * buffer.h: For performing buffered I/O on a file or socket descriptor.
17 * This is an abstraction to allow I/O to be performed regardless of the
18 * current system. That way, an integer file descriptor can be used under
19 * UNIX but a stdio FILE structure could be used on systems which don't
20 * support that or don't support it as efficiently.
22 * Two abstractions are defined: A file buffer, and a network buffer. A
23 * distinction is made so that mmap() can be used on files (but is not
24 * required). Also, the file buffer takes a file name as the object to
25 * open instead of a file descriptor. A lot of the network buffering
26 * is almost an exact duplicate of the non-mmap file buffering.
28 * If an error occurs, system-independent means to obtain an error string
29 * are also provided. However, if the underlying system is UNIX the error
30 * may not be accurate in a threaded environment.
32 * Rob McCool
36 #ifndef BUFFER_H
37 #define BUFFER_H
39 #ifdef XP_WIN32
40 #include <nt/ntbuffer.h>
41 #else
45 * We need certain system specific functions and symbols.
48 #include "file.h"
49 #include "net.h"
52 * Requires that the macro MALLOC be set to a "safe" malloc that will
53 * exit if no memory is available. If not under MCC httpd, define MALLOC
54 * to be the real malloc and play with fire, or make your own function.
57 #include "../netsite.h"
59 #ifdef FILE_UNIX_MMAP
60 #include <sys/types.h> /* caddr_t */
61 #endif
64 /* ------------------------------ Structures ------------------------------ */
66 #ifdef FILE_UNIX_MMAP
67 typedef struct {
68 SYS_FILE fd;
69 caddr_t fp;
70 int len;
72 char *inbuf; /* for buffer_grab */
73 int cursize;
75 int pos;
76 char *errmsg;
77 } filebuf;
79 #else
81 typedef struct {
82 SYS_FILE fd;
84 int pos, cursize, maxsize;
85 char *inbuf;
86 char *errmsg;
87 } filebuf;
89 #endif
91 typedef struct {
92 SYS_NETFD sd;
94 int pos, cursize, maxsize, rdtimeout;
95 char *inbuf;
96 char *errmsg;
97 } netbuf;
100 /* -------------------------------- Macros -------------------------------- */
104 * netbuf_getc gets a character from the given network buffer and returns
105 * it. (as an integer).
107 * It will return (int) IO_ERROR for an error and (int) IO_EOF for
108 * an error condition or EOF respectively.
111 #define netbuf_getc(b) \
112 ((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : netbuf_next(b,1))
114 #ifdef FILE_UNIX_MMAP
115 #define filebuf_getc(b) ((b)->pos == (b)->len ? IO_EOF : (b)->fp[(b)->pos++])
116 #else
117 #define filebuf_getc(b) \
118 ((b)->pos != (b)->cursize ? (int)((b)->inbuf[(b)->pos++]) : filebuf_next(b,1))
119 #endif
123 * buffer_error returns the last error that occurred with buffer. Don't use
124 * this unless you know an error occurred. Independent of network/file type.
127 #define buffer_error(b) ((b)->errmsg)
130 * buffer_flush flushes any data after the current pos to the file
131 * descriptor fd. Regardless of buffer type.
134 #define buffer_flush(buf,fd) \
135 system_write(fd,&(buf)->inbuf[(buf)->pos], (buf)->cursize - (buf)->pos)
138 /* ------------------------------ Prototypes ------------------------------ */
142 * buffer_open opens a new buffer reading the specified file, with an I/O
143 * buffer of size sz, and returns a new buffer structure which will hold
144 * the data.
146 * If FILE_UNIX_MMAP is defined, this may return NULL. If it does, check
147 * system_errmsg to get a message about the error.
150 filebuf *filebuf_open(SYS_FILE fd, int sz);
151 netbuf *netbuf_open(SYS_NETFD sd, int sz);
154 * filebuf_open_nostat is a convenience function for mmap() buffer opens,
155 * if you happen to have the stat structure already.
158 #ifdef FILE_UNIX_MMAP
159 #include <sys/stat.h>
160 filebuf *filebuf_open_nostat(SYS_FILE fd, int sz, struct stat *finfo);
162 #else
163 #define filebuf_open_nostat(fd,sz,finfo) filebuf_open(fd,sz)
164 #endif
167 * buffer_next loads size more bytes into the given buffer and returns the
168 * first one, or BUFFER_EOF on EOF and BUFFER_ERROR on error.
171 int filebuf_next(filebuf *buf, int advance);
172 int netbuf_next(netbuf *buf, int advance);
175 * buffer_close deallocates a buffer and closes its associated files
176 * (does not close a network socket).
179 void filebuf_close(filebuf *buf);
180 void netbuf_close(netbuf *buf);
183 * buffer_grab will set the buffer's inbuf array to an array of sz bytes
184 * from the buffer's associated object. It returns the number of bytes
185 * actually read (between 1 and sz). It returns IO_EOF upon EOF or IO_ERROR
186 * upon error. The cursize entry of the structure will reflect the size
187 * of the iobuf array.
189 * The buffer will take care of allocation and deallocation of this array.
192 int filebuf_grab(filebuf *buf, int sz);
193 int netbuf_grab(netbuf *buf, int sz);
197 * netbuf_buf2sd will send n bytes from the (probably previously read)
198 * buffer and send them to sd. If sd is -1, they are discarded. If n is
199 * -1, it will continue until EOF is received. Returns IO_ERROR on error
200 * and the number of bytes sent any other time.
203 int netbuf_buf2sd(netbuf *buf, SYS_NETFD sd, int len);
206 * filebuf_buf2sd assumes that nothing has been read from the filebuf,
207 * and just sends the file out to the given socket. Returns IO_ERROR on error
208 * and the number of bytes sent otherwise.
210 * Does not currently support you having read from the buffer previously. This
211 * can be changed transparently.
214 int filebuf_buf2sd(filebuf *buf, SYS_NETFD sd);
216 #endif
217 #endif