OSDN Git Service

* config/alpha/osf5.h (TARGET_LD_BUGGY_LDGP): New.
[pf3gnuchains/gcc-fork.git] / fastjar / pushback.c
1 /* $Id: pushback.c,v 1.1 2000/12/09 03:08:23 apbianco Exp $
2
3    $Log: pushback.c,v $
4    Revision 1.1  2000/12/09 03:08:23  apbianco
5    2000-12-08  Alexandre Petit-Bianco  <apbianco@cygnus.com>
6
7            * fastjar: Imported.
8
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
13    now.
14
15    Revision 1.1.1.1  1999/12/06 03:09:13  toast
16    initial checkin..
17
18
19
20    Revision 1.1  1999/05/10 08:32:37  burnsbr
21    Initial revision
22
23 */
24
25 /*
26   pushback.c - code for a pushback buffer to handle file I/O
27   Copyright (C) 1999  Bryan Burns
28   
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.
33   
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.
38   
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.
42  */
43
44 #include <unistd.h>
45 #include <string.h>
46 #include <stdio.h>
47
48 #include "jartool.h"
49 #include "pushback.h"
50
51 void pb_init(pb_file *pbf, int fd){
52   pbf->fd = fd;
53   pbf->next = pbf->pb_buff;
54   pbf->buff_amt = 0;
55 }
56
57 int pb_push(pb_file *pbf, void *buff, int amt){
58   int in_amt;
59   int wrap = 0;
60
61 #ifdef DEBUG
62   printf("%d bytes being pushed back to the buffer\n", amt);
63 #endif
64
65   /* determine how much we can take */
66   if((RDSZ - pbf->buff_amt) < amt)
67     in_amt = RDSZ - pbf->buff_amt;
68   else
69     in_amt = amt;
70
71   if(in_amt == 0)
72     return 0;
73
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);
77
78   /* write everything up til the end of the buffer */
79   memcpy(pbf->next, buff, (in_amt - wrap));
80
81   /* finish writing what's wrapped around */
82   memcpy(pbf->pb_buff, ((char *)buff + (in_amt - wrap)), wrap);
83          
84   /* update the buff_amt field */
85   pbf->buff_amt += in_amt;
86
87 #ifdef DEBUG
88   printf("%d bytes we can't accept\n", (amt - in_amt));
89 #endif
90
91   return in_amt;
92 }
93
94
95 int pb_read(pb_file *pbf, void *buff, int amt){
96   int out_amt = 0;
97   int wrap = 0;
98   void *bp = buff;
99   int tmp;
100
101 #ifdef DEBUG
102   printf("%d bytes requested from us\n", amt);
103 #endif
104   while(out_amt < amt){
105     /* if our push-back buffer contains some data */
106     if(pbf->buff_amt > 0){
107       
108 #ifdef DEBUG
109       printf("giving data from buffer\n");
110 #endif
111       
112       /* calculate how much we can actually give the caller */
113       if( (amt - out_amt) < pbf->buff_amt )
114         tmp = (amt - out_amt);
115       else
116         tmp = pbf->buff_amt;
117       
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);
121       
122       memcpy(bp, pbf->next, (tmp - wrap));
123       bp = &(((char *)bp)[tmp - wrap]);
124       
125       /* If we need to wrap, read from the start of the buffer */
126       if(wrap > 0){
127         memcpy(bp, pbf->pb_buff, wrap);
128         bp = &(((char *)bp)[wrap]);
129       }
130       
131       /* update the buff_amt field */
132       pbf->buff_amt -= tmp;
133       pbf->next += tmp;
134       
135 #ifdef DEBUG
136       printf("%d bytes remaining in buffer\n", pbf->buff_amt);
137 #endif
138       
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;
143       
144       out_amt += tmp;
145
146     } else {
147 #ifdef DEBUG
148       printf("Reading from file..\n");
149 #endif
150       
151       /* The pushback buffer was empty, so we just need to read from the file */
152       tmp = read(pbf->fd, bp, (amt - out_amt));
153       if(tmp == 0)
154         break;
155       else
156         out_amt += tmp;
157       
158       bp = &(((char *)bp)[tmp]);
159     }
160   }
161
162 #ifdef DEBUG
163   printf("managed to read %d bytes\n", out_amt);
164 #endif
165   return out_amt;
166 }