]> granicus.if.org Git - graphviz/commitdiff
gvcolor: remove assumption that color values fit in 64 characters
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 15 Jan 2023 21:58:56 +0000 (13:58 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 21 Jan 2023 04:48:33 +0000 (20:48 -0800)
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
cmd/tools/colxlate.c
cmd/tools/gvcolor.c

index 92bef47673451e4a7302c19612265b14be8768c3..412584cd1e56c177813b2a0eadc66a8e8dc2d9e9 100644 (file)
@@ -1,3 +1,5 @@
 #pragma once
 
-void colorxlate(char *str, char *buf);
+#include <cgraph/agxbuf.h>
+
+void colorxlate(char *str, agxbuf *buf);
index b9cefd3c7e62599980bd82c564dc52a64e44f0ec..bc4487bf4cc59001036b44f251348b34cbff9c15 100644 (file)
@@ -13,6 +13,7 @@
  * Contributors: Details at https://graphviz.org
  *************************************************************************/
 
+#include <cgraph/agxbuf.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -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);
 }
index 92e8991449ad7b92e0375395045c5169a896caa3..4a6ac6db80a103b2eabb4b16af074c653bcb7c41 100644 (file)
@@ -23,6 +23,7 @@
 #define        NC      3               /* size of HSB color vector */
 
 #include <assert.h>
+#include <cgraph/agxbuf.h>
 #include <cgraph/alloc.h>
 #include <cgraph/cgraph.h>
 #include <cgraph/exit.h>
@@ -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;