-#include <fcntl.h> /* open */
-#include <stdio.h> /* printf, fprintf */
+#include <stdio.h> /* fopen, fprintf, fread, printf */
#include <stdlib.h> /* malloc */
-#include <sys/stat.h> /* fstat */
-#include <unistd.h> /* read */
#include <cairo.h>
#include <gdk/gdk.h>
/*
* @brief Parse an SVG file into a diagram_t structure.
*
- * @param path
- * The path to the SVG file.
+ * @param fp
+ * A pointer to an open file stream.
*
* @return If successful, a pointer to a @c diagram_t structure is
* returned; if not, @c NULL is returned. You are expected to @c
* svgtiny_free the result if it is valid.
*/
-diagram_t* svgtiny_diagram_from_path(char* path, int width, int height) {
+diagram_t* svgtiny_diagram_from_file(FILE* fp, int width, int height) {
diagram_t *diagram;
- int fd;
size_t bytecount;
char *buffer;
size_t bytesread;
- struct stat sb;
svgtiny_code code;
- /* load file into memory buffer */
- fd = open(path, O_RDONLY);
- if (fd == -1) {
- perror(path);
- return NULL;
- }
-
- if (fstat(fd, &sb)) {
- perror(path);
- return NULL;
- }
- bytecount = sb.st_size;
+ /* Find the size of the file stream */
+ fseek(fp, 0L, SEEK_END);
+ bytecount = ftell(fp);
+ rewind(fp);
buffer = malloc(bytecount);
if (!buffer) {
return NULL;
}
- bytesread = read(fd, buffer, bytecount);
+ bytesread = fread(buffer, 1, bytecount, fp);
if (bytesread != bytecount) {
- perror(path);
- return NULL;
+ fprintf(stderr, "Read only %zd of %zd bytes from stream\n",
+ bytesread,
+ bytecount);
}
- close(fd);
-
+ fclose(fp);
- /* create svgtiny object */
diagram = svgtiny_create();
if (!diagram) {
fprintf(stderr, "svgtiny_create() failed\n");
return NULL;
}
- code = svgtiny_parse(diagram, buffer, bytecount, path, width, height);
+ code = svgtiny_parse(diagram, buffer, bytecount, "", width, height);
free(buffer);
if (code != svgtiny_OK) {
char* svgpath;
char* pngpath;
int pngwidth = 1024, pngheight = 1024;
+ FILE* fp;
diagram_t* diagram;
cairo_t *cr = 0;
svgpath = argv[1];
pngpath = argv[2];
- diagram = svgtiny_diagram_from_path(svgpath, pngwidth, pngheight);
+ fp = fopen(svgpath, "rb");
+ if (!fp) {
+ perror(svgpath);
+ return 1;
+ }
+
+ diagram = svgtiny_diagram_from_file(fp, pngwidth, pngheight);
if (!diagram) {
return 1;
}