summaryrefslogtreecommitdiff
path: root/support/word2x/col-align.cc
blob: b205c83faa0cf4194acb9beebe10984e312ae62d (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* $Id: col-align.cc,v 1.3 1997/03/22 20:20: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"

/* Calculate the widths and guess the alignment a column wants */
struct wd_info find_width(int rows, const char *const *cdata)
{
    int max_wd[3], i, not_num, is_num;
    int lt_sp, rt_sp, align_set;
    wd_info res;
    const char *cdp, *sc;
    num_info nd;

    max_wd[0]=0;
    max_wd[1]=0;
    max_wd[2]=0;
    is_num=not_num=align_set=0;
    res.align=ALIGN_LEFT;
    res.has_sign=0;

    for (i=0; i<rows; i++)
    {
	cdp=cdata[i];
	if (cdp==NULL)
	    continue;		// Handle blank entries

	nd=scan_num(cdp);
	if (nd.dot_pos==-1)
	    not_num++;
	else
	{
	    is_num++;
	    res.has_sign |= nd.has_sign;
	    if (nd.wd[0]>max_wd[0])
		max_wd[0]=nd.wd[0];
	    if (nd.wd[1]>max_wd[1])
		max_wd[1]=nd.wd[1];
	}
	if (strlen(cdp)>(unsigned) max_wd[2])
	    max_wd[2]=strlen(cdp);
	for (lt_sp=0, sc=cdp; isspace(*sc); sc++, lt_sp++)
	{
	    if (*sc==CH_SUSPECT)
		align_set=1;
	}
	if (*sc=='\0')
	    continue;		// Blank entry gives no information

	while (*sc!='\0')
	{
	    if (*sc==CH_SUSPECT)
		align_set=1;
	    if (isspace(*sc))
		rt_sp++;
	    else
		rt_sp=0;
	    sc++;
	}

	if (align_set)
	    continue;

	if (lt_sp==0 && rt_sp<=1)
	    continue;
	if (lt_sp-rt_sp<-1)
	{
	    res.align=ALIGN_LEFT;
	    continue;
	}
	if (lt_sp-rt_sp>1)
	{
	    res.align=ALIGN_RIGHT;
	    continue;
	}
	res.align=ALIGN_CENTER;
    }

    if (is_num>not_num)
    {
	/*
	 * Decimal alignment, set dp_col and leave the alignment
	 * alone to handle non-number entries nicely.
	 */
	res.width=max_wd[0]+(max_wd[1]>0) ? 0 : 1;
	res.dp_col=max_wd[0]+1;
    }
    else
    {
	res.width=0;
	res.dp_col=-1;
    }

    if (res.width<max_wd[2])
	res.width=max_wd[2];	// dp_col irrelant

    return res;
}