C 的方式

//读取文件验证
FILE * fp = NULL;
errno_t ret = fopen_s(&fp,w2s(strAppPath).c_str(), "rb");
if (ret)
{
OutputDebugString(_T("open active file failed."));
m_flag = 1;
return TRUE;
}
else
{
uint32_t length = 0;
size_t ret = fread(&length, 1, 4, fp);
if (ret != 4)
{
OutputDebugString(_T("read active file failed."));
m_flag = 1;
fclose(fp);
return TRUE;
}

char* pbuf = new char[length];
memset(pbuf, 0, length);
ret = fread(pbuf, 1, length, fp);
fclose(fp);
...
delete[] pbuf;
}

写入文件

FILE * fp = NULL;
errno_t ret = fopen_s(&fp, w2s(strAppPath).c_str(), "wb");
if (ret)
{
OutputDebugString(_T("open active file for write failed."));
AfxMessageBox(_T("保存激活信息失败,请确认程序目录可写。"));
return;
}
else {
strInfo = w2s(str.GetBuffer());
int iRet = fwrite(strInfo.c_str(), sizeof(char), strInfo.size(), fp);
if (iRet != strInfo.size())
{
OutputDebugString(_T("write register info failed."));
AfxMessageBox(_T("保存激活信息失败,请确认程序目录可写。。"));
}
}
fclose(fp);

C 的方式

//读取文件验证
FILE * fp = NULL;
errno_t ret = fopen_s(&fp,w2s(strAppPath).c_str(), "rb");
if (ret)
{
OutputDebugString(_T("open active file failed."));
m_flag = 1;
return TRUE;
}
else
{
uint32_t length = 0;
size_t ret = fread(&length, 1, 4, fp);
if (ret != 4)
{
OutputDebugString(_T("read active file failed."));
m_flag = 1;
fclose(fp);
return TRUE;
}

char* pbuf = new char[length];
memset(pbuf, 0, length);
ret = fread(pbuf, 1, length, fp);
fclose(fp);
...
delete[] pbuf;
}

写入文件

FILE * fp = NULL;
errno_t ret = fopen_s(&fp, w2s(strAppPath).c_str(), "wb");
if (ret)
{
OutputDebugString(_T("open active file for write failed."));
AfxMessageBox(_T("保存激活信息失败,请确认程序目录可写。"));
return;
}
else {
strInfo = w2s(str.GetBuffer());
int iRet = fwrite(strInfo.c_str(), sizeof(char), strInfo.size(), fp);
if (iRet != strInfo.size())
{
OutputDebugString(_T("write register info failed."));
AfxMessageBox(_T("保存激活信息失败,请确认程序目录可写。。"));
}
}
fclose(fp);

ECC 椭圆加密算法

easy-ecc

bitcoin use Secp256k1

使用

stdafx.h 里面包含

#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>
extern std::shared_ptr<spdlog::logger> g_logger;

stdafx.cpp 里面包含
std::shared_ptr<spdlog::logger> g_logger;

// 初始化
// 如果存在会返回实例,否则返回空
g_logger = spdlog::get("IoTBackend");
if (g_logger == nullptr)
{
g_logger = spdlog::rotating_logger_mt("IoTBackend", m_curInfo.appPath + "IoTBackend.log", 1048576 * 100, 10);
g_logger->set_level(spdlog::level::level_enum(logLevel));
g_logger->flush_on(spdlog::level::level_enum(logLevel));
g_logger->info("logger is started.");
}
g_logger->info(APP_VERSION);

// 关闭

// Release and close all loggers
spdlog::drop_all();

或者单独drop某个logger
spd::drop("console");
spd::drop("basic_logger");

格式化

spdlog::info("Hello, {}!", "World");
logger->info("Hello {} {} !!", "param1", 123.4);

spdlog::warn("Easy padding in numbers like {:08d}", 12);
spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
spdlog::info("Support for floats {:03.2f}", 1.23456);
spdlog::info("Positional args are {1} {0}..", "too", "supported");
spdlog::info("{:<30}", "left aligned");

条件输出

// trace=0 debug=1 info=2 warn=3 error=4 critical=5 off=6
g_logger->set_level(spdlog::level::trace);

m_pConf = confHelper::getInstance();
m_pConf->open(m_curInfo.appPath + "dev_noise_jnrs.json");
int32_t logLevel = m_pConf->readInt("logLevel");
g_logger->set_level(spdlog::level::level_enum(logLevel));
#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>

m_configPath = getAppPath() + _T("logs");

std::shared_ptr<spdlog::logger> m_logger;

if (FALSE == PathFileExists(m_configPath.c_str()))
{
// directory not exist
DWORD dwErrCode = GetLastError();
if (FALSE == CreateDirectory(m_configPath.c_str(), NULL))
{
OutputDebugString(L"videoHelper: 创建日志目录失败");
}
}

m_pConf = confHelper::getInstance();
m_pConf->open(m_curInfo.appPath + "dev_noise_jnrs.json");
int64_t logLevel = m_pConf->readInt("logLevel");

