aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/tbzd2t.x
blob: 63160cd7bfdb83753c7fb0658eb1cb014c840a36 (plain) (blame)
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
include <mach.h>		# for MAX_DIGITS, NDIGITS_DP and SZB_CHAR
include "tbtables.h"

# tbzd2t -- convert column of type double to text
# When reading a text table into memory, if non-numeric text is found in
# a column of type double, this routine may be called to convert the
# data type to text.
#
# Phil Hodge,  7-Jun-1994  Subroutine created.
# Phil Hodge, 10-Aug-1994  Update COL_LEN.

procedure tbzd2t (tp, cp, width, precision, fmt_code)

pointer tp		# i: pointer to table descriptor
pointer cp		# i: pointer to column descriptor
int	width		# i: the maximum width for this column
int	precision	# i: the precision for writing this column
char	fmt_code	# i: format code for print format
#--
pointer sp
pointer pform		# scratch for print format for this column
pointer new		# pointer to new memory for column data
int	wid, prec	# width & precision, modified for print format
int	row		# row number
int	op		# offset in new char array
errchk	calloc

begin
	call smark (sp)
	call salloc (pform, SZ_FNAME, TY_CHAR)

	# Assign the print format for writing previous values into
	# the text strings.  Left justify.
	Memc[pform] = '%'
	Memc[pform+1] = '-'

	wid = min (width, MAX_DIGITS)
	prec = precision
	if (fmt_code == 'g')
	    prec = max (prec, wid-2) 		# maximum precision
	prec = min (prec, NDIGITS_DP)
	call sprintf (Memc[pform+2], SZ_FNAME-2, "%d.%d%c")
	    call pargi (wid)
	    call pargi (prec)
	    call pargc (fmt_code)

	# Allocate memory for the array of strings.  Note that we use
	# width rather than wid.
	call calloc (new, (width+1) * TB_ALLROWS(tp), TY_CHAR)

	op = 0					# initial value

	# Copy each row.
	do row = 1, TB_NROWS(tp) {

	    if (IS_INDEFD(Memd[COL_OFFSET(cp)+row-1])) {
		Memc[new+op] = EOS
	    } else {
		call sprintf (Memc[new+op], width, Memc[pform])
		    call pargd (Memd[COL_OFFSET(cp) + row - 1])
	    }

	    op = op + width + 1			# add one for EOS
	}

	# Free the old memory, and save the new pointer.
	call mfree (COL_OFFSET(cp), TY_DOUBLE)
	COL_OFFSET(cp) = new

	# Specify the new data type and width.
	COL_DTYPE(cp) = -width
	COL_LEN(cp) = (width + SZB_CHAR-1) / SZB_CHAR

	call sfree (sp)
end