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
|
/************************************************************************
** VOXML.C -- Utility procedures for writing XML files, i.e. the raw
** VOTable output wrapped in a proprietary XML document.
**
** M. Fitzpatrick, NOAO, July 2007
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <string.h>
#include <math.h>
#include "VOClient.h"
#include "voAppsP.h"
extern int format, debug, errno, extract;
extern char *output;
extern Service *svcList;
extern Object *objList;
extern char *vot_getSName (char *root);
extern char *vot_getOName (char *root);
extern char *vot_procTimestamp (void);
void vot_concatXML (char *fname);
int vot_copyXMLFile (char *root, char *sname, FILE *fd);
void vot_cleanXML (void);
/************************************************************************
** CONCATXML -- Concatenate the XML file generated by the query into a
** single, hierarchical document grouped by the service.
*/
void
vot_concatXML (char *fname)
{
FILE *fd = (FILE *) NULL;
Service *svc = svcList; /* the service list */
Proc *proc; /* process list in each service */
int index = 0;
char sname[SZ_LINE], oname[SZ_LINE];
if (fname[0] == '-')
fd = stdout;
else if ((fd = fopen (fname, "w+")) == (FILE *) NULL) {
fprintf (stderr, "ERROR: Cannot open XML file: '%s'\n", fname);
return;
}
fprintf (fd, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf (fd, "<VOTABLES date=\"%s\">\n", vot_procTimestamp());
for (svc=svcList; svc; svc=svc->next) {
bzero (sname, SZ_FNAME);
strcpy (sname, vot_getSName (svc->proc->root));
for (proc=svc->proc; proc; proc=proc->next) {
bzero (oname, SZ_FNAME);
strcpy (oname, vot_getOName (proc->root));
fprintf (fd, "<VOTABLE_ENTRY index=\"%d\"", index);
fprintf (fd, " svc=\"%s\" ",
(strcmp ("tmp",sname) == 0 ? svc->name : sname));
if (!oname[0]) {
fprintf (fd, "pos=\"(%f,%f)\" ",
proc->obj->ra, proc->obj->dec);
}
fprintf (fd, "obj=\"%s\">\n", oname);
vot_copyXMLFile (proc->root, oname, fd);
fprintf (fd, "\n</VOTABLE_ENTRY>\n");
index++;
}
}
fprintf (fd, "</VOTABLES>\n");
/* Clean up the intermediate files if needed.
if (format & F_XML || (extract & EX_XML && extract & EX_COLLECT))
*/
if (format & F_XML && (extract & EX_COLLECT))
vot_cleanXML ();
/* Close the file descriptors.
*/
if (fd != stdout)
fclose (fd);
}
/************************************************************************
** COPYXMLFILE -- Copy a VOTable to the output XML file.
*/
int
vot_copyXMLFile (char *root, char *name, FILE *fd)
{
char *ip, line[4096], fname[SZ_FNAME];
FILE *ifd;
extern char *strcasestr();
bzero (fname, SZ_FNAME);
sprintf (fname, "%s.xml", root);
if (access (fname, R_OK) == 0) {
if ((ifd = fopen (fname, "r")) == (FILE *) NULL) {
fprintf (stderr, "Warning: Cannot open file '%s'\n", fname);
return (ERR);
}
} else {
return (OK);
}
/* Skip ahead to the start of the part we're interested in.
*/
bzero (line, 4096);
while (fgets (line, 4096, ifd)) {
if ((ip = strcasestr (line, "<VOTABLE"))) {
fprintf (fd, "%s", ip);
break;
}
bzero (line, 4096);
}
/* (Slow) Copy the file until the end of the Document.
*/
bzero (line, 4096);
while (fgets (line, 4096, ifd)) {
if (strcasestr (line, "</VOTABLE>")) {
fprintf (fd, "%s", line);
break;
}
fprintf (fd, " %s", line);
bzero (line, 4096);
}
fclose (ifd);
return (OK);
}
/************************************************************************
** CLEANXML -- Clean up the intermediate VOTable files when producing
** the compiled XML doc..
*/
void
vot_cleanXML ()
{
Service *svc = svcList; /* the service list */
Proc *proc; /* process list in each service */
char fname[SZ_FNAME];
for (svc=svcList; svc; svc=svc->next) {
for (proc=svc->proc; proc; proc=proc->next) {
bzero (fname, SZ_FNAME);
sprintf (fname, "%s.vot", proc->root);
unlink (fname);
}
}
}
|