aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/stxtools/tpcount.x
blob: f233aa26a5f249a49f2ada5e39aee58f601bae04 (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
include "template.h"

# TP_COUNT -- Count the number of groups in an image
#
# B.Simon	02-Oct-98	Original
# B.Simon	26-Apr-99	Check value of NEXTEND before using

int procedure tp_count (root)

char	root[ARB]	# i: image name minus any sections
#--
int	imtype, count, lo, hi, mid
pointer	sp, image, im

int	imgeti(), imaccf(), tp_imtype(), tp_hasgroup()
pointer	immap()

begin
	# If the image is not a geis or fits file it can only have one group

	imtype = tp_imtype (root) 
	if (imtype != TP_GEIS && imtype != TP_FITS)
	    return (1)

	# Open the image to read the number of groups or extensions
	# as recorded in the appropriate header keyword

	call smark (sp)
	call salloc (image, SZ_FNAME, TY_CHAR)

	call strcpy (root, Memc[image], SZ_FNAME)
	call strcat ("[0]", Memc[image], SZ_FNAME)

	iferr (im = immap (Memc[image], READ_ONLY, NULL)) {
	    # If image can't be opened, report an error condition

	    count = ERR

	} else if (imtype == TP_GEIS) {
	    # Number of groups is trustworthy, report it to user

	    count = imgeti (im, "GCOUNT")
	    call imunmap (im)

	} else {
	    # Number of extensions is not, it must be checked

	    lo = 1
	    hi = 0

	    # Check number of extensions

	    if (imaccf (im, "NEXTEND") == YES) {
		mid = imgeti (im, "NEXTEND")

		if (mid > 0) {
		    if (tp_hasgroup (root, mid) == NO) {
			hi = mid

		    } else {
			lo = mid
			mid = mid + 1

			if (tp_hasgroup (root, mid) == NO) {
			    hi = mid
			} else {
			    lo = mid
			}
		    }
		}
	    }

	    # Find bracket for number of extensions

	    while (hi < lo) {
		mid = 2 * lo

		if (tp_hasgroup (root, mid) == NO) {
		    hi = mid
		} else {
		    lo = mid
		}
	    }

	    # Use binary search to find actual number of extensions

	    while (hi - lo > 1) {
		mid = (hi + lo) / 2

		if (tp_hasgroup (root, mid) == NO) {
		    hi = mid
		} else {
		    lo = mid
		}
	    }

	    count = lo
	    call imunmap (im)
	}

	call sfree (sp)
	return (count)
end

# TP_HASGROUP -- Determine if group is preent in image

int procedure tp_hasgroup (root, index)

char	root[ARB]	# i: image name
int	index		# i: index of group to check
#--
int	has
pointer	sp, image, im

pointer	immap()

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

	call sprintf (Memc[image], SZ_FNAME, "%s[%d]")
	call pargstr (root)
	call pargi (index)

	iferr (im = immap (Memc[image], READ_ONLY, NULL)) {
	    has = NO
	} else {
	    call imunmap (im)
	    has = YES
	}

	call sfree (sp)
	return (has)
end