version 1.72, 2018/08/18 20:18:14 |
version 1.73, 2018/08/19 23:10:28 |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
|
|
#include <assert.h> |
#include <assert.h> |
|
#include <ctype.h> |
#include <stdint.h> |
#include <stdint.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
tblcalc_number(struct rofftbl *tbl, struct roffcol *col, |
tblcalc_number(struct rofftbl *tbl, struct roffcol *col, |
const struct tbl_opts *opts, const struct tbl_dat *dp) |
const struct tbl_opts *opts, const struct tbl_dat *dp) |
{ |
{ |
int i; |
const char *cp, *lastdigit, *lastpoint; |
size_t sz, ssz, d; |
size_t intsz, totsz; |
const char *str; |
|
char *cp; |
|
char buf[2]; |
char buf[2]; |
|
|
|
if (dp->string == NULL || *dp->string == '\0') |
|
return; |
|
|
/* |
/* |
* First calculate number width and decimal place (last + 1 for |
* Find the last digit and |
* non-decimal numbers). If the stored decimal is subsequent to |
* the last decimal point that is adjacent to a digit. |
* ours, make our size longer by that difference |
* The alignment indicator "\&" overrides everything. |
* (right-"shifting"); similarly, if ours is subsequent the |
|
* stored, then extend the stored size by the difference. |
|
* Finally, re-assign the stored values. |
|
*/ |
*/ |
|
|
str = dp->string ? dp->string : ""; |
lastdigit = lastpoint = NULL; |
sz = (*tbl->slen)(str, tbl->arg); |
for (cp = dp->string; cp[0] != '\0'; cp++) { |
|
if (cp[0] == '\\' && cp[1] == '&') { |
|
lastdigit = lastpoint = cp; |
|
break; |
|
} else if (cp[0] == opts->decimal && |
|
(isdigit((unsigned char)cp[1]) || |
|
(cp > dp->string && isdigit((unsigned char)cp[-1])))) |
|
lastpoint = cp; |
|
else if (isdigit((unsigned char)cp[0])) |
|
lastdigit = cp; |
|
} |
|
|
/* FIXME: TBL_DATA_HORIZ et al.? */ |
/* Not a number, treat as a literal string. */ |
|
|
buf[0] = opts->decimal; |
totsz = (*tbl->slen)(dp->string, tbl->arg); |
buf[1] = '\0'; |
if (lastdigit == NULL) { |
|
if (col->width < totsz) |
|
col->width = totsz; |
|
return; |
|
} |
|
|
if (NULL != (cp = strrchr(str, opts->decimal))) { |
/* Measure the width of the integer part. */ |
buf[1] = '\0'; |
|
for (ssz = 0, i = 0; cp != &str[i]; i++) { |
|
buf[0] = str[i]; |
|
ssz += (*tbl->slen)(buf, tbl->arg); |
|
} |
|
d = ssz; |
|
} else |
|
d = sz; |
|
|
|
/* Adjust the settings for this column. */ |
if (lastpoint == NULL) |
|
lastpoint = lastdigit + 1; |
|
intsz = 0; |
|
buf[1] = '\0'; |
|
for (cp = dp->string; cp < lastpoint; cp++) { |
|
buf[0] = cp[0]; |
|
intsz += (*tbl->slen)(buf, tbl->arg); |
|
} |
|
|
if (col->decimal > d) { |
/* |
sz += col->decimal - d; |
* If this number has more integer digits than all numbers |
d = col->decimal; |
* seen on earlier lines, shift them all to the right. |
|
* If it has fewer, shift this number to the right. |
|
*/ |
|
|
|
if (intsz > col->decimal) { |
|
col->nwidth += intsz - col->decimal; |
|
col->decimal = intsz; |
} else |
} else |
col->nwidth += d - col->decimal; |
totsz += col->decimal - intsz; |
|
|
if (sz > col->nwidth) |
/* Update the maximum total width seen so far. */ |
col->nwidth = sz; |
|
if (d > col->decimal) |
if (totsz > col->nwidth) |
col->decimal = d; |
col->nwidth = totsz; |
} |
} |