From 420987a54428750ed4ea85a6e24cae6494a9c094 Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Thu, 7 Apr 2016 14:27:27 -0400 Subject: [PATCH] Getting rid of that ugly switch statement modified: src/nms.c --- src/nms.c | 59 ++++++++++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/src/nms.c b/src/nms.c index fc0b5dd..8212c3d 100644 --- a/src/nms.c +++ b/src/nms.c @@ -29,43 +29,32 @@ int main(void) { int c, x = 1, y = 1; bool first = true; while ((c = getchar()) != EOF) { - switch (c) { - case '\n': - ++y; - x = 1; - break; - case ' ': - case '\t': - case '\f': - case '\r': - case '\v': - ++x; - break; - default: - if (first) { - start.source = c; - start.row = y; - start.col = x; - start.next = (struct winpos *) 0; - first = false; - ++x; - } else { - // Allocate space for the new struct in our linked list - // and point *next to it. - list_pointer->next = malloc(sizeof(struct winpos)); + if (c == NEWLINE) { + ++y; + x = 1; + } else if (isspace(c)) { + ++x; + } else if (first) { + start.source = c; + start.row = y; + start.col = x; + start.next = (struct winpos *) 0; + first = false; + ++x; + } else { + // Allocate space for the new struct in our linked list + // and point *next to it. + list_pointer->next = malloc(sizeof(struct winpos)); - // Now let's point list_pointer to the next structure - // and populate it. - list_pointer = list_pointer->next; - list_pointer->source = c; - list_pointer->row = y; - list_pointer->col = x; - list_pointer->next = (struct winpos *) 0; + // Now let's point list_pointer to the next structure + // and populate it. + list_pointer = list_pointer->next; + list_pointer->source = c; + list_pointer->row = y; + list_pointer->col = x; + list_pointer->next = (struct winpos *) 0; - ++x; - } - break; - + ++x; } }