aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/texpand/pushstack.x
blob: 58d8b7978d59aee225140cc8a7ab73f1b28d6906 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
include	<tbset.h>

define	SZ_COLVAL	SZ_LINE

# The following procedures treat a table as if it were a stack, that is,
# all reading and writing is done at the end of the table. The end of the
# table is indicated by TB_NROWS.
#
# B.Simon	25-Apr-88	Original
# B.Simon	27-Jan-98	Drop temporary tables

# PUSHSTACK -- Push a null row on the top of a table stack

procedure pushstack (tp)

pointer	tp		# i: Table descriptor
#--
int	top
int	tbpsta()

begin
	top = tbpsta (tp, TBL_NROWS) + 1
	call tbtwer (tp, top)
end

# POPSTACK -- Pop the top row from a table stack

procedure popstack (tp)

pointer	tp		# i: Table descriptor
#--
int	top
int	tbpsta()

begin
	top = tbpsta (tp, TBL_NROWS)
	if (top > 0)
	    call tbrdel (tp, top, top)
end

# NUMSTACK -- Return the number of rows in a table stack

int procedure numstack (tp)

pointer	tp		# i: Table descriptor
#--
int	tbpsta()

begin
	return (tbpsta (tp, TBL_NROWS))
end

# INITSTACK -- Initialize a table stack and return its descriptor

pointer procedure initstack (tp, extra)

pointer	tp		# i: Table to use as a template for the table stack
char	extra[ARB]	# i: Extra columns to add to the table stack
#--
char	comma
int	ic, jc
pointer	sp, cp, stack, colname, colunits, colfmt, tmproot, tmpfile

int	stridx()
pointer	tbtopn()

errchk	tbtopn, tbtcre

begin
	# Set up arrays in dynamic memory

	call smark (sp)
	call salloc (colname, SZ_COLNAME, TY_CHAR)
	call salloc (colunits, SZ_COLUNITS, TY_CHAR)
	call salloc (colfmt, SZ_COLFMT, TY_CHAR)
	call salloc (tmproot, SZ_FNAME, TY_CHAR)
	call salloc (tmpfile, SZ_FNAME, TY_CHAR)

	# Create the stack table

	call mktemp ("tmp$stk", Memc[tmproot], SZ_FNAME)
	call tbtext (Memc[tmproot], Memc[tmpfile], SZ_FNAME)
	stack = tbtopn (Memc[tmpfile], NEW_COPY, tp)

	# Set up column information that will not vary across columns

	Memc[colunits] = EOS
	Memc[colfmt] = EOS

	# Add column names from the extra string

	ic = 1
	comma = ','
	repeat {

	    # Copy the next comma delimeted column name

	    jc = stridx (comma, extra[ic])
	    if (jc == 0)
		call strcpy (extra[ic], Memc[colname], SZ_COLNAME)
	    else
		call strcpy (extra[ic], Memc[colname], jc-1)
	    ic = ic + jc

	    # Create the new column

	    if (Memc[colname] != EOS)
		call tbcdef (stack, cp, Memc[colname], Memc[colunits], 
			     Memc[colfmt], TY_INT, 1, 1)

	} until (jc == 0)

	# Return the stack table descriptor

	call tbtcre (stack)
	call sfree (sp)

	return (stack)
end

# FREESTACK -- Close and delete a table stack

procedure freestack (tp)

pointer	tp		# i: Table descriptor
#--
pointer	sp, table

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

	call tbtnam (tp, Memc[table], SZ_FNAME)
	call tbtclo (tp)

	call delete (Memc[table])
	call sfree (sp)
end

# PUTSTACKT -- Put a text string in the top row of a table stack

int procedure putstackt (tp, colname, colval)

pointer	tp		# i: Table descriptor
char	colname[ARB]	# i: Column name
char	colval[ARB]	# i: Column value
#--
int	top, found
pointer	cp

int	tbpsta()

begin
	top = tbpsta (tp, TBL_NROWS)
	call tbcfnd (tp, colname, cp, 1)

	found = NO
	if (cp != NULL) {
	    ifnoerr {
		call tbrptt (tp, cp, colval, ARB, 1, top)
	    } then {
		found = YES
	    }
	}

	return (found)
end

# PUTSTACKI -- Put an integer  in the top row of a table stack

procedure putstacki (tp, colname, colval)

pointer	tp		# i: Table descriptor
char	colname[ARB]	# i: Column name
int	colval		# i: Column value
#--
int	top
pointer	cp

int	tbpsta()

begin
	top = tbpsta (tp, TBL_NROWS)

	call tbcfnd (tp, colname, cp, 1)
	call tbepti (tp, cp, top, colval)

end

# ANDSTACK -- Combine the top two rows of the table stack

procedure andstack (tp)

pointer	tp		# i: Table descriptor
#--
int	top
int	tbpsta()

begin
	top = tbpsta (tp, TBL_NROWS)

	call movtbrow (tp, top, tp, top-1)
	call tbrdel (tp, top, top)
end

# MOVSTACK -- Move the top row of one table stack to another

procedure movstack (rtp, wtp)

pointer	rtp		# i: Table descriptor of table read from
pointer	wtp		# i: Table descriptor of table written to
#--
int	rtop, wtop

int	tbpsta()

begin
	call pushstack (wtp)

	rtop = tbpsta (rtp, TBL_NROWS)
	wtop = tbpsta (wtp, TBL_NROWS)

	call movtbrow (rtp, rtop, wtp, wtop)
	call tbrdel (rtp, rtop, rtop)

end