From: Michael Orlitzky Date: Fri, 4 Aug 2023 00:13:13 +0000 (-0400) Subject: example.c: take INPUT/OUTPUT filenames as arguments. X-Git-Tag: 0.0.1~54 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=19a7b3b57317a28363fec3e4c6ce1100459d2e8e;p=libsvgtiny-pixbuf.git example.c: take INPUT/OUTPUT filenames as arguments. --- diff --git a/example.c b/example.c index 1f54a00..f582682 100644 --- a/example.c +++ b/example.c @@ -1,8 +1,13 @@ +#include /* open */ +#include /* printf, fprintf */ +#include /* malloc */ +#include /* fstat */ +#include /* read */ + #include -#include -#include #include + /** * Render an svgtiny path using cairo. */ @@ -67,13 +72,14 @@ int main(int argc, char** argv) { /* Initial viewport width and height */ int width = 1024, height = 1024; float scale = 1.0; - char* svgpath = "Ghostscript_Tiger.svg"; - char* pngpath = "Ghostscript_Tiger.png"; - size_t svgsize = 68630; + char* svgpath; + char* pngpath; - FILE *fd; + int fd; + size_t svgsize; char *buffer; size_t bytesread; + struct stat sb; svgtiny_code code; struct svgtiny_diagram *diagram; @@ -83,12 +89,28 @@ int main(int argc, char** argv) { unsigned int i; + /* 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"); + return 2; + } + + svgpath = argv[1]; + pngpath = argv[2]; + /* load file into memory buffer */ - fd = fopen(svgpath, "rb"); - if (!fd) { + fd = open(svgpath, O_RDONLY); + if (fd == -1) { + perror(svgpath); + return 1; + } + + if (fstat(fd, &sb)) { perror(svgpath); return 1; } + svgsize = sb.st_size; buffer = malloc(svgsize); if (!buffer) { @@ -96,12 +118,12 @@ int main(int argc, char** argv) { return 1; } - bytesread = fread(buffer, 1, svgsize, fd); + bytesread = read(fd, buffer, svgsize); if (bytesread != svgsize) { perror(svgpath); return 1; } - fclose(fd); + close(fd); /* create svgtiny object */