feat:c++代码更新

This commit is contained in:
Surenjun 2024-10-14 14:17:01 +08:00
parent a82866741f
commit 8afec6f7cd
8 changed files with 148 additions and 69 deletions

View File

@ -212,6 +212,7 @@ elseif(JUDGE_OS_OHOS)
message("${MSG_NOTE} now compiler platform JUDGE_OS_OHOS")
set(JUDGE_INCLUDE_PLATFORM
${CMAKE_SYSROOT}/usr/include/ace
${CMAKE_SYSROOT}/usr/include/ace/xcomponent
${CMAKE_SYSROOT}/usr/include/native_window
${CMAKE_SOURCE_DIR}/sdk/api/js
@ -231,10 +232,10 @@ elseif(JUDGE_OS_OHOS)
)
set(JUDGE_LIB_FILE_PLATFORM
ace_napi.z
uv
ace_ndk.z
native_window #//for-surface-nativeXComponent
ace_napi.z
ace_ndk.z
hilog_ndk.z
)
@ -242,8 +243,8 @@ elseif(JUDGE_OS_ANDROID)
message("${MSG_NOTE} now compiler platform JUDGE_OS_ANDROID")
set(JUDGE_INCLUDE_PLATFORM
${CMAKE_SOURCE_DIR}/third/android/include #
${CMAKE_SOURCE_DIR}/sdk/api/jni
${CMAKE_SOURCE_DIR}/third/android/include #
)
file(GLOB_RECURSE JUDGE_SOURCE_PLATFORM
@ -381,6 +382,7 @@ function(showOutputInfo)
message(STATUS "${MSG_INFO} ENV{CFLAGS}=$ENV{CFLAGS}")
message(STATUS "${MSG_INFO} ENV{CXXFLAGS=$ENV{CXXFLAGS}")
message(STATUS "${MSG_INFO} CMAKE_SYSROOT = ${CMAKE_SYSROOT}")
message(STATUS "${MSG_INFO} CMAKE_DEFAULT_BUILD_TYPE = ${CMAKE_DEFAULT_BUILD_TYPE}")
message(STATUS "${MSG_INFO} CMAKE_C_COMPILER = ${CMAKE_C_COMPILER}")
message(STATUS "${MSG_INFO} CMAKE_CXX_COMPILER = ${CMAKE_CXX_COMPILER}")

View File

@ -12,9 +12,9 @@
#include "CleverHelper.h"
//字面量转化为字符串的宏定义
#define JUDGE_EXP_STR(x) #x
#define JUDGE_STR(x) #x
#define JUDGE_EXP_EXPAND(x) x
#define JUDGE_EXPAND(x) x
//可变参数列表
#define JUDGE_VA_ARGS(...) __VA_ARGS__
@ -65,8 +65,6 @@
#define sdk_ssleep(x) std::this_thread::sleep_for(std::chrono::seconds(x))
//距离单位换算枚举(以毫米为基准单位)
enum DisUnit
{
@ -114,6 +112,13 @@ struct SpaceStorage
int64 spaceSelf = 0; //当前进程占用(只针对内存有效,磁盘空间无效)
};
enum CharsetType
{
CharsetTypeUTF8,
CharsetTypeGBK
};
//============================================================
using ILock = std::lock_guard<std::mutex>;
using ULock = std::unique_lock<std::mutex>;

View File

@ -3,17 +3,17 @@
#if defined(JUDGE_OS_FAMILY_WINDOWS)
JUDGE_C_API bool __sdk_localtime_safe__(time_t* __t, tm* __tm)
bool __sdk_localtime_safe__(time_t* __t, tm* __tm)
{
return ::localtime_s(__tm, __t) == 0;
}
JUDGE_C_API int64 __sdk_mktime_safe__(tm* __tm)
int64 __sdk_mktime_safe__(tm* __tm)
{
return ::_mktime64(__tm);
}
JUDGE_C_API bool __sdk_strptime_safe__(const char* __s, const char* __fmt, tm* __tm)
bool __sdk_strptime_safe__(const char* __s, const char* __fmt, tm* __tm)
{
const char* _fmt = "%d-%d-%d %d:%d:%d";
if(NULL != ::strchr(__fmt, '/'))
@ -34,22 +34,22 @@
return true;
}
JUDGE_C_API bool __sdk_filepath_exist__(const char* __filepath)
bool __sdk_filepath_exist__(const char* __filepath)
{
return (::_access(__filepath, 0) == 0);
}
JUDGE_C_API int __sdk_mkdir__(const char* __path)
int __sdk_mkdir__(const char* __path)
{
return ::_mkdir(__path);
}
JUDGE_C_API int __sdk_rmdir__(const char* __path)
int __sdk_rmdir__(const char* __path)
{
return ::_rmdir(__path);
}
JUDGE_C_API bool __sdk_space_memory__(struct SpaceStorage* __space, int __unit)
bool __sdk_space_memory__(struct SpaceStorage* __space, int __unit)
{
const double _n = (double)__unit;
MEMORYSTATUSEX _m;
@ -65,7 +65,7 @@
return __ok;
}
JUDGE_C_API bool __sdk_space_disk__(const char* __driver, struct SpaceStorage* __space, int __unit)
bool __sdk_space_disk__(const char* __driver, struct SpaceStorage* __space, int __unit)
{
const double _n = (double)__unit;
ULARGE_INTEGER bytesAvail, bytesTotal, bytesFree;
@ -79,63 +79,70 @@
return __ok;
}
std::string __sdk_gbk_to_utf8__(const char* strgbk)
std::string __sdk_charset_convert__(const std::string& __str, int __from, int __to)
{
int wlen = MultiByteToWideChar(CP_ACP, 0, strgbk, -1, NULL, 0);
int from_charset = -1, to_charset = -1;
switch(__from)
{
case CharsetTypeUTF8: from_charset = CP_UTF8; break;
case CharsetTypeGBK: from_charset = CP_ACP; break;
default: TASSERT(false, ""); break;
}
switch(__to)
{
case CharsetTypeUTF8: to_charset = CP_UTF8; break;
case CharsetTypeGBK: to_charset = CP_ACP; break;
default: TASSERT(false, ""); break;
}
int wlen = MultiByteToWideChar(from_charset, 0, __str.data(), -1, NULL, 0);
std::wstring wstr(wlen+1, L'\0');
MultiByteToWideChar(CP_ACP, 0, strgbk, -1, wstr.data(), wlen);
int len = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, NULL, 0, NULL, NULL);
MultiByteToWideChar(from_charset, 0, __str.data(), -1, wstr.data(), wlen);
int len = WideCharToMultiByte(to_charset, 0, wstr.data(), -1, NULL, 0, NULL, NULL);
std::string str(len+1, '\0');
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, str.data(), len, NULL, NULL);
WideCharToMultiByte(to_charset, 0, wstr.data(), -1, str.data(), len, NULL, NULL);
return str;
}
std::string __sdk_utf8_to_gbk__(const char* strutf8)
{
int wlen = MultiByteToWideChar(CP_UTF8, 0, strutf8, -1, NULL, 0);
std::wstring wstr(wlen+1, L'\0');
MultiByteToWideChar(CP_UTF8, 0, strutf8, -1, wstr.data(), wlen);
int len = WideCharToMultiByte(CP_ACP, 0, wstr.data(), -1, NULL, 0, NULL, NULL);
std::string str(len+1, '\0');
WideCharToMultiByte(CP_ACP, 0, wstr.data(), -1, str.data(), len, NULL, NULL);
return str;
}
#elif defined(JUDGE_OS_FAMILY_LINUX)
JUDGE_C_API bool __sdk_localtime_safe__(time_t* __t, tm* __tm)
bool __sdk_localtime_safe__(time_t* __t, tm* __tm)
{
tm* p = ::localtime_r(__t, __tm);
return p != NULL;
}
JUDGE_C_API int64 __sdk_mktime_safe__(tm* __tm)
int64 __sdk_mktime_safe__(tm* __tm)
{
return ::mktime(__tm);
}
JUDGE_C_API bool __sdk_strptime_safe__(const char* __s, const char* __fmt, tm* __tm)
bool __sdk_strptime_safe__(const char* __s, const char* __fmt, tm* __tm)
{
char* p = ::strptime(__s, __fmt, __tm);
return p != NULL;
}
JUDGE_C_API bool __sdk_filepath_exist__(const char* __filepath)
bool __sdk_filepath_exist__(const char* __filepath)
{
return (::access(__filepath, F_OK) == 0);
}
JUDGE_C_API int __sdk_mkdir__(const char* __path)
int __sdk_mkdir__(const char* __path)
{
return ::mkdir(__path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
}
JUDGE_C_API int __sdk_rmdir__(const char* __path)
int __sdk_rmdir__(const char* __path)
{
return ::rmdir(__path);
}
JUDGE_C_API bool __sdk_space_memory__(struct SpaceStorage* __space, int __unit)
bool __sdk_space_memory__(struct SpaceStorage* __space, int __unit)
{
const double _n = (double)__unit;
long long page_size = ::sysconf(_SC_PAGE_SIZE);
@ -150,7 +157,7 @@
return __ok;
}
JUDGE_C_API bool __sdk_space_disk__(const char* __driver, struct SpaceStorage* __space, int __unit)
bool __sdk_space_disk__(const char* __driver, struct SpaceStorage* __space, int __unit)
{
const double _n = (double)__unit;
struct ::statfs disk;
@ -164,16 +171,44 @@
return __ok;
}
std::string __sdk_gbk_to_utf8__(const char* strgbk)
std::string __sdk_charset_convert__(const std::string& __str, int __from, int __to)
{
TASSERT(false, "not support!");
return std::string(strgbk);
}
std::string from_charset, to_charset;
std::string __sdk_utf8_to_gbk__(const char* strutf8)
{
TASSERT(false, "not support!");
return std::string(strutf8);
switch(__from)
{
case CharsetTypeUTF8: from_charset = "UTF-8"; break;
case CharsetTypeGBK: from_charset = "GBK"; break;
default: TASSERT(false, ""); break;
}
switch(__to)
{
case CharsetTypeUTF8: to_charset = "UTF-8"; break;
case CharsetTypeGBK: to_charset = "GBK"; break;
default: TASSERT(false, ""); break;
}
iconv_t cd = iconv_open(to_charset.data(), from_charset.data());
if(cd == (iconv_t)(-1))
{
return std::string();
}
char* inbuf = const_cast<char*>(__str.data());
size_t inbytesleft = __str.size();
size_t outbytesleft = inbytesleft * 4; // UTF-8 最坏情况下是 GBK 的 4 倍
std::string out(outbytesleft, '\0');
char* outbuf = out.data();
//转换编码
size_t ret = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
if(ret == (size_t)(-1))
{
iconv_close(cd);
return std::string();
}
//清理
iconv_close(cd);
return out;
}
#else
@ -181,3 +216,4 @@
# error "define-error-msg: unknow system."
#endif

View File

@ -277,6 +277,7 @@ using uint64 = unsigned long long; // using uint64 = std::uint64_t;
#include <sys/resource.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iconv.h>
#else
@ -297,17 +298,24 @@ using uint64 = unsigned long long; // using uint64 = std::uint64_t;
# define __sdk_os_assert__(exp) assert(false)
#endif
JUDGE_EXTERN_C_BEGIN
JUDGE_C_API bool __sdk_localtime_safe__(time_t* __t, tm* __tm);
JUDGE_C_API int64 __sdk_mktime_safe__(tm* __tm);
JUDGE_C_API bool __sdk_strptime_safe__(const char* __s, const char* __fmt, tm* __tm);
JUDGE_C_API bool __sdk_filepath_exist__(const char* __filepath);
JUDGE_C_API int __sdk_mkdir__(const char* __path);
JUDGE_C_API int __sdk_rmdir__(const char* __path);
JUDGE_C_API bool __sdk_space_memory__(struct SpaceStorage* __space, int __unit/*is SpaceUnit*/);
JUDGE_C_API bool __sdk_space_disk__(const char* __driver, struct SpaceStorage* __space, int __unit/*is SpaceUnit*/);
std::string __sdk_gbk_to_utf8__(const char* strgbk);
std::string __sdk_utf8_to_gbk__(const char* strutf8);
bool __sdk_localtime_safe__(time_t* __t, tm* __tm);
int64 __sdk_mktime_safe__(tm* __tm);
bool __sdk_strptime_safe__(const char* __s, const char* __fmt, tm* __tm);
bool __sdk_filepath_exist__(const char* __filepath);
int __sdk_mkdir__(const char* __path);
int __sdk_rmdir__(const char* __path);
bool __sdk_space_memory__(struct SpaceStorage* __space, int __unit/*is SpaceUnit*/);
bool __sdk_space_disk__(const char* __driver, struct SpaceStorage* __space, int __unit/*is SpaceUnit*/);
JUDGE_EXTERN_C_END
JUDGE_EXTERN_CXX_BEGIN
std::string __sdk_charset_convert__(const std::string& __str, int __from, int __to/*enum CharsetType*/);
JUDGE_EXTERN_CXX_END
/**********************************************************************************************
*
@ -447,6 +455,11 @@ std::string __sdk_utf8_to_gbk__(const char* strutf8);
* std:c++17 201703L
* std:c++20 202002L
*
**********************************************************************************************
*
* __VA_ARGS__
* ##__VA_ARGS__
*
**********************************************************************************************/

View File

@ -25,7 +25,7 @@
#define JUDGE_VERSION_MAJOR 1
#define JUDGE_VERSION_MINOR 0
#define JUDGE_VERSION_PATCH 2
#define JUDGE_VERSION_STAMP 2410121423b
#define JUDGE_VERSION_STAMP 2410141037b
#define JUDGE_AUX_EXP(x) #x
#define JUDGE_AUX_STR(x) JUDGE_AUX_EXP(x)

View File

@ -183,10 +183,12 @@ private:
*
**************************************************/
#define Logxx Loggerxx::GetInstance()
#ifdef JUDGE_USE_LOG
#define Logxx Loggerxx::GetInstance()
#define LOG_CALL_FUNC(func,format,...) Logxx->func(__FILE__,__LINE__,__FUNCTION__,LOG_TAG_SDK,UTF8S(format),##__VA_ARGS__)
//初始化日志模块
#define logInit(level) Logxx->init(level);
//卸载日志模块
@ -194,17 +196,17 @@ private:
//设置日志级别
#define logSetLevel(level) Logxx->setLevel(level);
//错误日志宏函数
#define logerror(format, ...) Logxx->error (__FILE__, __LINE__, __FUNCTION__, LOG_TAG_SDK, format, ##__VA_ARGS__);
#define logerror(format, ...) LOG_CALL_FUNC(error, format, ##__VA_ARGS__);
//警告日志宏函数
#define logwarning(format, ...) Logxx->warning(__FILE__, __LINE__, __FUNCTION__, LOG_TAG_SDK, format, ##__VA_ARGS__);
#define logwarning(format, ...) LOG_CALL_FUNC(warning, format, ##__VA_ARGS__);
//跟踪日志宏函数
#define logtrace(format, ...) Logxx->trace (__FILE__, __LINE__, __FUNCTION__, LOG_TAG_SDK, format, ##__VA_ARGS__);
#define logtrace(format, ...) LOG_CALL_FUNC(trace, format, ##__VA_ARGS__);
//信息日志宏函数
#define loginfo(format, ...) Logxx->info (__FILE__, __LINE__, __FUNCTION__, LOG_TAG_SDK, format, ##__VA_ARGS__);
#define loginfo(format, ...) LOG_CALL_FUNC(info, format, ##__VA_ARGS__);
//流水日志宏函数
#define lognote(format, ...) Logxx->note (__FILE__, __LINE__, __FUNCTION__, LOG_TAG_SDK, format, ##__VA_ARGS__);
#define lognote(format, ...) LOG_CALL_FUNC(note, format, ##__VA_ARGS__);
//调试日志宏函数
#define logdebug(format, ...) Logxx->debug (__FILE__, __LINE__, __FUNCTION__, LOG_TAG_SDK, format, ##__VA_ARGS__);
#define logdebug(format, ...) LOG_CALL_FUNC(debug, format, ##__VA_ARGS__);
#else

View File

@ -304,12 +304,12 @@ std::string Tools::toJson(const nlohmann::json& root)
std::string Tools::gbkToUtf8(const std::string& str)
{
return __sdk_gbk_to_utf8__(str.data());
return __sdk_charset_convert__(str.data(), CharsetTypeGBK, CharsetTypeUTF8);
}
std::string Tools::utf8ToGbk(const std::string& str)
{
return __sdk_utf8_to_gbk__(str.data());
return __sdk_charset_convert__(str.data(), CharsetTypeUTF8, CharsetTypeGBK);
}
std::string Tools::tolower(const std::string& s)

View File

@ -4881,14 +4881,27 @@ void ExamCarSub3::playItemSound(TKM3Item* item, bool enter)
if(item == nullptr) return;
std::string sou = item->Item_Code + (enter ? "1" : "2");
std::string code;
std::string code2;
int type = PlaySoundDefault;
if(item->ItemNo == Sub3ItemType01Sczb)
{
code = enter ? sound::sub3_40100 : ""; //"40100" sound::sub3_40100 item->Item_Code
//code = enter ? sound::sub3_40100 : ""; //"40100" sound::sub3_40100 item->Item_Code
if(enter)
{
code = sound::sub3_40100;
}
else
{
if(isItemPassed(Sub3ItemType41Mndg))
{
//有可能先考模拟灯光,也有可能做完模拟灯光在考上车准备
code2 = (isExamDrill() || isQualified() ? sound::sub3_402001 : "");
}
}
}
else if(item->ItemNo == Sub3ItemType02Qbxx)
{
code = enter ? sou : "";
//402001 起步只在模拟灯光结束的时候报一次请起步继续完成考试语音
}
else if(item->ItemNo == Sub3ItemType03Zxxs)
{
@ -4998,7 +5011,11 @@ void ExamCarSub3::playItemSound(TKM3Item* item, bool enter)
//ToDo1:播报语音"模拟夜间灯光使用考试结束,请关闭所有灯光';
//ToDo2:播报语音“请起步,继续完成考试';
code = (isExamDrill() || isQualified() ? sound::sub3_4170015 : "");
//code = (isExamDrill() || isQualified() ? sound::sub3_402001 : "");
if(isItemPassed(Sub3ItemType01Sczb))
{
//有可能先考模拟灯光,也有可能做完模拟灯光在考上车准备
code2 = (isExamDrill() || isQualified() ? sound::sub3_402001 : "");
}
}
}
else
@ -5010,6 +5027,10 @@ void ExamCarSub3::playItemSound(TKM3Item* item, bool enter)
{
createEventSound({item->ItemNo, code, type});
}
if(!code2.empty())
{
createEventSound({item->ItemNo, code2, type});
}
}
void ExamCarSub3::dealOffset()