-#include <stdio.h> /* fprintf, printf */
-#include <string.h> /* memcpy, memset, strstr */
+#include <string.h> /* memcpy, memset */
#include <cairo.h>
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixbuf.h> /* includes glib.h */
+#include <glib/gprintf.h>
#include <svgtiny.h>
/*
code = svgtiny_parse(diagram, buffer, bytecount, "", width, height);
- if (code != svgtiny_OK) {
- switch (code) {
+ switch(code) {
+ case svgtiny_OK:
+ /* The one success case. */
+ return diagram;
case svgtiny_OUT_OF_MEMORY:
g_set_error_literal(error,
G_FILE_ERROR,
diagram->error_line,
diagram->error_message);
break;
- }
- return NULL;
}
- return diagram;
+ /* All other cases above are failure */
+ return NULL;
}
/**
crs = cairo_surface_status(surface);
if (crs != CAIRO_STATUS_SUCCESS) {
- fprintf(stderr,
- "cairo_image_surface_create failed: %s\n",
- cairo_status_to_string(crs));
+ g_fprintf(stderr,
+ "cairo_image_surface_create failed: %s\n",
+ cairo_status_to_string(crs));
cairo_surface_destroy(surface);
return NULL;
}
cairo_surface_destroy(surface);
if (crs != CAIRO_STATUS_SUCCESS) {
- fprintf(stderr,
- "cairo_create failed: %s\n",
- cairo_status_to_string(crs));
+ g_fprintf(stderr,
+ "cairo_create failed: %s\n",
+ cairo_status_to_string(crs));
cairo_destroy(cr);
return NULL;
}
/* Check the status again just for good measure? */
crs = cairo_status(cr);
if (crs != CAIRO_STATUS_SUCCESS) {
- fprintf(stderr,
- "cairo error: %s\n",
- cairo_status_to_string(crs));
+ g_fprintf(stderr,
+ "cairo error: %s\n",
+ cairo_status_to_string(crs));
cairo_destroy(cr);
return NULL;
}
const guchar* buf,
guint buf_size,
GError** error) {
- size_t increment = 0;
SvgTinyContext* context = (SvgTinyContext*)data;
- /* YOLO, no error checking */
+ /* YOLO, what is error checking? */
context->svg_data = g_realloc(context->svg_data,
context->svg_data_size + buf_size);
memcpy(context->svg_data + context->svg_data_size, buf, buf_size);
char* xi;
char* xi_stop;
- xi_start = strstr(buffer, XI_SIGNATURE);
+ xi_start = g_strstr_len(buffer, buf_size, XI_SIGNATURE);
if (xi_start == NULL) {
return NULL;
}
xi = xi_start + strlen(XI_SIGNATURE);
- xi_stop = strstr(xi, "\"");
+ xi_stop = g_strstr_len(xi, (buffer + buf_size) - xi, "\"");
if(xi_stop == NULL) {
/* We found the start of an XInclude, but not the end of its
base64-encoded data? Play it safe and do nothing. */
"xi" into one */
*xi_stop = 0;
gsize decoded_size;
- guchar* decoded = g_base64_decode(xi, &decoded_size);
+
+ /* All files are ASCII, right? In GTK's gdkpixbufutils.c, our base64
+ * data is encoded from a binary file stream, i.e. bytes, without
+ * any regard for what the text inside represents. Elsewhere we are
+ * pretending that gchar is a reasonable data type to use for the
+ * contents of an SVG file; here we are saying that again out loud.
+ */
+ gchar* decoded = (gchar*)g_base64_decode(xi, &decoded_size);
+ *xi_stop = '"';
/* We need another round of processing to strip the <xml> and <svg>
* elements out of "decoded", but it's simpler to just overwrite
* element so much, because if one exists, it has to come before the
* <svg>. As a result, we just need to strip everything up to the
* leading <svg> tag. */
- guchar* svg_open_start = strstr(decoded, "<svg ");
- guchar* svg_open_end = strstr(svg_open_start, ">");
- memset(decoded, ' ', (1 + (svg_open_end - decoded)));
+ gchar* svg_open_start = g_strstr_len(decoded, decoded_size, "<svg ");
+ if (svg_open_start == NULL) {
+ /* Uhhhhh */
+ g_free(decoded);
+ return NULL;
+ }
+ else {
+ gchar* svg_open_end = g_strstr_len(svg_open_start, decoded_size, ">");
+ if (svg_open_end == NULL) {
+ /* Uhhhhh */
+ g_free(decoded);
+ return NULL;
+ }
+ memset(decoded, ' ', (1 + (svg_open_end - decoded)));
+ }
/* We're going to keep everything up to xi_start. If the <xi:include
* started at, say, position three, then this would compute a size
int main(int argc, char** argv) {
char* svgpath;
char* pngpath;
- FILE* fp;
GError* err = NULL;
GdkPixbuf* pb;
/* Parse arguments, and maybe print usage */
if (argc < 3) {
- printf("Usage: %s INPUT OUTPUT\n", argv[0]);
- printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
+ g_printf("Usage: %s INPUT OUTPUT\n", argv[0]);
+ g_printf("Convert an SVG file (INPUT) to a PNG file (OUTPUT)\n");
return 2;
}
pb = gdk_pixbuf_new_from_file(svgpath, &err);
if (!pb) {
- fprintf(stderr,
- "Error %d in gdk_pixbuf_new_from_file: %s\n",
- err->code,
- err->message);
+ g_fprintf(stderr,
+ "Error %d in gdk_pixbuf_new_from_file: %s\n",
+ err->code,
+ err->message);
g_error_free(err);
return 1;
}