chiark / gitweb /
pcre.c, etc.: Support the PCRE2 library.
[anag] / trackword.c
index 1dae3d16b6fd5d0b2c7da81ced9f5613ab538237..b2e3074c1bdfae043c95dd2dd60322270e36997f 100644 (file)
@@ -1,13 +1,11 @@
 /* -*-c-*-
- *
- * $Id: trackword.c,v 1.1 2001/02/04 17:14:42 mdw Exp $
  *
  * Matches trackwords
  *
  * (c) 2001 Mark Wooding
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Anag: a simple wordgame helper.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Anag 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 Anag; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: trackword.c,v $
- * Revision 1.1  2001/02/04 17:14:42  mdw
- * Initial checkin
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "anag.h"
 
 /*----- Data structures ---------------------------------------------------*/
 
-enum { N, NE, E, SE, S, SW, W, NW, NDIR };
+#define NDIR 9
 
 typedef struct tcell {
   struct tcell *hop[NDIR];
@@ -106,17 +96,18 @@ long isqrt(long a)
 
 static int track(tcell *c, const char *p)
 {
-  int i;
+  tcell **cc;
 
   if (*p++ != c->ch)
     return (0);
   if (!*p)
     return (1);
   c->f = 1;
-  for (i = 0; i < NDIR; i++) {
-    if (!c->hop[i] || c->hop[i]->f)
+
+  for (cc = c->hop; *cc; cc++) {
+    if ((*cc)->f)
       continue;
-    if (track(c->hop[i], p)) {
+    if (track(*cc, p)) {
       c->f = 0;
       return (1);
     }
@@ -150,7 +141,7 @@ node *trackword(const char *const *av)
   unsigned i, j;
   const char *p = av[0], *q, *l;
   size_t sz = strlen(p);
-  tcell *c;
+  tcell *c, **cc, **ccc;
 
   /* --- Work out the dimensions --- */
 
@@ -197,43 +188,48 @@ node *trackword(const char *const *av)
     for (i = 0; i < x; i++) {
       c->ch = *q++;
       c->f = 0;
+      cc = c->hop;
 
 #define NOK (j > 0)
 #define SOK (j < y - 1)
 #define WOK (i > 0)
 #define EOK (i < x - 1)
 
-      c->hop[N] = NOK ? c - x : 0;
-      c->hop[S] = SOK ? c + x : 0;
-      c->hop[W] = WOK ? c - 1 : 0;
-      c->hop[E] = EOK ? c + 1 : 0;
+      if (NOK) *cc++ = c - x;
+      if (SOK) *cc++ = c + x;
+      if (WOK) *cc++ = c - 1;
+      if (EOK) *cc++ = c + 1;
 
-      c->hop[NW] = NOK && WOK ? c - x - 1 : 0;
-      c->hop[NE] = NOK && EOK ? c - x + 1 : 0;
-      c->hop[SW] = SOK && WOK ? c + x - 1 : 0;
-      c->hop[SE] = SOK && EOK ? c + x + 1 : 0;
+      if (NOK && WOK) *cc++ = c - x - 1;
+      if (NOK && EOK) *cc++ = c - x + 1;
+      if (SOK && WOK) *cc++ = c + x - 1;
+      if (SOK && EOK) *cc++ = c + x + 1;
 
 #undef NOK
 #undef SOK
 #undef WOK
 #undef EOK
 
+      *cc++ = 0;
       c++;
     }
   }
   c->ch = 0;
 
-  /* --- Done --- */
+  /* --- Now prune out bogus links --- */
 
-  c = n->c;
-  for (j = 0; j < y; j++) {
-    for (i = 0; i < x; i++) {
-      printf("%c ", c->ch);
-      c++;
+  for (c = n->c; c->ch; c++) {
+    ccc = c->hop;
+    for (cc = c->hop; *cc; cc++) {
+      if (isalpha((unsigned char)(*cc)->ch)) {
+       *ccc++ = *cc;
+      }
     }
-    fputc('\n', stdout);
+    *ccc++ = 0;
   }
 
+  /* --- Done --- */
+
   return (&n->n);
 }