]> git.cworth.org Git - wordgame/blobdiff - dict.c
Allow a new game to be started by pressing Enter after Control-C
[wordgame] / dict.c
diff --git a/dict.c b/dict.c
index f975829c1877c74e1b85001c571fcbccbd2d8ccc..a842fa9dbda1863f6e936957c2a61d31d1e05ada 100644 (file)
--- a/dict.c
+++ b/dict.c
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
  */
 
-/* Portably, schmortability. I want ease of programming. */
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
+#include "dict.h"
+
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
 
-#include "dict.h"
-
 typedef struct _string {
     size_t size;
     char *s;
     size_t len;
 } string_t;
 
-#define TRIE_FLAGS_IS_WORD     (1<<0)
-
 typedef bool_t
 (*trie_predicate_t) (trie_t *trie);
 
 #define TRIE_CHAR_TO_INDEX(c)  (tolower (c) - 'a')
 #define TRIE_INDEX_TO_CHAR(i)  (i + 'a')
 
-void *
+static void *
 xmalloc (size_t size)
 {
     void *res;
@@ -54,7 +48,7 @@ xmalloc (size_t size)
     return res;
 }
 
-void *
+static void *
 xrealloc (void *ptr, size_t size)
 {
     void *res;
@@ -68,7 +62,7 @@ xrealloc (void *ptr, size_t size)
     return res;
 }
 
-void
+static void
 chomp (char *s)
 {
     int len = strlen (s);
@@ -78,7 +72,7 @@ chomp (char *s)
        s[len - 1] = '\0';
 }
 
-void
+static void
 string_init (string_t *string)
 {
     string->size = 0;
@@ -86,7 +80,7 @@ string_init (string_t *string)
     string->len = 0;
 }
 
-void
+static void
 string_fini (string_t *string)
 {
     string->size = 0;
@@ -95,7 +89,7 @@ string_fini (string_t *string)
     string->len = 0;
 }
 
