1 /* $Id: wrtfmt.c,v 1.1.1.1 2008/08/24 05:34:48 gmcgarry Exp $ */
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
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
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.
41 int wrt_I(unint
*n
,int w
,ftnlen len
);
42 int wrt_IM(unint
*n
,int w
,int m
,ftnlen len
);
44 int wrt_A(char *p
,ftnlen len
);
45 int wrt_AW(char *p
, int w
,ftnlen len
);
46 int wrt_G(ufloat
*p
,int w
,int d
,int e
,ftnlen len
);
47 int wrt_H(int a
,int b
);
52 { /*buggy, could move off front of record*/
53 for(;cursor
>0;cursor
--) (*putn
)(' ');
56 if(cursor
+recpos
<0) err(elist
->cierr
,110,"left off");
57 if(curunit
->useek
) fseek(cf
,(long)cursor
,1);
58 else err(elist
->cierr
,106,"fmt");
65 w_ed(struct syl
*p
, void *ptr
, ftnlen len
)
67 if(mv_cur()) return(mv_cur());
71 fprintf(stderr
,"w_ed, unexpected code: %d\n%s\n",
74 case I
: return(wrt_I(ptr
,p
->p1
,len
));
76 return(wrt_IM(ptr
,p
->p1
,p
->p2
,len
));
77 case L
: return(wrt_L(ptr
,p
->p1
));
78 case A
: return(wrt_A(ptr
,len
));
80 return(wrt_AW(ptr
,p
->p1
,len
));
84 return(wrt_E(ptr
,p
->p1
,p
->p2
,p
->p3
,len
));
87 return(wrt_G(ptr
,p
->p1
,p
->p2
,p
->p3
,len
));
88 case F
: return(wrt_F(ptr
,p
->p1
,p
->p2
,len
));
93 w_ned(struct syl
*p
, char *ptr
)
97 default: fprintf(stderr
,"w_ned, unexpected code: %d\n%s\n",
101 return((*donewrec
)());
102 case T
: cursor
= p
->p1
-recpos
;
104 case TL
: cursor
-= p
->p1
;
111 return(wrt_AP(p
->p1
));
113 return(wrt_H(p
->p1
,p
->p2
));
118 wrt_I(unint
*n
,int w
,ftnlen len
)
119 { int ndigit
,sign
,spare
,i
;
122 if(len
==sizeof(short)) x
=n
->is
;
123 else if(len
== sizeof(char)) x
= n
->ic
;
125 ans
=icvt(x
,&ndigit
,&sign
);
127 if(sign
|| cplus
) spare
--;
129 for(i
=0;i
<len
;i
++) (*putn
)('*');
131 { for(i
=0;i
<spare
;i
++) (*putn
)(' ');
132 if(sign
) (*putn
)('-');
133 else if(cplus
) (*putn
)('+');
134 for(i
=0;i
<ndigit
;i
++) (*putn
)(*ans
++);
140 wrt_IM(unint
*n
,int w
,int m
,ftnlen len
)
141 { int ndigit
,sign
,spare
,i
,xsign
;
144 if(sizeof(short)==len
) x
=n
->is
;
145 else if(len
== sizeof(char)) x
= n
->ic
;
147 ans
=icvt(x
,&ndigit
,&sign
);
148 if(sign
|| cplus
) xsign
=1;
150 if(ndigit
+xsign
>w
|| m
+xsign
>w
)
151 { for(i
=0;i
<w
;i
++) (*putn
)('*');
155 { for(i
=0;i
<w
;i
++) (*putn
)(' ');
159 spare
=w
-ndigit
-xsign
;
162 for(i
=0;i
<spare
;i
++) (*putn
)(' ');
163 if(sign
) (*putn
)('-');
164 else if(cplus
) (*putn
)('+');
165 for(i
=0;i
<m
-ndigit
;i
++) (*putn
)('0');
166 for(i
=0;i
<ndigit
;i
++) (*putn
)(*ans
++);
173 if(mv_cur()) return(mv_cur());
177 { if(*s
!=quote
) (*putn
)(*s
);
178 else if(*++s
==quote
) (*putn
)(*s
);
187 if(mv_cur()) return(mv_cur());
188 while(a
--) (*putn
)(*s
++);
193 wrt_L(ftnint
*n
, int len
)
203 wrt_A(char *p
,ftnlen len
)
205 while(len
-- > 0) (*putn
)(*p
++);
210 wrt_AW(char *p
, int w
,ftnlen len
)
225 * Trivial ecvt implementation.
228 Xecvt(double value
, int ndigit
, int *decpt
, int *sign
)
236 snprintf(fmt
, 10, "%%# .%de", ndigit
-1);
237 snprintf(nr
, MXSTR
, fmt
, value
);
238 *sign
= (*w
== '-' ? 1 : 0);
240 *decpt
= atoi(&nr
[ndigit
+3]) + 1;
247 wrt_E(ufloat
*p
,int w
,int d
,int e
, ftnlen len
)
252 s
=Xecvt( (len
==sizeof(float)?p
->pf
:p
->pd
) ,d
,&dp
,&sign
);
253 if(sign
|| cplus
) delta
=6;
256 { for(i
=0;i
<w
;i
++) (*putn
)('*');
259 for(i
=0;i
<w
-(delta
+d
);i
++) (*putn
)(' ');
260 if(sign
) (*putn
)('-');
261 else if(cplus
) (*putn
)('+');
262 if(scale
<0 && scale
> -d
)
265 for(i
=0;i
<-scale
;i
++)
267 for(i
=0;i
<d
+scale
;i
++)
270 else if(scale
>0 && scale
<d
+2)
271 { for(i
=0;i
<scale
;i
++)
274 for(i
=0;i
<d
-scale
;i
++)
279 for(i
=0;i
<d
;i
++) (*putn
)(*s
++);
281 if(p
->pf
!= 0) dp
-= scale
;
283 if(dp
< 100 && dp
> -100) (*putn
)('e');
289 if(e
>=3 || dp
>= 100)
290 { (*putn
)(dp
/100 + '0');
293 if(e
!=1) (*putn
)(dp
/10+'0');
299 wrt_G(ufloat
*p
,int w
,int d
,int e
,ftnlen len
)
301 int i
,oldscale
=scale
,n
,j
;
302 x
= len
==sizeof(float)?p
->pf
:p
->pd
;
304 if(x
<.1) return(wrt_E(p
,w
,d
,e
,len
));
305 for(i
=0;i
<=d
;i
++,up
*=10)
310 i
=wrt_F(p
,w
-n
,d
-i
,len
);
311 for(j
=0;j
<n
;j
++) (*putn
)(' ');
315 return(wrt_E(p
,w
,d
,e
,len
));
319 * Simple fcvt() implementation.
322 Xfcvt(double value
, int ndigit
, int *decpt
, int *sign
)
330 snprintf(fmt
, 10, "%%# .%df", ndigit
);
331 snprintf(nr
, MXSTR
, fmt
, value
);
332 *sign
= (*w
== '-' ? 1 : 0);
337 for (w
+= 1; *w
&& *w
!= '.'; w
++)
349 wrt_F(ufloat
*p
, int w
,int d
, ftnlen len
)
350 { int i
,delta
,dp
,sign
,n
;
354 x
= (len
==sizeof(float)?p
->pf
:p
->pd
);
357 for(i
=0;i
<scale
;i
++) x
*=10;
358 else for(i
=0;i
<-scale
;i
++) x
/=10;
360 s
=Xfcvt(x
,d
,&dp
,&sign
);
362 if(sign
|| cplus
) delta
=2;
364 n
= w
- (d
+delta
+(dp
>0?dp
:0));
367 for(i
=0;i
<w
;i
++) PUT('*');
370 for(i
=0;i
<n
;i
++) PUT(' ');
372 else if(cplus
) PUT('+');
373 for(i
=0;i
<dp
;i
++) PUT(*s
++);
375 for(i
=0;i
< -dp
&& i
<d
;i
++) PUT('0');