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