| | |
| | | #include <string.h> |
| | | #include <ctype.h> |
| | | #include <math.h> |
| | | |
| | | #include "utils_private.h" |
| | | #include "scpi/utils.h" |
| | | |
| | |
| | | ((val & 0x00FF000000000000ull) >> 40) | |
| | | ((val & 0xFF00000000000000ull) >> 56); |
| | | } |
| | | |
| | | int parse_pattern(const char* pattern, Segment segments[], int max_segments) { |
| | | int seg_count = 0; |
| | | const char* p = pattern; |
| | | |
| | | while (*p && seg_count < max_segments) |
| | | { |
| | | if (*p == '[') |
| | | { |
| | | // 处理可变段 |
| | | segments[seg_count].is_variable = TRUE; |
| | | // 跳过'[' |
| | | p++; |
| | | |
| | | int i = 0; |
| | | while (*p && *p != ']' && i < sizeof(segments[seg_count].text)-1) |
| | | { |
| | | segments[seg_count].text[i++] = toupper(*p++); |
| | | } |
| | | segments[seg_count].text[i] = '\0'; |
| | | |
| | | if (*p == ']') |
| | | { |
| | | // 跳过']' |
| | | p++; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | // 处理固定段 |
| | | segments[seg_count].is_variable = FALSE; |
| | | |
| | | int i = 0; |
| | | while (*p && *p != '[' && i < sizeof(segments[seg_count].text)-1) |
| | | { |
| | | segments[seg_count].text[i++] = toupper(*p++); |
| | | } |
| | | segments[seg_count].text[i] = '\0'; |
| | | } |
| | | |
| | | seg_count++; |
| | | } |
| | | |
| | | return seg_count; |
| | | } |
| | | |
| | | // 匹配命令与模式 |
| | | bool match_command(const char* command, Segment segments[], int seg_count) |
| | | { |
| | | const char* cmd = command; |
| | | int current_seg = 0; |
| | | |
| | | while (*cmd && current_seg < seg_count) |
| | | { |
| | | // 跳过命令中的分隔符(冒号) |
| | | if (*cmd == ':') |
| | | { |
| | | cmd++; |
| | | // 模式中也应该有对应的分隔符 |
| | | if (segments[current_seg].text[0] != ':') |
| | | { |
| | | return FALSE; |
| | | } |
| | | continue; |
| | | } |
| | | |
| | | // 获取当前段长度 |
| | | size_t seg_len = strlen(segments[current_seg].text); |
| | | |
| | | if (segments[current_seg].is_variable) |
| | | { |
| | | // 可变段 - 跳过对应长度的字符 |
| | | int i = 0; |
| | | while (*cmd && *cmd != ':' && i < seg_len) |
| | | { |
| | | cmd++; |
| | | i++; |
| | | } |
| | | } else |
| | | { |
| | | // 固定段 - 必须精确匹配 |
| | | if (strncasecmp(cmd, segments[current_seg].text, seg_len) != 0) |
| | | { |
| | | return FALSE; |
| | | } |
| | | cmd += seg_len; |
| | | } |
| | | |
| | | current_seg++; |
| | | } |
| | | |
| | | // 检查是否处理完所有命令和所有段 |
| | | return (*cmd == '\0') && (current_seg == seg_count); |
| | | } |
| | | |
| | | // 测试函数 |
| | | bool test_match(const char* pattern, const char* command) |
| | | { |
| | | Segment segments[16]; |
| | | int seg_count = parse_pattern(pattern, segments, 16); |
| | | return (match_command(command, segments, seg_count)); |
| | | |
| | | } |
| | | |
| | | |