From 8ec1d776751f62c43335d36c3427dc1ab2d84a61 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期六, 25 七月 2020 13:52:06 +0800 Subject: [PATCH] commit --- test/protocle_parse.c | 39 ++++++++++++++++++++++----------------- 1 files changed, 22 insertions(+), 17 deletions(-) diff --git a/test/protocle_parse.c b/test/protocle_parse.c index 9acef45..0280b7c 100644 --- a/test/protocle_parse.c +++ b/test/protocle_parse.c @@ -3,10 +3,10 @@ #define ACTION_LIDENTIFIER "<**" #define ACTION_RIDENTIFIER "**>" -#define TOPIC_LIDENTIFIER '{' -#define TOPIC_RIDENTIFIER '}' +#define TOPIC_LIDENTIFIER "{" +#define TOPIC_RIDENTIFIER "}" -int parse_pubsub_topic(char *str, char **_action, size_t *_action_len, char **_topic, size_t *_topic_len) { +int parse_pubsub_topic(char *str, char **_action, char **_topic, size_t *head_len ) { char *ptr = str; char *str_end_ptr = str + strlen(str); char *action_start_ptr; @@ -35,14 +35,20 @@ ptr += strlen(ACTION_RIDENTIFIER); // printf("%s\n", ptr); // printf("%s\n", str_end_ptr-1); - if( (*ptr == TOPIC_LIDENTIFIER) && (*(str_end_ptr-1) == TOPIC_RIDENTIFIER) ) { - topic_start_ptr = ptr; - topic_end_ptr = str_end_ptr; - topic_len = topic_end_ptr - topic_start_ptr + 1; - ptr++; - // while(*(++ptr) != '}') { - // length++; - // } + if(strncmp(ptr, TOPIC_LIDENTIFIER, strlen(TOPIC_LIDENTIFIER)) == 0 ) { + topic_start_ptr = ptr+strlen(TOPIC_LIDENTIFIER); + + + while(strncmp(++ptr, TOPIC_RIDENTIFIER, strlen(TOPIC_RIDENTIFIER)) != 0) { + if(ptr >= str_end_ptr) { + return 0; + } + } + topic_end_ptr = ptr; + topic_len = topic_end_ptr - topic_start_ptr; + + ptr += strlen(TOPIC_RIDENTIFIER); + } else { return 0; } @@ -53,13 +59,11 @@ char *topic = (char *)calloc(1, topic_len+1); strncpy(topic, topic_start_ptr, topic_len); *_topic = topic; - *_topic_len = topic_len; char *action = (char *)calloc(1, action_len+1); strncpy(action, action_start_ptr, action_len); *_action = action; - *_action_len = action_len; - + *head_len = ptr-str; return 1; } @@ -68,12 +72,13 @@ char *action; size_t action_len; char *topic; - size_t topic_len; + size_t head_len; - char *str = "<**sub**>{缁忔祹}"; - if(parse_pubsub_topic(str, &action, &action_len, &topic, &topic_len)) { + char *str = "<**pub**>{缁忔祹}abcdef"; + if(parse_pubsub_topic(str, &action, &topic, &head_len)) { printf("action:%s\n", action); printf("topic:%s\n", topic); + printf("content:%s\n", str+head_len); free(action); free(topic); } else { -- Gitblit v1.8.0