1879e4870b5754618381eb836dade5e5d634d07f
3 * Remember the longest word seen so far
5 * (c) 2005 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Anag: a simple wordgame helper.
12 * Anag is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Anag is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Anag; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Data structures ---------------------------------------------------*/
38 typedef struct node_longest
{
44 /*----- Main code ---------------------------------------------------------*/
46 static void empty(node_longest
*n
)
50 for (l
= n
->l
; l
; l
= ll
) {
59 static void insert(node_longest
*n
, const char *p
, size_t sz
)
64 l
= xmalloc(sizeof(*l
));
65 l
->p
= xmalloc(sz
+ 1);
66 memcpy(l
->p
, p
, sz
+ 1);
72 static int aa_output(void *p
)
77 if (!n
->l
) return (0);
78 for (l
= n
->l
; l
; l
= l
->next
)
83 static int n_longest(node
*nn
, const char *p
, size_t sz
)
85 node_longest
*n
= (node_longest
*)nn
;
94 static int n_shortest(node
*nn
, const char *p
, size_t sz
)
96 node_longest
*n
= (node_longest
*)nn
;
97 if (n
->len
&& sz
> n
->len
)
99 if (!n
->len
|| sz
< n
->len
)
105 static node
*mklongest(int (*nf
)(node
*, const char *, size_t),
108 node_longest
*n
= xmalloc(sizeof(*n
));
113 atend_register(aa
, n
);
117 node
*longest(const char *const *av
)
118 { return mklongest(n_longest
, aa_output
); }
119 node
*shortest(const char *const *av
)
120 { return mklongest(n_shortest
, aa_output
); }
122 /*----- That's all, folks -------------------------------------------------*/