]> sjero.net Git - wget/blob - src/rbuf.c
[svn] Renamed xread/xwrite/xclose to fd_read/fd_write/fd_close. The "x" prefix is
[wget] / src / rbuf.c
1 /* Buffering read.
2    Copyright (C) 1995, 1996, 1997, 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 /* This is a simple implementation of buffering IO-read functions.  */
31
32 #include <config.h>
33
34 #include <stdio.h>
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #else
38 # include <strings.h>
39 #endif
40
41 #include "wget.h"
42 #include "rbuf.h"
43 #include "connect.h"
44
45 void
46 rbuf_initialize (struct rbuf *rbuf, int fd)
47 {
48   rbuf->fd = fd;
49   rbuf->buffer_pos = rbuf->buffer;
50   rbuf->buffer_left = 0;
51 }
52
53 int
54 rbuf_initialized_p (struct rbuf *rbuf)
55 {
56   return rbuf->fd != -1;
57 }
58
59 void
60 rbuf_uninitialize (struct rbuf *rbuf)
61 {
62   rbuf->fd = -1;
63 }
64
65 int
66 rbuf_read_bufferful (struct rbuf *rbuf)
67 {
68   return fd_read (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer), -1);
69 }
70
71 /* Currently unused -- see RBUF_READCHAR.  */
72 #if 0
73 /* Function version of RBUF_READCHAR.  */
74 int
75 rbuf_readchar (struct rbuf *rbuf, char *store)
76 {
77   return RBUF_READCHAR (rbuf, store);
78 }
79 #endif
80
81 /* Like rbuf_readchar(), only don't move the buffer position.  */
82 int
83 rbuf_peek (struct rbuf *rbuf, char *store)
84 {
85   if (!rbuf->buffer_left)
86     {
87       int res;
88       rbuf->buffer_pos = rbuf->buffer;
89       rbuf->buffer_left = 0;
90       res = fd_read (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer), -1);
91       if (res <= 0)
92         return res;
93       rbuf->buffer_left = res;
94     }
95   *store = *rbuf->buffer_pos;
96   return 1;
97 }
98
99 #define MIN(p,q) (((p) <= (q)) ? (p) : (q))
100
101 /* Flush RBUF's buffer to WHERE.  Flush MAXSIZE bytes at most.
102    Returns the number of bytes actually copied.  If the buffer is
103    empty, 0 is returned.  */
104 int
105 rbuf_flush (struct rbuf *rbuf, char *where, int maxsize)
106 {
107   if (!rbuf->buffer_left)
108     return 0;
109   else
110     {
111       int howmuch = MIN (rbuf->buffer_left, maxsize);
112
113       if (where)
114         memcpy (where, rbuf->buffer_pos, howmuch);
115       rbuf->buffer_left -= howmuch;
116       rbuf->buffer_pos += howmuch;
117       return howmuch;
118     }
119 }
120
121 /* Discard any cached data in RBUF.  */
122 void
123 rbuf_discard (struct rbuf *rbuf)
124 {
125   rbuf->buffer_left = 0;
126   rbuf->buffer_pos = rbuf->buffer;
127 }