#include "json_parser.h" #include "chinese_char_map.h" #include #include #include // 简单的JSON解析函数,解析格式: {"cmd":1,"text":"hello"} int ParseJsonCommand(const char* json_str, JsonCommand* command) { if (!json_str || !command) { return -1; } // 初始化结构体 command->cmd = 0; memset(command->text, 0, MAX_TEXT_LENGTH); // 查找cmd字段 const char* cmd_pos = strstr(json_str, "\"cmd\":"); if (cmd_pos) { cmd_pos += 6; // 跳过"cmd": // 跳过空格 while (*cmd_pos == ' ') cmd_pos++; command->cmd = atoi(cmd_pos); } // 查找text字段 const char* text_pos = strstr(json_str, "\"text\":"); if (text_pos) { text_pos += 7; // 跳过"text": // 跳过空格和引号 while (*text_pos == ' ' || *text_pos == '\"') text_pos++; // 复制文本直到遇到引号或字符串结束 int i = 0; while (*text_pos && *text_pos != '\"' && i < MAX_TEXT_LENGTH - 1) { command->text[i++] = *text_pos++; } command->text[i] = '\0'; } return 0; } // 创建IP地址消息,格式: {"type":"ip","address":"192.168.1.100"} int CreateIpMessage(char* buffer, int buffer_size, const char* ip_address) { if (!buffer || !ip_address || buffer_size < 50) { return -1; } int len = snprintf(buffer, buffer_size, "{\"type\":\"ip\",\"address\": \"%s\"}", ip_address); return (len > 0 && len < buffer_size) ? 0 : -1; } // 查找中文字符在fonts3数组中的索引 int FindChineseCharIndex(const char* utf8_char) { if (!utf8_char) { return -1; } // 遍历映射表查找匹配的字符 for (int i = 0; i < CHINESE_CHAR_MAP_SIZE; i++) { if (strcmp(utf8_char, chinese_char_map[i].utf8_char) == 0) { return chinese_char_map[i].font_index; } } // 未找到匹配的字符 return -1; }