aboutsummaryrefslogtreecommitdiff
path: root/pkg/xtools/catquery/cqgfields.x
blob: 23b94221c09d5898cc995ba35c0b98e8a0afc233 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
include <ctype.h>
include "cqdef.h"
include "cq.h"

# CQ_SETRECORD -- Set the the current record. What action this procedure takes
# depends on the input data type. In the case of text files this task
# sets the current record pointer and figures where in the record each
# column begins. For blocked text files the foffsets determine where each
# record begins.

int procedure cq_setrecord (res, recptr)

pointer	res			#I the results descriptor
int	recptr			#U the current record pointer

pointer	buf

begin
	# The record is outside the record data range.
	if (recptr <= 0) {
	    CQ_RECPTR(res) = 0
	    CQ_FNFIELDS(res) = 0
	    call aclri (Memi[CQ_FINDICES(res)], CQ_MAX_NFIELDS + 1)
	    return (BOF)
	}
	if (recptr > CQ_RNRECS(res))
	    return (EOF)

	CQ_RECPTR(res) = recptr
	switch (CQ_RTYPE(res)) {
	case CQ_STEXT:
	    buf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    call cq_find_fields (Memc[buf], Memi[CQ_FINDICES(res)],
	        CQ_MAX_NFIELDS, CQ_FNFIELDS(res))
	case CQ_BTEXT:
	    ;
	default:
	}

	return (recptr)
end


# CQ_GVALC -- Fetch a record field as a string value.

int procedure cq_gvalc (res, recptr, field, str, maxch)

pointer	res			#I the results descriptor
int	recptr			#I the current record pointer
char	field[ARB]		#I the record field name.
char	str[ARB]		#O the output string parameter
int	maxch			#I the maximum number of characters

pointer	fbuf
int	fnum, fip, fsize
int	cq_fnumber(), cq_setrecord()

begin
	# The record is outside the record data range.
	str[1] = EOS
	if (recptr <= 0 || recptr > CQ_RNRECS(res))
	    return (0)

	# Find the field number.
	fnum = cq_fnumber (res, field)
	if (fnum <= 0)
	    return (0)

	# Set the current record if necessary.
	if (recptr != CQ_RECPTR(res)) {
	    if (cq_setrecord (res, recptr) != recptr)
		return (0)
	}

	# Extract the requested field as a string. If the data is in binary
	# internally this will require formatting a string. If the data is
	# text this requires extracting the appropriate piece of text.

	switch (CQ_RTYPE(res)) {

	case CQ_STEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fnum = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fip = Memi[CQ_FINDICES(res)+fnum-1]
	    fsize = min (maxch, Memi[CQ_FINDICES(res)+fnum] -
	        Memi[CQ_FINDICES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], str, fsize)

	case CQ_BTEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fip = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fsize = min (maxch, Memi[CQ_FSIZES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], str, fsize)

	default:
	    fsize = 0

	}

	return (fsize)
end


# CQ_GVALD -- Return a double precision field value 

int procedure cq_gvald (res, recptr, field, dval)

pointer	res			#I the results descriptor
int	recptr			#I the current record pointer
char	field[ARB]		#I the record field name.
double	dval			#O the output double value

pointer	fbuf, sp, line
int	fnum, fip, fsize, nchars
int	cq_fnumber(), ctod(), cq_setrecord()

