Browse Source

#IQRV #comment [Merge]诗友去分行项目:更合并ibPublicFunc的更新

gifur 3 years ago
parent
commit
56a82ed670

+ 2 - 2
Other/libpublicFun/CMakeLists.txt

@@ -6,7 +6,7 @@ set(${MODULE_PREFIX}_SRCS
     publicFunExport.cpp
     publicFunExport.h
     ${ThirdPartyHeadRoot}/CJson/cJSON.c
-    ping.cpp
+    #ping.cpp
 )
 
 add_library(${MODULE_NAME} SHARED ${${MODULE_PREFIX}_SRCS})
@@ -16,7 +16,7 @@ add_library(${MODULE_NAME} SHARED ${${MODULE_PREFIX}_SRCS})
 target_include_directories(${MODULE_NAME} PRIVATE
 	"${CONAN_RVCFRAMEWORK_ROOT}/include"
 	${RVC_COMMON_INCLUDE_DIR}
-    ${ThirdPartyHeadRoot}/CJson
+    ${THIRD_PARTY_BASE_DIR}/Include/CJson
     )
 
 target_compile_definitions(${MODULE_NAME} PUBLIC "LIBPUBLIC_EXPORTS")

+ 203 - 1
Other/libpublicFun/publicFunExport.cpp

@@ -1,8 +1,43 @@
 #include <iostream>
 #include "publicFunExport.h"
-#include "cJSON.h"
 #include <memory>
+#include <math.h>
+#include <cstring>
+int str2int(const string str, int& ret) {
+	if (str.size() == 0) return 1;
+	ret = 0;
+	int symbol = 0;
+	for (int i = 0; i < str.size(); i++) {
+		if (i == 0 && str[i] == '-') {
+			symbol = 1;
+			continue;
+		}
+		if (i == 0 && str[i] == '+') continue;
+		if (i == 0) {
+			while (str[i] == '0' && i < str.size()) {
+				++i;
+			}
+			if (i == str.size()) return 0;
+		}
+		if (str[i] < '0' || str[i] >'9') return 2;
 
+		ret += (int)(str[i] - '0') * pow((double)10, (double)(str.size() - i - 1));
+	}
+	if (symbol) ret -= 2 * ret;
+	return 0;
+}
+
+int str2int(const string str) {
+	int ret;
+	str2int(str, ret);
+	return ret;
+}
+
+string int2str(const int num) {
+	char cNum[12] = { 0 };
+	sprintf(cNum, "%d", num);
+	return string(cNum);
+}
 
 std::pair<bool, std::string> generateJsonStr(std::map<std::string, std::string>& objectArr)
 {
@@ -24,3 +59,170 @@ std::pair<bool, std::string> generateJsonStr(std::map<std::string, std::string>&
 	});
 	return std::make_pair(true, dstPrintStr.get());
 }
