Преглед на файлове

Z991239-6390 #comment toolkit 添加utf8转码

80374374 преди 2 месеца
родител
ревизия
8cef4ec591
променени са 5 файла, в които са добавени 74 реда и са изтрити 55 реда
  1. 2 1
      Framework/RVCComm/CMakeLists.txt
  2. 20 1
      Framework/RVCComm/HTTPClient.cpp
  3. 0 53
      Framework/RVCComm/HTTPClient.h
  4. 42 0
      Framework/libtoolkit/charset.c
  5. 10 0
      Framework/libtoolkit/charset.h

+ 2 - 1
Framework/RVCComm/CMakeLists.txt

@@ -36,7 +36,8 @@ target_link_directories(${MODULE_NAME} PRIVATE
 
 target_link_libraries(${MODULE_NAME} 
 	${PRIVATE_KEYWORD} ${KYSM_LIBRARY_NAME} 
-	${PRIVATE_KEYWORD} winpr 
+	${PRIVATE_KEYWORD} winpr
+	${PRIVATE_KEYWORD} libtoolkit
 	${PRIVATE_KEYWORD} ${OPENSSL_SSL_LIBRARY}
 	${PRIVATE_KEYWORD} ${OPENSSL_CRYPTO_LIBRARY}
 	${PRIVATE_KEYWORD} ${CONAN_LIBS_LIBSSH2}

+ 20 - 1
Framework/RVCComm/HTTPClient.cpp

@@ -11,6 +11,7 @@
 #include "toolkit.h"
 #include <winpr/string.h>
 #endif //_MSC_VER
+#include "charset.h"
 
 #define DEBUG_LOG(msg)	\
       do {	\
@@ -56,7 +57,7 @@ static void init_libcurl_once(void)
 	gLibcurlMnt.Init();
 }
 
-#include "charset.h"
+
 static std::string GBK2UTF8(const std::string str)
 {
 	std::string result("");
@@ -114,6 +115,24 @@ void ConvertGBKToUtf8(std::string& str)
 	const std::string result = GBK2UTF8(str);
 	str = result;
 }
+
+#else
+
+void ConvertUtf8ToGBK(std::string& str)
+{
+	char* dst = ConvertUtf8ToGBK(str.c_str());
+	str = dst;
+	if(dst != NULL) free(dst);
+}
+
+void ConvertGBKToUtf8(std::string& str)
+{
+	int len = 0;
+	char* dst = ConvertGBKToUtf8(str.c_str(), &len);
+	str = dst;
+	if (dst != NULL) free(dst);
+}
+
 #endif
 
 // Static members initialization

+ 0 - 53
Framework/RVCComm/HTTPClient.h

@@ -253,59 +253,6 @@ private:
    CHTTPClient& operator=(const CHTTPClient& Copy);
 };
 
-///*TODO(80374374@3/9/2023): 抽出作为公共函数 */
-#if defined(_MSC_VER)
-char* ConvertUtf8ToGBK(const char* strUtf8)
-{
-    int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL, 0);
-    WCHAR* wszGBK = new WCHAR[len + 1];
-    memset(wszGBK, 0, len * 2 + 2);
-    MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, wszGBK, len);
-
-    len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
-    char* szGBK = new char[len + 1];
-    memset(szGBK, 0, len + 1);
-    WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
-    delete[] wszGBK;
-
-    return szGBK;
-}
-
-void ConvertUtf8ToGBK(std::string& str)
-{
-    char* dst = ConvertUtf8ToGBK(str.c_str());
-    str = dst;
-    delete[] dst;
-}
-
-
-char* ConvertGBKToUtf8(const char* gbk, int* n)
-{
-    int len = MultiByteToWideChar(CP_ACP, 0, gbk, -1, NULL, 0);
-    WCHAR* wszGBK = new WCHAR[len + 1];
-    memset(wszGBK, 0, len * 2 + 2);
-    MultiByteToWideChar(CP_ACP, 0, gbk, -1, wszGBK, len);
-
-    len = WideCharToMultiByte(CP_UTF8, 0, wszGBK, -1, NULL, 0, NULL, NULL);
-    char* szUtf8 = new char[len + 1];
-    memset(szUtf8, 0, len + 1);
-    WideCharToMultiByte(CP_UTF8, 0, wszGBK, -1, szUtf8, len, NULL, NULL);
-
-    delete[] wszGBK;
-    *n = len - 1;
-    return szUtf8;
-}
-
-void ConvertGBKToUtf8(std::string& str)
-{
-    int len = 0;
-    char* dst = ConvertGBKToUtf8(str.c_str(), &len);
-    str = dst;
-    delete[] dst;
-}
-#endif //_MSC_VER
-
-
 RVCCOMM_API IHttpFunc* create_http(LogFnCallback oLogger)
 {
 	return new CHTTPClient(oLogger);

+ 42 - 0
Framework/libtoolkit/charset.c

@@ -604,3 +604,45 @@ TOOLKIT_API int  toolkit_detect_utf8_str(const char* str)
     //if (validate_utf8(str, strlen(str)) == UTF8_ACCEPT) return 1;
     return is_utf8(str);
 }
+
+TOOLKIT_API char* ConvertGBKToUtf8(const char* gbk, int* n)
+{
+#ifdef _MSC_VER
+	int len = MultiByteToWideChar(CP_ACP, 0, gbk, -1, NULL, 0);
+	WCHAR* wszGBK = malloc(sizeof(WCHAR) * (len + 1));
+	memset(wszGBK, 0, len * 2 + 2);
+	MultiByteToWideChar(CP_ACP, 0, gbk, -1, wszGBK, len);
+
+	len = WideCharToMultiByte(CP_UTF8, 0, wszGBK, -1, NULL, 0, NULL, NULL);
+	char* szUtf8 = malloc(sizeof(char) * (len + 1));
+	memset(szUtf8, 0, len + 1);
+	WideCharToMultiByte(CP_UTF8, 0, wszGBK, -1, szUtf8, len, NULL, NULL);
+
+	free(wszGBK);
+	*n = len - 1;
+	return szUtf8;
+#else
+    return NULL;
+#endif
+}
+
+TOOLKIT_API char* ConvertUtf8ToGBK(const char* strUtf8)
+{
+#ifdef _MSC_VER
+	int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, NULL, 0);
+	WCHAR* wszGBK = malloc(sizeof(WCHAR)*(len + 1));
+	memset(wszGBK, 0, len * 2 + 2);
+	MultiByteToWideChar(CP_UTF8, 0, strUtf8, -1, wszGBK, len);
+
+	len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
+    char* szGBK = malloc(sizeof(char) * (len + 1));
+	memset(szGBK, 0, len + 1);
+	WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
+	free(wszGBK);
+
+	return szGBK;
+#else
+    return strUtf8;
+#endif
+
+}

+ 10 - 0
Framework/libtoolkit/charset.h

@@ -24,6 +24,16 @@ extern "C" {
 
     TOOLKIT_API int  toolkit_detect_utf8_str(const char* str);
 
+    //only valid in win and the user remember to free the return if not null
+    //return strUtf8 input param directory in UOS, please use toolkit_utf82gbk if you need
+    //copy from httpclient.h
+    TOOLKIT_API char* ConvertUtf8ToGBK(const char* strUtf8);
+
+	//only valid in win and the user remember to free the return if not null
+	//return NULL directory in UOS, please use toolkit_gbk2utf8 if you need
+	//copy from httpclient.h
+    TOOLKIT_API char* ConvertGBKToUtf8(const char* gbk, int* n);
+
 #ifdef __cplusplus
 } // extern "C" {
 #endif