From: Michael Orlitzky Date: Fri, 4 Aug 2023 02:14:45 +0000 (-0400) Subject: example.c: switch back to the stream-based file API. X-Git-Tag: 0.0.1~48 X-Git-Url: http://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=6b08d4c36787505216607ecee1ad433ff72816b6;p=libsvgtiny-pixbuf.git example.c: switch back to the stream-based file API. GdkPixbuf's module->load function takes a FILE* and not a path (or descriptor), so let's go back to that. --- diff --git a/example.c b/example.c index 3dab2ba..bc09e4c 100644 --- a/example.c +++ b/example.c @@ -1,8 +1,5 @@ -#include /* open */ -#include /* printf, fprintf */ +#include /* fopen, fprintf, fread, printf */ #include /* malloc */ -#include /* fstat */ -#include /* read */ #include #include @@ -73,35 +70,25 @@ void render_path(cairo_t *cr, struct svgtiny_shape *path) { /* * @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) { @@ -109,22 +96,21 @@ diagram_t* svgtiny_diagram_from_path(char* path, int width, int height) { 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) { @@ -247,6 +233,7 @@ int main(int argc, char** argv) { char* svgpath; char* pngpath; int pngwidth = 1024, pngheight = 1024; + FILE* fp; diagram_t* diagram; cairo_t *cr = 0; @@ -263,7 +250,13 @@ int main(int argc, char** argv) { 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; }