-void
+static void
 string_append_char (string_t *string, char c)
 {
     if (string->size == 0) {
@@ -114,7 +108,7 @@ string_append_char (string_t *string, char c)
     string->s[string->len] = '\0';
 }
 
-void
+static void
 string_chop (string_t *string)
 {
     if (string->len == 0)
@@ -145,7 +139,7 @@ trie_fini (trie_t *trie)
            trie_destroy (trie->next[i]);
 }
 
-trie_t *
+static trie_t *
 trie_create (void)
 {
     trie_t *trie;
@@ -165,26 +159,24 @@ trie_destroy (trie_t *trie)
     free (trie);
 }
 
-void
+static trie_t *
 trie_add (trie_t       *trie,
          const char    *word_chunk)
 {
     char c = word_chunk[0];
     int i;
 
-    if (c == '\0') {
-       trie->flags |= TRIE_FLAGS_IS_WORD;
-       return;
-    }
+    if (c == '\0')
+       return trie;
 
     i = TRIE_CHAR_TO_INDEX (c);
     if (trie->next[i] == NULL)
        trie->next[i] = trie_create ();
 
-    trie_add (trie->next[i], word_chunk + 1);
+    return trie_add (trie->next[i], word_chunk + 1);
 }
 
-trie_t *
+static trie_t *
 trie_find (trie_t      *trie,
           const char   *word_chunk)
 {
@@ -196,7 +188,7 @@ trie_find (trie_t   *trie,
     return trie;
 }
 
-int
+static int
 trie_count (trie_t             *trie,
            trie_predicate_t     predicate)
 {
@@ -216,13 +208,15 @@ trie_count (trie_t                *trie,
     return count;
 }
 
-int
-trie_print (trie_t             *trie,
-           string_t            *string,
-           trie_predicate_t     predicate,
-           int                  length,
-           int                  min_length,
-           int                  max_length)
+static int
+trie_for_each (trie_t          *trie,
+              string_t         *string,
+              trie_predicate_t  predicate,
+              int               length,
+              int               min_length,
+              int               max_length,
+              dict_action_t     action,
+              void             *closure)
 {
     char c;
     int i;
@@ -231,7 +225,8 @@ trie_print (trie_t          *trie,
     if (length >= min_length && (predicate) (trie))
     {
        count = 1;
-       printf ("%s ", string->s);
+
+       action (closure, string->s, &trie->flags);
     }
 
     if (length == max_length)
@@ -245,8 +240,9 @@ trie_print (trie_t          *trie,
        c = TRIE_INDEX_TO_CHAR (i);
 
        string_append_char (string, c);
-       count += trie_print (trie->next[i], string, predicate,
-                            length + 1, min_length, max_length);
+       count += trie_for_each (trie->next[i], string, predicate,
+                               length + 1, min_length, max_length,
+                               action, closure);
        string_chop (string);
     }
 
@@ -269,7 +265,10 @@ void
 dict_add_word (dict_t          *dict,
               const char       *word)
 {
-    trie_add (dict, word);
+    trie_t *trie;
+
+    trie = trie_add (dict, word);
+    trie->flags |= DICT_ENTRY_FLAG_IS_WORD;
 }
 
 void
@@ -376,16 +375,20 @@ dict_count (dict_t                        *dict,
 }
 
 int
-dict_print (dict_t *dict)
+dict_for_each_if (dict_t                       *dict,
+                 dict_action_t                  action,
+                 void                          *closure,
+                 dict_entry_predicate_t         predicate)
+
 {
     int count;
     string_t string;
 
     string_init (&string);
 
-    dict_entry_predicate = NULL;
-    count = trie_print (dict, &string, dict_predicate,
-                       0, 0, -1);
+    dict_entry_predicate = predicate;
+    count = trie_for_each (dict, &string, dict_predicate,
+                          0, 0, -1, action, closure);
 
     string_fini (&string);
 
@@ -393,26 +396,18 @@ dict_print (dict_t *dict)
 }
 
 int
-dict_print_if (dict_t                  *dict,
-              dict_entry_predicate_t    predicate)
+dict_for_each (dict_t          *dict,
+              dict_action_t     action,
+              void             *closure)
 {
-    int count;
-    string_t string;
-
-    string_init (&string);
-
-    dict_entry_predicate = predicate;
-    count = trie_print (dict, &string, dict_predicate,
-                       0, 0, -1);
-
-    string_fini (&string);
-
-    return count;
+    return dict_for_each_if (dict, action, closure, NULL);
 }
 
 int
-dict_print_by_length_if (dict_t                        *dict,
-                        dict_entry_predicate_t  predicate)
+dict_for_each_by_length_if (dict_t                     *dict,
+                           dict_action_t                action,
+                           void                        *closure,
+                           dict_entry_predicate_t       predicate)
 {
     int length, total, words, count = 0;
     string_t string;
@@ -425,12 +420,11 @@ dict_print_by_length_if (dict_t                   *dict,
 
     length = 1;
     do {
-       words = trie_print (dict, &string, dict_predicate,
-                           0, length, length);
-       if (words) {
-           printf ("\n");
+       words = trie_for_each (dict, &string, dict_predicate,
+                              0, length, length,
+                              action, closure);
+       if (words)
            count += words;
-       }
        length++;
     } while (count < total);
 
@@ -438,3 +432,67 @@ dict_print_by_length_if (dict_t                    *dict,
     
     return count;
 }
+
+int
+dict_for_each_by_length (dict_t                *dict,
+                        dict_action_t   action,
+                        void           *closure)
+{
+    return dict_for_each_by_length_if (dict, action, closure, NULL);
+}
+
+static void
+dict_action_print (void *closure, char *word, dict_entry_t *entry)
+{
+    int *length_of_last = closure;
+    int length = strlen (word);
+
+    if (length == *length_of_last)
+       printf(" ");
+    else if (*length_of_last)
+       printf("\n");
+
+    printf ("%s", word);
+
+    *length_of_last = length;
+}
+
+int
+dict_print (dict_t *dict)
+{
+    int length_of_last = 0;
+
+    return dict_for_each (dict,
+                         dict_action_print, &length_of_last);
+}
+
+int
+dict_print_by_length (dict_t *dict)
+{
+    int length_of_last = 0;
+
+    return dict_for_each_by_length (dict,
+                                   dict_action_print, &length_of_last);
+}
+
+int
+dict_print_if (dict_t                  *dict,
+              dict_entry_predicate_t    predicate)
+{
+    int length_of_last = 0;
+
+    return dict_for_each_if (dict,
+                            dict_action_print, &length_of_last,
+                            predicate);
+}
+
+int
+dict_print_by_length_if (dict_t                        *dict,
+                        dict_entry_predicate_t  predicate)
+{
+    int length_of_last = 0;
+
+    return dict_for_each_by_length_if (dict,
+                                      dict_action_print, &length_of_last,
+                                      predicate);
+}