summaryrefslogtreecommitdiff
path: root/support/word2x/num_unit_probe.c
blob: d1016fb43af5ccd19929dd240e8f2c3ebddf2674 (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
/* $Id: num_unit_probe.c,v 1.1 1997/04/22 20:32:17 dps Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif /* HAVE_CTYPE_H */
#define __EXCLUDE_READER_CLASSES
#include "lib.h"

struct unit_number n_unit_probe(const char *txt, struct unit_info *dp)
{    
    int i,j,n,c,l,unit;
    struct unit_number u_num;
    
    u_num.unit_num=-1;
    unit=dp->unit_type-1;
    l=strlen(txt);
    i=0;
    while(1)
    {
	n=0;
	for (c=0; i<l && isdigit(txt[i]); i++, c++)
	    n=n*10+txt[i]-'0';
	if (c==0)
	    break;
	unit++;
	if (unit>=NUNITS)
	{
	    unit=NUNITS-1;
	    break;
	}
	if (dp->unit_number[unit]==-1)
	{
	    if (n>MAX_START_NUM)
		return u_num;
	    
	    dp->unit_number[unit]=n;
	    for (j=unit+1; j<NUNITS; j++)
		dp->unit_number[j]=0;
	}
	else if (dp->unit_number[unit]+1==n)
	{
	    dp->unit_number[unit]++;
	    for (j=unit+1; j<NUNITS; j++)
		dp->unit_number[j]=0;
	}
	else if (dp->unit_number[unit]!=n)
	   return u_num;
	
	if (txt[i]!='.')
	    break;
	i++;
    }
    
    if (unit==dp->unit_type-1)
	return u_num;
    u_num.unit_num=unit;
    u_num.offset=i;
    return u_num;
}