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
) puts(l
->p
);
82 static int n_longest(node
*nn
, const char *p
, size_t sz
)
84 node_longest
*n
= (node_longest
*)nn
;
85 if (sz
< n
->len
) return (0);
86 if (sz
> n
->len
) empty(n
);
91 static int n_shortest(node
*nn
, const char *p
, size_t sz
)
93 node_longest
*n
= (node_longest
*)nn
;
94 if (n
->len
&& sz
> n
->len
) return (0);
95 if (!n
->len
|| sz
< n
->len
) empty(n
);
100 static node
*mklongest(int (*nf
)(node
*, const char *, size_t),
103 node_longest
*n
= xmalloc(sizeof(*n
));
108 atend_register(aa
, n
);
112 node
*longest(const char *const *av
)
113 { return mklongest(n_longest
, aa_output
); }
114 node
*shortest(const char *const *av
)
115 { return mklongest(n_shortest
, aa_output
); }
117 /*----- That's all, folks -------------------------------------------------*/