aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/voapps/lib/voXML.c
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/voclient/voapps/lib/voXML.c')
-rw-r--r--vendor/voclient/voapps/lib/voXML.c166
1 files changed, 166 insertions, 0 deletions
diff --git a/vendor/voclient/voapps/lib/voXML.c b/vendor/voclient/voapps/lib/voXML.c
new file mode 100644
index 00000000..dbe01ff6
--- /dev/null
+++ b/vendor/voclient/voapps/lib/voXML.c
@@ -0,0 +1,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);
+ }
+ }
+}