From 19a7b3b57317a28363fec3e4c6ce1100459d2e8e Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 3 Aug 2023 20:13:13 -0400 Subject: [PATCH] example.c: take INPUT/OUTPUT filenames as arguments. --- example.c | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) 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 */ -- 2.43.2