the base is nearly done.
need to implement various comparison functions, now.
diff --git a/sort.c b/sort.c
index f3f9fca..9651526 100644
--- a/sort.c
+++ b/sort.c
@@ -32,23 +32,26 @@
"Usage: sort [OPTION]... [FILE]...\n\n"
;
-/* structs ________________________________________________________________ */
+/* typedefs _______________________________________________________________ */
/* line node */
-typedef struct {
- char *data; /* line data */
- struct Line *next; /* pointer to next line node */
+typedef struct Line {
+ char *data; /* line data */
+ struct Line *next; /* pointer to next line node */
} Line;
/* singly-linked list of lines */
typedef struct {
- int len; /* number of Lines */
- Line *sorted; /* array fed to qsort */
+ int len; /* number of Lines */
+ Line **sorted; /* array fed to qsort */
- Line *head; /* head of List */
- Line *current /* current Line */
+ Line *head; /* head of List */
+ Line *current; /* current Line */
} List;
+/* comparison function */
+typedef int (Compare)(const void *, const void *);
+
/* methods ________________________________________________________________ */
@@ -105,10 +108,16 @@
/* Comparison */
static int
-compare_ascii(const void *, const void *);
+compare_ascii(const void *a, const void *b)
+{
+ return 0;
+}
static int
-compare_numeric(const void *, const void *);
+compare_numeric(const void *a, const void *b)
+{
+ return 0;
+}
/* List */
@@ -125,7 +134,7 @@
}
/* for simplicity, the List gains ownership of the line */
-static void
+static List *
list_insert(List *self, Line *line)
{
if (line == NULL) { return NULL; }
@@ -137,26 +146,54 @@
/* all subsequent insertions */
} else {
- self->current->next = line;
+ /* <?> the following cast shouldn't be necessary */
+ self->current->next = line;
self->current = line;
}
self->len++;
return self;
}
-/* */
+/* order the list according to compare() */
static List *
-list_sort(List *self);
+list_sort(List *self, Compare *compare)
+{
+ int i;
+ Line *line;
+
+ /* mallocate array of Line*s */
+ self->sorted = (Line **) malloc(self->len * sizeof(Line*));
+ if (self->sorted == NULL) { return NULL; }
+
+ /* fill array w/ List's contents */
+ i = 0;
+ line = self->head;
+ while (line) {
+ self->sorted[i++] = line;
+ line = line->next;
+ }
+
+ /* apply qsort */
+ qsort(self->sorted, sizeof(Line*), self->len, compare);
+ return self;
+}
/* precondition: list must be sorted */
static List *
list_writeToFile(List *self, FILE* dst)
{
+ int i;
+ Line **line = self->sorted;
+
if (self->sorted == NULL) { return NULL; }
+ for (i = 0; i < self->len; i++) {
+ fprintf(dst, "%s", line[i]->data);
+ }
+ return self;
}
/* deallocate */
-static List *
+static void
list_release(List *self)
{
Line *i;
@@ -166,9 +203,8 @@
while (i) {
die = i;
i = die->next;
- line_delete(die);
+ line_release(die);
}
- return self; /* bad poetry? */
}
@@ -182,8 +218,10 @@
int
sort_main(int argc, char **argv)
{
- int i;
- char opt;
+ int i;
+ char opt;
+ List list;
+ Line *l;
/* default behaviour */
@@ -204,9 +242,17 @@
}
}
+ /* initialize list */
+ list_init(&list);
+
/* go through remaining args (if any) */
if (i >= argc) {
-
+ while ( (l = line_newFromFile(stdin))) {
+ list_insert(&list, l);
+ }
+ list_sort(&list, compare_ascii);
+ list_writeToFile(&list, stdout);
+ list_release(&list);
} else {
for ( ; i < argc; i++) {
}
@@ -215,4 +261,9 @@
exit(0);
}
-/* $Id: sort.c,v 1.3 1999/12/22 17:57:31 beppu Exp $ */
+/* $Id: sort.c,v 1.4 1999/12/22 22:24:52 beppu Exp $ */
+/* $Log: sort.c,v $
+ * Revision 1.4 1999/12/22 22:24:52 beppu
+ * the base is nearly done.
+ * need to implement various comparison functions, now.
+ * */