fast/Plugin/Plc/stdafx.cpp
2025-01-20 10:30:01 +08:00

189 lines
4.1 KiB
C++

// stdafx.cpp : 只包括标准包含文件的源文件
// Vcs-Client.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
#define MIN_XYZ_VALUE (-999999)
float* g_xys = new float[3000 * 3000 * 2];
int g_xyz_flag = 0;
#define Q_R 1500
#define X0 1500
#define Y0 1500
inline float abs_m(float lf)
{
if (lf < 0) lf *= -1;
return lf;
}
CString g_strSavePath = "";
CString g_strCurTime = "";
void LogOutToFile(const char* fmt, ...)
{
static CRITICAL_SECTION stCritical;
static BOOL bInit = FALSE;
static CString strLogPath;
if (FALSE == bInit)
{
bInit = TRUE;
GetModuleFileName(NULL, strLogPath.GetBuffer(2048), 2047);
strLogPath.ReleaseBuffer();
strLogPath = strLogPath.Left(strLogPath.ReverseFind('\\'));
strLogPath += "\\log\\runtime.log";
InitializeCriticalSection(&stCritical);
}
EnterCriticalSection(&stCritical);
va_list ap;
va_start(ap, fmt);
char pBuffer[800] = "";
if (vsnprintf_s(pBuffer, 796, _TRUNCATE, fmt, ap) > 0)
{
if (strlen(pBuffer) == 0 || pBuffer[strlen(pBuffer) - 1] != '\n')
{
pBuffer[strlen(pBuffer) + 1] = '\0';//最后再追加一个结束符,因为原结束符需要修改为换行符
pBuffer[strlen(pBuffer)] = '\n';
}
}
va_end(ap);
TRACE("%s\n", pBuffer);
FILE* pFile = NULL;
fopen_s(&pFile, strLogPath.GetBuffer(), "ab+");
if (NULL != pFile)
{
char acTime[32] = { 0 };
SYSTEMTIME stTime;
GetLocalTime(&stTime);
sprintf_s(acTime, 32, "[%02d-%02d %02d:%02d:%02d.%03d] ", stTime.wMonth, stTime.wDay, stTime.wHour, stTime.wMinute, stTime.wSecond, stTime.wMilliseconds);
fwrite(acTime, 1, strlen(acTime), pFile);
fwrite(pBuffer, 1, strlen(pBuffer), pFile);
fwrite("\r\n", 1, strlen("\r\n"), pFile);
fclose(pFile);
}
LeaveCriticalSection(&stCritical);
}
int g_nMsgIdx = 998;
string _UnicodeToUtf8(CString Unicodestr)
{
wchar_t* unicode = Unicodestr.AllocSysString();
int len;
len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
char *szUtf8 = (char*)malloc(len + 1);
memset(szUtf8, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
string result = szUtf8;
free(szUtf8);
return result;
}
// UTF8与多字节(MultiByte)互转
CString UTF8AndMB_Convert(const CString &strSource, UINT nSourceCodePage, UINT nTargetCodePage)
{
int nSourceLen = strSource.GetLength();
int nWideBufLen = MultiByteToWideChar(nSourceCodePage, 0, strSource, -1, NULL, 0);
wchar_t* pWideBuf = new wchar_t[nWideBufLen + 1];
memset(pWideBuf, 0, (nWideBufLen + 1) * sizeof(wchar_t));
MultiByteToWideChar(nSourceCodePage, 0, strSource, -1, (LPWSTR)pWideBuf, nWideBufLen);
char* pMultiBuf = NULL;
int nMiltiBufLen = WideCharToMultiByte(nTargetCodePage, 0, (LPWSTR)pWideBuf, -1, (char *)pMultiBuf, 0, NULL, NULL);
pMultiBuf = new char[nMiltiBufLen + 1];
memset(pMultiBuf, 0, nMiltiBufLen + 1);
WideCharToMultiByte(nTargetCodePage, 0, (LPWSTR)pWideBuf, -1, (char *)pMultiBuf, nMiltiBufLen, NULL, NULL);
CStringA strTarget(pMultiBuf);
delete[] pWideBuf;
delete[] pMultiBuf;
return strTarget;
}
CStringA UTF8ToMB(const CStringA& utf8Str)
{
if (IsTextUTF8(utf8Str, utf8Str.GetLength()))
{
return UTF8AndMB_Convert(utf8Str, CP_UTF8, CP_ACP);
}
return utf8Str;
}
bool IsTextUTF8(const char* str, ULONGLONG length)
{
DWORD nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
UCHAR chr;
bool bAllAscii = true; //如果全部都是ASCII, 说明不是UTF-8
for (int i = 0; i < length; i++)
{
chr = *(str + i);
if ((chr & 0x80) != 0) // 判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx
bAllAscii = false;
if (nBytes == 0) //如果不是ASCII码,应该是多字节符,计算字节数
{
if (chr >= 0x80)
{
if (chr >= 0xFC && chr <= 0xFD)
nBytes = 6;
else if (chr >= 0xF8)
nBytes = 5;
else if (chr >= 0xF0)
nBytes = 4;
else if (chr >= 0xE0)
nBytes = 3;
else if (chr >= 0xC0)
nBytes = 2;
else
{
return false;
}
nBytes--;
}
}
else //多字节符的非首字节,应为 10xxxxxx
{
if ((chr & 0xC0) != 0x80)
{
return false;
}
nBytes--;
}
}
if (nBytes > 0) //违返规则
{
return false;
}
if (bAllAscii) //如果全部都是ASCII, 说明不是UTF-8
{
return false;
}
return true;
}