aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2014-09-23 02:41:40 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2014-09-23 03:05:01 +0200
commit75d3388a351f011fc928ab12168818a5c5f5236f (patch)
treeef39f75092616c1a2d1272aff98b66f28d759585
parent56f7847ce3f6c3f56ed5735afb4431402cf3675e (diff)
downloadtermo-75d3388a351f011fc928ab12168818a5c5f5236f.tar.gz
termo-75d3388a351f011fc928ab12168818a5c5f5236f.tar.xz
termo-75d3388a351f011fc928ab12168818a5c5f5236f.zip
Introduce bsearch(3)
-rw-r--r--driver-ti.c42
1 files changed, 15 insertions, 27 deletions
diff --git a/driver-ti.c b/driver-ti.c
index 54ef27c..ce2a820 100644
--- a/driver-ti.c
+++ b/driver-ti.c
@@ -443,7 +443,7 @@ peekkey (termkey_t *tk, void *info,
return TERMKEY_RES_NONE;
}
-static struct
+static struct func
{
const char *funcname;
termkey_type_t type;
@@ -501,36 +501,24 @@ funcs[] =
};
static int
+func_compare (const void *key, const void *element)
+{
+ return strcmp (key, ((struct func *) element)->funcname);
+}
+
+static int
funcname2keysym (const char *funcname,
termkey_type_t *typep, termkey_sym_t *symp, int *modmaskp, int *modsetp)
{
- // Binary search
-
- int start = 0;
- int end = sizeof funcs / sizeof funcs[0];
- // is "one past" the end of the range
-
- // XXX: bsearch()?
- while (1)
+ struct func *func = bsearch (funcname, funcs,
+ sizeof funcs / sizeof funcs[0], sizeof funcs[0], func_compare);
+ if (func)
{
- int i = (start + end) / 2;
- int cmp = strcmp (funcname, funcs[i].funcname);
-
- if (cmp == 0)
- {
- *typep = funcs[i].type;
- *symp = funcs[i].sym;
- *modmaskp = funcs[i].mods;
- *modsetp = funcs[i].mods;
- return 1;
- }
- else if (end == start + 1)
- // That was our last choice and it wasn't it - not found
- break;
- else if (cmp > 0)
- start = i;
- else
- end = i;
+ *typep = func->type;
+ *symp = func->sym;
+ *modmaskp = func->mods;
+ *modsetp = func->mods;
+ return 1;
}
if (funcname[0] == 'f' && isdigit (funcname[1]))