lilypond-0.1.47
[lilypond.git] / lily / keyword.cc
blob43c87d95b5573877bfa4d7bcb6793d9a79274302
1 /*
2 keyword.cc -- keywords and identifiers
3 */
5 #include <stdlib.h>
8 #include "keyword.hh"
11 /* for qsort */
12 int
13 tabcmp (void const * p1, void const * p2)
15 return strcmp (((Keyword_ent const *) p1)->name,
16 ((Keyword_ent const *) p2)->name);
19 Keyword_table::Keyword_table (Keyword_ent *tab)
21 table = tab;
23 /* count keywords */
24 for (maxkey = 0; table[maxkey].name; maxkey++);
26 /* sort them */
27 qsort (table, maxkey, sizeof (Keyword_ent), tabcmp);
31 lookup with binsearch, return tokencode.
33 int
34 Keyword_table::lookup (char const *s) const
36 int lo;
37 int hi;
38 int cmp;
39 int result;
40 lo = 0;
41 hi = maxkey;
43 /* binary search */
46 cmp = (lo + hi) / 2;
48 result = strcmp (s, table[cmp].name);
50 if (result < 0)
51 hi = cmp;
52 else
53 lo = cmp;
55 while (hi - lo > 1);
56 if (!strcmp (s, table[lo].name))
58 return table[lo].tokcode;
60 else
61 return -1; /* not found */