lilypond-1.4.1
[lilypond.git] / lily / keyword.cc
blobe235d6a9734337e162da0cd262b3e341bf616992
1 /*
2 keyword.cc -- keywords and identifiers
3 */
4 #include <string.h>
5 #include <stdlib.h>
6 #include "keyword.hh"
9 /* for qsort */
10 int
11 tabcmp (void const * p1, void const * p2)
13 return strcmp (( (Keyword_ent const *) p1)->name,
14 ((Keyword_ent const *) p2)->name);
17 Keyword_table::Keyword_table (Keyword_ent *tab)
19 table = tab;
21 /* count keywords */
22 for (maxkey = 0; table[maxkey].name; maxkey++)
25 /* sort them */
26 qsort (table, maxkey, sizeof (Keyword_ent), tabcmp);
30 lookup with binsearch, return tokencode.
32 int
33 Keyword_table::lookup (char const *s) const
35 int lo;
36 int hi;
37 int cmp;
38 int result;
39 lo = 0;
40 hi = maxkey;
42 /* binary search */
45 cmp = (lo + hi) / 2;
47 result = strcmp (s, table[cmp].name);
49 if (result < 0)
50 hi = cmp;
51 else
52 lo = cmp;
54 while (hi - lo > 1);
55 if (!strcmp (s, table[lo].name))
57 return table[lo].tokcode;
59 else
60 return -1; /* not found */