]> granicus.if.org Git - graphviz/commitdiff
eliminate use of agfprintf() and its use of vsnprintf()
authorellson <devnull@localhost>
Thu, 12 Mar 2009 19:59:11 +0000 (19:59 +0000)
committerellson <devnull@localhost>
Thu, 12 Mar 2009 19:59:11 +0000 (19:59 +0000)
lib/common/output.c
lib/common/types.h
lib/graph/graph.h

index e180af38337c6b79d2ed5344856924c7eabd6065..fda80ee569add508cbf17903bb131ef3d3348f0c 100644 (file)
 int Y_off;           /* ymin + ymax */
 double YF_off;       /* Y_off in inches */
 
-#ifdef WITH_CGRAPH
-/* agfprintf:
- * Note that this function is unsafe due to the fixed buffer size.
- * It should only be used when the caller is sure the input will not
- * overflow the buffer. In particular, it should be avoided for
- * input coming from users. Also, if vsnprintf is available, the
- * code should check for return values to use it safely.
- */
-static void agfprintf(FILE *fp, const char *format, ...)
+static void printstring(FILE * f, char *prefix, char *s)
 {
-    char buf[BUFSIZ];
-    size_t len;
-    va_list argp;
+    if (prefix) agputs(prefix, f);
+    agputs(s, f);
+}
 
-    va_start(argp, format);
-#ifdef HAVE_VSNPRINTF
-    len = vsnprintf((char *)buf, sizeof(buf), format, argp);
-#else
-    len = vsprintf((char *)buf, format, argp);
-#endif
-    va_end(argp);
+static void printint(FILE * f, char *prefix, int i)
+{
+    char buf[BUFSIZ];
+    
+    if (prefix) agputs(prefix, f);
+    sprintf(buf, "%d", i);
+    agputs(buf, f);
+}
 
-// FIXME
-//    ioput(g, fp, buf);
+static void printdouble(FILE * f, char *prefix, double v)
+{
+    char buf[BUFSIZ];
+    
+    if (prefix) agputs(prefix, f);
+    sprintf(buf, "%.5g", v);
+    agputs(buf, f);
 }
-#endif
 
-static void printptf(FILE * f, pointf pt)
+static void printpoint(FILE * f, pointf p)
 {
-    agfprintf(f, " %.5g %.5g", PS2INCH(pt.x), PS2INCH(YDIR(pt.y)));
+    printdouble(f, " ", PS2INCH(p.x));
+    printdouble(f, " ", PS2INCH(YDIR(p.y)));
 }
 
 /* setYInvert:
@@ -78,7 +76,7 @@ static char* canon (graph_t *g, char* s)
 {
 #ifndef WITH_CGRAPH
     char* ns = agstrdup (s);
-    char* cs = agcanonical (ns);
+    char* cs = agcanonStr (ns);
     agstrfree (ns);
 #else
     char* ns = agstrdup (g, s);
@@ -88,24 +86,16 @@ static char* canon (graph_t *g, char* s)
     return cs;
 }
 
-static void writenodeandport(FILE * fp, node_t * node, char *port)
+static void writenodeandport(FILE * f, node_t * node, char *port)
 {
     char *name;
     if (IS_CLUST_NODE(node))
        name = canon (agraphof(node), strchr(agnameof(node), ':') + 1);
     else
-#ifndef WITH_CGRAPH
-       name = agcanonical (agnameof(node));
-#else
        name = agcanonStr (agnameof(node));
-#endif
-    agfprintf(fp, "%s", name); /* slimey i know */
+    printstring(f, " ", name); /* slimey i know */
     if (port && *port)
-#ifndef WITH_CGRAPH
-       agfprintf(fp, ":%s", agcanonical(port));
-#else
-       agfprintf(fp, ":%s", agcanonStr(port));
-#endif
+       printstring(f, ":", agcanonStr(port));
 }
 
 /* _write_plain:
@@ -123,30 +113,31 @@ void write_plain(GVJ_t * job, graph_t * g, FILE * f, boolean extend)
 //    setup_graph(job, g);
     setYInvert(g);
     pt = GD_bb(g).UR;
-    agfprintf(f, "graph %.5g %.5g %.5g\n", job->zoom, PS2INCH(pt.x), PS2INCH(pt.y));
+    printdouble(f, "graph ", job->zoom);
+    printdouble(f, " ", PS2INCH(pt.x));
+    printdouble(f, " ", PS2INCH(pt.y));
+    agputc('\n', f);
     for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
        if (IS_CLUST_NODE(n))
            continue;
-#ifndef WITH_CGRAPH
-       agfprintf(f, "node %s ", agcanonical(agnameof(n)));
-#else
-       agfprintf(f, "node %s ", agcanonStr(agnameof(n)));
-#endif
-       printptf(f, ND_coord(n));
+       printstring(f, "node ", agcanonStr(agnameof(n)));
+       printpoint(f, ND_coord(n));
        if (ND_label(n)->html)   /* if html, get original text */
 #ifndef WITH_CGRAPH
