diff options
author | Connor Lane Smith <cls@lubutu.com> | 2011-05-14 17:46:20 +0100 |
---|---|---|
committer | Connor Lane Smith <cls@lubutu.com> | 2011-05-14 17:46:20 +0100 |
commit | 3c067598fc3070243ae0c784bfea906dce0a6fbb (patch) | |
tree | 9f58b13285224acd0233193cc2ff33fe8ced2930 | |
parent | 86019c58555b5d9514cfe2eba14248588a812f16 (diff) |
use array for items
-rw-r--r-- | dmenu.c | 20 |
1 files changed, 10 insertions, 10 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* See LICENSE file for copyright and license details. */ | 1 | /* See LICENSE file for copynext and license details. */ |
2 | #include <ctype.h> | 2 | #include <ctype.h> |
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
@@ -19,8 +19,7 @@ | |||
19 | typedef struct Item Item; | 19 | typedef struct Item Item; |
20 | struct Item { | 20 | struct Item { |
21 | char *text; | 21 | char *text; |
22 | Item *next; /* traverses all items */ | 22 | Item *left, *right; |
23 | Item *left, *right; /* traverses matching items */ | ||
24 | }; | 23 | }; |
25 | 24 | ||
26 | static void appenditem(Item *item, Item **list, Item **last); | 25 | static void appenditem(Item *item, Item **list, Item **last); |
@@ -386,7 +385,7 @@ match(void) { | |||
386 | 385 | ||
387 | len = strlen(text); | 386 | len = strlen(text); |
388 | matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL; | 387 | matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL; |
389 | for(item = items; item; item = item->next) | 388 | for(item = items; item && item->text; item++) |
390 | if(!fstrncmp(text, item->text, len + 1)) | 389 | if(!fstrncmp(text, item->text, len + 1)) |
391 | appenditem(item, &lexact, &exactend); | 390 | appenditem(item, &lexact, &exactend); |
392 | else if(!fstrncmp(text, item->text, len)) | 391 | else if(!fstrncmp(text, item->text, len)) |
@@ -445,16 +444,17 @@ paste(void) { | |||
445 | void | 444 | void |
446 | readstdin(void) { | 445 | readstdin(void) { |
447 | char buf[sizeof text], *p; | 446 | char buf[sizeof text], *p; |
448 | Item *item, **end; | 447 | size_t i, size = 0; |
449 | 448 | ||
450 | for(end = &items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) { | 449 | for(i = 0; fgets(buf, sizeof buf, stdin); items[++i].text = NULL) { |
450 | if(i+1 == size / sizeof *items || !items) | ||
451 | if(!(items = realloc(items, (size += BUFSIZ)))) | ||
452 | eprintf("cannot realloc %u bytes:", size); | ||
451 | if((p = strchr(buf, '\n'))) | 453 | if((p = strchr(buf, '\n'))) |
452 | *p = '\0'; | 454 | *p = '\0'; |
453 | if(!(item = calloc(1, sizeof *item))) | 455 | if(!(items[i].text = strdup(buf))) |
454 | eprintf("cannot malloc %u bytes:", sizeof *item); | ||
455 | if(!(item->text = strdup(buf))) | ||
456 | eprintf("cannot strdup %u bytes:", strlen(buf)+1); | 456 | eprintf("cannot strdup %u bytes:", strlen(buf)+1); |
457 | inputw = MAX(inputw, textw(dc, item->text)); | 457 | inputw = MAX(inputw, textw(dc, items[i].text)); |
458 | } | 458 | } |
459 | } | 459 | } |
460 | 460 | ||