]> sjero.net Git - wget/blob - src/rbuf.h
[svn] Initial revision
[wget] / src / rbuf.h
1 /* Declarations for rbuf.c.
2    Copyright (C) 1998 Free Software Foundation, Inc.
3
4 This file is part of Wget.
5
6 This program 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 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #ifndef RBUF_H
21 #define RBUF_H
22
23 /* Retrieval stream */
24 struct rbuf
25 {
26   int fd;
27   char buffer[4096];            /* the input buffer */
28   char *buffer_pos;             /* current position in the buffer */
29   size_t buffer_left;           /* number of bytes left in the buffer:
30                                    buffer_left = buffer_end - buffer_pos */
31   int internal_dont_touch_this; /* used by RBUF_READCHAR macro */
32 };
33
34 /* Read a character from RBUF.  If there is anything in the buffer,
35    the character is returned from the buffer.  Otherwise, refill the
36    buffer and return the first character.
37
38    The return value is the same as with read(2).  On buffered read,
39    the function returns 1.
40
41    #### That return value is totally screwed up, and is a direct
42    result of historical implementation of header code.  The macro
43    should return the character or EOF, and in case of error store it
44    to rbuf->err or something.  */
45 #define RBUF_READCHAR(rbuf, store)                                      \
46 ((rbuf)->buffer_left                                                    \
47  ? (--(rbuf)->buffer_left,                                              \
48     *((char *) (store)) = *(rbuf)->buffer_pos++, 1)                     \
49  : ((rbuf)->buffer_pos = (rbuf)->buffer,                                \
50     ((((rbuf)->internal_dont_touch_this                                 \
51        = iread ((rbuf)->fd, (rbuf)->buffer,                             \
52                 sizeof ((rbuf)->buffer))) <= 0)                         \
53      ? (rbuf)->internal_dont_touch_this                                 \
54      : ((rbuf)->buffer_left = (rbuf)->internal_dont_touch_this - 1,     \
55         *((char *) (store)) = *(rbuf)->buffer_pos++,                    \
56         1))))
57
58 /* Return the file descriptor of RBUF.  */
59 #define RBUF_FD(rbuf) ((rbuf)->fd)
60
61 /* Function declarations */
62 void rbuf_initialize PARAMS ((struct rbuf *, int));
63 int rbuf_initialized_p PARAMS ((struct rbuf *));
64 void rbuf_uninitialize PARAMS ((struct rbuf *));
65 int rbuf_readchar PARAMS ((struct rbuf *, char *));
66 int rbuf_peek PARAMS ((struct rbuf *, char *));
67 int rbuf_flush PARAMS ((struct rbuf *, char *, int));
68 void rbuf_discard PARAMS ((struct rbuf *));
69
70 #endif /* RBUF_H */