aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/voapps/votinfo.c
blob: 2a89e703d3e50d3ac4b35216478bf390c6e0c8f1 (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
/*
 *  VOTINFO -- Print information about the structure of a VOTable.
 *
 *    Usage:
 *		votinfo [-v] [-w] <votable>
 *
 *  @file       votinfo.c
 *  @author     Mike Fitzpatrick
 *  @date       6/03/11
 *
 *  @brief      Print information about the structure of a VOTable.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "voApps.h"
#include "votParse.h"

#define N_ITEMS	    	"|param|info|rows|cols|resources|"

#define	N_PARAMS	0		/* numberOf values		*/
#define	N_INFO		1
#define	N_ROWS		2
#define	N_COLS		3
#define	N_RESOURCES	4

#define	SZ_RESBUF	8192


static int  vot		= 0;		/* VOTable handle		*/
static int  size	= 0;		/* print size only		*/
static int  warn	= 0;		/* warning options		*/
static int  numberOf	= 0;		/* element values		*/
static int  verbose	= 0;		/* verbose output		*/
static int  getCols	= 0;		/* get column names		*/
static int  getDesc	= 0;		/* get <DESCRIPTION>		*/
static int  getInfo	= 0;		/* get <INFO>			*/
static int  getParam	= 0;		/* get <PARAM>			*/
static int  getQuery	= 0;		/* get <INFO> QUERY_STATUS val  */
static int  do_return   = 0;		/* return result?		*/

static char resbuf[SZ_RESBUF];		/* result buffer		*/


/*  Task specific option declarations.
 */
int  votinfo (int argc, char **argv, size_t *len, void **result);

static Task  self       = {  "votinfo",  votinfo,  0,  0,  0  };

static char  *opts = "%:bc::dhin:pqrsvw";
static struct option long_opts[] = {
        { "test",         1, 0,   '%'},
        { "help",         2, 0,   'h'},
        { "brief",        2, 0,   'b'},
        { "columns",      2, 0,   'c'},
        { "description",  2, 0,   'd'},
        { "info",         2, 0,   'i'},
        { "numberOf",     1, 0,   'n'},
        { "param",        2, 0,   'p'},
        { "query_status", 2, 0,   'q'},
        { "return",       2, 0,   'r'},
        { "size",         2, 0,   's'},
        { "verbose",      2, 0,   'v'},
        { "warn",         2, 0,   'w'},
        { NULL,           0, 0,    0 }
};


static void Usage (void);
static void Tests (char *input);

extern void ppMultiLine (char *result, int poffset, int pwidth, int maxchars);
extern int strdic (char *in_str, char *out_str, int maxchars, char *dict);



/**
 *  Application entry point.
 */
int
votinfo (int argc, char **argv, size_t *reslen, void **result)
{
    char  *iname, *id, *name, *ucd, *desc, *value, *numpar = NULL, *clist=NULL;
    char **pargv, optval[SZ_FNAME], param[SZ_FNAME];
    int   res, tab, data, tdata, field, handle, status = OK;
    int   i, nlen, ncols, nrows, pos, ch;


    /*  Initialize. 
     */
    size     = 0;
    warn     = 0;
    verbose  = 0;
    iname    = NULL;
    memset (param, 0, SZ_FNAME);
    memset (optval, 0, SZ_FNAME);
    memset (resbuf, 0, SZ_RESBUF);

    *reslen = 0;
    *result = NULL;

    
    /*  Parse the argument list.
     */
    pargv = vo_paramInit (argc, argv, opts, long_opts);
    while ((ch = vo_paramNext (opts,long_opts,argc,pargv,optval,&pos)) != 0)  {
        if (ch > 0) {
	    switch (ch) {
	    case '%':  Tests (optval);			return (self.nfail);
	    case 'h':  Usage ();			return (OK);

	    case 'b':  verbose=0;		    	     break;
	    case 'c':  
		if (optval[0]) {
		    if (optval[0] == 'i') {	    	/* ID */
			clist = "id";
		    } else if (optval[0] == 'n') {	/* NAME */
			clist = "name";
		    } else if (optval[0] == 'u') {	/* UCD */
			clist = "ucd";
		    }
		}
		getCols=1;	    	    	     break;
	    case 'd':  getDesc=1;	    	    	     break;
	    case 'i':  getInfo=1;	    	    	     break;
	    case 'n':  numberOf++, numpar = strdup (optval); break;
	    case 'p':  getParam=1;	    	    	     break;
	    case 'q':  getQuery=1;	    	    	     break;
	    case 'r':  do_return=1;	    	    	     break;
	    case 's':  size++;      			     break;
	    case 'v':  verbose++;		    	     break;
	    case 'w':  warn++;				     break;
	    default:
		fprintf (stderr, "Invalid option '%s'\n", optval);
		return (1);
	    }

        } else if (ch == PARG_ERR) {
            return (ERR);

	} else {
	    iname = strdup (optval);
	    break;
	}
    }


    /* Sanity checks
     */
    if (iname == NULL) 
	iname = strdup ("stdin");
    vot_setWarnings (warn);

    /* Open the table.  This also parses it.
    */
    if ( (vot = vot_openVOTABLE (iname) ) <= 0) {
	fprintf (stderr, "Error opening VOTable '%s'\n", iname);
	return (1);
    }

    res   = vot_getRESOURCE (vot);	/* get handles		*/
    tab   = vot_getTABLE (res);
    if ((data  = vot_getDATA (tab))) {
        tdata = vot_getTABLEDATA (data);
        nrows = vot_getNRows (tdata);
        ncols = vot_getNCols (tdata);
    } else
        tdata = nrows = ncols = 0;

    if (numberOf) {
	switch (strdic (numpar, param, SZ_FNAME, N_ITEMS)) {
	case N_PARAMS:
    	    if ((handle = vot_getPARAM (res)) > 0) {
		if (do_return)
	            vo_setResultFromInt (vot_getLength(handle), reslen, result);
		else
		    printf ("%d\n", vot_getLength(handle));
	    } else
		printf ("0\n");
	    break;
	case N_INFO:
    	    if ((handle = vot_getINFO (res)) > 0) {
		if (do_return)
	            vo_setResultFromInt (vot_getLength(handle), reslen, result);
		else
		    printf ("%d\n", vot_getLength(handle));
	    } else
                printf ("0\n");
	    break;
	case N_ROWS:
	    if (do_return)
	        vo_setResultFromInt (nrows, reslen, result);
	    else
		printf ("%d\n", nrows);
	    break;
	case N_COLS:
	    if (do_return)
	        vo_setResultFromInt (ncols, reslen, result);
	    else
		printf ("%d\n", ncols);
	    break;
	case N_RESOURCES:
	    if (do_return)
	        vo_setResultFromInt (vot_getLength (res), reslen, result);
	    else
		printf ("%d\n", vot_getLength (res));
	    break;
	default:
	    fprintf (stderr, "Unknown number request '%s'\n", param);
	    status = ERR;
	}


    } else if (size) {
	if (do_return) {
	    sprintf (resbuf, "%d %d", nrows, ncols);
	    vo_setResultFromString (resbuf, reslen, result);
	} else
	    printf ("%d %d\n", nrows, ncols);


    } else if (getCols) {

	printf ("%3s  %20.20s\t%20.20s\t%20.20s\n", "Col",
	    "ID", "Name", "UCD");
	printf ("%3s  %20.20s\t%20.20s\t%20.20s\n", "===", "==", "====", "===");

        for (i=1, field = vot_getFIELD(tab); field; field=vot_getNext (field)) {
	    id   = vot_getAttr (field, "id");
	    name = vot_getAttr (field, "name");
	    ucd  = vot_getAttr (field, "ucd");

	    printf ("%3d  %20.20s\t%20.20s\t%20.20s\n", i,
		(id ? id : "(none)"), (name ? name : "(none)"),
		(ucd ? ucd : "(none)"));
	    if (verbose) {
    	        if ( (desc = vot_getValue (vot_getDESCRIPTION (field))) )
		        printf ("\t  Desc:  %-s\n", desc);
	    }
	    i++;
        }


    } else if (getDesc) {
	char *desc = NULL;

        if ((handle = vot_getDESCRIPTION (vot)))
	    desc = vot_getValue (handle);
	else if ((handle = vot_getDESCRIPTION (res)))
	    desc = vot_getValue (handle);
	else 
	    desc = "none";
	if (do_return) 
	    vo_setResultFromString ((desc ? desc : "none"), reslen, result);
	else
	    printf ("%s\n", (desc ? desc : "none"));


    } else if (getParam || getInfo) {
        handle = (getParam ? vot_getPARAM (res) : vot_getINFO (res));
        if (handle > 0) {
	    char  buf[SZ_FNAME];

            nlen = vot_getLength (handle);
	    for (nlen-- ; handle; handle = vot_getNext (handle), nlen--) {
	        name  = vot_getAttr (handle, "name");
	        value = vot_getAttr (handle, "value");
		memset (buf, 0, SZ_FNAME);
    	        sprintf (buf, "%s = %s\n", name, value);
		strcat (resbuf, buf);
	    }

	    if (do_return)
	        vo_setResultFromString (resbuf, reslen, result);
	    else
	        printf ("%s", resbuf);
	}


    } else if (getQuery) {
	int  found = 0, resval = 0;

        if ((handle = vot_getINFO (res)) > 0) {
            nlen = vot_getLength (handle);
	    for (nlen-- ; handle; handle = vot_getNext (handle), nlen--) {
	        name = vot_getAttr (handle, "name");
		if (name && strncasecmp (name, "QUERY_STATUS", 12) == 0) {
	            value = vot_getAttr (handle, "value");
		    found++;
		    break;
		}
	    }
	}

	resval = (found ? (strncasecmp (value, "OK", 2) != 0) : -1);
	if (do_return)
	    vo_setResultFromInt (resval, reslen, result);
	else
	    printf ("%d\n", resval);


    } else {
        /* Print a table summary.
        */
        printf ("%s\n\n", (iname[0] == '-' ? "stdin" : iname));
        printf ("    Resources:  %d\tType: %-12s\t"
	    "    Table Size:  %d x %d\n", 
	    vot_getLength (res), vot_getDATATypeString (data), ncols, nrows);

        if ((handle = vot_getINFO (res)) > 0)
            printf ("         INFO:  %d\n", vot_getLength (handle));

        if ((handle = vot_getPARAM (res)) > 0)
            printf ("        PARAM:  %d\t", 
		(nlen=vot_getLength (handle)));
	for (nlen-- ; handle; handle=vot_getNext(handle),nlen--) {
	    if ((name  = vot_getAttr (handle, "id")) == NULL)
	        if ((name  = vot_getAttr (handle, "name")) == NULL)
		    name = "<noname>";
	    value = vot_getAttr (handle, "value");
    	    printf ("%s = %s  ", name, value);
	    if (nlen)
    	        printf ("\n\t\t\t");
        }
        printf ("\n");

#ifdef OLD_VOTABLE
        if ((handle = vot_getCOOSYS (res)))
            printf ("       COOSYS:  %d\n", vot_getLength (handle));
#endif

        if ((handle = vot_getDESCRIPTION (res))) {
            desc = vot_getValue (handle);
            printf ("  Description:  ");
            ppMultiLine (desc, 16, 60, 4096);
            printf ("\n");
        }


        /*  Print the column info in verbose mode.
        */
        if (verbose) {
            printf ("\n\t\t\tName\t\t\tUCD\n");
	    i = 0;
            for (field=vot_getFIELD(tab); field; field=vot_getNext (field)) {
	        name = vot_getAttr (field, "name");
	        ucd  = vot_getAttr (field, "ucd");

	        printf ("      Field %2d:  %-20s\t%-30s\n",
	            ++i, (name ? name : ""), (ucd ? ucd : ""));
	        if (verbose > 1) {
    	            if ( (desc = vot_getValue (vot_getDESCRIPTION (field))) )
	    	        printf ("\t  Desc:  %-s\n", desc);
	        }
            }
        }
    }
	    
    if (iname) free (iname);

    vo_paramFree (argc, pargv);
    vot_closeVOTABLE (vot);		/* close the table  	*/

    return (status);
}


/**
 *  USAGE -- Print task help summary.
 */
static void
Usage (void)
{
    fprintf (stderr, "\n  Usage:\n\t"
        "votinfo [<opts>] votable.xml\n\n"
        "  where\n"
        "       -b,--brief		brief output\n"
        "       -d,--description	print <DESCRIPTION> elements\n"
        "       -h,--help		this message\n"
        "       -i,--info		print <INFO> elements\n"
        "       -n,--numberOf=<what>	get number of specified elements\n"
        "       -p,--param		print <PARAM> elements\n"
        "       -q,--query_status	print QUERY_STATUS\n"
        "       -r,--return		return result from method\n"
        "       -s,--size		print table size\n"
        "       -v,--verbose		verbose otuput\n"
        "       -w,--warn		print parser warnings\n"
	"\n"
	"  <what> is one of\n"
	"	    param		<PARAM> elements\n"
	"	    info		<INFO> elements\n"
	"	    rows		table rows\n"
	"	    cols		table cols\n"
	"	    resources		<RESOURCE> elements\n"
	"\n"
 	"  Examples:\n\n"
	"    1)  Print summary information about a VOTable\n\n"
	"	    %% votinfo -v test.xml\n"
	"\n"
	"    2)  Print the <PARAM> elements in a table, then get a count\n\n"
	"	    %% votinfo -p test.xml\n"
	"	    %% votinfo --numberOf=param test.xml\n"
	"\n"
	"    3)  Determine whether a VOTable contains a successful result\n\n"
	"	    %% votinfo -q test.xml\n\n"
	"        A zero indicates 'OK', a one is an 'ERR', and -1 means that\n"
	"        an <INFO> with a 'QUERY_STATUS' was not found\n"
	"\n"
    );
}


/**
 *  Tests -- Task unit tests.
 */
static void
Tests (char *input)
{
    Task *task = &self;

    vo_taskTest (task, "--help", NULL);

    if (access (input, F_OK) != 0) {
	fprintf (stderr, "Warning:  cannot open file '%s'\n", input);
	return;
    }

    vo_taskTest (task, "-v", input, NULL);			// Ex 1
    vo_taskTest (task, "-p", input, NULL);			// Ex 2
    vo_taskTest (task, "--numberOf=param", input, NULL);	// Ex 3
    vo_taskTest (task, "--numberOf=info", input, NULL);
    vo_taskTest (task, "--numberOf=rows", input, NULL);
    vo_taskTest (task, "--numberOf=cols", input, NULL);
    vo_taskTest (task, "--numberOf=resource", input, NULL);

    vo_taskTest (task, "-n", "param", input, NULL);
    vo_taskTest (task, "-n", "info", input, NULL);
    vo_taskTest (task, "-n", "rows", input, NULL);
    vo_taskTest (task, "-n", "cols", input, NULL);
    vo_taskTest (task, "-n", "resource", input, NULL);

    vo_taskTest (task, "-q", input, NULL);			// Ex 4

    vo_taskTest (task, "-b", input, NULL);
    vo_taskTest (task, "-i", input, NULL);
    vo_taskTest (task, "-d", input, NULL);
    vo_taskTest (task, "-s", input, NULL);

    vo_taskTestReport (self);
}