summaryrefslogtreecommitdiff
path: root/svg
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2009-11-19 18:41:44 +0000
committerTor Andersson <tor.andersson@artifex.com>2009-11-19 18:41:44 +0000
commite803acaca5326327787b7e1b536ff807b3f3f51a (patch)
treea06bede8f2f72de73f81a95085b4db9673054539 /svg
parentf8b1fe858d3dee5340b0da54f5cb4341cca416c4 (diff)
Add a function process_file to the plmain interface. This is a very simple hack that will let those interpreters who want to handle random access files directly to do so. So far this is only the SVG and XPS interpreters.
git-svn-id: http://svn.ghostscript.com/ghostscript/trunk@10357 a1074d23-0009-0410-80fe-cf8c14f379e6
Diffstat (limited to 'svg')
-rw-r--r--svg/svgtop.c98
1 files changed, 76 insertions, 22 deletions
diff --git a/svg/svgtop.c b/svg/svgtop.c
index 440be835d..ac4abd757 100644
--- a/svg/svgtop.c
+++ b/svg/svgtop.c
@@ -227,6 +227,55 @@ svg_imp_get_device_memory(pl_interp_instance_t *pinstance, gs_memory_t **ppmem)
return 0;
}
+static int
+svg_imp_process_file(pl_interp_instance_t *pinstance, char *filename)
+{
+ svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
+ svg_context_t *ctx = instance->ctx;
+ svg_item_t *root;
+ FILE *file;
+ int code;
+ char buf[4096];
+ int n;
+
+ dprintf1("svg_imp_process_file %s\n", filename);
+
+ file = fopen(filename, "rb");
+ if (!file)
+ return gs_error_ioerror;
+
+ while (!feof(file))
+ {
+ n = fread(buf, 1, sizeof buf, file);
+ if (n == 0)
+ if (ferror(file))
+ return gs_error_ioerror;
+ code = svg_feed_xml_parser(ctx, buf, n);
+ if (code < 0)
+ {
+ gs_catch(code, "cannot parse SVG document");
+ fclose(file);
+ return code;
+ }
+ }
+
+ root = svg_close_xml_parser(ctx);
+ if (!root)
+ {
+ gs_catch(-1, "cannot parse xml document");
+ fclose(file);
+ return -1;
+ }
+
+ code = svg_parse_document(ctx, root);
+ if (code < 0)
+ gs_catch(code, "cannot parse SVG document");
+
+ svg_free_item(ctx, root);
+ fclose(file);
+ return code;
+}
+
/* Parse a cursor-full of data */
static int
svg_imp_process(pl_interp_instance_t *pinstance, stream_cursor_read *buf)
@@ -236,6 +285,8 @@ svg_imp_process(pl_interp_instance_t *pinstance, stream_cursor_read *buf)
int code;
code = svg_feed_xml_parser(ctx, (const char*)buf->ptr + 1, buf->limit - buf->ptr);
+ if (code < 0)
+ gs_catch(code, "cannot parse SVG document");
buf->ptr = buf->limit;
@@ -253,6 +304,30 @@ svg_imp_flush_to_eoj(pl_interp_instance_t *pinstance, stream_cursor_read *pcurso
return 0;
}
+/* Parser action for end-of-file */
+static int
+svg_imp_process_eof(pl_interp_instance_t *pinstance)
+{
+ svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
+ svg_context_t *ctx = instance->ctx;
+ svg_item_t *root;
+ int code;
+
+ dputs("-- svg_imp_process_eof --\n");
+
+ root = svg_close_xml_parser(ctx);
+ if (!root)
+ return gs_rethrow(-1, "cannot parse xml document");
+
+ code = svg_parse_document(ctx, root);
+ if (code < 0)
+ gs_catch(code, "cannot parse SVG document");
+
+ svg_free_item(ctx, root);
+
+ return code;
+}
+
/* Report any errors after running a job */
static int
svg_imp_report_errors(pl_interp_instance_t *pinstance,
@@ -276,28 +351,6 @@ svg_imp_init_job(pl_interp_instance_t *pinstance)
return svg_open_xml_parser(ctx);
}
-/* Parser action for end-of-file */
-static int
-svg_imp_process_eof(pl_interp_instance_t *pinstance)
-{
- svg_interp_instance_t *instance = (svg_interp_instance_t *)pinstance;
- svg_context_t *ctx = instance->ctx;
- svg_item_t *root;
- int code;
-
- dputs("-- svg_imp_process_eof --\n");
-
- root = svg_close_xml_parser(ctx);
- if (!root)
- return gs_rethrow(-1, "cannot parse xml document");
-
- code = svg_parse_document(ctx, root);
-
- svg_free_item(ctx, root);
-
- return code;
-}
-
/* Wrap up interp instance after a "job" */
static int
svg_imp_dnit_job(pl_interp_instance_t *pinstance)
@@ -369,6 +422,7 @@ const pl_interp_implementation_t svg_implementation =
svg_imp_set_post_page_action,
svg_imp_set_device,
svg_imp_init_job,
+ svg_imp_process_file,
svg_imp_process,
svg_imp_flush_to_eoj,
svg_imp_process_eof,