]> granicus.if.org Git - graphviz/commitdiff
Remove all uses of sscanf for efficiency's sake
authorerg <devnull@localhost>
Sat, 22 Jan 2011 23:54:34 +0000 (23:54 +0000)
committererg <devnull@localhost>
Sat, 22 Jan 2011 23:54:34 +0000 (23:54 +0000)
lib/common/emit.c
lib/xdot/xdot.c
lib/xdot/xdot.h

index 5dd3221a79389bd9bf84a41214afe17b5932eaa1..ad4ec25e5a5bf8c6d15771f4be668b24fcf63beb 100644 (file)
@@ -47,12 +47,30 @@ void* init_xdot (Agraph_t* g)
     xdot* xd = NULL;
 
     if ((p = agget(g, "_draw_")) && p[0]) {
+#ifdef DEBUG
+       if (Verbose) {
+           start_timer();
+       }
+#endif
        xd = parseXDotF (p, NULL, sizeof (exdot_op));
 
        if (!xd) {
            agerr(AGWARN, "Could not parse \"_draw_\" attribute in graph %s\n", agnameof(g));
            agerr(AGPREV, "  \"%s\"\n", p);
        }
+#ifdef DEBUG
+       if (Verbose) {
+           xdot_stats stats;
+           double et = elapsed_sec();
+           statXDot (xd, &stats);
+           fprintf (stderr, "%d ops %.2f sec\n", stats.cnt, et);
+           fprintf (stderr, "%d polygons %d points\n", stats.n_polygon, stats.n_polygon_pts);
+           fprintf (stderr, "%d polylines %d points\n", stats.n_polyline, stats.n_polyline_pts);
+           fprintf (stderr, "%d beziers %d points\n", stats.n_bezier, stats.n_bezier_pts);
+           fprintf (stderr, "%d ellipses\n", stats.n_ellipse);
+           fprintf (stderr, "%d texts\n", stats.n_text);
+       }
+#endif
     }
     return xd;
 }
index 00abdf22337317b81acdfdc00740ae44524bb166..ec54d3f84ce4eb5231a88c4b5705175f171aeed8 100755 (executable)
@@ -37,11 +37,21 @@ static char *parseReal(char *s, double *fp)
 
 static char *parseInt(char *s, int *ip)
 {
-    int r, sz;
+    char* endp;
+
+#ifdef UNUSED
     r = sscanf(s, "%d%n", ip, &sz);
     if (r != 1) return 0;
     else return (s + sz);
+#endif
+
+    *ip = (int)strtol (s, &endp, 10);
+    if (s == endp)
+       return 0;
+    else
+       return endp;
 }
+
 #ifdef UNUSED
 static char *parsePoint(char *s, xdot_point * pp)
 {
@@ -55,11 +65,40 @@ static char *parsePoint(char *s, xdot_point * pp)
 
 static char *parseRect(char *s, xdot_rect * rp)
 {
+    char* endp;
+#ifdef UNUSED
     int r, sz;
     r = sscanf(s, "%lf %lf %lf %lf%n", &(rp->x), &(rp->y), &(rp->w),
               &(rp->h), &sz);
     if (r != 4) return 0;
     else return (s + sz);
+#endif
+
+    rp->x = strtod (s, &endp);
+    if (s == endp)
+       return 0;
+    else
+       s = endp;
+
+    rp->y = strtod (s, &endp);
+    if (s == endp)
+       return 0;
+    else
+       s = endp;
+
+    rp->w = strtod (s, &endp);
+    if (s == endp)
+       return 0;
+    else
+       s = endp;
+
+    rp->y = strtod (s, &endp);
+    if (s == endp)
+       return 0;
+    else
+       s = endp;
+
+    return s;
 }
 
 static char *parsePolyline(char *s, xdot_polyline * pp)
@@ -631,6 +670,48 @@ void freeXDot (xdot * x)
     free(x);
 }
 
+int statXDot (xdot* x, xdot_stats* sp)
+{
+    int i;
+    xdot_op *op;
+    char *base;
+
+    if (!x || !sp) return 1;
+    memset(sp, 0, sizeof(xdot_stats));
+    sp->cnt = x->cnt;
+    base = (char *) (x->ops);
+    for (i = 0; i < x->cnt; i++) {
+       op = (xdot_op *) (base + i * x->sz);
+       switch (op->kind) {
+       case xd_filled_ellipse:
+       case xd_unfilled_ellipse:
+           sp->n_ellipse++;
+           break;
+       case xd_filled_polygon:
+       case xd_unfilled_polygon:
+           sp->n_polygon++;
+           sp->n_polygon_pts += op->u.polygon.cnt;
+           break;
+       case xd_filled_bezier:
+       case xd_unfilled_bezier:
+           sp->n_bezier++;
+           sp->n_bezier_pts += op->u.bezier.cnt;
+           break;
+       case xd_polyline:
+           sp->n_polyline++;
+           sp->n_polyline_pts += op->u.polyline.cnt;
+           break;
+       case xd_text:
+           sp->n_text++;
+           break;
+       default :
+           break;
+       }
+    }
+    
+    return 0;
+}
+
 #if 0
 static void execOp(xdot_op * op, int param)
 {
index fec0f5d19c8c96091194ff1e19a025256752c64a..e2283fc44f0b11ec16db42cba58f8edd4ac9ce0e 100755 (executable)
@@ -101,6 +101,18 @@ typedef struct {
     int flags;
 } xdot;
 
+typedef struct {
+    int cnt;  /* no. of xdot ops */
+    int n_ellipse;
+    int n_polygon;
+    int n_polygon_pts;
+    int n_polyline;
+    int n_polyline_pts;
+    int n_bezier;
+    int n_bezier_pts;
+    int n_text;
+} xdot_stats;
+
 /* ops are indexed by xop_kind */
 extern xdot* parseXDotF (char*, drawfunc_t opfns[], int sz);
 extern xdot* parseXDotFOn (char*, drawfunc_t opfns[], int sz, xdot*);
@@ -108,5 +120,6 @@ extern xdot* parseXDot (char*);
 extern char* sprintXDot (xdot*);
 extern void fprintXDot (FILE*, xdot*);
 extern void freeXDot (xdot*);
+extern int statXDot (xdot*, xdot_stats*);
 
 #endif