aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/lib/tabvar.x
blob: 339f2d95f90dce4df65a830611b44479a1cf278a (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
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
110
111
112
113
114
115
116
117
118
include	<tbset.h>

# TABVAR -- Retrieve a table column given its name
#
# B.Simon	03-May-91	Original
# B.Simon	23-Jun-97	Peicewise evaluation of column

procedure tabvar (stack, colname)

pointer	stack		# u: Expression stack pointer
char	colname[ARB]	# i: Column name
#--
include "../tabvar.com"

int	i, coltype, nrows
pointer	sp, nullbuf, buffer, errmsg, cp

string	badcolnam  "Column name not found (%s)"

bool	streq()
int	tbcigi()
pointer	stk_alloc()

begin
	# Allocate dynamic memory for strings

	call smark (sp)
	call salloc (errmsg, SZ_LINE, TY_CHAR)

	# Get column pointer from name

	call tbcfnd (tabptr, colname, cp, 1)
	if (cp == NULL) {
	    if (streq (colname, "rownum")) {
		call rowvar (stack)
		return
	    } else {
		call sprintf (Memc[errmsg], SZ_LINE, badcolnam)
		call pargstr (colname)
		call error (1, Memc[errmsg])
	    }
	}

	# Get column type

	coltype = tbcigi (cp, TBL_COL_DATATYPE)
	if (coltype == TY_BOOL || coltype == TY_SHORT || coltype == TY_LONG) {
	    coltype = TY_INT
	} else if (coltype < 0) {
	    coltype = TY_DOUBLE
	}

	# Allocate a buffer on the expression evaluator stack

	nrows = (lastrow - firstrow) + 1
	call malloc (nullbuf, nrows, TY_BOOL)
	buffer = stk_alloc (stack, nrows, coltype)

	# Copy the table column into the buffer
	# Substitute the user supplied vales for nulls

	switch (coltype) {
	case TY_SHORT, TY_INT, TY_LONG:
	    call tbcgti (tabptr, cp, Memi[buffer], Memb[nullbuf], 
			 firstrow, lastrow)
	    do i = 0, nrows-1 {
		if (Memb[nullbuf+i])
		    Memi[buffer+i] = nullval
	    }

	case TY_REAL:
	    call tbcgtr (tabptr, cp, Memr[buffer], Memb[nullbuf], 
			 firstrow, lastrow)
	    do i = 0, nrows-1 {
		if (Memb[nullbuf+i])
		    Memr[buffer+i] = nullval
	    }
	case TY_DOUBLE:
	    call tbcgtd (tabptr, cp, Memd[buffer], Memb[nullbuf], 
			 firstrow, lastrow)
	    do i = 0, nrows-1 {
		if (Memb[nullbuf+i])
		    Memd[buffer+i] = nullval
	    }
	}

	# Update the null array
	call stk_ornull (stack, Memb[nullbuf], nrows)

	call mfree (nullbuf, TY_BOOL)
	call sfree (sp)

end

# ROWVAR -- Handle the variable "rownum"

procedure rowvar (stack)

pointer	stack		# u: Expression stack pointer
#--
include "../tabvar.com"

int	irow, nrows
pointer	buffer

pointer	stk_alloc()

begin
	# Allocate a buffer on the expression evaluator stack

	nrows = (lastrow - firstrow) + 1
	buffer = stk_alloc (stack, nrows, TY_INT)

	# Fill the buffer with the row number
	do irow = 0, nrows-1
	    Memi[buffer+irow] = firstrow + irow
end