// 如果存在会返回实例,否则返回空
m_logger = spdlog::get("videoHelper");
if (m_logger == nullptr)
{
m_logger = spdlog::rotating_logger_mt("videoHelper", "c:/collectorShanghaiXiTongHa360ams.txt", 1048576 * 10, 3);
g_logger->set_level(spdlog::level::level_enum(logLevel));
g_logger->flush_on(spdlog::level::level_enum(logLevel));
m_logger->info("m_logger is started.");

}
// Create a file rotating logger with 10mb size max and 3 rotated files. 1048576*10
m_logger = spdlog::rotating_logger_mt("videoHelper", w2s(m_configPath) + "/videoHelperLog.txt", 1048576 * 10, 3);
// m_logger->info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
// m_logger->warn("Easy padding in numbers like {:08d}", 12);
// m_logger->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
// m_logger->info("Support for floats {:03.2f}", 1.23456);
// m_logger->info("Positional args are {1} {0}..", "too", "supported");
// m_logger->info("{:>8} aligned, {:<8} aligned", "right", "left");

m_logger->info("m_logger is started.");

释放

// Release and close all loggers
spdlog::drop_all();

或者单独drop某个logger
spd::drop("console");
spd::drop("basic_logger");

异步log

目前暂时未对性能造成影响,如果有影响了,可以使用这个看看

#include "spdlog/async.h"
void async_example()
{
// default thread pool settings can be modified *before* creating the async logger:
// spdlog::init_thread_pool(8192, 1); // queue with 8k items and 1 backing thread.
auto async_file = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/async_log.txt");
// alternatively:
// auto async_file = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>("async_file_logger", "logs/async_log.txt");

}

分割路径

_splitpath_s

BOOL SetCurrentPathToModulePath(HMODULE hModule)
{
TCHAR szPath[MAX_PATH];
if(::GetModuleFileName(hModule, szPath, MAX_PATH))
{
TCHAR drive[MAX_PATH], dir[MAX_PATH], fname[MAX_PATH], ext[MAX_PATH];
_tsplitpath_s(szPath,drive, MAX_PATH,dir, MAX_PATH,fname, MAX_PATH,ext, MAX_PATH);
lstrcpy(szPath, drive);
lstrcat(szPath, dir);

return ::SetCurrentDirectory(szPath);
}

return FALSE;
}

路径是否存在

if (FALSE == PathFileExists( (m_configPath + _T("logs")).c_str))
{
// directory not exist
DWORD dwErrCode = GetLastError();
}

crate folder

//create a path with title name
if ( FALSE == CreateDirectory(tmpSrcPath,NULL) )
{
AfxMessageBox(TEXT("Create directory failed"));
return;
}

文件相关处理,copy,move,delete

CopyFile(),DeleteFile(),MoveFile()


#define OP_RENAME 0
#define OP_COPY 1
#define OP_DELETE 2
#define OP_MOVE 3
/*
rename file or directory
@param iType [in] 0 rename, 1 copy,2 delete,3 move
0 success
not zero failed.
*/
int filePathProc(const wchar_t *pFrom, const wchar_t *pTo, int iType)
{
SHFILEOPSTRUCT FileOp = { 0 };
wchar_t src[MAX_PATH] = { '\0' };
wchar_t dst[MAX_PATH] = { '\0' };
if ( NULL == pTo )
{
FileOp.pTo = NULL;
} else
{
_tcscpy(dst, pTo);
FileOp.pTo = dst;
}
if ( NULL == pFrom )
{
return 1;
}
_tcscpy(src, pFrom);

switch (iType)
{
case 0:
FileOp.wFunc = FO_RENAME;
break;
case 1:
FileOp.wFunc = FO_COPY;
break;
case 2:
FileOp.wFunc = FO_DELETE;
FileOp.pTo = NULL;
break;
case 3:
FileOp.wFunc = FO_MOVE;
default:
break;
}
FileOp.fFlags = FOF_NOCONFIRMATION | FOF_ALLOWUNDO | FOF_NOCONFIRMMKDIR; //不出现确认对话框
FileOp.pFrom = src;

return SHFileOperation(&FileOp);
}

int bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("456")).c_str(), OP_RENAME);
_ASSERT(false == bRet);

string strUtf8;
UnicodeConverter::toUTF8(utf16Path + TEXT("456"), strUtf8);
File f1( strUtf8 );
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("456")).c_str(), (utf16Path + TEXT("123")).c_str(), OP_RENAME);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("123"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("1233")).c_str(), OP_COPY);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1233"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("1234")).c_str(), OP_COPY);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1234"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("1234")).c_str(), (utf16Path + TEXT("1233")).c_str(), OP_MOVE);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1233\\1234"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("1233")).c_str(), NULL, OP_DELETE);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("123"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

分割路径

_splitpath_s

