From 59de397a553df266d3b611b8e4b5c3decc05ff2f Mon Sep 17 00:00:00 2001 From: Tom <25043847+Douile@users.noreply.github.com> Date: Tue, 19 Feb 2019 20:01:20 +0000 Subject: [PATCH] v0.2 Fix segmentation issues, add sigint handler, add support for arguments, now working Also in build.sh moves old file so gdb will not run --- wordament.c | 120 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 90 insertions(+), 30 deletions(-) diff --git a/wordament.c b/wordament.c index 0204896..385c80a 100644 --- a/wordament.c +++ b/wordament.c @@ -1,16 +1,27 @@ #include #include #include +#include // switch to lower case because word list is lower case #define ALPHABET_SIZE 26 #define OFFSET 97 #define WORDLIST "./words/words_3_9.txt" -#define MAXLEN 10 +#define MAXLEN 20 #define NEWLINE '\n' #define ROW 4 typedef enum {false, true} bool; +static void catch_handler(int signo) { + if (signo == SIGINT) { + printf("\nCaught SIGINT exiting...\n"); + exit(EXIT_SUCCESS); + } else if (signo == SIGSEGV) { + printf("\nMemory error...\n"); + exit(EXIT_FAILURE); + } +} + typedef struct Trie_t Trie_t; typedef struct Trie_t { bool end; @@ -85,7 +96,7 @@ char * readline(FILE *file) { c = fgetc(file); line[i] = c; i++; - } while (c != NEWLINE && c != 0); + } while (c != NEWLINE && c != 0 && c != EOF); line[i-1] = 0; size_t size = sizeof(char)*i; line = realloc(line,size); @@ -189,14 +200,14 @@ void set_visited(unsigned int *v,int x,int y,bool visited) { if (visited == true) { *v = *v | n; } else { - *v = ~(~*v | n); + *v = *v & ~n; } } bool has_visited(unsigned int *v,int x,int y) { int i = y*ROW+x; - unsigned int c = 1; - unsigned int r = (c << i) & *v; + unsigned int c = 1 << i; + unsigned int r = c & *v; bool visited = false; if (r == c) { visited = true; @@ -205,15 +216,23 @@ bool has_visited(unsigned int *v,int x,int y) { } Wordlist_t * new_wordlist(char *word) { - Wordlist_t *wordlist = malloc(sizeof(Wordlist_t)); + Wordlist_t *wordlist = NULL; + wordlist = malloc(sizeof(Wordlist_t)); wordlist->word = word; + wordlist->next = NULL; return wordlist; } -Wordlist_t * add_word(Wordlist_t *wordlist,char *word) { +void add_word(Wordlist_t *head,char *word) { Wordlist_t *new_word = new_wordlist(word); - wordlist->next = new_word; - return new_word; + Wordlist_t *node = head; + while (node->next != NULL) { + node = node->next; + if (strcmp(node->word,word) == 0) { + return; + } + } + node->next = new_word; } // Wordlist_t * new_wordlist() { @@ -289,8 +308,8 @@ void find_words(Trie_t *node,char **board,Wordlist_t *wordlist,char *stack,unsig size_t size = strlen(stack); char *word = malloc(sizeof(char)*size); strcpy(word,stack); - printf("%s\n",word); - wordlist = add_word(wordlist,word); + //printf("%s\n",word); + add_word(wordlist,word); } for (int dx=-1;dx<=1;dx++) { int x = ox+dx; @@ -310,50 +329,91 @@ void find_words(Trie_t *node,char **board,Wordlist_t *wordlist,char *stack,unsig int main(int argc,char **argv) { - printf("Loading wordlist\n"); - FILE *file = fopen(WORDLIST,"r"); + signal(SIGINT,catch_handler); + signal(SIGSEGV,catch_handler); + char* wordlist = NULL; + bool debug = false; + bool verboose = false; + int minlen = 0; + for (int i=1;ichildren['a'-OFFSET]->children['s'-OFFSET]->children['s'-OFFSET]->end; - if (ass == true) { - printf("Ass\n"); - } else { - printf("No ass\n"); + if (debug == true) { + bool ass = head->children['a'-OFFSET]->children['s'-OFFSET]->children['s'-OFFSET]->end; + if (ass == true) { + printf("Ass\n"); + } else { + printf("No ass\n"); + } } printf("Loading done\n"); // print_trie(head,0,4); size_t size = sizeof(char*)*ROW*ROW; - printf("Board size: %d\n",size); + if (verboose == true || debug == true) { + printf("Board size: %d\n",size); + } char **board = malloc(size); for (int y=0;ynext; + unsigned int w_count = 0; + while (node != NULL) { + if (minlen > 0) { + if (strlen(node->word) >= minlen) { + printf("%s\n",node->word); + w_count++; + } + } else { + printf("%s\n",node->word); + w_count++; + } node = node->next; - printf("%s\n",node->word); - } while (node->next != NULL); + } + printf("Found %d words\n",w_count); //printf("Found %d words\n",wordlist->size); // for (int i=0;isize;i++) { // printf("%s\n",wordlist->words[i]); // } - return 0; + return EXIT_SUCCESS; }