summaryrefslogtreecommitdiff
path: root/support/word2x/scan_num.cc
blob: f0f2d639d5cae015baf452b4ad28e2e2f58d40a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* $Id: scan_num.cc,v 1.4 1997/04/10 20:42:47 dps Exp $ */
/* Simple number analyser */
#define __EXCLUDE_READER_CLASSES
#include "lib.h"

num_info scan_num(const char *scan)
{
    int nd, p;
    num_info r;

    r.wd[0]=r.wd[1]=r.has_sign=nd=0;
    p=1;

    switch (*scan)
    {
    case '-':
    case '+':
	scan++;
	r.wd[0]++;
	p++;
	r.has_sign=1;
	break;

    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
	break;

    default:
	r.dot_pos=-1;
	return r;
    }

    for ( ; *scan!='\0'; scan++, p++)
    {
	switch (*scan)
	{
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9':
	    r.wd[nd]++;	// Digit
	    break;

	case '.':
	    r.dot_pos=p;
	    if ((++nd)==1)
		break;
	    /* Fall through */
	default:
	    r.dot_pos=-1;
	    return r;
	}
    }
    if (r.wd[0]+r.wd[1]<=r.has_sign)
    {
	r.dot_pos=-1;
	return r;
    }
    if (nd==0)
	r.dot_pos=p;
    return r;
}