add dictionary caching function
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 13 Jun 2024 15:15:50 +0000 (17:15 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 13 Jun 2024 15:15:50 +0000 (17:15 +0200)
function.c
function.h
scrabble.c
todo.txt

index 94d406f17bd07e2d115e4044a40108617b666553..cec4f701d11a6610ec6f68df08122a3cb403aa0c 100644 (file)
@@ -432,11 +432,11 @@ void findwords (word_t *words, play_t *play, play_t *turn)
     }
 }
 
-void checkspelling (word_t *words, char *dict)
+void checkspellingfromfile (word_t *words, char *dict)
 {
-    int i = 0;
     FILE *fd = fopen (dict, "r");
     if (fd) {
+        int i;
         char str[128] = {0};
         while (fscanf (fd, "%s", str) > 0) {
             int stop = 1;
@@ -462,4 +462,70 @@ void checkspelling (word_t *words, char *dict)
     }
 }
 
+#define NBWORDS 4096
+
+char **cachedictionary (char *dict)
+{
+    char **listofwords = NULL;
+    int nbwords = 0;
+    FILE *fd = fopen (dict, "r");
+    if (fd) {
+        char str[128] = {0};
+        listofwords = (char **) calloc (NBWORDS + 1, sizeof (char *));
+        CHECKALLOC (listofwords);
+        while (fscanf (fd, "%s", str) > 0) {
+            listofwords[nbwords] = strdup (str);
+            CHECKALLOC (listofwords[nbwords]);
+            nbwords++;
+            if (nbwords % NBWORDS == 0) {
+                listofwords = (char  **) realloc (listofwords, (nbwords + NBWORDS + 1) * sizeof (char *));
+                CHECKALLOC (listofwords);
+            }
+        }
+        fclose (fd);
+    }
+    return listofwords;
+}
+
+void freecache (char **listofwords)
+{
+    if (listofwords) {
+        char **pt = listofwords;
+        while (*pt) {
+            free (*pt);
+            pt++;
+        }
+    }
+    free (listofwords);
+}
+
+
+void checkspellingfromcache (word_t *words, char **listofwords)
+{
+    if (listofwords) {
+        int i;
+        while (*listofwords) {
+            int stop = 1;
+            for (i = 0; i < words->maxnbwords; i++) {
+                if (words->status[i] == notchecked) {
+                    if (strcmp (words->tab[i], *listofwords) == 0) {
+                        words->status[i] = correct;
+                    } else {
+                        stop = 0;
+                    }
+                }
+            }
+            listofwords++;
+            if (stop) {
+                break;
+            }
+        }
+        for (i = 0; i < words->maxnbwords; i++) {
+            if (words->status[i] == notchecked) {
+                words->status[i] = incorrect;
+            }
+        }
+    }
+}
+
 /* vim: set ts=4 sw=4 et: */
index 1aab1b6a48beef462b2d1b11cd516fdb59894d75..fa27ebbbe13e3336c08931f4b82c5927acfc711f 100644 (file)
@@ -55,7 +55,13 @@ void freeword (word_t *words);
 
 void findwords (word_t *words, play_t *play, play_t *turn);
 
-void checkspelling (word_t *words, char *dict);
+void checkspellingfromfile (word_t *words, char *dict);
+
+char **cachedictionary (char *dict);
+
+void freecache (char **listofwords);
+
+void checkspellingfromcache (word_t *words, char **listofwords);
 
 #endif /* __FUNCTION_H__ */
 
index c289caef84ceafb783ce633ab8fed24faf776a37..d2d1b5a2e139e3a5fb327bd6a0b7f0afa477d73b 100644 (file)
@@ -28,6 +28,7 @@ char *boardname = "15x15-7";
 char *language = "fr";
 int nbplayers = 2;
 char *dict = NULL;
+int cache = 0;
 
 int xoffset = 4;
 int yoffset = 3;
@@ -50,8 +51,9 @@ char *help =
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-b board] [-d dict] [-h] [-l lang] [-n nb] [-v level]\n", progname);
+    fprintf (fd, "usage: %s [-b board] [-c] [-d dict] [-h] [-l lang] [-n nb] [-v level]\n", progname);
     fprintf (fd, " -b: board (%s)\n", boardname);
+    fprintf (fd, " -c: cache dictionary\n");
     fprintf (fd, " -d: dictionary (%s)\n", (dict) ? dict : "none");
     fprintf (fd, " -h: help message\n");
     fprintf (fd, " -l: language (%s)\n", language);
@@ -94,6 +96,9 @@ int main (int argc, char *argv[])
                 return usage (1);
             }
             break;
+        case 'c':
+            cache = 1;
+            break;
         case 'd':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg) {
@@ -157,12 +162,15 @@ int main (int argc, char *argv[])
             return 1;
         }
     }
+    char **listofwords = NULL;
+    if (cache && dict) {
+        listofwords = cachedictionary (dict);
+    }
 
     srand (time (NULL));
 
     play_t *play = initplay (board->xsize, board->ysize);
     VERBOSE (DEBUG, printf ("play: 0x%p\nboard: 0x%p\n", play, board));
-
     draw_t *draw = initdraw (bag);
     draw_t **game = (draw_t **) calloc (nbplayers, sizeof (draw_t *));
     CHECKALLOC (game);
@@ -292,7 +300,11 @@ int main (int argc, char *argv[])
             if (putletter (play, letter, x, y)) {
                 putletter (turn[n], letter, x, y);
                 findwords (words, play, turn[n]);
-                checkspelling (words, dict);
+                if (cache) {
+                    checkspellingfromcache (words, listofwords);
+                } else {
+                    checkspellingfromfile (words, dict);
+                }
                 letter = getnewletter (game[n]);
                 drawwindow (game[n], score[n], xdrawwin[n], ydrawwin[n], 1);
             }
@@ -308,7 +320,11 @@ int main (int argc, char *argv[])
                     removeletter (play, x, y);
                     removeletter (turn[n], x, y);
                     findwords (words, play, turn[n]);
-                    checkspelling (words, dict);
+                    if (cache) {
+                        checkspellingfromcache (words, listofwords);
+                    } else {
+                        checkspellingfromfile (words, dict);
+                    }
                     letter = getnextletter (game[n]);
                     drawwindow (game[n], score[n], xdrawwin[n], ydrawwin[n], 1);
                 }
@@ -345,6 +361,7 @@ int main (int argc, char *argv[])
     free (score);
     freedraw (draw);
     freeword (words);
+    freecache (listofwords);
 
     return 0;
 }
index 5d74d90b465f451b4a03af74eac7dd05f0de6ae5..16b643214b05b12234bf39dc390ce164214c0dd0 100644 (file)
--- a/todo.txt
+++ b/todo.txt
@@ -5,4 +5,4 @@
 = comptage des points
 = déplacement des lettre dans le présentoir
 = score
-* vérification orthograpphe
+= vérification orthograpphe