lilypond-1.3.12
[lilypond.git] / lily / keyword.cc
blobb273df2d6e2dbcb1d25bd645cdbbdeb94bcbe154
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++)
27 /* sort them */
28 qsort (table, maxkey, sizeof (Keyword_ent), tabcmp);
32 lookup with binsearch, return tokencode.
34 int
35 Keyword_table::lookup (char const *s) const
37 int lo;
38 int hi;
39 int cmp;
40 int result;
41 lo = 0;
42 hi = maxkey;
44 /* binary search */
47 cmp = (lo + hi) / 2;
49 result = strcmp (s, table[cmp].name);
51 if (result < 0)
52 hi = cmp;
53 else
54 lo = cmp;
56 while (hi - lo > 1);
57 if (!strcmp (s, table[lo].name))
59 return table[lo].tokcode;
61 else
62 return -1; /* not found */