*.c: General spring-clean of the coding style.
[anag] / anagram.c
CommitLineData
6e403221 1/* -*-c-*-
2 *
6e403221 3 * Matches anagrams and subgrams
4 *
5 * (c) 2001 Mark Wooding
6 */
7
0279756e 8/*----- Licensing notice --------------------------------------------------*
6e403221 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 *
6e403221 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 *
6e403221 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
6e403221 27/*----- Header files ------------------------------------------------------*/
28
29#include "anag.h"
30
31/*----- Data structures ---------------------------------------------------*/
32
904ac13a
MW
33typedef unsigned countmap[UCHAR_MAX + 1];
34
6e403221 35typedef struct node_gram {
36 node n;
37 size_t sz;
904ac13a 38 countmap count;
6e403221 39} node_gram;
40
41/*----- Main code ---------------------------------------------------------*/
42
43/* --- @compile@ --- *
44 *
45 * Arguments: @node_gram *n@ = pointer to node to fill in
46 * @const char *p@ = pointer to string to compile from
47 *
904ac13a 48 * Returns: ---
6e403221 49 *
50 * Use: Fills in an anagram node with letter frequencies.
51 */
52
904ac13a 53static void compile(const char *p, countmap count)
6e403221 54{
904ac13a
MW
55 memset(count, 0, sizeof(countmap));
56 while (*p) count[(unsigned char)*p++]++;
6e403221 57}
58
59/* --- Node matcher --- */
60
61static int n_gram(node *nn, const char *p, size_t sz)
62{
904ac13a
MW
63 /* A string X is a subgram of Y unless, for some character CH, X has more
64 * characters of value CH than Y.
65 *
66 * Special feature: count `.' in the pattern as meaning `don't care'.
67 */
68
6e403221 69 node_gram *n = (node_gram *)nn;
904ac13a 70 countmap count;
6e403221 71 const char *q;
72 unsigned i;
73
904ac13a
MW
74 if (n->sz && sz != n->sz) return (0);
75 memcpy(count, n->count, sizeof(count));
6e403221 76 q = p + sz;
77 while (p < q) {
78 i = (unsigned char)*p++;
904ac13a
MW
79 if (count[i]) count[i]--;
80 else if (count['.']) count['.']--;
81 else return (0);
6e403221 82 }
83 return (1);
84}
85
86/* --- Node creation --- */
87
88node *anagram(const char *const *av)
89{
90 node_gram *n = xmalloc(sizeof(*n));
91 n->n.func = n_gram;
904ac13a
MW
92 compile(av[0], n->count);
93 n->sz = strlen(av[0]);
6e403221 94 return (&n->n);
95}
96
97node *subgram(const char *const *av)
98{
99 node_gram *n = xmalloc(sizeof(*n));
100 n->n.func = n_gram;
904ac13a 101 compile(av[0], n->count);
6e403221 102 n->sz = 0;
6e403221 103 return (&n->n);
104}
105
106/*----- That's all, folks -------------------------------------------------*/