| | |
| | | /*- |
| | | * Copyright (c) 2013 Jan Breuer |
| | | * Richard.hmm |
| | | * Copyright (c) 2012 Jan Breuer |
| | | * BSD 2-Clause License |
| | | * |
| | | * All Rights Reserved |
| | | * Copyright (c) 2012-2018, Jan Breuer, Richard.hmm |
| | | * All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are |
| | | * met: |
| | | * 1. Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * 2. Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| | | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| | | * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE |
| | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| | | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| | | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| | | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| | | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| | | * * Redistributions of source code must retain the above copyright notice, this |
| | | * list of conditions and the following disclaimer. |
| | | * |
| | | * * Redistributions in binary form must reproduce the above copyright notice, |
| | | * this list of conditions and the following disclaimer in the documentation |
| | | * and/or other materials provided with the distribution. |
| | | * |
| | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| | | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| | | */ |
| | | |
| | | /** |
| | |
| | | #include <string.h> |
| | | #include <ctype.h> |
| | | #include <math.h> |
| | | |
| | | #include "utils_private.h" |
| | | #include "scpi/utils.h" |
| | | |
| | |
| | | } else { |
| | | |
| | | switch (base) { |
| | | case 2: |
| | | x = 0x80000000L; |
| | | break; |
| | | case 8: |
| | | x = 0x40000000L; |
| | | break; |
| | | default: |
| | | case 10: |
| | | base = 10; |
| | | x = 1000000000L; |
| | | break; |
| | | case 16: |
| | | x = 0x10000000L; |
| | | break; |
| | | case 2: |
| | | x = 0x80000000L; |
| | | break; |
| | | case 8: |
| | | x = 0x40000000L; |
| | | break; |
| | | default: |
| | | case 10: |
| | | base = 10; |
| | | x = 1000000000L; |
| | | break; |
| | | case 16: |
| | | x = 0x10000000L; |
| | | break; |
| | | } |
| | | |
| | | // add sign for numbers in base 10 |
| | | /* add sign for numbers in base 10 */ |
| | | if (sign && ((int32_t) val < 0) && (base == 10)) { |
| | | uval = -val; |
| | | ADD_CHAR('-'); |
| | | } |
| | | |
| | | // remove leading zeros |
| | | /* remove leading zeros */ |
| | | while ((uval / x) == 0) { |
| | | x /= base; |
| | | } |
| | |
| | | } else { |
| | | |
| | | switch (base) { |
| | | case 2: |
| | | x = 0x8000000000000000ULL; |
| | | break; |
| | | case 8: |
| | | x = 0x8000000000000000ULL; |
| | | break; |
| | | default: |
| | | case 10: |
| | | x = 10000000000000000000ULL; |
| | | base = 10; |
| | | break; |
| | | case 16: |
| | | x = 0x1000000000000000ULL; |
| | | break; |
| | | case 2: |
| | | x = 0x8000000000000000ULL; |
| | | break; |
| | | case 8: |
| | | x = 0x8000000000000000ULL; |
| | | break; |
| | | default: |
| | | case 10: |
| | | x = 10000000000000000000ULL; |
| | | base = 10; |
| | | break; |
| | | case 16: |
| | | x = 0x1000000000000000ULL; |
| | | break; |
| | | } |
| | | |
| | | // add sign for numbers in base 10 |
| | | /* add sign for numbers in base 10 */ |
| | | if (sign && ((int64_t) val < 0) && (base == 10)) { |
| | | uval = -val; |
| | | ADD_CHAR('-'); |
| | | } |
| | | |
| | | // remove leading zeros |
| | | /* remove leading zeros */ |
| | | while ((uval / x) == 0) { |
| | | x /= base; |
| | | } |
| | |
| | | */ |
| | | size_t strBaseToInt64(const char * str, int64_t * val, int8_t base) { |
| | | char * endptr; |
| | | *val = strtoll(str, &endptr, base); |
| | | *val = SCPIDEFINE_strtoll(str, &endptr, base); |
| | | return endptr - str; |
| | | } |
| | | |
| | |
| | | */ |
| | | size_t strBaseToUInt64(const char * str, uint64_t * val, int8_t base) { |
| | | char * endptr; |
| | | *val = strtoull(str, &endptr, base); |
| | | *val = SCPIDEFINE_strtoull(str, &endptr, base); |
| | | return endptr - str; |
| | | } |
| | | |
| | |
| | | */ |
| | | size_t strToFloat(const char * str, float * val) { |
| | | char * endptr; |
| | | *val = strtof(str, &endptr); |
| | | *val = SCPIDEFINE_strtof(str, &endptr); |
| | | return endptr - str; |
| | | } |
| | | |
| | |
| | | |
| | | if (num) { |
| | | if (len1 == len2) { |
| | | //*num = 1; |
| | | /* *num = 1; */ |
| | | } else { |
| | | int32_t tmpNum; |
| | | i = len1 + strBaseToInt32(str2 + len1, &tmpNum, 10); |
| | |
| | | pattern_sep_pos_short = patternSeparatorShortPos(pattern, new_pattern_len); |
| | | |
| | | return compareStrAndNum(pattern, new_pattern_len, str, str_len, num) || |
| | | compareStrAndNum(pattern, pattern_sep_pos_short, str, str_len, num); |
| | | compareStrAndNum(pattern, pattern_sep_pos_short, str, str_len, num); |
| | | } 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); |
| | | compareStr(pattern, pattern_sep_pos_short, str, str_len); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /* now support optional keywords in pattern style, e.g. [:MEASure]:VOLTage:DC? */ |
| | | if (pattern_ptr[0] == '[') { // skip first '[' |
| | | if (pattern_ptr[0] == '[') { /* skip first '[' */ |
| | | SKIP_PATTERN(1); |
| | | brackets++; |
| | | } |
| | | if (pattern_ptr[0] == ':') { // skip first ':' |
| | | if (pattern_ptr[0] == ':') { /* skip first ':' */ |
| | | SKIP_PATTERN(1); |
| | | } |
| | | |
| | |
| | | if ((pattern_sep_pos > 0) && pattern_ptr[pattern_sep_pos - 1] == '#') { |
| | | if (numbers && (numbers_idx < numbers_len)) { |
| | | number_ptr = numbers + numbers_idx; |
| | | *number_ptr = default_value; // default value |
| | | *number_ptr = default_value; /* default value */ |
| | | } else { |
| | | number_ptr = NULL; |
| | | } |
| | |
| | | |
| | | /* command complete, but pattern not */ |
| | | if (cmd_len == 0) { |
| | | // verify all subsequent pattern parts are also optional |
| | | /* verify all subsequent pattern parts are also optional */ |
| | | while (pattern_len) { |
| | | pattern_sep_pos = patternSeparatorPos(pattern_ptr, pattern_len); |
| | | switch (pattern_ptr[pattern_sep_pos]) { |
| | | case '[': |
| | | brackets++; |
| | | break; |
| | | case ']': |
| | | brackets--; |
| | | break; |
| | | default: |
| | | break; |
| | | case '[': |
| | | brackets++; |
| | | break; |
| | | case ']': |
| | | brackets--; |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | SKIP_PATTERN(pattern_sep_pos + 1); |
| | | if (brackets == 0) { |
| | |
| | | |
| | | /* both command and patter contains command separator at this position */ |
| | | if ((pattern_len > 0) |
| | | && ((pattern_ptr[0] == cmd_ptr[0]) |
| | | && ((pattern_ptr[0] == cmd_ptr[0]) |
| | | && (pattern_ptr[0] == ':'))) { |
| | | SKIP_PATTERN(1); |
| | | SKIP_CMD(1); |
| | | } else if ((pattern_len > 1) |
| | | && (pattern_ptr[1] == cmd_ptr[0]) |
| | | && (pattern_ptr[0] == '[') |
| | | && (pattern_ptr[1] == ':')) { |
| | | SKIP_PATTERN(2); // for skip '[' in "[:" |
| | | && (pattern_ptr[1] == cmd_ptr[0]) |
| | | && (pattern_ptr[0] == '[') |
| | | && (pattern_ptr[1] == ':')) { |
| | | SKIP_PATTERN(2); /* for skip '[' in "[:" */ |
| | | SKIP_CMD(1); |
| | | brackets++; |
| | | } else if ((pattern_len > 1) |
| | | && (pattern_ptr[1] == cmd_ptr[0]) |
| | | && (pattern_ptr[0] == ']') |
| | | && (pattern_ptr[1] == ':')) { |
| | | SKIP_PATTERN(2); // for skip ']' in "]:" |
| | | && (pattern_ptr[1] == cmd_ptr[0]) |
| | | && (pattern_ptr[0] == ']') |
| | | && (pattern_ptr[1] == ':')) { |
| | | SKIP_PATTERN(2); /* for skip ']' in "]:" */ |
| | | SKIP_CMD(1); |
| | | brackets--; |
| | | } else if ((pattern_len > 2) |
| | | && (pattern_ptr[2] == cmd_ptr[0]) |
| | | && (pattern_ptr[0] == ']') |
| | | && (pattern_ptr[1] == '[') |
| | | && (pattern_ptr[2] == ':')) { |
| | | SKIP_PATTERN(3); // for skip '][' in "][:" |
| | | && (pattern_ptr[2] == cmd_ptr[0]) |
| | | && (pattern_ptr[0] == ']') |
| | | && (pattern_ptr[1] == '[') |
| | | && (pattern_ptr[2] == ':')) { |
| | | SKIP_PATTERN(3); /* for skip '][' in "][:" */ |
| | | SKIP_CMD(1); |
| | | //brackets++; |
| | | //brackets--; |
| | | /* brackets++; */ |
| | | /* brackets--; */ |
| | | } else { |
| | | result = FALSE; |
| | | break; |
| | |
| | | } else { |
| | | SKIP_PATTERN(pattern_sep_pos); |
| | | if ((pattern_ptr[0] == ']') && (pattern_ptr[1] == ':')) { |
| | | SKIP_PATTERN(2); // for skip ']' in "]:" , pattern_ptr continue, while cmd_ptr remain unchanged |
| | | SKIP_PATTERN(2); /* for skip ']' in "]:" , pattern_ptr continue, while cmd_ptr remain unchanged */ |
| | | brackets--; |
| | | } else if ((pattern_len > 2) && (pattern_ptr[0] == ']') |
| | | && (pattern_ptr[1] == '[') |
| | | && (pattern_ptr[2] == ':')) { |
| | | SKIP_PATTERN(3); // for skip ']' in "][:" , pattern_ptr continue, while cmd_ptr remain unchanged |
| | | //brackets++; |
| | | //brackets--; |
| | | && (pattern_ptr[1] == '[') |
| | | && (pattern_ptr[2] == ':')) { |
| | | SKIP_PATTERN(3); /* for skip ']' in "][:" , pattern_ptr continue, while cmd_ptr remain unchanged */ |
| | | /* brackets++; */ |
| | | /* brackets--; */ |
| | | } else { |
| | | result = FALSE; |
| | | break; |
| | |
| | | * |
| | | * prev and current should be in the same memory buffer |
| | | */ |
| | | |
| | | //组合复合SCPI命令 |
| | | scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) { |
| | | size_t i; |
| | | |
| | |
| | | return TRUE; |
| | | |
| | | /* Common command or command root - nothing to do */ |
| | | // 当前命令是公共命令(*)或根命令(:) |
| | | if (current->ptr[0] == '*' || current->ptr[0] == ':') |
| | | return TRUE; |
| | | |
| | |
| | | |
| | | current->ptr -= i; |
| | | current->len += i; |
| | | //复合命令组合 |
| | | memmove(current->ptr, prev->ptr, i); |
| | | return TRUE; |
| | | } |
| | |
| | | } |
| | | #endif |
| | | |
| | | #if USE_MEMORY_ALLOCATION_FREE && !HAVE_STRNDUP |
| | | char *OUR_strndup(const char *s, size_t n) { |
| | | size_t len = SCPIDEFINE_strnlen(s, n); |
| | | //msvc下需要强转 |
| | | char * result =(char*) malloc(len + 1); |
| | | if (!result) { |
| | | return NULL; |
| | | } |
| | | memcpy(result, s, len); |
| | | result[len] = '\0'; |
| | | return result; |
| | | } |
| | | #endif |
| | | |
| | | #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE |
| | | |
| | | /** |
| | |
| | | return NULL; |
| | | } |
| | | |
| | | size_t len = SCPIDEFINE_strnlen(s, n) + 1; // additional '\0' at end |
| | | size_t len = SCPIDEFINE_strnlen(s, n) + 1; /* additional '\0' at end */ |
| | | if (len > heap->count) { |
| | | return NULL; |
| | | } |
| | |
| | | heap->wr += len; |
| | | heap->count -= len; |
| | | |
| | | // ensure '\0' a the end |
| | | /* ensure '\0' a the end */ |
| | | if (heap->wr > 0) { |
| | | heap->data[heap->wr - 1] = '\0'; |
| | | } else { |
| | |
| | | |
| | | #endif |
| | | |
| | | // Floating point to string conversion routines |
| | | // |
| | | // Copyright (C) 2002 Michael Ringgaard. All rights reserved. |
| | | // |
| | | // Redistribution and use in source and binary forms, with or without |
| | | // modification, are permitted provided that the following conditions |
| | | // are met: |
| | | // |
| | | // 1. Redistributions of source code must retain the above copyright |
| | | // notice, this list of conditions and the following disclaimer. |
| | | // 2. Redistributions in binary form must reproduce the above copyright |
| | | // notice, this list of conditions and the following disclaimer in the |
| | | // documentation and/or other materials provided with the distribution. |
| | | // 3. Neither the name of the project nor the names of its contributors |
| | | // may be used to endorse or promote products derived from this software |
| | | // without specific prior written permission. |
| | | // |
| | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| | | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| | | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| | | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| | | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| | | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| | | // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| | | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| | | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| | | // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| | | // SUCH DAMAGE. |
| | | /* |
| | | * Floating point to string conversion routines |
| | | * |
| | | * Copyright (C) 2002 Michael Ringgaard. All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions |
| | | * are met: |
| | | * |
| | | * 1. Redistributions of source code must retain the above copyright |
| | | * notice, this list of conditions and the following disclaimer. |
| | | * 2. Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * 3. Neither the name of the project nor the names of its contributors |
| | | * may be used to endorse or promote products derived from this software |
| | | * without specific prior written permission. |
| | | * |
| | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| | | * SUCH DAMAGE. |
| | | */ |
| | | |
| | | static char *scpi_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf, size_t bufsize) { |
| | | int r1, r2; |
| | |
| | | char * SCPI_dtostre(double __val, char * __s, size_t __ssize, unsigned char __prec, unsigned char __flags) { |
| | | char buffer[SCPI_DTOSTRE_BUFFER_SIZE]; |
| | | |
| | | int sign = signbit(__val); |
| | | int sign = SCPIDEFINE_signbit(__val); |
| | | char * s = buffer; |
| | | int decpt; |
| | | if (sign) { |
| | | __val = -__val; |
| | | s[0] = '-'; |
| | | s++; |
| | | } else if (!isnan(__val)) { |
| | | } else if (!SCPIDEFINE_isnan(__val)) { |
| | | if (SCPI_DTOSTRE_PLUS_SIGN & __flags) { |
| | | s[0] = '+'; |
| | | s++; |
| | |
| | | } |
| | | } |
| | | |
| | | if (!isfinite(__val)) { |
| | | if (isnan(__val)) { |
| | | if (!SCPIDEFINE_isfinite(__val)) { |
| | | if (SCPIDEFINE_isnan(__val)) { |
| | | strcpy(s, (__flags & SCPI_DTOSTRE_UPPERCASE) ? "NAN" : "nan"); |
| | | } else { |
| | | strcpy(s, (__flags & SCPI_DTOSTRE_UPPERCASE) ? "INF" : "inf"); |
| | | } |
| | | strncpy(__s, buffer, __ssize); |
| | | __s[__ssize - 1] = '\0'; |
| | | return __s; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | strncpy(__s, buffer, __ssize); |
| | | __s[__ssize - 1] = '\0'; |
| | | return __s; |
| | | } |
| | | |
| | |
| | | */ |
| | | uint16_t SCPI_Swap16(uint16_t val) { |
| | | return ((val & 0x00FF) << 8) | |
| | | ((val & 0xFF00) >> 8); |
| | | ((val & 0xFF00) >> 8); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | uint32_t SCPI_Swap32(uint32_t val) { |
| | | return ((val & 0x000000FF) << 24) | |
| | | ((val & 0x0000FF00) << 8) | |
| | | ((val & 0x00FF0000) >> 8) | |
| | | ((val & 0xFF000000) >> 24); |
| | | return ((val & 0x000000FFul) << 24) | |
| | | ((val & 0x0000FF00ul) << 8) | |
| | | ((val & 0x00FF0000ul) >> 8) | |
| | | ((val & 0xFF000000ul) >> 24); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | uint64_t SCPI_Swap64(uint64_t val) { |
| | | return ((val & 0x00000000000000FFul) << 56) | |
| | | ((val & 0x000000000000FF00ul) << 40) | |
| | | ((val & 0x0000000000FF0000ul) << 24) | |
| | | ((val & 0x00000000FF000000ul) << 8) | |
| | | ((val & 0x000000FF00000000ul) >> 8) | |
| | | ((val & 0x0000FF0000000000ul) >> 24) | |
| | | ((val & 0x00FF000000000000ul) >> 40) | |
| | | ((val & 0xFF00000000000000ul) >> 56); |
| | | return ((val & 0x00000000000000FFull) << 56) | |
| | | ((val & 0x000000000000FF00ull) << 40) | |
| | | ((val & 0x0000000000FF0000ull) << 24) | |
| | | ((val & 0x00000000FF000000ull) << 8) | |
| | | ((val & 0x000000FF00000000ull) >> 8) | |
| | | ((val & 0x0000FF0000000000ull) >> 24) | |
| | | ((val & 0x00FF000000000000ull) >> 40) | |
| | | ((val & 0xFF00000000000000ull) >> 56); |
| | | } |
| | | |
| | | |
| | | // 用于提取并处理选项的函数 |
| | | int extract_required_options(const char* pattern, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int max_tags) { |
| | | const char* start = strchr(pattern, '<'); // 查找第一个 '<' |
| | | const char* end = NULL; |
| | | int tag_count = 0; |
| | | |
| | | // 处理 <...> 内的选项 |
| | | while (start != NULL && tag_count < max_tags) |
| | | { |
| | | end = strchr(start, '>'); // 查找对应的 '>' |
| | | |
| | | if (end == NULL) { |
| | | break; // 如果没有找到 '>',则退出 |
| | | } |
| | | |
| | | // 提取 <...> 中的内容 |
| | | int len = end - start - 1; |
| | | if (len > 0 && len < MAX_OPTION_LEN) { |
| | | char buffer[MAX_OPTION_LEN]; |
| | | strncpy(buffer, start + 1, len); // 复制 '<' 和 '>' 之间的内容 |
| | | buffer[len] = '\0'; // 结束符 |
| | | |
| | | // 处理 | 分隔符,提取多个选项 |
| | | char* token = strtok(buffer, "|"); |
| | | int option_count = 0; |
| | | while (token && option_count < MAX_OPTION_LEN) { |
| | | // 去除 token 中的 "[:", "]" 和空格 |
| | | char* p = token; |
| | | while (*p == ' ' || *p == '[' || *p == ':') p++; // 去除前导空格和 [: |
| | | char* q = p + strlen(p) - 1; |
| | | while (q > p && (*q == ' ' || *q == ']')) q--; // 去除尾随空格和 ] |
| | | *(q + 1) = '\0'; // 确保结尾是'\0' |
| | | |
| | | // 拷贝选项到 options 数组 |
| | | if (option_count < MAX_OPTION_LEN) { // 确保不会越界 |
| | | strncpy(options[tag_count][option_count], p, MAX_OPTION_LEN - 1); |
| | | options[tag_count][option_count][MAX_OPTION_LEN - 1] = '\0'; // 确保结束符 |
| | | option_count++; |
| | | } |
| | | token = strtok(NULL, "|"); |
| | | } |
| | | } |
| | | |
| | | // 移动到下一个 '<' 位置,继续查找 |
| | | start = strchr(end + 1, '<'); |
| | | tag_count++; |
| | | } |
| | | |
| | | return tag_count; |
| | | } |
| | | |
| | | // 用于检查输入是否能匹配每一组选项 |
| | | int match_input_to_options(const char* input, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int num_tags) { |
| | | char input_copy[MAX_INPUT_LEN]; |
| | | strncpy(input_copy, input, MAX_INPUT_LEN - 1); |
| | | input_copy[MAX_INPUT_LEN - 1] = '\0'; // 确保结尾是'\0' |
| | | |
| | | // 拆分输入字符串,按 ":" 分割 |
| | | char* token = strtok(input_copy, ":"); |
| | | int group_idx = 0; |
| | | |
| | | // 对每一组进行匹配 |
| | | while (token != NULL && group_idx < num_tags) { |
| | | int match_found = 0; |
| | | // 检查当前组的每个选项是否与输入的 token 匹配 |
| | | printf("Checking input token: '%s' against group %d options\n", token, group_idx + 1); // Debug info |
| | | for (int i = 0; i < MAX_OPTION_LEN && options[group_idx][i][0] != '\0'; i++) { |
| | | printf(" Comparing with option: '%s'\n", options[group_idx][i]); // Debug info |
| | | if (strcmp(options[group_idx][i], token) == 0) { |
| | | match_found = 1; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // 如果当前组的某个选项没有匹配上,返回 false |
| | | if (!match_found) { |
| | | return 0; // 不匹配 |
| | | } |
| | | |
| | | // 处理下一个输入部分 |
| | | token = strtok(NULL, ":"); |
| | | group_idx++; |
| | | } |
| | | |
| | | return (group_idx == num_tags); |
| | | } |
| | | |
| | | |
| | | // 测试匹配函数 |
| | | bool test_match(const char* pattern, const char* command) |
| | | { |
| | | char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN] = { {{0}} }; |
| | | int num_tags = extract_required_options(pattern, options, MAX_TAGS); |
| | | return match_input_to_options(command, options, num_tags); |
| | | } |