From: erg Date: Sat, 22 Jan 2011 23:54:34 +0000 (+0000) Subject: Remove all uses of sscanf for efficiency's sake X-Git-Tag: LAST_LIBGRAPH~32^2~1094 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a0dd4c16d128e809ce2254542fa292e4646df575;p=graphviz Remove all uses of sscanf for efficiency's sake --- diff --git a/lib/common/emit.c b/lib/common/emit.c index 5dd3221a7..ad4ec25e5 100644 --- a/lib/common/emit.c +++ b/lib/common/emit.c @@ -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; } diff --git a/lib/xdot/xdot.c b/lib/xdot/xdot.c index 00abdf223..ec54d3f84 100755 --- a/lib/xdot/xdot.c +++ b/lib/xdot/xdot.c @@ -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) { diff --git a/lib/xdot/xdot.h b/lib/xdot/xdot.h index fec0f5d19..e2283fc44 100755 --- a/lib/xdot/xdot.h +++ b/lib/xdot/xdot.h @@ -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