BOOL SetCurrentPathToModulePath(HMODULE hModule)
{
TCHAR szPath[MAX_PATH];
if(::GetModuleFileName(hModule, szPath, MAX_PATH))
{
TCHAR drive[MAX_PATH], dir[MAX_PATH], fname[MAX_PATH], ext[MAX_PATH];
_tsplitpath_s(szPath,drive, MAX_PATH,dir, MAX_PATH,fname, MAX_PATH,ext, MAX_PATH);
lstrcpy(szPath, drive);
lstrcat(szPath, dir);

return ::SetCurrentDirectory(szPath);
}

return FALSE;
}

路径是否存在

if (FALSE == PathFileExists( (m_configPath + _T("logs")).c_str))
{
// directory not exist
DWORD dwErrCode = GetLastError();
}

crate folder

//create a path with title name
if ( FALSE == CreateDirectory(tmpSrcPath,NULL) )
{
AfxMessageBox(TEXT("Create directory failed"));
return;
}

文件相关处理,copy,move,delete

CopyFile(),DeleteFile(),MoveFile()


#define OP_RENAME 0
#define OP_COPY 1
#define OP_DELETE 2
#define OP_MOVE 3
/*
rename file or directory
@param iType [in] 0 rename, 1 copy,2 delete,3 move
0 success
not zero failed.
*/
int filePathProc(const wchar_t *pFrom, const wchar_t *pTo, int iType)
{
SHFILEOPSTRUCT FileOp = { 0 };
wchar_t src[MAX_PATH] = { '\0' };
wchar_t dst[MAX_PATH] = { '\0' };
if ( NULL == pTo )
{
FileOp.pTo = NULL;
} else
{
_tcscpy(dst, pTo);
FileOp.pTo = dst;
}
if ( NULL == pFrom )
{
return 1;
}
_tcscpy(src, pFrom);

switch (iType)
{
case 0:
FileOp.wFunc = FO_RENAME;
break;
case 1:
FileOp.wFunc = FO_COPY;
break;
case 2:
FileOp.wFunc = FO_DELETE;
FileOp.pTo = NULL;
break;
case 3:
FileOp.wFunc = FO_MOVE;
default:
break;
}
FileOp.fFlags = FOF_NOCONFIRMATION | FOF_ALLOWUNDO | FOF_NOCONFIRMMKDIR; //不出现确认对话框
FileOp.pFrom = src;

return SHFileOperation(&FileOp);
}

int bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("456")).c_str(), OP_RENAME);
_ASSERT(false == bRet);

string strUtf8;
UnicodeConverter::toUTF8(utf16Path + TEXT("456"), strUtf8);
File f1( strUtf8 );
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("456")).c_str(), (utf16Path + TEXT("123")).c_str(), OP_RENAME);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("123"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("1233")).c_str(), OP_COPY);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1233"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("123")).c_str(), (utf16Path + TEXT("1234")).c_str(), OP_COPY);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1234"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("1234")).c_str(), (utf16Path + TEXT("1233")).c_str(), OP_MOVE);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("1233\\1234"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());

bRet = filePathProc((utf16Path + TEXT("1233")).c_str(), NULL, OP_DELETE);
_ASSERT(false == bRet);
UnicodeConverter::toUTF8(utf16Path + TEXT("123"), strUtf8);
f1 = (strUtf8);
_ASSERT(true == f1.exists());


func callHK() {
HCNetSDK, err := syscall.LoadLibrary("HCNetSDK.dll")
if err != nil {
abort("LoadLibrary", err)
}
defer syscall.FreeLibrary(HCNetSDK)
proc, err := syscall.GetProcAddress(HCNetSDK, "NET_DVR_Init")
if err != nil {
abort("NET_DVR_Init", err)
}
r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
fmt.Print(uint32(r))
}

MFC

CString appPath;
GetModuleFileName(NULL,appPath.GetBuffer(MAX_PATH),MAX_PATH);
appPath.ReleaseBuffer();
appPath = appPath.Left( appPath.ReverseFind('\\') + 1);

win32

string getAppPath()
{
TCHAR path[MAX_PATH + 2] = { '\0' };
if (0 == GetModuleFileName(NULL, path, MAX_PATH))
{
//failed
AfxMessageBox(TEXT("Get File Path Failed"));
ASSERT(FALSE);
}
TCHAR* pf = _tcsrchr(path, '\\');//strrchr
if (NULL == pf)
{
//not found
AfxMessageBox(TEXT("Invalide File Path"));
ASSERT(FALSE);
}
*(pf + 1) = '\0';//path is xx\\

string str = &path[0];
return str;
}

Linux

int GetAppPath(std::string &sPath)
{
char buf[4097] = { 0 };
long size;
char *ptr;
size = pathconf(".", _PC_PATH_MAX);
if ((ptr = new char(size)) != NULL)
{
memset(ptr, 0, size);
sprintf(ptr, "/proc/%d/exe", getpid());
} else return -1;
readlink(ptr, buf, size);
delete[] ptr;
ptr = NULL;
sPath = buf;
int nPos = sPath.find_last_of("/");
sPath = sPath.substr(0, nPos + 1);
return 0;
}