+
+std::vector<std::string> parseJsonStr(const std::string jsonStr,const std::vector<std::string>& keys) {
+	std::vector<std::string> values;	
+	std::vector<std::string>::const_iterator it = keys.begin();
+	auto cJsonPtr = std::shared_ptr<cJSON>(cJSON_Parse(jsonStr.c_str()), [](cJSON* json) {
+		if (json) cJSON_Delete(json);
+	});
+	while (it != keys.end()) {
+		auto ret = cJSON_GetObjectItem(cJsonPtr.get(),(*it).c_str());
+		switch (ret->type)
+		{
+		case cJSON_False:
+			values.push_back(STR_FALSE);
+			break;
+		case cJSON_True:
+			values.push_back(STR_TRUE);
+			break;
+		case cJSON_NULL:
+			values.push_back(STR_NULL);
+			break;
+		case cJSON_Number:
+			values.push_back(int2str(ret->valueint));
+			break;
+		case cJSON_String:
+			values.push_back(ret->valuestring);
+			break;
+		default:
+			values.push_back("error type");
+			break;
+		}
+		it++;
+	}
+	return values;
+}
+
+
+RVCJson::RVCJson():pJson(NULL) {}
+RVCJson::RVCJson(bool notNewArray):pJson(NULL) {
+	if (notNewArray) {
+		pJson = cJSON_CreateObject();
+	}
+	else {
+		pJson = cJSON_CreateArray();
+	}
+	
+}
+RVCJson::~RVCJson() {
+	
+}
+
+void RVCJson::SetJson(const char* jsonStr) {
+	if (pJson)
+		cJSON_Delete(pJson);
+	pJson = cJSON_Parse(jsonStr);
+}
+void RVCJson::SetJson(RVCJson* rvcJson) {
+	if (pJson) cJSON_Delete(pJson);
+	if (!rvcJson) return;
+	pJson = cJSON_Parse(cJSON_PrintUnformatted(rvcJson->pJson));
+}
+void RVCJson::AddNullToObject(const char*key) {
+	cJSON_AddNullToObject(pJson, key);
+}
+void RVCJson::AddBoolToObject(const char*key, bool value)
+{
+	if (value) {
+		cJSON_AddTrueToObject(pJson, key);
+	}
+	else {
+		cJSON_AddFalseToObject(pJson, key);
+	}
+}
+void RVCJson::AddNumberToObject(const char*key, double value)
+{
+	cJSON_AddNumberToObject(pJson, key, value);
+}
+void RVCJson::AddStringToObject(const char*key, char*value)
+{
+	cJSON_AddStringToObject(pJson, key, value);
+}
+void RVCJson::AddItemToObject(const char*key, RVCJson* rvcJson) {
+	if (!key) return;
+	if (!rvcJson) return;
+	if (!pJson) return;
+	RVCJson tmpJson;
+	tmpJson.SetJson(rvcJson);
+	cJSON_AddItemToObject(pJson, key, tmpJson.pJson);
+}
+void RVCJson::AddItemToArray(RVCJson* rvcJson) {
+	if (!pJson) return;
+	if (!rvcJson) return;
+	if (pJson->type != cJSON_Array) return;
+	RVCJson tmpJson;
+	tmpJson.SetJson(rvcJson);
+	cJSON_AddItemToArray(pJson,tmpJson.pJson);
+}
+
+RVCJson* RVCJson::Get() {
+	return  this;
+}
+
+char*RVCJson::GetJsonStr() {
+	char* ret = cJSON_PrintUnformatted(pJson);
+	if (ret == NULL) return NULL;
+	char* retStr = new char[strlen(ret) + 1];
+	memset(retStr,0, strlen(ret) + 1);
+	memcpy(retStr, ret, strlen(ret));
+	free(ret);
+	return retStr;
+}
+bool RVCJson::GetBoolValue(const char*key) {
+	cJSON* ret = cJSON_GetObjectItem(pJson, key);
+	if (ret != NULL) {
+		if (ret->type == cJSON_True) return true;
+	}
+	return false;
+}
+double RVCJson::GetNumberValue(const char*key) {
+	
+	cJSON* ret = cJSON_GetObjectItem(pJson, key);
+	if (ret) {
+		if (ret->type == cJSON_Number) return ret->valuedouble;
+	}
+	return 0.0;
+}
+int RVCJson::GetNumberIntValue(const char*key) {
+	
+	cJSON* ret = cJSON_GetObjectItem(pJson, key);
+	if (ret) {
+		if (ret->type == cJSON_Number) return ret->valueint;
+	}
+	return 0.0;
+}
+char* RVCJson::GetStringValue(const char*key) {
+	cJSON* ret = cJSON_GetObjectItem(pJson, key);
+	if (ret) {
+		if (ret->type == cJSON_String) return ret->valuestring;
+		if (ret->type == cJSON_NULL) return NULL;
+	}
+	return NULL;
+}
+RVCJson* RVCJson::GetJsonValue(const char*key){
+	cJSON* ret = cJSON_GetObjectItem(pJson, key);
+	RVCJson *rvcJson = new RVCJson();
+	rvcJson->SetJson(cJSON_PrintUnformatted(ret));
+	return rvcJson;
+}
+
+int RVCJson::GetArraySize() {
+	if (!pJson) return 0;
+	return cJSON_GetArraySize(pJson);
+}
+
+RVCJson* RVCJson::GetArrayValue(int index) {
+	if (!pJson) return NULL;
+	cJSON* ret = cJSON_GetArrayItem(pJson,index);
+	RVCJson* rvcJson = new RVCJson();
+	rvcJson->SetJson(cJSON_PrintUnformatted(ret));
+	return rvcJson;
+}
+
+void RVCJson::Destory() {
+	if (pJson) {
+		cJSON_Delete(pJson);
+		pJson = NULL;
+	}
+}

+ 47 - 3
Other/libpublicFun/publicFunExport.h

@@ -1,15 +1,59 @@
-#pragma once
-
+#ifndef _OTHER_PUBLICFUNCEXPORT_HEADER_
+#define _OTHER_PUBLICFUNCEXPORT_HEADER_
+#include "cJSON.h"
 #include <map>
 #include <string>
+#include <vector>
 #include "publicExport.h"
+using namespace std;
+#define STR_TRUE "True"
+#define STR_FALSE "False"
+#define STR_NULL "Null"
 
 
 PUBLIC_API std::pair<bool, std::string> generateJsonStr(std::map<std::string, std::string> &objectArr);
+PUBLIC_API std::vector<std::string> parseJsonStr(const std::string jsonStr, const std::vector<std::string>& keys);
+
+PUBLIC_API int str2int(const string str);
+
+PUBLIC_API string int2str(const int num);
+class PUBLIC_API RVCJson{
+public:
+	cJSON* pJson;
+
+public:
+
+	RVCJson();
+	RVCJson(bool notNewArray);
+	~RVCJson();
+
+	void SetJson(const char*jsonStr);
+	void SetJson(RVCJson* cmbJson);
+	void AddNullToObject(const char*key);
+	void AddBoolToObject(const char*key, bool value);
+	void AddNumberToObject(const char*key, double value);
+	void AddStringToObject(const char*key, char*value);
+	void AddItemToObject(const char*key, RVCJson* rvcJson);
+	void AddItemToArray(RVCJson* rvcJson);
+
+	RVCJson* Get();
+	char*GetJsonStr();
+	bool GetBoolValue(const char*key);
+	double GetNumberValue(const char*key);
+	int GetNumberIntValue(const char*key);
+	char*GetStringValue(const char*key);
+	RVCJson* GetJsonValue(const char*key);
+	int GetArraySize();
+	RVCJson* GetArrayValue(int index);
+
+	void Destory();
+};
+
 /*!
  * @brief Ping protocols
  * @param[in]  
  * @param[out] 
  * @return : 0: ping connect succ; 1: ping failed
  */
-PUBLIC_API int Ping(const std::string& dst_ip);
+PUBLIC_API int Ping(const std::string& dst_ip);
+#endif //_OTHER_PUBLICFUNCEXPORT_HEADER_