8d85745d71bbe350dc4e4b827b8d74d667a88692
3 * $Id: trackword.c,v 1.4 2004/04/08 01:36:19 mdw Exp $
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Anag: a simple wordgame helper.
14 * Anag is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Anag is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Anag; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Data structures ---------------------------------------------------*/
37 typedef struct tcell
{
38 struct tcell
*hop
[NDIR
];
43 typedef struct node_track
{
50 /*----- Utility functions -------------------------------------------------*/
54 * Arguments: @long a@ = the target value
56 * Returns: %$\lfloor \sqrt{a} \rfloor$%
58 * Use: Computes an integer square root. This algorithm is
59 * Newton-Raphson with integers. I've stolen this more-or-less
60 * wholesale fom the multiprecision integer square-root function
68 /* --- Initial guess is half the input length --- */
70 for (i
= q
= a
; q
; q
>>= 2)
73 /* --- Do the main series iteration --- */
77 if (!q
|| (q
< 0 && -q
<= 2 * i
))
87 /*----- Main code ---------------------------------------------------------*/
91 * Arguments: @tcell *c@ = pointer to start cell
92 * @const char *p@ = string to match
94 * Returns: Nonzero for a match, zero if no match was found.
96 * Use: Searches for a match in a trackword.
99 static int track(tcell
*c
, const char *p
)
109 for (cc
= c
->hop
; *cc
; cc
++) {
121 /* --- Node matcher --- */
123 static int n_track(node
*nn
, const char *p
, size_t sz
)
125 node_track
*n
= (node_track
*)nn
;
130 for (c
= n
->c
; c
->ch
; c
++) {
137 /* --- Node creation --- */
139 node
*trackword(const char *const *av
)
144 const char *p
= av
[0], *q
, *l
;
145 size_t sz
= strlen(p
);
146 tcell
*c
, **cc
, **ccc
;
148 /* --- Work out the dimensions --- */
154 die("bad trackword `%s': not square, and no line markers", p
);
159 die("bad trackword `%s': no columns", p
);
170 die("bad trackword `%s': inconsistent line lengths", p
);
176 die("bad trackword `%s': no rows", p
);
178 /* --- Build the match node --- */
180 n
= xmalloc(sizeof(*n
));
183 n
->c
= xmalloc((x
* y
+ 1) * sizeof(tcell
));
187 for (j
= 0; j
< y
; j
++) {
190 for (i
= 0; i
< x
; i
++) {
196 #define SOK (j < y - 1)
198 #define EOK (i < x - 1)
200 if (NOK
) *cc
++ = c
- x
;
201 if (SOK
) *cc
++ = c
+ x
;
202 if (WOK
) *cc
++ = c
- 1;
203 if (EOK
) *cc
++ = c
+ 1;
205 if (NOK
&& WOK
) *cc
++ = c
- x
- 1;
206 if (NOK
&& EOK
) *cc
++ = c
- x
+ 1;
207 if (SOK
&& WOK
) *cc
++ = c
+ x
- 1;
208 if (SOK
&& EOK
) *cc
++ = c
+ x
+ 1;
221 /* --- Now prune out bogus links --- */
223 for (c
= n
->c
; c
->ch
; c
++) {
225 for (cc
= c
->hop
; *cc
; cc
++) {
226 if (isalpha((unsigned char)(*cc
)->ch
)) {
238 /*----- That's all, folks -------------------------------------------------*/