]> thyrsus.com Git - comparator.git/commitdiff
Sort the output.
authorEric S. Raymond <esr@thyrsus.com>
Tue, 2 Sep 2003 07:27:12 +0000 (07:27 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Tue, 2 Sep 2003 07:27:12 +0000 (07:27 +0000)
report.c

index 578640d182570cb5386a8af42b52c2c8ebc98193..4d27173431899ffcebe3d31ebb38c2936f8fd1dc 100644 (file)
--- a/report.c
+++ b/report.c
@@ -450,12 +450,27 @@ int sortchunk(void *a, void *b)
                      ((struct sorthash_t *)b)->file));
 }
 
+int sortmatch(void *a, void *b)
+/* sort by file and first line */
+{
+    struct range_t *s = ((struct match_t *)a)->matches;
+    struct range_t *t = ((struct match_t *)b)->matches;
+
+    int cmp = strcmp(s->file, t->file);
+    if (cmp)
+       return(cmp);
+    else
+       return(s->start - t->start);
+
+    return(0);
+}
+
 main(int argc, char *argv[])
 {
     extern char        *optarg;        /* set by getopt */
     extern int optind;         /* set by getopt */
-    int        status;
-    struct match_t *hitlist;
+    int        i, status, matchcount;
+    struct match_t *hitlist, *sorted, *match;
 
     while ((status = getopt(argc, argv, "h")) != EOF)
     {
@@ -492,19 +507,31 @@ main(int argc, char *argv[])
     printf("Shred-Size: %d\n", shiflist[0].shred_size);
     puts("%%");
 
-    for (; hitlist->next; hitlist = hitlist->next)
-       if (hitlist->matches)
-       {
-           int i;
+    /* we go through a little extra effort to emit a sorted list */
+    matchcount = 0;
+    for (match = hitlist; match->next; match = match->next)
+       if (match->matches)
+           matchcount++;
 
-           for (i=0; i < hitlist->nmatches; i++)
-           {
-               struct range_t  *rp = hitlist->matches+i;
+    sorted = (struct match_t *)calloc(sizeof(struct match_t), matchcount);
+    i = 0;
+    for (match = hitlist; match->next; match = match->next)
+       if (match->matches)
+           sorted[i++] = *match;
+    qsort(sorted, matchcount, sizeof(struct match_t), sortmatch);
 
-               printf("%s:%d:%d\n",  rp->file, rp->start, rp->end);
-           }
-           printf("-\n");
+    for (match = sorted; match < sorted + matchcount; match++)
+    {
+       int     i;
+
+       for (i=0; i < match->nmatches; i++)
+       {
+           struct range_t      *rp = match->matches+i;
+
+           printf("%s:%d:%d\n",  rp->file, rp->start, rp->end);
        }
+       printf("-\n");
+    }
 
     exit(0);
 }