From dda9ac6ef9298a04aa614af6af6d1f7933e17e0e Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sun, 15 Jan 2023 13:58:56 -0800 Subject: [PATCH] gvcolor: remove assumption that color values fit in 64 characters This code is still pretty challenging to follow, but at least we remove one more thing the reader had to worry about. Gitlab: #1950 --- cmd/tools/colorxlate.h | 4 +++- cmd/tools/colxlate.c | 13 ++++++------- cmd/tools/gvcolor.c | 8 +++++--- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cmd/tools/colorxlate.h b/cmd/tools/colorxlate.h index 92bef4767..412584cd1 100644 --- a/cmd/tools/colorxlate.h +++ b/cmd/tools/colorxlate.h @@ -1,3 +1,5 @@ #pragma once -void colorxlate(char *str, char *buf); +#include + +void colorxlate(char *str, agxbuf *buf); diff --git a/cmd/tools/colxlate.c b/cmd/tools/colxlate.c index b9cefd3c7..bc4487bf4 100644 --- a/cmd/tools/colxlate.c +++ b/cmd/tools/colxlate.c @@ -13,6 +13,7 @@ * Contributors: Details at https://graphviz.org *************************************************************************/ +#include #include #include #include @@ -50,10 +51,9 @@ static int colorcmpf(const void *a0, const void *a1) return strcmp(p0->name, p1->name); } -void colorxlate(char *str, char *buf) { +void colorxlate(char *str, agxbuf *buf) { static hsbcolor_t *last; char canon[128]; - char *p; hsbcolor_t fake; if (last == NULL || strcmp(last->name, str)) { @@ -64,12 +64,11 @@ void colorxlate(char *str, char *buf) { if (last == NULL) { if (!isdigit((int)canon[0])) { fprintf(stderr, "warning: %s is not a known color\n", str); - strcpy(buf, str); + agxbput(buf, str); } else - for (p = buf; (*p = *str++); p++) - if (*p == ',') - *p = ' '; + for (const char *p = str; *p != '\0'; ++p) + agxbputc(buf, *p == ',' ? ' ' : *p); } else - sprintf(buf, "%.3f %.3f %.3f", ((double) last->h) / 255, + agxbprint(buf, "%.3f %.3f %.3f", ((double) last->h) / 255, ((double) last->s) / 255, ((double) last->b) / 255); } diff --git a/cmd/tools/gvcolor.c b/cmd/tools/gvcolor.c index 92e899144..4a6ac6db8 100644 --- a/cmd/tools/gvcolor.c +++ b/cmd/tools/gvcolor.c @@ -23,6 +23,7 @@ #define NC 3 /* size of HSB color vector */ #include +#include #include #include #include @@ -64,11 +65,12 @@ static int cmpf(Agnode_t ** n0, Agnode_t ** n1) static void setcolor(char *p, double *v) { - char buf[64]; + agxbuf buf = {0}; if (sscanf(p, "%lf %lf %lf", v, v + 1, v + 2) != 3 && p[0]) { - colorxlate(p, buf); - sscanf(buf, "%lf %lf %lf", v, v + 1, v + 2); + colorxlate(p, &buf); + sscanf(agxbuse(&buf), "%lf %lf %lf", v, v + 1, v + 2); } + agxbfree(&buf); } static char **Files; -- 2.50.1