]> git.donarmstrong.com Git - lilypond.git/blob - lily/keyword.cc
b273df2d6e2dbcb1d25bd645cdbbdeb94bcbe154
[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
27   /* sort them */
28   qsort (table, maxkey, sizeof (Keyword_ent), tabcmp);
29 }
30
31 /*
32   lookup with binsearch, return tokencode.
33 */
34 int
35 Keyword_table::lookup (char const *s) const
36 {
37   int lo;
38   int hi;
39   int cmp;
40   int result;
41   lo = 0;
42   hi = maxkey;
43
44   /* binary search */
45   do
46   {
47       cmp = (lo + hi) / 2;
48
49       result = strcmp (s, table[cmp].name);
50
51       if (result < 0)
52           hi = cmp;
53       else
54           lo = cmp;
55     }
56   while (hi - lo > 1);
57   if (!strcmp (s, table[lo].name))
58   {
59       return table[lo].tokcode;
60     }
61   else
62       return -1;              /* not found */
63 }
64