From a5a84c429ac548eafd1d3903225a4ce72104201f Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周三, 22 4月 2015 03:50:26 +0800 Subject: [PATCH] Support RESPONSE MESSAGE UNIT SEPARATOR, issue #21 --- libscpi/src/utils.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 141 insertions(+), 25 deletions(-) diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c index 90f6ed4..e5f4ee9 100644 --- a/libscpi/src/utils.c +++ b/libscpi/src/utils.c @@ -42,6 +42,7 @@ #include <ctype.h> #include "utils_private.h" +#include "scpi/utils.h" static size_t patternSeparatorShortPos(const char * pattern, size_t len); static size_t patternSeparatorPos(const char * pattern, size_t len); @@ -72,37 +73,63 @@ * @param val integer value * @param str converted textual representation * @param len string buffer length + * @param base output base * @return number of bytes written to str (without '\0') */ -// TODO: add support for other bases +size_t SCPI_LongToStr(int32_t val, char * str, size_t len, int8_t base) { + const char digits[] = "0123456789ABCDEF"; -size_t longToStr(int32_t val, char * str, size_t len, int8_t base) { - uint32_t x = 1000000000L; +#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 } /** @@ -112,8 +139,8 @@ * @param len string buffer length * @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); +size_t SCPI_DoubleToStr(double val, char * str, size_t len) { + return SCPIDEFINE_doubleToStr(val, str, len); } /** @@ -153,11 +180,41 @@ return FALSE; } - if (SCPI_strncasecmp(str1, str2, len2) == 0) { + if (SCPIDEFINE_strncasecmp(str1, str2, len2) == 0) { return TRUE; } return FALSE; +} + +/** + * Compare two strings, one be longer but may contains only numbers in that section + * @param str1 + * @param len1 + * @param str2 + * @param len2 + * @return TRUE if strings match + */ +scpi_bool_t compareStrAndNum(const char * str1, size_t len1, const char * str2, size_t len2) { + scpi_bool_t result = FALSE; + size_t i; + + if (len2 < len1) { + return FALSE; + } + + if (SCPIDEFINE_strncasecmp(str1, str2, len1) == 0) { + result = TRUE; + } + + for (i = len1; i < len2; i++) { + if (!isdigit(str2[i])) { + result = FALSE; + break; + } + } + + return result; } /** @@ -236,9 +293,22 @@ * @return */ scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len) { - int pattern_sep_pos_short = patternSeparatorShortPos(pattern, pattern_len); - return compareStr(pattern, pattern_len, str, str_len) || - compareStr(pattern, pattern_sep_pos_short, str, str_len); + int pattern_sep_pos_short; + + if (pattern[pattern_len - 1] == '#') { + size_t new_pattern_len = pattern_len - 1; + + pattern_sep_pos_short = patternSeparatorShortPos(pattern, new_pattern_len); + + return compareStrAndNum(pattern, new_pattern_len, str, str_len) || + compareStrAndNum(pattern, pattern_sep_pos_short, str, str_len); + } else { + + pattern_sep_pos_short = patternSeparatorShortPos(pattern, pattern_len); + + return compareStr(pattern, pattern_len, str, str_len) || + compareStr(pattern, pattern_sep_pos_short, str, str_len); + } } /** @@ -259,7 +329,7 @@ const char * pattern_end = pattern + pattern_len; const char * cmd_ptr = cmd; - size_t cmd_len = SCPI_strnlen(cmd, len); + size_t cmd_len = SCPIDEFINE_strnlen(cmd, len); const char * cmd_end = cmd + cmd_len; /* now support optional keywords in pattern style, e.g. [:MEASure]:VOLTage:DC? */ @@ -275,7 +345,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++; } @@ -373,6 +443,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 */ @@ -394,12 +509,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