]> git.donarmstrong.com Git - lilypond.git/blob - lily/keyword.cc
release: 0.1.19
[lilypond.git] / lily / keyword.cc
1 /*
2   keyword.cc -- keywords and identifiers
3  */
4
5 #include <stdlib.h>
6
7
8 #include "keyword.hh"
9
10
11 /* for qsort */
12 int
13       tabcmp (void const * p1, void const * p2)
14 {
15   return strcmp (((Keyword_ent const *) p1)->name,
16                 ((Keyword_ent const *) p2)->name);
17 }
18
19 Keyword_table::Keyword_table (Keyword_ent *tab)
20 {
21   table = tab;
22
23   /* count keywords */
24   for (maxkey = 0; table[maxkey].name; maxkey++);
25
26   /* sort them */
27   qsort (table, maxkey, sizeof (Keyword_ent), tabcmp);
28 }
29
30 /*
31   lookup with binsearch, return tokencode.
32 */
33 int
34 Keyword_table::lookup (char const *s) const
35 {
36   int lo;
37   int hi;
38   int cmp;
39   int result;
40   lo = 0;
41   hi = maxkey;
42
43   /* binary search */
44   do
45   {
46       cmp = (lo + hi) / 2;
47
48       result = strcmp (s, table[cmp].name);
49
50       if (result < 0)
51           hi = cmp;
52       else
53           lo = cmp;
54     }
55   while (hi - lo > 1);
56   if (!strcmp (s, table[lo].name))
57   {
58       return table[lo].tokcode;
59     }
60   else
61       return -1;              /* not found */
62 }
63