aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/tbeoff.x
blob: 4f39a98b718207e545d09cc915500d3c5015118d (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
include <tbset.h>
include "tbtables.h"
include "tblerr.h"

# tbeoff -- get offset to an element
# This function returns the offset to an element (a specific row and
# column) in a table.
#
# Phil Hodge, 14-Sep-1987  Function created.
# Phil Hodge, 28-Jul-1994  Add elnum to calling sequence.

long procedure tbeoff (tp, cptr, rownum, elnum)

pointer tp			# i: pointer to table descriptor
pointer cptr			# i: pointer to column descriptor
int	rownum			# i: row number
int	elnum			# i: element number
#--
long	offset			# the offset in char
int	sz_element		# size of one element (if entry is an array)
int	tbeszt()

begin
	if (TB_TYPE(tp) == TBL_TYPE_S_ROW)
	    offset = TB_BOD(tp) + (rownum-1) * TB_ROWLEN(tp) +
			COL_OFFSET(cptr)

	else if (TB_TYPE(tp) == TBL_TYPE_S_COL)
	    offset = TB_BOD(tp) + COL_OFFSET(cptr) * TB_ALLROWS(tp) +
			(rownum-1) * COL_LEN(cptr)

	else if (TB_TYPE(tp) == TBL_TYPE_TEXT)
	    return (0)			# offset is meaningless

	else
	    call error (ER_TBCORRUPTED,
		"tbeoff:  bad table type; table or memory corrupted?")

	if (elnum > 1) {
	    # Not the first element.  First get the size of one element.
	    switch (COL_DTYPE(cptr)) {
	    case TBL_TY_REAL:
		sz_element = SZ_REAL
	    case TBL_TY_DOUBLE:
		sz_element = SZ_DOUBLE
	    case TBL_TY_INT:
		sz_element = SZ_INT32
	    case TBL_TY_SHORT:
		sz_element = SZ_SHORT
	    case TBL_TY_BOOL:
		sz_element = SZ_BOOL
	    default:
		sz_element = tbeszt (cptr)	# character type
	    }

	    offset = offset + (elnum-1) * sz_element
	}

	return (offset)
end