Fix maintainer address.
[anag] / longest.c
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Remember the longest word seen so far
6 *
7 * (c) 2005 Mark Wooding
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Anag: a simple wordgame helper.
13 *
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.
18 *
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.
23 *
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.
27 */
28
29 /*----- Header files ------------------------------------------------------*/
30
31 #include "anag.h"
32
33 /*----- Data structures ---------------------------------------------------*/
34
35 typedef struct link {
36 struct link *next;
37 char *p;
38 } link;
39
40 typedef struct node_longest {
41 node n;
42 size_t len;
43 link *l, **lt;
44 } node_longest;
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 static void empty(node_longest *n)
49 {
50 link *l, *ll;
51
52 for (l = n->l; l; l = ll) {
53 ll = l->next;
54 free(l->p);
55 free(l);
56 }
57 n->l = 0;
58 n->lt = &n->l;
59 }
60
61 static void insert(node_longest *n, const char *p, size_t sz)
62 {
63 link *l;
64
65 n->len = sz;
66 l = xmalloc(sizeof(*l));
67 l->p = xmalloc(sz + 1);
68 memcpy(l->p, p, sz + 1);
69 l->next = 0;
70 *n->lt = l;
71 n->lt = &l->next;
72 }
73
74 static int aa_output(void *p)
75 {
76 node_longest *n = p;
77 link *l;
78
79 if (!n->l) return (0);
80 for (l = n->l; l; l = l->next)
81 puts(l->p);
82 return (1);
83 }
84
85 static int n_longest(node *nn, const char *p, size_t sz)
86 {
87 node_longest *n = (node_longest *)nn;
88 if (sz < n->len)
89 return (0);
90 if (sz > n->len)
91 empty(n);
92 insert(n, p, sz);
93 return (0);
94 }
95
96 static int n_shortest(node *nn, const char *p, size_t sz)
97 {
98 node_longest *n = (node_longest *)nn;
99 if (n->len && sz > n->len)
100 return (0);
101 if (!n->len || sz < n->len)
102 empty(n);
103 insert(n, p, sz);
104 return (0);
105 }
106
107 static node *mklongest(int (*nf)(node *, const char *, size_t),
108 int (*aa)(void *))
109 {
110 node_longest *n = xmalloc(sizeof(*n));
111 n->n.func = nf;
112 n->len = 0;
113 n->l = 0;
114 n->lt = &n->l;
115 atend_register(aa, n);
116 return (&n->n);
117 }
118
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); }
123
124 /*----- That's all, folks -------------------------------------------------*/