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