}
}
-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;
}
}
+#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: */
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__ */
char *language = "fr";
int nbplayers = 2;
char *dict = NULL;
+int cache = 0;
int xoffset = 4;
int yoffset = 3;
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);
return usage (1);
}
break;
+ case 'c':
+ cache = 1;
+ break;
case 'd':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg) {
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);
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);
}
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);
}
free (score);
freedraw (draw);
freeword (words);
+ freecache (listofwords);
return 0;
}