c++文件读取和写入宏定义
#pragma once
#include <string>
#include <fstream>
#include <streambuf>
#define FILE_READER(F, M) std::string((std::istreambuf_iterator<char>(std::ifstream(F, M | std::ios::in).rdbuf())), std::istreambuf_iterator<char>())
#define FILE_WRITER(D, S, F, M) std::ofstream(F, M | std::ios::out).write((D), (S))
#define FILE_READER_STRING(F) std::string((std::istreambuf_iterator<char>(std::ifstream(F, std::ios::in).rdbuf())), std::istreambuf_iterator<char>())
#define FILE_WRITER_STRING(S, F) std::ofstream(F, std::ios::out).write((S.data()), (S.size()))
c++编码转换
//通用版将wstring转化为string
__inline static
std::string WCToMB(const std::wstring& ws, unsigned long cp = CP_ACP)
{
if (ws.empty() == true)return ("");
std::string s(WideCharToMultiByte(cp, 0, ws.data(), ws.size(), NULL, 0, NULL, NULL), ('\0'));
return s.substr(0, WideCharToMultiByte(cp, 0, ws.c_str(), ws.size(), (LPSTR)s.data(), (int)s.size(), NULL, NULL));
}
//通用版将string转化为wstring
__inline static
std::wstring MBToWC(const std::string& s, unsigned long cp = CP_ACP)
{
if (s.empty() == true)return (L"");
std::wstring ws(MultiByteToWideChar(cp, 0, s.data(), s.size(), NULL, 0), (L'\0'));
return ws.substr(0, MultiByteToWideChar(cp, 0, s.data(), s.size(), (LPWSTR)ws.data(), (int)ws.size()));
}
#define ANSIToWIDE(A) MBToWC(A,CP_ACP)
#define WIDEToANSI(W) WCToMB(W,CP_ACP)
#define UTF8ToWIDE(U) MBToWC(U,CP_UTF8)
#define WIDEToUTF8(W) WCToMB(W,CP_UTF8)
#define ANSIToUTF8(A) WIDEToUTF8(ANSIToWIDE(A))
#define UTF8ToANSI(U) WIDEToANSI(UTF8ToWIDE(U))
c++字符串格式化
__inline static
std::string format_string(const char* format, ...)
{
va_list args = nullptr;
va_start(args, format);
std::vector<wchar_t> buffer(BUFSIZ);
int length = vsnprintf((char* const)buffer.data(), buffer.size(), format, args);
if (length > buffer.size())
{
buffer.resize(length);
vsnprintf((char* const)buffer.data(), length, format, args);
}
va_end(args);
return ((char* const)buffer.data());
}
__inline static
std::wstring format_wstring(const wchar_t* format, ...)
{
va_list args = nullptr;
va_start(args, format);
std::vector<wchar_t> buffer(BUFSIZ);
int length = _vsnwprintf(buffer.data(), buffer.size(), format, args);
if (length > buffer.size())
{
buffer.resize(length);
_vsnwprintf(buffer.data(), length, format, args);
}
va_end(args);
return (buffer.data());
}
class ConsoleManager
{
FILE* pStdIn = NULL;
FILE* pStdOut = NULL;
FILE* pStdErr = NULL;
public:
static ConsoleManager* CM()
{
static ConsoleManager ConsoleManagerInstance = {};
return &ConsoleManagerInstance;
}
public:
void FormatPrintf(const char* format, ...)
{
va_list va = nullptr;
va_start(va, format);
if (pStdOut != NULL)
{
::vfprintf(pStdOut, format, va);
::fflush(pStdOut);
}
else
{
::vfprintf(stdout, format, va);
}
va_end(va);
}
void FormatPrintfW(const wchar_t* format, ...)
{
va_list va = nullptr;
va_start(va, format);
if (pStdOut != NULL)
{
::vfwprintf(pStdOut, format, va);
::fflush(pStdOut);
}
else
{
::vfwprintf(stdout, format, va);
}
va_end(va);
}
void StartConsole()
{
::setlocale(LC_ALL, ("chs"));
if (::AllocConsole() == TRUE)
{
::freopen_s(&pStdIn, ("CONIN$"), ("r"), stdin);
::freopen_s(&pStdOut, ("CONOUT$"), ("w"), stdout);
::freopen_s(&pStdErr, ("CONOUT$"), ("w"), stderr);
}
}
void CloseConsole()
{
if (pStdIn != NULL)
{
::fclose(pStdIn);
pStdIn = NULL;
}
if (pStdOut != NULL)
{
::fclose(pStdOut);
pStdOut = NULL;
}
if (pStdErr != NULL)
{
::fclose(pStdErr);
pStdErr = NULL;
}
::FreeConsole();
}
public:
ConsoleManager()
{
#if defined(_DEBUG) || defined(DEBUG)
StartConsole();
#endif
}
virtual ~ConsoleManager()
{
#if defined(_DEBUG) || defined(DEBUG)
CloseConsole();
#endif
}
};