begin
	dval = INDEFD

	# The record is outside the record data range.
	if (recptr <= 0 || recptr > CQ_RNRECS(res))
	    return (0)

	# Find the field number.
	fnum = cq_fnumber (res, field)
	if (fnum <= 0)
	    return (0)

	# Set the current record if necessary.
	if (recptr != CQ_RECPTR(res)) {
	    if (cq_setrecord (res, recptr) != recptr)
		return (0)
	}

	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Extract the requested field as a double precision value. If the data
	# is in binary internally this may imply a type conversion. If the data
	# is text this requires decoding the string value.

	switch (CQ_RTYPE(res)) {

	case CQ_STEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fnum = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fip = Memi[CQ_FINDICES(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FINDICES(res)+fnum] -
	        Memi[CQ_FINDICES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctod (Memc[line], fip, dval) 

	case CQ_BTEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fip = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FSIZES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctod (Memc[line], fip, dval)

	default:
	    nchars = 0

	}

	call sfree (sp)

	return (nchars)
end


# CQ_GVALR -- Return a real precision field value.

int procedure cq_gvalr (res, recptr, field, rval)

pointer	res			#I the results descriptor
int	recptr			#I the current record pointer
char	field[ARB]		#I the record field name.
real	rval			#O the output real value

pointer	fbuf, sp, line
int	fnum, fip, fsize, nchars
int	cq_fnumber(), ctor(), cq_setrecord()

begin
	rval = INDEFR

	# The record is outside the record data range.
	if (recptr <= 0 || recptr > CQ_RNRECS(res))
	    return (0)

	# Find the field number.
	fnum = cq_fnumber (res, field)
	if (fnum <= 0)
	    return (0)

	# Set the current record if necessary.
	if (recptr != CQ_RECPTR(res)) {
	    if (cq_setrecord (res, recptr) != recptr)
		return (0)
	}

	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Extract the requested field as a double precision value. If the data
	# is in binary internally this may imply a type conversion. If the data
	# is text this requires decoding the string value.

	switch (CQ_RTYPE(res)) {

	case CQ_STEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fnum = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fip = Memi[CQ_FINDICES(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FINDICES(res)+fnum] -
	        Memi[CQ_FINDICES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctor (Memc[line], fip, rval)

	case CQ_BTEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fip = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FSIZES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctor (Memc[line], fip, rval)

	default:
	    nchars = 0

	}

	call sfree (sp)

	return (nchars)
end


# CQ_GVALL -- Return a long integer field value.

int procedure cq_gvall (res, recptr, field, lval)

pointer	res			#I the results descriptor
int	recptr			#I the current record pointer
char	field[ARB]		#I the record field name.
long	lval			#I the output long value

pointer	fbuf, sp, line
int	fnum, fip, fsize, nchars
int	cq_fnumber(), ctol(), cq_setrecord()

begin
	lval = INDEFL

	# The record is outside the record data range.
	if (recptr <= 0 || recptr > CQ_RNRECS(res))
	    return (0)

	# Find the field number.
	fnum = cq_fnumber (res, field)
	if (fnum <= 0)
	    return (0)

	# Set the current record if necessary.
	if (recptr != CQ_RECPTR(res)) {
	    if (cq_setrecord (res, recptr) != recptr)
		return(0)
	}

	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Extract the requested field as a double precision value. If the data
	# is in binary internally this may imply a type conversion. If the data
	# is text this requires decoding the string value.

	switch (CQ_RTYPE(res)) {

	case CQ_STEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fnum = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fip = Memi[CQ_FINDICES(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FINDICES(res)+fnum] -
	        Memi[CQ_FINDICES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctol (Memc[line], fip, lval)

	case CQ_BTEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fip = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FSIZES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctol (Memc[line], fip, lval) 

	default:
	    nchars = 0

	}

	call sfree (sp)

	return (nchars)
end


# CQ_GVALI -- Return an integer field value 

int procedure cq_gvali (res, recptr, field, ival)

pointer	res			#I the results descriptor
int	recptr			#I the current record pointer
char	field[ARB]		#I the record field name.
int	ival			#I the output int value

pointer	fbuf, sp, line
int	fnum, fip, fsize, nchars
int	cq_fnumber(), ctoi(), cq_setrecord()

begin
	ival = INDEFI

	# The record is outside the record data range.
	if (recptr <= 0 || recptr > CQ_RNRECS(res))
	    return (0)

	# Find the field number.
	fnum = cq_fnumber (res, field)
	if (fnum <= 0)
	    return (0)

	# Set the current record if necessary.
	if (recptr != CQ_RECPTR(res)) {
	    if (cq_setrecord (res, recptr) != recptr)
		return (0)
	}

	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Extract the requested field as a double precision value. If the data
	# is in binary internally this may imply a type conversion. If the data
	# is text this requires decoding the string value.

	switch (CQ_RTYPE(res)) {

	case CQ_STEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fnum = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fip = Memi[CQ_FINDICES(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FINDICES(res)+fnum] -
	        Memi[CQ_FINDICES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctoi (Memc[line], fip, ival)

	case CQ_BTEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fip = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FSIZES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctoi (Memc[line], fip, ival) 

	default:
	    nchars = 0
	}

	call sfree (sp)

	return (nchars)
end


# CQ_GVALS -- Return a short integer field value 

int procedure cq_gvals (res, recptr, field, sval)

pointer	res			#I the results descriptor
int	recptr			#I the current record pointer
char	field[ARB]		#I the record field name.
short	sval			#O the output short value

pointer	fbuf, sp, line
int	fnum, fip, fsize, nchars, ival
int	cq_fnumber(), ctoi(), cq_setrecord()

begin
	sval = INDEFS

	# The record is outside the record data range.
	if (recptr <= 0 || recptr > CQ_RNRECS(res))
	    return (0)

	# Find the field number.
	fnum = cq_fnumber (res, field)
	if (fnum <= 0)
	    return (0)

	# Set the current record if necessary.
	if (recptr != CQ_RECPTR(res)) {
	    if (cq_setrecord (res, recptr) != recptr)
		return (0)
	}

	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Extract the requested field as a double precision value. If the data
	# is in binary internally this may imply a type conversion. If the data
	# is text this requires decoding the string value.

	switch (CQ_RTYPE(res)) {

	case CQ_STEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fnum = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fip = Memi[CQ_FINDICES(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FINDICES(res)+fnum] -
	        Memi[CQ_FINDICES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctoi (Memc[line], fip, ival)
	    if (nchars > 0)
	        sval = ival

	case CQ_BTEXT:
	    fbuf = CQ_RBUF(res) + Memi[CQ_RINDEX(res)+recptr-1] - 1
	    fip = Memi[CQ_FOFFSETS(res)+fnum-1]
	    fsize = min (SZ_LINE, Memi[CQ_FSIZES(res)+fnum-1])
	    call strcpy (Memc[fbuf+fip-1], Memc[line], fsize)
	    fip = 1
	    nchars = ctoi (Memc[line], fip, ival)
	    if (nchars > 0)
		sval = ival

	default:
	    nchars = 0

	}

	call sfree (sp)

	return (nchars)
end


# CQ_FIND_FIELDS -- This procedure finds the starting column for each field
# in the input line.  These column numbers are returned in the array
# field_pos; the number of fields is also returned.

procedure cq_find_fields (linebuf, field_pos, max_fields, nfields)

char    linebuf[ARB]                    #I the input buffer
int     field_pos[max_fields]           #O the output field positions
int     max_fields                      #I the maximum number of fields
int     nfields                         #O the computed number of fields

bool    in_field
int     ip, field_num

begin
        field_num = 1
        field_pos[1] = 1
        in_field = false

        for (ip=1; linebuf[ip] != '\n' && linebuf[ip] != EOS; ip=ip+1) {
            if (! IS_WHITE(linebuf[ip]))
                in_field = true
            else if (in_field) {
                in_field = false
                field_num = field_num + 1
                field_pos[field_num] = ip
            }
        }

        field_pos[field_num+1] = ip
        nfields = field_num
end