* add p cc
[mascara-docs.git] / compilers / pcc / pcc-libs-1.0.0 / libI77 / rdfmt.c
blobea6ad33a9c500fc2878c49614851f0b6e36fdbe5
1 /* $Id: rdfmt.c,v 1.3 2008/03/01 13:44:12 ragge Exp $ */
2 /*
3 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * Redistributions of source code and documentation must retain the above
10 * copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditionsand the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed or owned by Caldera
17 * International, Inc.
18 * Neither the name of Caldera International, Inc. nor the names of other
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
22 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 #include <stdlib.h>
37 #include "fio.h"
38 #include "fmt.h"
39 extern int cursor;
41 int rd_I(unint *n, int w, ftnlen len);
42 int rd_L(ftnint *n, int w);
43 int rd_A(char *p, ftnlen len);
44 int rd_AW(char *p, int w, ftnlen len);
45 int rd_F(ufloat *p, int w, int d, ftnlen len);
46 int rd_POS(char *s);
47 int rd_H(int n, char *s);
50 int
51 rd_ed(struct syl *p, void *ptr, ftnlen len)
53 int ch;
54 for(;cursor>0;cursor--) if((ch=(*getn)())<0) return(ch);
55 if(cursor<0)
56 { if(recpos+cursor < 0) err(elist->cierr,110,"fmt")
57 if(curunit->useek) fseek(cf,(long) cursor,1);
58 else err(elist->cierr,106,"fmt");
59 cursor=0;
61 switch(p->op)
63 default: fprintf(stderr,"rd_ed, unexpected code: %d\n%s\n",
64 p->op,fmtbuf);
65 abort();
66 case I: ch = (rd_I(ptr,p->p1,len));
67 break;
68 case IM: ch = (rd_I(ptr,p->p1,len));
69 break;
70 case L: ch = (rd_L(ptr,p->p1));
71 break;
72 case A: ch = (rd_A(ptr,len));
73 break;
74 case AW:
75 ch = (rd_AW(ptr,p->p1,len));
76 break;
77 case E: case EE:
78 case D:
79 case G:
80 case GE:
81 case F: ch = (rd_F(ptr,p->p1,p->p2,len));
82 break;
84 if(ch == 0) return(ch);
85 else if(feof(cf)) return(EOF);
86 clearerr(cf);
87 return(errno);
90 int
91 rd_ned(struct syl *p, char *ptr)
93 switch(p->op)
95 default: fprintf(stderr,"rd_ned, unexpected code: %d\n%s\n",
96 p->op,fmtbuf);
97 abort();
98 case APOS:
99 return(rd_POS((char /* XXX */ *)p->p1));
100 case H: return(rd_H(p->p1,(char /* XXX */ *)p->p2));
101 case SLASH: return((*donewrec)());
102 case TR:
103 case X: cursor += p->p1;
104 return(1);
105 case T: cursor=p->p1-recpos;
106 return(1);
107 case TL: cursor -= p->p1;
108 return(1);
113 rd_I(unint *n, int w, ftnlen len)
115 long x=0;
116 int i,sign=0,ch;
117 for(i=0;i<w;i++)
119 if((ch=(*getn)())<0) return(ch);
120 switch(ch)
122 default:
123 return(errno=115);
124 case ',': goto done;
125 case '+': break;
126 case '-':
127 sign=1;
128 break;
129 case ' ':
130 if(cblank) x *= 10;
131 break;
132 case '\n': break;
133 case '0': case '1': case '2': case '3': case '4':
134 case '5': case '6': case '7': case '8': case '9':
135 x=10*x+ch-'0';
136 break;
139 done:
140 if(sign) x = -x;
141 if(len==sizeof(short)) n->is=x;
142 else n->il=x;
143 return(0);
147 rd_L(ftnint *n, int w)
149 int ch,i,v = -1;
150 for(i=0;i<w;i++)
151 { if((ch=(*getn)())<0) return(ch);
152 if(ch=='t' && v==-1) v=1;
153 else if(ch=='f' && v==-1) v=0;
154 else if(ch==',') return(0);
156 if(v==-1)
157 { errno=116;
158 return(1);
160 *n=v;
161 return(0);
165 rd_F(ufloat *p, int w, int d, ftnlen len)
167 double x,y;
168 int i,sx,sz,ch,dot,ny,z,sawz;
170 ch = 0; /* XXX */
171 x=y=0;
172 sawz=z=ny=dot=sx=sz=0;
173 for(i=0;i<w;)
174 { i++;
175 if((ch=(*getn)())<0) return(ch);
176 else if((ch==' ' && !cblank) || ch=='+') continue;
177 else if(ch=='-') sx=1;
178 else if(ch<='9' && ch>='0')
179 x=10*x+ch-'0';
180 else if(ch=='e' || ch=='d' || ch=='.')
181 break;
182 else if(cblank && ch==' ') x*=10;
183 else if(ch==',')
184 { i=w;
185 break;
187 else if(ch!='\n') return(errno=115);
189 if(ch=='.') dot=1;
190 while(i<w && ch!='e' && ch!='d' && ch!='+' && ch!='-')
191 { i++;
192 if((ch=(*getn)())<0) return(ch);
193 else if(ch<='9' && ch>='0')
194 y=10*y+ch-'0';
195 else if(cblank && ch==' ')
196 y *= 10;
197 else if(ch==',') {i=w; break;}
198 else if(ch==' ') continue;
199 else continue;
200 ny++;
202 if(ch=='-') sz=1;
203 while(i<w)
204 { i++;
205 sawz=1;
206 if((ch=(*getn)())<0) return(ch);
207 else if(ch=='-') sz=1;
208 else if(ch<='9' && ch>='0')
209 z=10*z+ch-'0';
210 else if(cblank && ch==' ')
211 z *= 10;
212 else if(ch==',') break;
213 else if(ch==' ') continue;
214 else if(ch=='+') continue;
215 else if(ch!='\n') return(errno=115);
217 if(!dot)
218 for(i=0;i<d;i++) x /= 10;
219 for(i=0;i<ny;i++) y /= 10;
220 x=x+y;
221 if(sz)
222 for(i=0;i<z;i++) x /=10;
223 else for(i=0;i<z;i++) x *= 10;
224 if(sx) x = -x;
225 if(!sawz)
227 for(i=scale;i>0;i--) x /= 10;
228 for(i=scale;i<0;i++) x *= 10;
230 if(len==sizeof(float)) p->pf=x;
231 else p->pd=x;
232 return(0);
236 rd_A(char *p, ftnlen len)
238 int i,ch;
239 for(i=0;i<len;i++)
240 { GET(ch);
241 *p++=VAL(ch);
243 return(0);
247 rd_AW(char *p, int w, ftnlen len)
249 int i,ch;
250 if(w>=len)
251 { for(i=0;i<w-len;i++)
252 GET(ch);
253 for(i=0;i<len;i++)
254 { GET(ch);
255 *p++=VAL(ch);
257 return(0);
259 for(i=0;i<w;i++)
260 { GET(ch);
261 *p++=VAL(ch);
263 for(i=0;i<len-w;i++) *p++=' ';
264 return(0);
268 rd_H(int n, char *s)
269 { int i,ch;
270 for(i=0;i<n;i++)
271 if((ch=(*getn)())<0) return(ch);
272 else if(ch=='\n') *s++ = ' ';
273 else *s++ = ch=='\n'?' ':ch;
274 return(1);
278 rd_POS(char *s)
280 char quote;
281 int ch;
283 quote= *s++;
284 for(;*s;s++)
285 if(*s==quote && *(s+1)!=quote) break;
286 else if((ch=(*getn)())<0) return(ch);
287 else *s = ch=='\n'?' ':ch;
288 return(1);