-           lbl = agcanonical (agxget(n, N_label->index));
+           lbl = agcanonStr (agxget(n, N_label->index));
 #else
            lbl = agcanonStr (agxget(n, N_label));
 #endif
        else
            lbl = canon(agraphof(n),ND_label(n)->text);
-       agfprintf(f, " %.5g %.5g %s %s %s %s %s\n",
-               ND_width(n), ND_height(n), lbl,
-               late_nnstring(n, N_style, "solid"),
-               ND_shape(n)->name,
-               late_nnstring(n, N_color, DEFAULT_COLOR),
-               late_nnstring(n, N_fillcolor, DEFAULT_FILL));
+        printdouble(f, " ", ND_width(n));
+        printdouble(f, " ", ND_height(n));
+        printstring(f, " ", lbl);
+       printstring(f, " ", late_nnstring(n, N_style, "solid"));
+       printstring(f, " ", ND_shape(n)->name);
+       printstring(f, " ", late_nnstring(n, N_color, DEFAULT_COLOR));
+       printstring(f, " ", late_nnstring(n, N_fillcolor, DEFAULT_FILL));
+       agputc('\n', f);
     }
     for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
        for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
@@ -177,26 +168,26 @@ void write_plain(GVJ_t * job, graph_t * g, FILE * f, boolean extend)
                    bz = ED_spl(e)->list[i];
                    splinePoints += bz.size;
                }
-               agfprintf(f, "edge ");
+               printstring(f, NULL, "edge");
                writenodeandport(f, agtail(e), tport);
-               agfprintf(f, " ");
                writenodeandport(f, aghead(e), hport);
-               agfprintf(f, " %d", splinePoints);
+               printint(f, " ", splinePoints);
                for (i = 0; i < ED_spl(e)->size; i++) {
                    bz = ED_spl(e)->list[i];
                    for (j = 0; j < bz.size; j++)
-                       printptf(f, bz.list[j]);
+                       printpoint(f, bz.list[j]);
                }
            }
            if (ED_label(e)) {
-               agfprintf(f, " %s", canon(agraphof(agtail(e)),ED_label(e)->text));
-               printptf(f, ED_label(e)->pos);
+               printstring(f, " ", canon(agraphof(agtail(e)),ED_label(e)->text));
+               printpoint(f, ED_label(e)->pos);
            }
-           agfprintf(f, " %s %s\n", late_nnstring(e, E_style, "solid"),
-                   late_nnstring(e, E_color, DEFAULT_COLOR));
+           printstring(f, " ", late_nnstring(e, E_style, "solid"));
+           printstring(f, " ", late_nnstring(e, E_color, DEFAULT_COLOR));
+           agputc('\n', f);
        }
     }
-    agfprintf(f, "stop\n");
+    agputs("stop\n", f);
 }
 
 static void set_record_rects(node_t * n, field_t * f, agxbuf * xb)
index 0f590dcf38899d65e8097ac14b12c826ff610a7d..eec88bc93e0f414df6a9514001b69756582fb351 100644 (file)
@@ -762,6 +762,7 @@ typedef enum {NATIVEFONTS,PSFONTS,SVGFONTS} fontname_kind;
 #define agfindgraphattr(g,a) agfindattr((g)->root,a)
 #define agfindnodeattr(g,a) agfindattr((g)->proto->n,a)
 #define agfindedgeattr(g,a) agfindattr((g)->proto->e,a)
+#define agcanonStr(s) agcanonical(s)
 #endif
 
 
index 0b5ab28fe103e7f477f202f029a73f2c612ab0d1..b0566875442aaab7c8b75f910dc6a174a2cfc7ba 100644 (file)
@@ -167,7 +167,6 @@ extern "C" {
         char * (*myfgets) (char *s, int size, FILE *stream),
        size_t (*myfwrite) (const void *ptr, size_t size, size_t nmemb, FILE *stream),
        int (*myferror) (FILE *stream) );
-    extern void agfprintf(FILE *fp, const char *format, ...);
     extern int agputs(const char *s, FILE *fp);
     extern int agputc(int c, FILE *fp);
     extern int agwrite(Agraph_t *, FILE *);