From a2006d9935f97a233f75942e1a3eab69f27cf4b3 Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周日, 19 4月 2015 17:59:23 +0800 Subject: [PATCH] Update c++ example, make special numbers more correct --- libscpi/src/utils.c | 111 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 91 insertions(+), 20 deletions(-) diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c index 922b870..710d8e0 100644 --- a/libscpi/src/utils.c +++ b/libscpi/src/utils.c @@ -74,35 +74,60 @@ * @param len string buffer length * @return number of bytes written to str (without '\0') */ -// TODO: add support for other bases - size_t longToStr(int32_t val, char * str, size_t len, int8_t base) { - uint32_t x = 1000000000L; + const char digits[] = "0123456789ABCDEF"; + +#define ADD_CHAR(c) if (pos < len) str[pos++] = (c) + uint32_t x = 0; int_fast8_t digit; size_t pos = 0; + uint32_t uval = val; - if (val == 0) { - if (pos < len) str[pos++] = '0'; + if (uval == 0) { + ADD_CHAR('0'); } else { - if (val < 0) { - val = -val; - if (pos < len) str[pos++] = '-'; + + switch (base) { + case 2: + x = 0x80000000L; + break; + case 8: + x = 0x40000000L; + break; + case 10: + x = 1000000000L; + break; + case 0x10: + x = 0x10000000L; + break; + default: + x = 1000000000L; + base = 10; + break; } - while ((val / x) == 0) { - x /= 10; + // add sign for numbers in base 10 + if ((val < 0) && (base == 10)) { + uval = -val; + ADD_CHAR('-'); + } + + // remove leading zeros + while ((uval / x) == 0) { + x /= base; } do { - digit = (uint8_t) (val / x); - if (pos < len) str[pos++] = digit + '0'; - val -= digit * x; - x /= 10; + digit = (uint8_t) (uval / x); + ADD_CHAR(digits[digit]); + uval -= digit * x; + x /= base; } while (x && (pos < len)); } if (pos < len) str[pos] = 0; return pos; +#undef ADD_CHAR } /** @@ -113,7 +138,7 @@ * @return number of bytes written to str (without '\0') */ size_t doubleToStr(double val, char * str, size_t len) { - return snprintf(str, len, "%lg", val); + return SCPI_doubleToStr(val, str, len); } /** @@ -180,7 +205,7 @@ result = TRUE; } - for (i = len1; i<len2; i++) { + for (i = len1; i < len2; i++) { if (!isdigit(str2[i])) { result = FALSE; break; @@ -318,7 +343,7 @@ if (cmd_ptr[0] == ':') { /* handle errornouse ":*IDN?" */ - if((cmd_len >= 2) && (cmd_ptr[1] != '*')) { + if ((cmd_len >= 2) && (cmd_ptr[1] != '*')) { cmd_len--; cmd_ptr++; } @@ -416,6 +441,51 @@ return result; } +/** + * Compose command from previsou command anc current command + * + * @param prev pointer to previous command + * @param current pointer of current command + * + * prev and current should be in the same memory buffer + */ +scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) { + size_t i; + + /* Invalid input */ + if (current == NULL || current->ptr == NULL || current->len == 0) + return FALSE; + + /* no previous command - nothing to do*/ + if (prev->ptr == NULL || prev->len == 0) + return TRUE; + + /* Common command or command root - nothing to do */ + if (current->ptr[0] == '*' || current->ptr[0] == ':') + return TRUE; + + /* Previsou command was common command - nothing to do */ + if (prev->ptr[0] == '*') + return TRUE; + + /* Find last occurence of ':' */ + for (i = prev->len; i > 0; i--) { + if (prev->ptr[i - 1] == ':') { + break; + } + } + + /* Previous command was simple command - nothing to do*/ + if (i == 0) + return TRUE; + + current->ptr -= i; + current->len += i; + memmove(current->ptr, prev->ptr, i); + return TRUE; +} + + #if !HAVE_STRNLEN /* use FreeBSD strnlen */ @@ -437,12 +507,13 @@ #endif #if !HAVE_STRNCASECMP && !HAVE_STRNICMP + int OUR_strncasecmp(const char *s1, const char *s2, size_t n) { unsigned char c1, c2; - for(; n != 0; n--) { - c1 = tolower((unsigned char)*s1++); - c2 = tolower((unsigned char)*s2++); + for (; n != 0; n--) { + c1 = tolower((unsigned char) *s1++); + c2 = tolower((unsigned char) *s2++); if (c1 != c2) { return c1 - c2; } -- Gitblit v1.9.1