From cb9f491cfe4f48de2f3776a86994e63cd1bd538c Mon Sep 17 00:00:00 2001 From: augustss Date: Sun, 24 Sep 2000 02:17:52 +0000 Subject: [PATCH] llow parsing of usage strings. --- lib/libusb/usage.c | 39 ++++++++++++++++++++++++++++++++++++++- lib/libusb/usb.3 | 20 ++++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/lib/libusb/usage.c b/lib/libusb/usage.c index d87afdc88c7a..2b98e4333be6 100644 --- a/lib/libusb/usage.c +++ b/lib/libusb/usage.c @@ -1,4 +1,4 @@ -/* $NetBSD: usage.c,v 1.6 2000/08/13 22:22:02 augustss Exp $ */ +/* $NetBSD: usage.c,v 1.7 2000/09/24 02:17:52 augustss Exp $ */ /* * Copyright (c) 1999 Lennart Augustsson @@ -192,3 +192,40 @@ hid_usage_in_page(unsigned int u) sprintf(b, "0x%04x", i); return b; } + +int +hid_parse_usage_page(const char *name) +{ + int k; + + if (!pages) + errx(1, "no hid table\n"); + + for (k = 0; k < npages; k++) + if (strcmp(pages[k].name, name) == 0) + return pages[k].usage; + return -1; +} + +/* XXX handle hex */ +int +hid_parse_usage_in_page(const char *name) +{ + const char *sep = strchr(name, ':'); + int k, j; + unsigned int l; + + if (sep == NULL) + return -1; + l = sep - name; + for (k = 0; k < npages; k++) + if (strncmp(pages[k].name, name, l) == 0) + goto found; + return -1; + found: + sep++; + for (j = 0; j < pages[k].pagesize; j++) + if (strcmp(pages[k].page_contents[j].name, sep) == 0) + return (pages[k].usage << 16) | pages[k].page_contents[j].usage; + return (-1); +} diff --git a/lib/libusb/usb.3 b/lib/libusb/usb.3 index d20b0ea341f7..73610058c6ec 100644 --- a/lib/libusb/usb.3 +++ b/lib/libusb/usb.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: usb.3,v 1.12 2000/09/24 02:13:24 augustss Exp $ +.\" $NetBSD: usb.3,v 1.13 2000/09/24 02:17:52 augustss Exp $ .\" .\" Copyright (c) 1999 Lennart Augustsson .\" All rights reserved. @@ -67,6 +67,10 @@ .Fn hid_usage_page "int i" .Ft char * .Fn hid_usage_in_page "u_int u" +.Ft int +.Fn hid_parse_usage_page "const char *" +.Ft char * +.Fn hid_parse_usage_in_page "const char *" .Ft void .Fn hid_init "char *file" .Ft int @@ -162,7 +166,19 @@ will return the symbolic name of a usage page, and the function .Fn hid_usage_in_page will return the symbolic name of the usage within the page. Both these functions may return a pointer to static data. -Before either of these functions can be called the usage table +.Pp +The functions +.Fn hid_parse_usage_page +and +.Fn hid_parse_usage_in_page +are the inverses of +.Fn hid_usage_page +and +.Fn hid_usage_in_page . +They take a usage string and return the number of the usage, or -1 +if it cannot be found. +.Pp +Before any of these functions can be called the usage table must be parsed, this is done by calling .Fn hid_init with the name of the table. Passing