implemented numeric sort (sort -g)

This commit is contained in:
John Beppu 1999-12-23 00:02:49 +00:00
parent 568cb7b45f
commit ee512a3f86
2 changed files with 60 additions and 18 deletions

View File

@ -115,9 +115,9 @@ compare_ascii(const void *a, const void *b)
Line *x, *y; Line *x, *y;
doh = (Line **) a; doh = (Line **) a;
x = (Line *) *doh; x = *doh;
doh = (Line **) b; doh = (Line **) b;
y = (Line *) *doh; y = *doh;
// fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data); // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data);
return strcmp(x->data, y->data); return strcmp(x->data, y->data);
@ -127,7 +127,19 @@ compare_ascii(const void *a, const void *b)
static int static int
compare_numeric(const void *a, const void *b) compare_numeric(const void *a, const void *b)
{ {
return 0; Line **doh;
Line *x, *y;
int xint, yint;
doh = (Line **) a;
x = *doh;
doh = (Line **) b;
y = *doh;
xint = strtoul(x->data, NULL, 10);
yint = strtoul(y->data, NULL, 10);
return (xint - yint);
} }
@ -232,14 +244,20 @@ sort_main(int argc, char **argv)
char opt; char opt;
List list; List list;
Line *l; Line *l;
Compare *compare;
/* default behaviour */ /* init */
compare = compare_ascii;
list_init(&list);
/* parse argv[] */ /* parse argv[] */
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') { if (argv[i][0] == '-') {
opt = argv[i][1]; opt = argv[i][1];
switch (opt) { switch (opt) {
case 'g':
compare = compare_numeric;
break;
case 'h': case 'h':
usage(sort_usage); usage(sort_usage);
break; break;
@ -252,15 +270,12 @@ sort_main(int argc, char **argv)
} }
} }
/* initialize list */
list_init(&list);
/* go through remaining args (if any) */ /* go through remaining args (if any) */
if (i >= argc) { if (i >= argc) {
while ( (l = line_newFromFile(stdin))) { while ( (l = line_newFromFile(stdin))) {
list_insert(&list, l); list_insert(&list, l);
} }
list_sort(&list, compare_ascii); list_sort(&list, compare);
list_writeToFile(&list, stdout); list_writeToFile(&list, stdout);
list_release(&list); list_release(&list);
} else { } else {
@ -271,15 +286,21 @@ sort_main(int argc, char **argv)
exit(0); exit(0);
} }
/* $Id: sort.c,v 1.6 1999/12/22 23:02:12 beppu Exp $ */ /* $Id: sort.c,v 1.7 1999/12/23 00:02:49 beppu Exp $ */
/* /*
* $Log: sort.c,v $ * $Log: sort.c,v $
* Revision 1.7 1999/12/23 00:02:49 beppu
* implemented numeric sort (sort -g)
*
* Revision 1.6 1999/12/22 23:02:12 beppu * Revision 1.6 1999/12/22 23:02:12 beppu
* oops.. qsort(2) misunderstanding on my part. * oops.. qsort(2) misunderstanding on my part.
* it's ok, now. * it's ok, now.
* *
* Revision 1.5 1999/12/22 22:27:01 beppu * Revision 1.5 1999/12/22 22:27:01 beppu
* playing w/ $Log: sort.c,v $ * playing w/ $Log: sort.c,v $
* playing w/ Revision 1.7 1999/12/23 00:02:49 beppu
* playing w/ implemented numeric sort (sort -g)
* playing w/
* playing w/ Revision 1.6 1999/12/22 23:02:12 beppu * playing w/ Revision 1.6 1999/12/22 23:02:12 beppu
* playing w/ oops.. qsort(2) misunderstanding on my part. * playing w/ oops.. qsort(2) misunderstanding on my part.
* playing w/ it's ok, now. * playing w/ it's ok, now.

39
sort.c
View File

@ -115,9 +115,9 @@ compare_ascii(const void *a, const void *b)
Line *x, *y; Line *x, *y;
doh = (Line **) a; doh = (Line **) a;
x = (Line *) *doh; x = *doh;
doh = (Line **) b; doh = (Line **) b;
y = (Line *) *doh; y = *doh;
// fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data); // fprintf(stdout, "> %p: %s< %p: %s", x, x->data, y, y->data);
return strcmp(x->data, y->data); return strcmp(x->data, y->data);
@ -127,7 +127,19 @@ compare_ascii(const void *a, const void *b)
static int static int
compare_numeric(const void *a, const void *b) compare_numeric(const void *a, const void *b)
{ {
return 0; Line **doh;
Line *x, *y;
int xint, yint;
doh = (Line **) a;
x = *doh;
doh = (Line **) b;
y = *doh;
xint = strtoul(x->data, NULL, 10);
yint = strtoul(y->data, NULL, 10);
return (xint - yint);
} }
@ -232,14 +244,20 @@ sort_main(int argc, char **argv)
char opt; char opt;
List list; List list;
Line *l; Line *l;
Compare *compare;
/* default behaviour */ /* init */
compare = compare_ascii;
list_init(&list);
/* parse argv[] */ /* parse argv[] */
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') { if (argv[i][0] == '-') {
opt = argv[i][1]; opt = argv[i][1];
switch (opt) { switch (opt) {
case 'g':
compare = compare_numeric;
break;
case 'h': case 'h':
usage(sort_usage); usage(sort_usage);
break; break;
@ -252,15 +270,12 @@ sort_main(int argc, char **argv)
} }
} }
/* initialize list */
list_init(&list);
/* go through remaining args (if any) */ /* go through remaining args (if any) */
if (i >= argc) { if (i >= argc) {
while ( (l = line_newFromFile(stdin))) { while ( (l = line_newFromFile(stdin))) {
list_insert(&list, l); list_insert(&list, l);
} }
list_sort(&list, compare_ascii); list_sort(&list, compare);
list_writeToFile(&list, stdout); list_writeToFile(&list, stdout);
list_release(&list); list_release(&list);
} else { } else {
@ -271,15 +286,21 @@ sort_main(int argc, char **argv)
exit(0); exit(0);
} }
/* $Id: sort.c,v 1.6 1999/12/22 23:02:12 beppu Exp $ */ /* $Id: sort.c,v 1.7 1999/12/23 00:02:49 beppu Exp $ */
/* /*
* $Log: sort.c,v $ * $Log: sort.c,v $
* Revision 1.7 1999/12/23 00:02:49 beppu
* implemented numeric sort (sort -g)
*
* Revision 1.6 1999/12/22 23:02:12 beppu * Revision 1.6 1999/12/22 23:02:12 beppu
* oops.. qsort(2) misunderstanding on my part. * oops.. qsort(2) misunderstanding on my part.
* it's ok, now. * it's ok, now.
* *
* Revision 1.5 1999/12/22 22:27:01 beppu * Revision 1.5 1999/12/22 22:27:01 beppu
* playing w/ $Log: sort.c,v $ * playing w/ $Log: sort.c,v $
* playing w/ Revision 1.7 1999/12/23 00:02:49 beppu
* playing w/ implemented numeric sort (sort -g)
* playing w/
* playing w/ Revision 1.6 1999/12/22 23:02:12 beppu * playing w/ Revision 1.6 1999/12/22 23:02:12 beppu
* playing w/ oops.. qsort(2) misunderstanding on my part. * playing w/ oops.. qsort(2) misunderstanding on my part.
* playing w/ it's ok, now. * playing w/ it's ok, now.