1 /* $Id: pushback.c,v 1.1 2000/12/09 03:08:23 apbianco Exp $
4 Revision 1.1 2000/12/09 03:08:23 apbianco
5 2000-12-08 Alexandre Petit-Bianco <apbianco@cygnus.com>
9 Revision 1.2 2000/08/23 19:42:17 cory
10 Added support for more Unix platforms. The following code has been hacked
11 to work on AIX, Solaris, True 64, and HP-UX.
12 Added bigendian check. Probably works on most big and little endian platforms
15 Revision 1.1.1.1 1999/12/06 03:09:13 toast
20 Revision 1.1 1999/05/10 08:32:37 burnsbr
26 pushback.c - code for a pushback buffer to handle file I/O
27 Copyright (C) 1999 Bryan Burns
29 This program is free software; you can redistribute it and/or
30 modify it under the terms of the GNU General Public License
31 as published by the Free Software Foundation; either version 2
32 of the License, or (at your option) any later version.
34 This program is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 GNU General Public License for more details.
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
51 void pb_init(pb_file
*pbf
, int fd
){
53 pbf
->next
= pbf
->pb_buff
;
57 int pb_push(pb_file
*pbf
, void *buff
, int amt
){
62 printf("%d bytes being pushed back to the buffer\n", amt
);
65 /* determine how much we can take */
66 if((RDSZ
- pbf
->buff_amt
) < amt
)
67 in_amt
= RDSZ
- pbf
->buff_amt
;
74 /* figure out if we need to wrap around, and if so, by how much */
75 if(((pbf
->pb_buff
+ RDSZ
) - pbf
->next
) < in_amt
)
76 wrap
= in_amt
- ((pbf
->pb_buff
+ RDSZ
) - pbf
->next
);
78 /* write everything up til the end of the buffer */
79 memcpy(pbf
->next
, buff
, (in_amt
- wrap
));
81 /* finish writing what's wrapped around */
82 memcpy(pbf
->pb_buff
, ((char *)buff
+ (in_amt
- wrap
)), wrap
);
84 /* update the buff_amt field */
85 pbf
->buff_amt
+= in_amt
;
88 printf("%d bytes we can't accept\n", (amt
- in_amt
));
95 int pb_read(pb_file
*pbf
, void *buff
, int amt
){
102 printf("%d bytes requested from us\n", amt
);
104 while(out_amt
< amt
){
105 /* if our push-back buffer contains some data */
106 if(pbf
->buff_amt
> 0){
109 printf("giving data from buffer\n");
112 /* calculate how much we can actually give the caller */
113 if( (amt
- out_amt
) < pbf
->buff_amt
)
114 tmp
= (amt
- out_amt
);
118 /* Determine if we're going to need to wrap around the buffer */
119 if(tmp
> ((pbf
->pb_buff
+ RDSZ
) - pbf
->next
))
120 wrap
= tmp
- ((pbf
->pb_buff
+ RDSZ
) - pbf
->next
);
122 memcpy(bp
, pbf
->next
, (tmp
- wrap
));
123 bp
= &(((char *)bp
)[tmp
- wrap
]);
125 /* If we need to wrap, read from the start of the buffer */
127 memcpy(bp
, pbf
->pb_buff
, wrap
);
128 bp
= &(((char *)bp
)[wrap
]);
131 /* update the buff_amt field */
132 pbf
->buff_amt
-= tmp
;
136 printf("%d bytes remaining in buffer\n", pbf
->buff_amt
);
139 /* if the buffer is empty, reset the next header to the front of the
140 buffer so subsequent pushbacks/reads won't have to wrap */
141 if(pbf
->buff_amt
== 0)
142 pbf
->next
= pbf
->pb_buff
;
148 printf("Reading from file..\n");
151 /* The pushback buffer was empty, so we just need to read from the file */
152 tmp
= read(pbf
->fd
, bp
, (amt
- out_amt
));
158 bp
= &(((char *)bp
)[tmp
]);
163 printf("managed to read %d bytes\n", out_amt
);