aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/tjoin/openitab.x
blob: ebdbcf975562a28f99dd35049696a0ab382d2205 (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
include <tbset.h>
include "tjoin.h"

# B.Simon	16-Apr-99	first code

# OPEN_ITAB -- Open one of the input tables used in the join

pointer procedure open_itab (intable, column)

char	intable[ARB]	# i: Input table name
char	column[ARB]	# i: List of join columns
#--
int	ic, icol
pointer	tj, sp, cname, errtxt

string	nojoincol "No column supplied as join column"
string	badcolnam "Column name not found in table (%s[c:%s])"
string  notopen   "Could not open table (%s)"

bool	strne()
int	tbpsta(), tbcnum(), word_count(), word_fetch()
pointer	tbtopn()

begin
	# Allocate memory for temporary strings

	call smark (sp)
	call salloc (cname, SZ_COLNAME, TY_CHAR)
	call salloc (errtxt, SZ_LINE, TY_CHAR)

	# Allocate memory for data structure

	call calloc (tj, LEN_TJSTRUCT, TY_INT)

	# Open table and put descriptor in structure

	iferr {
	    TJ_TAB(tj) = tbtopn (intable, READ_ONLY, NULL)
	} then {
	    call sprintf (Memc[errtxt], SZ_LINE, notopen)
	    call pargstr (intable)
	    call error (1, Memc[errtxt])
	}

	# Create array of data columns

	TJ_DNUM(tj) = tbpsta (TJ_TAB(tj), TBL_NCOLS)
	call malloc (TJ_DPTR(tj), TJ_DNUM(tj), TY_INT)

	do icol = 1, TJ_DNUM(tj)
	    TJ_DCOL(tj,icol) = tbcnum (TJ_TAB(tj), icol)

	# Create array of join columns

	TJ_JNUM(tj) = word_count (column)
	if (TJ_JNUM(tj) == 0)
	    call error (1, nojoincol)

	call malloc (TJ_JPTR(tj), TJ_JNUM(tj), TY_INT)

	ic = 1
	icol = 1
	while (word_fetch (column, ic, Memc[cname], SZ_COLNAME) > 0) {
	    call tbcfnd (TJ_TAB(tj), Memc[cname], TJ_JCOL(tj,icol), 1)

	    if (TJ_JCOL(tj,icol) == NULL) {
		if (strne (Memc[cname], ROWNAME)) {
		    call sprintf (Memc[errtxt], SZ_LINE, badcolnam)
		    call pargstr (intable)
		    call pargstr (Memc[cname])
		    call error (1, Memc[errtxt])
		}
	    }

	    icol = icol + 1
	}

	# Free temporary memory and return descriptor of new structure
 
	call sfree (sp)
	return (tj)
end