]> sjero.net Git - wget/blob - src/rbuf.h
[svn] Renamed xread/xwrite/xclose to fd_read/fd_write/fd_close. The "x" prefix is
[wget] / src / rbuf.h
1 /* Declarations for rbuf.c.
2    Copyright (C) 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables.  You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL".  If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so.  If you do not wish to do
28 so, delete this exception statement from your version.  */
29
30 #ifndef RBUF_H
31 #define RBUF_H
32
33 /* Retrieval stream */
34 struct rbuf
35 {
36   int fd;
37   char buffer[4096];            /* the input buffer */
38   char *buffer_pos;             /* current position in the buffer */
39   size_t buffer_left;           /* number of bytes left in the buffer:
40                                    buffer_left = buffer_end - buffer_pos */
41   int internal_dont_touch_this; /* used by RBUF_READCHAR macro */
42 };
43
44 /* Read a character from RBUF.  If there is anything in the buffer,
45    the character is returned from the buffer.  Otherwise, refill the
46    buffer and return the first character.
47
48    The return value is the same as with read(2).  On buffered read,
49    the function returns 1.
50
51    #### That return value is totally screwed up, and is a direct
52    result of historical implementation of header code.  The macro
53    should return the character or EOF, and in case of error store it
54    to rbuf->err or something.  */
55
56 #define RBUF_READCHAR(rbuf, store)                                      \
57 ((rbuf)->buffer_left                                                    \
58  ? (--(rbuf)->buffer_left,                                              \
59     *((char *) (store)) = *(rbuf)->buffer_pos++, 1)                     \
60  : ((rbuf)->buffer_pos = (rbuf)->buffer,                                \
61     ((((rbuf)->internal_dont_touch_this                                 \
62        = rbuf_read_bufferful (rbuf)) <= 0)                              \
63      ? (rbuf)->internal_dont_touch_this                                 \
64      : ((rbuf)->buffer_left = (rbuf)->internal_dont_touch_this - 1,     \
65         *((char *) (store)) = *(rbuf)->buffer_pos++,                    \
66         1))))
67
68 /* Return the file descriptor of RBUF.  */
69 #define RBUF_FD(rbuf) ((rbuf)->fd)
70
71 /* Function declarations */
72 void rbuf_initialize PARAMS ((struct rbuf *, int));
73 int rbuf_initialized_p PARAMS ((struct rbuf *));
74 void rbuf_uninitialize PARAMS ((struct rbuf *));
75 int rbuf_readchar PARAMS ((struct rbuf *, char *));
76 int rbuf_peek PARAMS ((struct rbuf *, char *));
77 int rbuf_flush PARAMS ((struct rbuf *, char *, int));
78 void rbuf_discard PARAMS ((struct rbuf *));
79
80 /* Internal, but used by the macro. */
81 int rbuf_read_bufferful PARAMS ((struct rbuf *));
82
83 #endif /* RBUF_H */