5 * Remember the longest word seen so far
7 * (c) 2005 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 ---------------------------------------------------*/
40 typedef struct node_longest
{
46 /*----- Main code ---------------------------------------------------------*/
48 static void empty(node_longest
*n
)
52 for (l
= n
->l
; l
; l
= ll
) {
61 static void insert(node_longest
*n
, const char *p
, size_t sz
)
66 l
= xmalloc(sizeof(*l
));
67 l
->p
= xmalloc(sz
+ 1);
68 memcpy(l
->p
, p
, sz
+ 1);
74 static int aa_output(void *p
)
79 if (!n
->l
) return (0);
80 for (l
= n
->l
; l
; l
= l
->next
)
85 static int n_longest(node
*nn
, const char *p
, size_t sz
)
87 node_longest
*n
= (node_longest
*)nn
;
96 static int n_shortest(node
*nn
, const char *p
, size_t sz
)
98 node_longest
*n
= (node_longest
*)nn
;
99 if (n
->len
&& sz
> n
->len
)
101 if (!n
->len
|| sz
< n
->len
)
107 static node
*mklongest(int (*nf
)(node
*, const char *, size_t),
110 node_longest
*n
= xmalloc(sizeof(*n
));
115 atend_register(aa
, n
);
119 node
*longest(const char *const *av
)
120 { return mklongest(n_longest
, aa_output
); }
121 node
*shortest(const char *const *av
)
122 { return mklongest(n_shortest
, aa_output
); }
124 /*----- That's all, folks -------------------------------------------------*/