aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/ptools/ptutils/t_tbcrename.x
blob: 8f54e9d5894f729ad2cb04d000b06d4b02c5f28e (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
include <tbset.h>

# T_TBCRENAME -- Rename a list of columns in an ST table. 

procedure t_tbcrename ()

int	tlist			# the tables list descriptor
int	columns			# the input columns list descriptor
int	names			# the output column names list descriptor

pointer	sp, table, incname, outcname, tp, colptr
int	clpopnu(), clplen(), clgfil(), access(), tbpsta()
pointer	tbtopn()

begin
	# Open the lists of tables and keywords.
	tlist = clpopnu ("table")
	if (clplen (tlist) <= 0)
	    return
	columns = clpopnu ("columns")
	names = clpopnu ("names")
	if (clplen (columns) != clplen (names))
	    call error (0,
	        "The number of new names does not equal the number of columns")

	# Allocate working space.
	call smark (sp)
	call salloc (table, SZ_FNAME, TY_CHAR)
	call salloc (incname, SZ_COLNAME, TY_CHAR)
	call salloc (outcname, SZ_COLNAME, TY_CHAR)

	# Loop over the list of ST tables.
	while (clgfil (tlist, Memc[table], SZ_FNAME) != EOF) {

	    # If the file is not an ST table go to the next file in the list.
	    if (access (Memc[table], 0, TEXT_FILE) == YES)
		next
	    iferr (tp = tbtopn (Memc[table], READ_WRITE, 0))
		next
	    if (tbpsta (tp, TBL_WHTYPE) == TBL_TYPE_TEXT)
		next

	    # Loop over the input column list.
	    while (clgfil (columns, Memc[incname], SZ_COLNAME) != EOF &&
	        clgfil (names, Memc[outcname], SZ_COLNAME) != EOF) {

		# If the output column already exists in the table skip
		# to the next input column.
		call tbcfnd (tp, Memc[outcname], colptr, 1)
		if (colptr != NULL)
		    next

		# If the input column does not exist in the table skip to the
		# next column.

		call tbcfnd (tp, Memc[incname], colptr, 1)
		if (colptr == NULL)
		    next

		# Rename the column.
		call tbcnam (tp, colptr, Memc[outcname])
	    }

	    call tbtclo (tp)
	    call clprew (columns)
	    call clprew (names)
	}

	call clpcls (columns)
	call clpcls (names)
	call clpcls (tlist)
	call sfree (sp)
end