Blame


1 b2cfc4e2 2003-09-30 devnull /*
2 b2cfc4e2 2003-09-30 devnull * The authors of this software are Rob Pike and Ken Thompson.
3 b2cfc4e2 2003-09-30 devnull * Copyright (c) 2002 by Lucent Technologies.
4 b2cfc4e2 2003-09-30 devnull * Permission to use, copy, modify, and distribute this software for any
5 b2cfc4e2 2003-09-30 devnull * purpose without fee is hereby granted, provided that this entire notice
6 b2cfc4e2 2003-09-30 devnull * is included in all copies of any software which is or includes a copy
7 b2cfc4e2 2003-09-30 devnull * or modification of this software and in all copies of the supporting
8 b2cfc4e2 2003-09-30 devnull * documentation for such software.
9 b2cfc4e2 2003-09-30 devnull * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10 b2cfc4e2 2003-09-30 devnull * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
11 b2cfc4e2 2003-09-30 devnull * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12 b2cfc4e2 2003-09-30 devnull * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13 b2cfc4e2 2003-09-30 devnull */
14 b2cfc4e2 2003-09-30 devnull #include <stdarg.h>
15 b2cfc4e2 2003-09-30 devnull #include <string.h>
16 b2cfc4e2 2003-09-30 devnull #include "utf.h"
17 b2cfc4e2 2003-09-30 devnull #include "fmt.h"
18 b2cfc4e2 2003-09-30 devnull #include "fmtdef.h"
19 b2cfc4e2 2003-09-30 devnull
20 b2cfc4e2 2003-09-30 devnull /*
21 b2cfc4e2 2003-09-30 devnull * Reads a floating-point number by interpreting successive characters
22 b2cfc4e2 2003-09-30 devnull * returned by (*f)(vp). The last call it makes to f terminates the
23 b2cfc4e2 2003-09-30 devnull * scan, so is not a character in the number. It may therefore be
24 b2cfc4e2 2003-09-30 devnull * necessary to back up the input stream up one byte after calling charstod.
25 b2cfc4e2 2003-09-30 devnull */
26 b2cfc4e2 2003-09-30 devnull
27 b2cfc4e2 2003-09-30 devnull double
28 b2cfc4e2 2003-09-30 devnull fmtcharstod(int(*f)(void*), void *vp)
29 b2cfc4e2 2003-09-30 devnull {
30 b2cfc4e2 2003-09-30 devnull double num, dem;
31 b2cfc4e2 2003-09-30 devnull int neg, eneg, dig, exp, c;
32 b2cfc4e2 2003-09-30 devnull
33 b2cfc4e2 2003-09-30 devnull num = 0;
34 b2cfc4e2 2003-09-30 devnull neg = 0;
35 b2cfc4e2 2003-09-30 devnull dig = 0;
36 b2cfc4e2 2003-09-30 devnull exp = 0;
37 b2cfc4e2 2003-09-30 devnull eneg = 0;
38 b2cfc4e2 2003-09-30 devnull
39 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
40 b2cfc4e2 2003-09-30 devnull while(c == ' ' || c == '\t')
41 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
42 b2cfc4e2 2003-09-30 devnull if(c == '-' || c == '+'){
43 b2cfc4e2 2003-09-30 devnull if(c == '-')
44 b2cfc4e2 2003-09-30 devnull neg = 1;
45 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
46 b2cfc4e2 2003-09-30 devnull }
47 b2cfc4e2 2003-09-30 devnull while(c >= '0' && c <= '9'){
48 b2cfc4e2 2003-09-30 devnull num = num*10 + c-'0';
49 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
50 b2cfc4e2 2003-09-30 devnull }
51 b2cfc4e2 2003-09-30 devnull if(c == '.')
52 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
53 b2cfc4e2 2003-09-30 devnull while(c >= '0' && c <= '9'){
54 b2cfc4e2 2003-09-30 devnull num = num*10 + c-'0';
55 b2cfc4e2 2003-09-30 devnull dig++;
56 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
57 b2cfc4e2 2003-09-30 devnull }
58 b2cfc4e2 2003-09-30 devnull if(c == 'e' || c == 'E'){
59 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
60 b2cfc4e2 2003-09-30 devnull if(c == '-' || c == '+'){
61 b2cfc4e2 2003-09-30 devnull if(c == '-'){
62 b2cfc4e2 2003-09-30 devnull dig = -dig;
63 b2cfc4e2 2003-09-30 devnull eneg = 1;
64 b2cfc4e2 2003-09-30 devnull }
65 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
66 b2cfc4e2 2003-09-30 devnull }
67 b2cfc4e2 2003-09-30 devnull while(c >= '0' && c <= '9'){
68 b2cfc4e2 2003-09-30 devnull exp = exp*10 + c-'0';
69 b2cfc4e2 2003-09-30 devnull c = (*f)(vp);
70 b2cfc4e2 2003-09-30 devnull }
71 b2cfc4e2 2003-09-30 devnull }
72 b2cfc4e2 2003-09-30 devnull exp -= dig;
73 b2cfc4e2 2003-09-30 devnull if(exp < 0){
74 b2cfc4e2 2003-09-30 devnull exp = -exp;
75 b2cfc4e2 2003-09-30 devnull eneg = !eneg;
76 b2cfc4e2 2003-09-30 devnull }
77 b2cfc4e2 2003-09-30 devnull dem = __fmtpow10(exp);
78 b2cfc4e2 2003-09-30 devnull if(eneg)
79 b2cfc4e2 2003-09-30 devnull num /= dem;
80 b2cfc4e2 2003-09-30 devnull else
81 b2cfc4e2 2003-09-30 devnull num *= dem;
82 b2cfc4e2 2003-09-30 devnull if(neg)
83 b2cfc4e2 2003-09-30 devnull return -num;
84 b2cfc4e2 2003-09-30 devnull return num;
85 b2cfc4e2 2003-09-30 devnull }