From db57152615104b4634c42946a912200a58bc5f93 Mon Sep 17 00:00:00 2001
From: cheliequan <liequanche@126.com>
Date: 星期五, 13 一月 2023 17:58:49 +0800
Subject: [PATCH] 1.重构代码,将模板函数分成两个函数 2.增加get_value_by_key函数

---
 util.h |   41 +++++++++++++++++++++--------------------
 1 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/util.h b/util.h
index 8e38043..b5c19ba 100644
--- a/util.h
+++ b/util.h
@@ -31,36 +31,23 @@
 bool bus_dbapi_get_topic_data(void *handle, const char* topic, crepmsg **pptr_repmsg, size_t len);
 extern "C" int bus_dbapi_get_json_data(const char * json, char ** pptr_data, uint8_t *ptr_type);
 bool bus_dbapi_get_str(void *handle, const char* topic, char **pptr_str);
-template <class T>  bool bus_dbapi_get_list (void *handle, const char* topic, T & list)
-
+int get_value_by_key(char *json, const char *key, char **pptr_data);
+template <class T>  bool bus_dbapi_get_list (char *json, T & list)
 {
-
 	char * ptr_value = NULL;
 	size_t data_len = 0;
     //get the topic json data
 	yyjson_type type = YYJSON_TYPE_OBJ;
-	crepmsg *repmsg = NULL;
-    size_t len = 0;
-	//T t;
-	//typedef decltype(list) vec_type;
 	
 	using TMPT = typename std::remove_reference<decltype(*list.begin())>::type;
 	TMPT t;
-	//vec_type::value_type t;
-	int ret = 0;
-	
-	ret = bus_dbapi_get_topic_data(handle, topic, &repmsg, len);
-    if ( ret < 0 || NULL == repmsg)
-    {
-        return false;
-    }
 
 	//parse the json to get value of the key "data"  
-    int msglen = strlen(repmsg->data);
+    int msglen = strlen(json);
     char* msgdata = (char *)malloc(msglen);
     memset(msgdata, 0 , msglen);
 
-    if(0 == bus_dbapi_get_json_data((const char *)repmsg->data, &msgdata, &type))
+    if(0 == bus_dbapi_get_json_data(json, &msgdata, &type))
     {
         printf("======>> protomsg: %s\n", msgdata);            
     }
@@ -94,8 +81,7 @@
 			else
 			{
 				printf("======>> json_to_proto fail\n");
-			    yyjson_doc_free(doc);
-            	free_reply_msg(repmsg);				
+			    yyjson_doc_free(doc);		
 			    free(msgdata);				
 				return false;
 			} 		
@@ -106,8 +92,23 @@
 	}
 
     free(msgdata);
-	free_reply_msg(repmsg);
 
     return true;
 } 
+
+template <class T>  bool bus_dbapi_get_list (void *handle, const char* topic, T & list)
+{
+	crepmsg *repmsg = NULL;
+    size_t len = 0;
+	bool iRet = false;
+	iRet = bus_dbapi_get_topic_data(handle, topic, &repmsg, len);
+	if ( (false == iRet)|| (NULL == repmsg))
+	{
+	    return false;
+	}
+	
+    iRet = bus_dbapi_get_list(repmsg->data, list);
+   	free_reply_msg(repmsg);
+	return iRet;
+} 
 #endif

--
Gitblit v1.8.0