aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/libvoclient/vocRegistry_f77.c
blob: 1714051c6013a27ddc159a5f27c7f63a61df919e (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
/**
 *  VOC_REGISTRYQUERY -- Utility code to act as a client interface to
 *  the NVO Registry service.
 *
 *  RegistryQuery
 *  ----------------------
 * 
 *  High-Level Query:
 * 
 *           res = voc_regSearch (term1, term2, orValues)
 *      res = voc_regSearchBySvc (svc, term, orValues)
 * 
 *  Programmatic Query:
 * 
 *          query = voc_regQuery (term, orValues) 	// OR keyword list?
 * 
 *          voc_regAddSearchTerm (query, term, orValue)	// OR term w/ previous
 *       voc_regRemoveSearchTerm (query, term)		// remove search term
 *     count = voc_regGetSTCount (query)
 * 
 *   str = voc_regGetQueryString (query)		// GET form of query
 * 
 *          res = voc_regExecute (query)		// return result obj
 *       str = voc_regExecuteRaw (query)		// return raw XML
 * 
 *  RegistryQueryResult
 * 
 *     count = voc_resGetCount  (res)
 * 
 *         str = voc_resGetStr  (res, attribute, index)
 *      dval = voc_resGetFloat  (res, attribute, index)
 *        ival = voc_resGetInt  (res, attribute, index)
 * 
 *     For this implementation, we've chose to use the NVO Registry at
 *  JHU/STScI, specifically the QueryRegistry() method which provides a
 *  'SimpleResource' form of the resource record.  Support for the newer
 *  IVOA standard will be added later, for now we can quickly access the most
 *  commonly used fields of a resource using both a keyword and SQL form of
 *  the search.
 * 
 *
 *  @file       vocRegistry_f77.c
 *  @author     Michael Fitzpatrick
 *  @version    July 2006
 *
 *************************************************************************
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

#define _VOCLIENT_LIB_
#include "VOClient.h"


/*  Procedure name mapping.
 */
#ifdef _NO_US_

#define VF_REGSEARCH 		vfregsearch
#define VF_REGSEARCHBYSVC	vfregsearchbysvc
#define VF_REGQUERY 		vfregquery
#define VF_REGADDSEARCHTERM 	vfregaddsearchterm
#define VF_REGREMOVESEARCHTERM 	vfregremovesearchterm
#define VF_REGGETSTCOUNT 	vfreggetstcount
#define VF_REGGETQUERYSTRING 	vfreggetquerystring
#define VF_REGEXECUTE 		vfregexecute
#define VF_REGEXECUTERAW 	vfregexecuteraw
#define VF_RESGETCOUNT 		vfresgetcount
#define VF_RESGETSTR 		vfresgetstr
#define VF_RESGETFLOAT 		vfresgetfloat
#define VF_RESGETINT 		vfresgetint

#else

#define VF_REGSEARCH 		vfregsearch_
#define VF_REGSEARCHBYSVC	vfregsearchbysvc_
#define VF_REGQUERY 		vfregquery_
#define VF_REGADDSEARCHTERM 	vfregaddsearchterm_
#define VF_REGREMOVESEARCHTERM 	vfregremovesearchterm_
#define VF_REGGETSTCOUNT 	vfreggetstcount_
#define VF_REGGETQUERYSTRING 	vfreggetquerystring_
#define VF_REGEXECUTE 		vfregexecute_
#define VF_REGEXECUTERAW 	vfregexecuteraw_
#define VF_RESGETCOUNT 		vfresgetcount_
#define VF_RESGETSTR 		vfresgetstr_
#define VF_RESGETFLOAT 		vfresgetfloat_
#define VF_RESGETINT 		vfresgetint_

#endif


/*  Prototype declarations.
 */
void VF_REGSEARCH (char *term1,  char *term2, int *orValues, RegResult *result,
			int *ier, int len1, int len2);
void VF_REGSEARCHBYSVC (char *svc,  char *term, int *orValues, 
			RegResult *result, int *ier, int slen, int tlen);
void VF_REGQUERY (char *term, int *orValues, RegQuery *query, int *ier, 
			int len);
void VF_REGADDSEARCHTERM (RegQuery *query, char *term, int *orValue, int tlen);
void VF_REGREMOVESEARCHTERM (RegQuery *query, char *term, int tlen);
void VF_REGGETSTCOUNT (RegQuery *query, int *count);
void VF_REGGETQUERYSTRING (RegQuery *query, char *qstr, int *len, int qlen);
void VF_REGEXECUTE (RegQuery *query, RegResult *result);
void VF_REGEXECUTERAW (RegQuery *query, char *raw, int *len, int rlen);
void VF_RESGETCOUNT (RegResult *res, int *count);
void VF_RESGETSTR (RegResult *res, char *attr, int *index, char *str, int *len,
			int alen, int slen);
void VF_RESGETFLOAT (RegResult *res, char *attr, int *index, double *dval,
			int alen);
void VF_RESGETINT (RegResult *res, char *attr, int *index, int *ival, int alen);


extern VOClient *vo;                    /* Interface runtime struct     */


/*  Private interface declarations.
 */
extern char *sstrip (char *instr, int len);
extern void  spad (char *outstr, int len);




/**
 *  VF_REGSEARCH --  High-level procedure to form a query and execute it
 *  immediately.  We allow that 'term1' may be a complex SQL WHERE predicate,
 *  and that 'term2' (or vice versa) is a search-keyword list.  The
 *  'orValues' applies to the keyword list (if present), otherwise it applies
 *  to the two search term elements.  The default action if two terms are
 *  specified is to logically AND them.
 *
 *  The thinking here is that one might want SIAP services for Quasars.  This
 *  is easily expressed in an SQL form to get SIAP resources, however a
 *  Quasar may be known as a QSO, AGN, active-nuclei, etc and so we need a 
 *  easy way to OR the keywords but AND that result with the SQL predicate.
 *
 *  @brief  High-level Registry query interface
 *  @fn     call vf_regSearch (char *term1, char *term2, int *orValues,
 *			int *result, int *ier)
 *
 *  @param  term1       first search term
 *  @param  term2       second search term
 *  @param  orValues    logically OR values?
 *  @param  result      handle to result object
 *  @param  ier         function error code (OK or ERR)
 *  @returns            nothing
 */
void
VF_REGSEARCH (char *term1,  char *term2, int *orValues, RegResult *result, 
	int *ier, int len1, int len2)
{
    char *_term1 = sstrip (term1, len1);
    char *_term2 = sstrip (term2, len2);

    *result = voc_regSearch (_term1, _term2, *orValues);
    *ier = (*result ? OK : ERR);

    free ((char *) _term1);
    free ((char *) _term2);
}


/**
 *  VF_REGSEARCHBYSERVICE -- Search the Registry using a search term and
 *  constrain by service type.
 *
 *  @brief  Search Registry using a search term and service constraint
 *  @fn     call vf_regSearchByService (char *svc, char *term, int *orValues,
 *			int *result, int *ier)
 *
 *  @param  svc         service type constraint
 *  @param  term        keyword search term
 *  @param  orValues    logically OR values?
 *  @param  result      handle to result object
 *  @param  ier         function error code (OK or ERR)
 *  @returns            nothing
 */

void
VF_REGSEARCHBYSVC (char *svc,  char *term, int *orValues, RegResult *result, 
	int *ier, int slen, int tlen)
{
    char *_svc = sstrip (svc, slen);
    char *_term = sstrip (term, tlen);

    *result = voc_regSearchByService (_svc, _term, *orValues);
    *ier = (*result ? OK : ERR);

    free ((char *) _svc);
    free ((char *) _term);
}


/**
 * VF_REGQUERY --  Create a RegistryQuery object.
 *
 *  @brief  Create a RegistryQuery object.
 *  @fn     call vf_regQuery (char *term, int *orValues, int *query, int *ier)
 *
 *  @param  term        keyword search term
 *  @param  orValues    logically OR values?
 *  @param  query       handle to query object
 *  @param  ier         function error code (OK or ERR)
 *  @returns            nothing
 */
void  
VF_REGQUERY (char *term, int *orValues, RegQuery *query, int *ier, int len)
{
    char *_term = sstrip (term, len);

    *query = voc_regQuery (_term, *orValues);
    *ier = (*query ? OK : ERR);

    free ((char *) _term);
}


/**
 *  VF_REGADDSEARCHTERM -- Add a search term (sql predicate or keyword list)
 *  to the specified query.
 *
 *  @brief  Add a search term to the specified query
 *  @fn     call vf_regAddSearchTerm (RegQuery *query, char *term, int *orValue)
 *
 *  @param  query       Registry query handle
 *  @param  term        keyword search term
 *  @param  orValues    logically OR values?
 *  @returns            nothing
 */
void
VF_REGADDSEARCHTERM (RegQuery *query, char *term, int *orValue, int len)
{
    char *_term = sstrip (term, len);

    voc_regAddSearchTerm (*query, _term, *orValue);

    free ((char *) _term);
}


/**
 *  VF_REGREMOVESEARCHTERM -- Remove the search term from the query.
 *
 *  @brief  Remove a search term to the specified query
 *  @fn     call vf_regRemoveSearchTerm (RegQuery *query, char *term)
 *
 *  @param  query       Registry query handle
 *  @param  term        keyword search term
 *  @returns            nothing
 */
void
VF_REGREMOVESEARCHTERM (RegQuery *query, char *term, int len)
{
    char *_term = sstrip (term, len);

    voc_regRemoveSearchTerm (*query, _term);

    free ((char *) _term);
}


/**
 *  VF_REGGETSTCOUNT -- Get the number of search terms in the current query.
 *
 *  @brief  Get the number of search terms in the current query.
 *  @fn     call vf_regGetSTCount (RegQuery *query, int *count)
 *
 *  @param  query       Registry query handle
 *  @returns            nothing
 */
void
VF_REGGETSTCOUNT (RegQuery *query, int *count)
{
    *count = voc_resGetCount (*query);
}


/**
 *  VF_REGGETQUERYSTRING -- Get the current query as an http GET URL.
 *
 *  @brief  Get the current query as an http GET URL.
 *  @fn     call vf_regGetQueryString (RegQuery *query, char *qstr, int *len)
 *
 *  @param  query       Registry query handle
 *  @param  qstr        returned query string
 *  @param  len         length of query string
 *  @returns            nothing
 */
void
VF_REGGETQUERYSTRING (RegQuery *query, char *qstr, int *len, int qlen)
{
    char *_result = voc_regGetQueryString (*query);

    memset (qstr, 0, qlen);
    if ((*len = strlen(_result)) > qlen)
        fprintf (stderr, "Warning: truncating result string: len=%d maxch=%d\n",
            *len, qlen);
    spad (strncpy (qstr, _result, *len), qlen);

    free ((char *) _result);
}


/**
 *  VF_REGEXECUTE -- Execute the specified query, returning a result object
 *  code or NULL.
 *
 *  @brief  Execute the specified query
 *  @fn     call vf_regExecute (RegQuery *query, RegResult *result)
 *
 *  @param  query       Registry query handle
 *  @param  result      Registry result handle
 *  @returns            nothing
 */
void
VF_REGEXECUTE (RegQuery *query, RegResult *result)
{
    *result = voc_regExecute (*query);
}


/**
 *  VF_REGEXECUTERAW -- Execute the specified query and return the raw
 *  resulting XML string.
 *
 *  @brief  Execute the specified query and return raw result string
 *  @fn     call vf_regExecuteRaw (RegQuery *query, char *raw, int *len)
 *
 *  @param  query       Registry query handle
 *  @param  raw         raw result string
 *  @param  len         length of result string
 *  @returns            nothing
 */
void
VF_REGEXECUTERAW (RegQuery *query, char *raw, int *len, int rlen)
{
    char *_result = voc_regExecuteRaw (*query);

    memset (raw, 0, rlen);
    if ((*len = strlen(_result)) > rlen)
        fprintf (stderr, "Warning: truncating result string: len=%d maxch=%d\n",
            *len, rlen);
    spad (strncpy (raw, _result, *len), rlen);

    free ((char *) _result);
}


/****************************************************************************/
/*********************  RegistryQueryResult Methods  ************************/
/****************************************************************************/


/**
 *  VF_RESGETCOUNT -- Return a count of the number of results records.
 *
 *  @brief  Return a count of the number of results records.
 *  @fn     call vf_resGetCount (RegResult *res, int *count)
 *
 *  @param  res         Registry result handle
 *  @param  count       result count
 *  @returns            nothing
 */
void
VF_RESGETCOUNT (RegResult *res, int *count)
{
    *count = voc_resGetCount (*res);
}


/**
 *  VF_GETSTR -- Get a string-valued attribute from the result resource
 *  record.  Currently recognized real-valued attributes include:
 *
 *	Title			Resource title (long version)
 *	ShortName		Short name of Resource
 *	ServiceURL		Service URL (if appropriate)
 *	ReferenceURL		URL to reference about Resource
 *	Description		Text description of resource
 *	Identifier		Standard ivo identifier of resource
 *	ServiceType		Service Type (Cone, Siap, etc)
 *	Type			Resource Type (catalog, survey, etc)
 *	CoverageSpatial		Spatial coverage (STC)
 *	CoverageTemporal	Temporal coverage of data
 *
 *	CoverageSpectral	Spectral coverage (csv list of bandpasses)
 *	ContentLevel		Content level (research, EPO, etc -- csv list)
 *
 *  Attribute string are case-insensitive.
 *
 *  @brief  Get a string-valued attribute from the result resource record
 *  @fn     call vf_resGetStr (RegResult *res, char *attr, int *index, 
 *			char *str, int *len)
 *
 *  @param  res         Registry result handle
 *  @param  attr        record attribute
 *  @param  index       record index
 *  @param  str         attribute string
 *  @param  len         length of attribute string
 *  @returns            nothing
 */
void
VF_RESGETSTR (RegResult *res, char *attr, int *index, char *str, int *len,
	int alen, int slen)
{
    char *_attr = sstrip (attr, alen);

    char *_result = voc_resGetStr (*res, _attr, *index-1);

    memset (str, 0, slen);
    if ((*len = strlen(_result)) > slen)
        fprintf (stderr, "Warning: truncating result string: len=%d maxch=%d\n",
            *len, slen);
    spad (strncpy (str, _result, *len), slen);

    free ((char *) _result);
    free ((char *) _attr);
}


/**
 *  VF_GETFLOAT -- Get a real-valued attribute from the result resource
 *  record.  Currently recognized real-valued attributes include:
 *
 *	MaxSR			maximum search radius
 *
 *  Attribute string are case-insensitive.
 *
 *  @brief  Get a real-valued attribute from the result resource record
 *  @fn     call vf_resGetFloat (RegResult *res, char *attr, int *index,
 *		double *dval)
 *
 *  @param  res         Registry result handle
 *  @param  attr        record attribute
 *  @param  index       record index
 *  @param  dval        double-precision value
 *  @returns            nothing
 */
void
VF_RESGETFLOAT (RegResult *res, char *attr, int *index, double *dval, int alen)
{
    char *_attr = sstrip (attr, alen);

    *dval = voc_resGetFloat (*res, _attr, *index-1);

    free ((char *) _attr);
}


/**
 *  VF_GETINT -- Get a integer-valued attribute from the result resource
 *  record.  Currently recognized real-valued attributes include:
 *
 *	MaxRecords		maximum records returned by the service
 *
 *  Attribute string are case-insensitive.
 *
 *  @brief  Get an int-valued attribute from the result resource record
 *  @fn     call vf_resGetInt (RegResult *res, char *attr, int *index, 
 *				int *ival)
 *
 *  @param  res         Registry result handle
 *  @param  attr        record attribute
 *  @param  index       record index
 *  @param  ival        integer value
 *  @returns            nothing
 */
void
VF_RESGETINT (RegResult *res, char *attr, int *index, int *ival, int alen)
{
    char *_attr = sstrip (attr, alen);

    *ival = voc_resGetInt (*res, _attr, *index-1);

    free ((char *) _attr);
}