]> git.cworth.org Git - wordgame/commitdiff
Hide implementation details of dict in dict-impl.h. Add automatic dependency computat...
authorCarl Worth <cworth@raht.cworth.org>
Thu, 21 Sep 2006 06:29:22 +0000 (23:29 -0700)
committerCarl Worth <cworth@raht.cworth.org>
Thu, 21 Sep 2006 06:35:57 +0000 (23:35 -0700)
.gitignore
Makefile
dict-impl.h [new file with mode: 0644]
dict.c
dict.h
grid.c

index e572698aa60e7c48496abc093673049acd1e5ff1..6793c73895b9720b2ca5d75f1380e52f95d751dc 100644 (file)
@@ -1,3 +1,4 @@
+Makefile.dep
 grid
 *.o
 *~
index 651c1efa94d4f4f8a440eb645b153d8e5bf5c564..8d8e5a5e36dace4e4d7b619037d430184409adff 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,18 +1,20 @@
 WGCFLAGS=-Wall -Wextra -Wno-unused-parameter
 
 PROGRAMS=grid
+all: $(PROGRAMS)
 
 LIBRARY=dict.o
 
-all: $(PROGRAMS)
+grid: grid.o $(LIBRARY)
+       $(CC) $(CFLAGS) $(WGCFLAGS) $(LDFLAGS) -lreadline -lm -o $@ $^
 
 %.o: %.c
-       $(CC) $(CFLAGS) $(WGCFLAGS) -c -o $@ $^
+       $(CC) $(CFLAGS) $(WGCFLAGS) -c -o $@ $<
 
-%.c: dict.h
-
-grid: grid.o $(LIBRARY)
-       $(CC) $(CFLAGS) $(WGCFLAGS) $(LDFLAGS) -lreadline -lm -o $@ $^
+Makefile.dep: *.c
+       $(CC) -M $(CPPFLAGS) $^ > $@
+-include Makefile.dep
 
+.PHONY: clean
 clean:
-       rm -f $(PROGRAMS) *.o
+       rm -f $(PROGRAMS) *.o Makefile.dep
diff --git a/dict-impl.h b/dict-impl.h
new file mode 100644 (file)
index 0000000..2c5514d
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright © 2006 Carl Worth
+ *
+ * This program is free software; you can redistribute it and\/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
+ */
+
+#ifndef _DICT_IMPL_H_
+#define _DICT_IMPL_H_
+
+#include <stdint.h>
+
+#ifndef FALSE
+# define FALSE 0
+#endif
+
+#ifndef TRUE
+# define TRUE 1
+#endif
+
+typedef int bool_t;
+
+typedef struct _string {
+    size_t size;
+    char *s;
+    size_t len;
+} string_t;
+
+void
+string_init (string_t *string);
+
+void
+string_fini (string_t *string);
+
+void
+string_append_char (string_t *string, char c);
+
+void
+string_chop (string_t *string);
+
+void
+chomp (char *s);
+
+#define TRIE_FLAGS_IS_WORD     (1<<0)
+#define TRIE_FLAGS_SEEN                (1<<1)
+
+typedef struct _trie {
+    uint32_t flags;
+    struct _trie *next[26];
+} trie_t;
+
+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')
+
+trie_t *
+trie_create (void);
+
+void
+trie_add (trie_t       *trie,
+         const char    *word_chunk);
+
+trie_t *
+trie_find (trie_t      *trie,
+          const char   *word_chunk);
+
+int
+trie_print_seen (trie_t *trie, string_t *word);
+
+int
+trie_print_unseen (trie_t *trie, string_t *word);
+
+struct _dict {
+    trie_t *trie;
+};
+
+#endif /* _DICT_IMPL_H_ */
diff --git a/dict.c b/dict.c
index 98c9476ce08ba09c99043d1c770249e8a3b929ac..a3f088f0e084277416ce6ba3bd754cdee5abfacb 100644 (file)
--- a/dict.c
+++ b/dict.c
@@ -227,8 +227,14 @@ trie_print_unseen (trie_t *trie, string_t *word)
 }
 
 void
-dict_init (dict_t      *dict,
-          const char   *filename)
+dict_init (dict_t      *dict)
+{
+    dict->trie = trie_create ();
+}
+
+void
+dict_add_words_from_file (dict_t       *dict,
+                         const char    *filename)
 {
     FILE *file;
     char *line = NULL;
@@ -242,8 +248,6 @@ dict_init (dict_t   *dict,
        exit (1);
     }
 
-    dict->trie = trie_create ();
-
     while (1) {
        bytes_read = getline (&line, &line_size, file);
        if (bytes_read == -1)
diff --git a/dict.h b/dict.h
index 4d4885c7e9423651b592d85e1c6e28cc4c9b1f31..16a345cf0a7f970f78799be533a539f2c37a688b 100644 (file)
--- a/dict.h
+++ b/dict.h
 #ifndef _DICT_H_
 #define _DICT_H_
 
-#include <stdint.h>
+#include "dict-impl.h"
 
-#ifndef FALSE
-# define FALSE 0
-#endif
-
-#ifndef TRUE
-# define TRUE 1
-#endif
-
-typedef int bool_t;
-
-typedef struct _string {
-    size_t size;
-    char *s;
-    size_t len;
-} string_t;
+/* Only looks opaque. Perfectly stack-allocatable */
+typedef struct _dict dict_t;
 
 void
-string_init (string_t *string);
+dict_init (dict_t *dict);
 
 void
-string_fini (string_t *string);
-
-void
-string_append_char (string_t *string, char c);
-
-void
-string_chop (string_t *string);
-
-void
-chomp (char *s);
-
-#define TRIE_FLAGS_IS_WORD     (1<<0)
-#define TRIE_FLAGS_SEEN                (1<<1)
-
-typedef struct _trie {
-    uint32_t flags;
-    struct _trie *next[26];
-} trie_t;
-
-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')
-
-trie_t *
-trie_create (void);
-
-void
-trie_add (trie_t       *trie,
-         const char    *word_chunk);
-
-trie_t *
-trie_find (trie_t      *trie,
-          const char   *word_chunk);
-
-int
-trie_print_seen (trie_t *trie, string_t *word);
-
-int
-trie_print_unseen (trie_t *trie, string_t *word);
-
-typedef struct _dict {
-    trie_t *trie;
-} dict_t;
-
+dict_add_word (dict_t          *dict,
+              const char       *word);
 void
-dict_init (dict_t      *dict,
-          const char   *filename);
+dict_add_words_from_file (dict_t       *dict,
+                         const char    *filename);
 
 void
 dict_fini (dict_t *dict);
diff --git a/grid.c b/grid.c
index b9c307bafd50e7a2e49c8b4653802de8a5ec6686..78c6c56d1370564c5165541a37f5be2192346174 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -176,7 +176,8 @@ main (void)
     gettimeofday (&tv, NULL);
     srand (tv.tv_sec ^ tv.tv_usec);
 
-    dict_init (&dict, "words.txt");
+    dict_init (&dict);
+    dict_add_words_from_file (&dict, "words.txt");
     board_init (&board);
 
     solution = trie_create ();