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