From 5238f976bfc7c750cdc737a68079a89250cbd274 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 17 Mar 2016 23:01:09 +1300 Subject: Dynamic allocation of lines Massive refactor including implementing my own read_line function --- sort.c | 83 ++++++++++++++++++++++++++++++++++++++++++++---------------------- sort.h | 22 ++++++++++++++++++ 2 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 sort.h diff --git a/sort.c b/sort.c index c2c55fb..f8a3dcc 100644 --- a/sort.c +++ b/sort.c @@ -1,43 +1,72 @@ -#include -#include -#include +#include "sort.h" -#define MAX_LINE_LENGTH (2 << 8) -#define LINES_VECTOR_SIZE_INITIAL (2 << 6) -#define LINES_VECTOR_SIZE_INCREMENT (2 << 4) +int main(void) { + vec *lines; + unsigned long i; + char *line; -static int qsort_strcmp(const void *p1, const void *p2); + lines = vec_init(); -int main(void) -{ - char **lv; - unsigned long la, lc, li; + while ((line = read_line(stdin)) != NULL) { + vec_add(lines, line); + } + + qsort(lines->data, lines->count, sizeof(char *), qsort_strcmp); + + for (i = 0; i < lines->count; i++) { + fputs(lines->data[i], stdout); + } + + exit(EXIT_SUCCESS); +} + +char *read_line(FILE* stream) { + char *buf; + char *line; + unsigned long line_length; + unsigned long line_size; - la = LINES_VECTOR_SIZE_INITIAL; - lv = (char **) malloc(la * sizeof(char *)); + line_size = BUF_SIZE; + line = malloc(line_size * sizeof(char *)); + strncpy(line, "", 1); - for (lc = 0 ; ; lc++) { - if (lc == la) { - la += LINES_VECTOR_SIZE_INCREMENT; - lv = (char **) realloc(lv, la * sizeof(char *)); + buf = malloc(BUF_SIZE); + for (;;) { + if (fgets(buf, BUF_SIZE, stream) == NULL) { + free(buf); + return NULL; } - lv[lc] = (char *) malloc(MAX_LINE_LENGTH * sizeof(char)); - if ((fgets(lv[lc], MAX_LINE_LENGTH, stdin)) == NULL) { - break; + line = strncat(line, buf, BUF_SIZE); + line_length = strlen(line); + if (line[line_length - 1] == '\n') { + free(buf); + return line; + } else { + line_size <<= 1; + line = realloc(line, line_size * sizeof(char *)); } } +} - qsort(lv, lc, sizeof(char *), qsort_strcmp); +vec *vec_init(void) { + vec *v; + v = malloc(sizeof(vec)); + v->count = 0; + v->size = VEC_SIZE; + v->data = malloc(v->size * sizeof(char *)); + return v; +} - for (li = 0; li < lc; li++) { - fputs(lv[li], stdout); +void vec_add(vec *v, char *s) { + if (v->count == v->size) { + v->size <<= 1; + v->data = realloc(v->data, v->size * sizeof(char *)); } - - exit(EXIT_SUCCESS); + v->data[v->count++] = s; + return; } -static int qsort_strcmp(const void *p1, const void *p2) -{ +static int qsort_strcmp(const void *p1, const void *p2) { return strcmp(* (char * const *) p1, * (char * const *) p2); } diff --git a/sort.h b/sort.h new file mode 100644 index 0000000..9b499c8 --- /dev/null +++ b/sort.h @@ -0,0 +1,22 @@ +#ifndef __SORT_H +#define __SORT_H + +#include +#include +#include + +#define VEC_SIZE 64 +#define BUF_SIZE 64 + +typedef struct { + unsigned long count, size; + char **data; +} vec; + +char *read_line(FILE* stream); +vec *vec_init(void); +void vec_add(vec *v, char *s); +static int qsort_strcmp(const void *p1, const void *p2); + +#endif + -- cgit v1.2.3