add hacky implementation of C1 speech/thought bubbles, unfinished
[openc2e.git] / dialect.cpp
blobe5a22d51cb9b2825448ad0e11860817ab87738a2
1 #include "dialect.h"
2 #include "exceptions.h"
3 #include <stdlib.h>
4 #include <string.h>
6 static int cmd_cmp(const void *pa, const void *pb) {
7 const cmdinfo *a = (const cmdinfo *)pa;
8 const cmdinfo *b = (const cmdinfo *)pb;
10 return strcmp(a->lookup_key, b->lookup_key);
13 static const cmdinfo *find_cmd(const struct cmdinfo *tbl, int cnt, const char *name) {
14 cmdinfo key;
15 key.lookup_key = name;
16 return (const cmdinfo *)bsearch((void *)&key, (void *)tbl, cnt, sizeof key, cmd_cmp);
19 static int count_cmds(const struct cmdinfo *tbl) {
20 int i = 0;
21 while (tbl[i].lookup_key)
22 i++;
23 return i;
26 Dialect::Dialect(const cmdinfo *cmds_, const std::string &n) : cmdcnt(count_cmds(cmds_)), cmds(cmds_), name(n) {
29 const cmdinfo *Dialect::find_command(const char *name) const {
30 const cmdinfo *ci = find_cmd(cmds, cmdcnt, name);
31 if (!ci)
32 throw parseException(std::string("Command not found: ") + name);
33 return ci;
36 int Dialect::cmd_index(const cmdinfo *ci) const {
37 assert(ci >= cmds && ci < cmds + cmdcnt);
38 return (ci - cmds);
41 std::map<std::string, boost::shared_ptr<Dialect> > dialects;
43 void registerDelegates() {
44 registerAutoDelegates();
47 void freeDelegates() {
48 dialects.clear();
51 /* vim: set noet: */