summaryrefslogtreecommitdiffstats
path: root/src/device.c
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2012-09-22 18:16:26 +0200
committerPali Rohár <pali.rohar@gmail.com>2012-09-22 18:16:26 +0200
commit54ddeb6407e2284989964bc83f6ee2bbff987a4a (patch)
treec3248f38a438cb76e711f0bf42a8ba65966e9a06 /src/device.c
parent8167e841cecd61eb1161e3e7e12cbc12cab2719a (diff)
download0xFFFF-54ddeb6407e2284989964bc83f6ee2bbff987a4a.tar.bz2
device: Implement hwrevs as array of int16_t
Diffstat (limited to 'src/device.c')
-rw-r--r--src/device.c199
1 files changed, 199 insertions, 0 deletions
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 <stdlib.h>
#include <stdint.h>
#include <string.h>
+#include <math.h>
#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;
+
+}