aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/tbzi2t.x
blob: 5ce65996ce48c08146dddd9e00e2cdcdcf1dd3db (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
include <mach.h>		# for SZB_CHAR
include "tbtables.h"

# tbzi2t -- convert integer column to text
# When reading a text table into memory, if non-numeric text is found in
# a column of type integer, 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 tbzi2t (tp, cp, width)

pointer tp		# i: pointer to table descriptor
pointer cp		# i: pointer to column descriptor
int	width		# i: the maximum width for this column
#--
pointer new		# pointer to new memory for column data
int	row		# row number
int	op		# offset in new char array
errchk	calloc

begin
	# Allocate memory for the array of strings.
	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_INDEFI(Memi[COL_OFFSET(cp)+row-1])) {
		Memc[new+op] = EOS
	    } else {
		call sprintf (Memc[new+op], width, "%d")
		    call pargi (Memi[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_INT)
	COL_OFFSET(cp) = new

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