aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/tbhgnp.x
blob: 58f3f921e4e22081c144911929aa8d936be71452 (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
include <ctype.h>		# for IS_WHITE
include <tbset.h>
include "tbtables.h"

# tbhgnp -- get Nth parameter
# Get the keyword and value string of header parameter number parnum.
# The string str should be SZ_PARREC in length, although not that much
# will be used since only the value will be copied to str.
# A keyword may not contain embedded blanks.
# If the parameter has an associated comment string, that string will
# not be returned; use tbhgcm to get the comment.
# Trailing blanks will be trimmed from the parameter value.
#
# Phil Hodge,  9-Mar-1989  Change dtype from char to int.
# Phil Hodge,  9-Mar-1995  Ignore comment; trim trailing blanks from value.
# Phil Hodge, 12-May-1995  Check for both ' and " as string delimiter.
# Phil Hodge,  8-Jun-1995  Modify for FITS tables.
# Phil Hodge, 27-Nov-1995  Add cmt to calling sequence of tbfgnp.

procedure tbhgnp (tp, parnum, keyword, dtype, str)

pointer tp			# i: pointer to table descriptor
int	parnum			# i: number of the parameter to be gotten
char	keyword[SZ_KEYWORD]	# o: keyword for the parameter
int	dtype			# o: data type (TY_CHAR, etc)
char	str[SZ_PARREC]		# o: string containing the value of the param.
#--
pointer sp
pointer par			# buffer for parameter record
pointer cmt			# scratch for comment from FITS record
int	k			# loop index for copying keyword
int	ip			# loop indices for copying value
int	char_type		# data type as a letter (t, b, i, r, d)
int	index			# location of comment in string
int	nchar, ctowrd()
int	strlen()
errchk	tbhrpr

begin
	if (TB_TYPE(tp) == TBL_TYPE_FITS) {
	    call smark (sp)
	    call salloc (cmt, SZ_PARREC, TY_CHAR)	# thrown away
	    call tbfgnp (tp, parnum, keyword, dtype, str, Memc[cmt], SZ_PARREC)
	    call sfree (sp)
	    return
	}

	if (parnum < 1 || parnum > TB_NPAR(tp)) {
	    keyword[1] = EOS
	    dtype = 0
	    str[1] = EOS
	    return
	}

	call smark (sp)
	call salloc (par, SZ_PARREC, TY_CHAR)

	call tbhrpr (tp, parnum, Memc[par])	# read parameter record

	# Copy the keyword to output and append EOS.
	do k = 1, SZ_KEYWORD {
	    if (Memc[par+k-1] == ' ') {		# stop at first blank
		keyword[k] = EOS
		break
	    }
	    keyword[k] = Memc[par+k-1]
	}
	keyword[SZ_KEYWORD+1] = EOS

	char_type = Memc[par+LOCN_DTYPE-1]	# data type
	switch (char_type) {
	case 'r':
	    dtype = TY_REAL
	case 'i':
	    dtype = TY_INT
	case 'd':
	    dtype = TY_DOUBLE
	case 'b':
	    dtype = TY_BOOL
	default:
	    dtype = TY_CHAR
	}

	# Find the comment, if any.
	call tbhfcm (Memc[par], index)

	# If there is a comment, chop it off.
	if (index > 0) {
	    # Backspace over whitespace.
	    while (index > START_OF_VALUE) {
		if (IS_WHITE(Memc[par+index-2]))	# element is [index-1]
		    index = index - 1
		else
		    break
	    }
	    Memc[par+index-1] = EOS
	}

	# Copy the portion of the record containing the value to output.
	ip = START_OF_VALUE
	if (Memc[par+ip-1] == '"' || Memc[par+ip-1] == '\'')
	    nchar = ctowrd (Memc[par], ip, str, SZ_PARREC)
	else
	    call strcpy (Memc[par+ip-1], str, SZ_PARREC)

	# Trim trailing blanks.
	do ip = strlen (str), 1, -1 {
	    if (str[ip] == ' ')
		str[ip] = EOS
	    else
		break
	}

	call sfree (sp)
end