aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/imutil/src/hselect.x
blob: 5be85627ea2f12354c6901212826aea175c6ba74 (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<error.h>
include	<evexpr.h>
include	<ctype.h>

define	LEN_USERAREA	28800		# allow for the largest possible header


# HSELECT -- Perform a relational select operation upon a set of images.
# Our function is to select all images from the input set matching some
# criteria, printing the listed fields of each selected image on the standard
# output in list form.
#
# N.B.: this task shares code with the HEDIT task.

procedure t_hselect()

pointer	sp, im, image, fields, expr, missing, section
int	imlist, ip, min_lenuserarea
int	imtopenp(), imtgetim(), envfind(), ctoi()
pointer	immap()

begin
	call smark (sp)
	call salloc (image,   SZ_FNAME, TY_CHAR)
	call salloc (fields,  SZ_LINE,  TY_CHAR)
	call salloc (expr,    SZ_LINE,  TY_CHAR)
	call salloc (missing, SZ_LINE,  TY_CHAR)
	call salloc (section, SZ_FNAME, TY_CHAR)

	# Get the primary operands.
	imlist = imtopenp ("images")
	call clgstr ("fields", Memc[fields], SZ_LINE)
	call clgstr ("expr",   Memc[expr],   SZ_LINE)
	call clgstr ("missing", Memc[missing], SZ_LINE)

	# Main processing loop.  An image is processed in each pass through
	# the loop.

	while (imtgetim (imlist, Memc[image], SZ_FNAME) != EOF) {

	    # Check size of user area
	    if (envfind ("min_lenuserarea", Memc[section], SZ_FNAME) > 0) {
		ip = 1
		if (ctoi (Memc[section], ip, min_lenuserarea) <= 0)
		    min_lenuserarea = LEN_USERAREA
		else
		    min_lenuserarea = max (LEN_USERAREA, min_lenuserarea)
	    } else
		min_lenuserarea = LEN_USERAREA

	    # Open the image.
	    iferr (im = immap (Memc[image], READ_ONLY, min_lenuserarea)) {
		call erract (EA_WARN)
		next
	    }

	    call he_getopsetimage (im, Memc[image], Memc[image])
	    call hs_select (im, Memc[image], Memc[fields], Memc[expr],
	        Memc[missing])

	    call imunmap (im)
	    call flush (STDOUT)
	}

	call imtclose (imlist)
	call sfree (sp)
end


# HS_SELECT -- Evaluate the user supplied boolean expression using the
# header parameter values for an image, and print the values of the listed
# parameters on the standard output if the expression is true.

procedure hs_select (im, image, fields, expr, missing)

pointer	im			# image descriptor
char	image[ARB]		# name of image being evaluated
char	fields[ARB]		# fields to be passed if record is selected
char	expr[ARB]		# exression to be evaluated
char	missing[ARB]		# missing output value

int	fieldno
pointer	o, sp, field, value, flist
pointer	evexpr(), imofnlu()
int	locpr(), imgnfn()
extern	he_getop()
errchk	evexpr, imofnlu, imgnfn

begin
	call smark (sp)
	call salloc (field, SZ_FNAME, TY_CHAR)
	call salloc (value, SZ_LINE,  TY_CHAR)

	# Evaluate selection criteria.
	o = evexpr (expr, locpr(he_getop), 0)
	if (O_TYPE(o) != TY_BOOL)
	    call error (1, "expression must be boolean")

	# Print the values of the listed fields if the record was selected.
	if (O_VALB(o)) {
	    flist = imofnlu (im, fields)

	    fieldno = 1
	    while (imgnfn (flist, Memc[field], SZ_FNAME) != EOF) {
		iferr {
		    call he_gval (im, image, Memc[field], Memc[value], SZ_LINE)
		} then {
		    call printf ("\t%s")
		        call pargstr (missing)
		} else {
		    if (fieldno == 1) {
			call printf ("%s")
			    call he_pargstr (Memc[value])
		    } else {
			call printf ("\t%s")
			    call he_pargstr (Memc[value])
		    }
		}
		fieldno = fieldno + 1
	    }
	    call printf ("\n")

	    call imcfnl (flist)
	    call flush (STDOUT)
	}

	call xev_freeop (o)
	call mfree (o, TY_STRUCT)
	call sfree (sp)
end