From 54ddeb6407e2284989964bc83f6ee2bbff987a4a Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sat, 22 Sep 2012 18:16:26 +0200 Subject: device: Implement hwrevs as array of int16_t --- src/Makefile | 1 + src/device.c | 199 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/device.h | 18 ++++++ 3 files changed, 218 insertions(+) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index f064e6b..dbae7fe 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,6 +2,7 @@ include ../config.mk CPPFLAGS += -DVERSION=\"$(VERSION)\" -D_GNU_SOURCE -I. CFLAGS += -W -Wall -Wno-unused-parameter -Wno-unused-result -O2 -pedantic -std=c99 +LIBS += -lm ifdef WITH_USB CPPFLAGS += -DWITH_USB LIBS += -lusb diff --git a/src/device.c b/src/device.c index 8e652b6..0922a65 100644 --- a/src/device.c +++ b/src/device.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "global.h" @@ -73,3 +74,201 @@ const char * device_to_long_string(enum device device) { return long_devices[device]; } + +int hwrevs_is_valid(const int16_t * hwrevs, int16_t hwrev) { + + int i; + for ( i = 0; hwrevs[i] != -1; ++i ) + if ( hwrev == hwrevs[i] ) + return 1; + return 0; + +} + +int16_t * hwrevs_alloc_from_string(const char * str) { + + const char * ptr = str; + const char * oldptr = ptr; + int count = 2; + int16_t * ret; + int16_t tmp; + int i; + char buf[5]; + + while ( (ptr = strchr(ptr, ',')) ) { + ++count; + ++ptr; + } + + ret = calloc(count, sizeof(int16_t)); + if ( ! ret ) + return NULL; + + i = 0; + + while ( (ptr = strchr(ptr, ',')) ) { + strncpy(buf, oldptr, ptr-oldptr); + buf[4] = 0; + tmp = atoi(buf); + if ( tmp >= 0 ) + ret[i++] = tmp; + ++ptr; + oldptr = ptr; + } + + tmp = atoi(oldptr); + if ( tmp >= 0 ) + ret[i++] = tmp; + + ret[i++] = -1; + return ret; + +} + +char * hwrevs_alloc_to_string(const int16_t * hwrevs) { + + char * ret; + char * ptr; + int i; + int len = 0; + + for ( i = 0; hwrevs[i] != -1; ++i ) + len += log10(hwrevs[i])+2; + + ret = calloc(1, len); + if ( ! ret ) + return NULL; + + ptr = ret; + for ( i = 0; hwrevs[i] != -1; ++i ) + ptr += sprintf(ptr, "%d,", hwrevs[i]); + + *(ptr-1) = 0; + return ret; + +} + +char ** device_hwrevs_alloc_to_256bufs(const struct device_hwrevs * device_hwrevs) { + + int16_t * hwrevs = device_hwrevs->hwrevs; + const char * device; + int count = 0; + char ** ret; + int i, j, k; + + device = device_to_string(device_hwrevs->device); + if ( ! device ) + return NULL; + + for ( i = 0; hwrevs[i] != -1; ++i ) + if ( hwrevs[i] >= 0 && hwrevs[i] <= 9999 ) + ++count; + + count = count/30 + 2; + + ret = malloc(count*sizeof(char *) + count*256); + if ( ! ret ) + return NULL; + + i = -1; + j = 0; + + while ( hwrevs[i+1] != -1 ) { + + ret[j] = (char *)ret + count*sizeof(char *) + j*256; + + memset(ret[j], 0, 256); + strncpy(ret[j], device, 16); + + k = 0; + + for ( k = 0; k < 30; ++k ) { + + ++i; + + if ( hwrevs[i] < 0 || hwrevs[i] > 9999 ) + continue; + + memset(ret[j] + k*8, 0, 8); + snprintf(ret[j] + k*8, 8, "%d", hwrevs[i]); + + } + + ++j; + + } + + ret[j] = NULL; + + return ret; + +} + +struct device_hwrevs device_hwrevs_alloc_from_256buf(const char * buf, size_t size) { + + int i; + int len; + int count; + char str[17]; + const char * ptr1; + const char * ptr; + struct device_hwrevs ret; + + len = strnlen(buf, size); + if ( len > 16 ) + len = 16; + + memset(str, 0, sizeof(str)); + memcpy(str, buf, len); + ptr1 = buf + len; + + ret.device = device_from_string(str); + + ptr = ptr1; + count = 1; + while ( ptr < buf + size ) { + + while ( ptr < buf + size && *ptr < 32 ) + ++ptr; + + if ( ptr >= buf + size ) + break; + + len = strnlen(ptr, buf + size - ptr); + if ( len > 8 ) + len = 8; + + ptr += len + 1; + ++count; + + } + + ret.hwrevs = calloc(count, sizeof(int16_t)); + if ( ! ret.hwrevs ) + return ret; + + ptr = ptr1; + i = 0; + while ( ptr < buf + size ) { + + while ( ptr < buf + size && *ptr < 32 ) + ++ptr; + + if ( ptr >= buf + size ) + break; + + len = strnlen(ptr, buf + size - ptr); + if ( len > 8 ) + len = 8; + + memset(str, 0, sizeof(str)); + memcpy(str, ptr, len); + ptr += len + 1; + ret.hwrevs[i++] = atoi(str); + + } + + ret.hwrevs[i] = -1; + return ret; + +} diff --git a/src/device.h b/src/device.h index 0410494..d614d4e 100644 --- a/src/device.h +++ b/src/device.h @@ -31,8 +31,26 @@ enum device { DEVICE_COUNT, }; +/* + hwrevs - array of int16_t + - terminated by -1 + - valid numbers: 0-9999 +*/ +struct device_hwrevs { + enum device device; + int16_t * hwrevs; +}; + enum device device_from_string(const char * device); const char * device_to_string(enum device device); const char * device_to_long_string(enum device device); +int hwrevs_is_valid(const int16_t * hwrevs, int16_t hwrev); + +int16_t * hwrevs_alloc_from_string(const char * str); +char * hwrevs_alloc_to_string(const int16_t * hwrevs); + +char ** device_hwrevs_alloc_to_256bufs(const struct device_hwrevs * device_hwrevs); +struct device_hwrevs device_hwrevs_alloc_from_256buf(const char * buf, size_t size); + #endif -- cgit v1.2.3