device: Implement hwrevs as array of int16_t

This commit is contained in:
Pali Rohár committed 2012-09-22 18:16:26 +02:00
1 parent 8167e841ce
commit 54ddeb6407
3 files changed
+218

No files matched your search

+1
View File
@@ -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
+199
View File
@@ -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;
}
+18
View File
@@ -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