finish terminal helper
This commit is contained in:
parent
7258c4c92b
commit
6ebb457bd1
@ -5,157 +5,157 @@
|
||||
|
||||
namespace YYCC::DialogHelper {
|
||||
|
||||
template<bool TIsOpen, bool TMultiSelection>
|
||||
bool GeneralFileDialog(const FileDialogParameter& params, std::vector<std::string>& ret) {
|
||||
// make sure multi-selection only available in open mode
|
||||
static_assert(TIsOpen && (!TIsOpen && TMultiSelection == false));
|
||||
//template<bool TIsOpen, bool TMultiSelection>
|
||||
//bool GeneralFileDialog(const FileDialogParameter& params, std::vector<std::string>& ret) {
|
||||
// // make sure multi-selection only available in open mode
|
||||
// static_assert(TIsOpen && (!TIsOpen && TMultiSelection == false));
|
||||
|
||||
// build filter
|
||||
std::wstring w_filter;
|
||||
for (const auto& filter_pair : params.m_Filter) {
|
||||
w_filter += EncodingHelper::UTF8ToWchar(filter_pair.first.c_str());
|
||||
w_filter += L'\0';
|
||||
w_filter += EncodingHelper::UTF8ToWchar(filter_pair.second.c_str());
|
||||
w_filter += L'\0';
|
||||
}
|
||||
// build title
|
||||
std::wstring w_title(EncodingHelper::UTF8ToWchar(params.m_Title.c_str()));
|
||||
// build default extension
|
||||
std::wstring w_default_ext(EncodingHelper::UTF8ToWchar(params.m_DefaultExtension.c_str()));
|
||||
// build initial directory
|
||||
std::wstring w_init_dir(EncodingHelper::UTF8ToWchar(params.m_InitialDirectory.c_str()));
|
||||
// prepare file name receiver and preset it as initial file name
|
||||
std::wstring path_receiver(EncodingHelper::UTF8ToWchar(params.m_InitialFileName.c_str()));
|
||||
path_receiver.resize(std::max(MAX_PATH, path_receiver.size()), L'\0');
|
||||
// // build filter
|
||||
// std::wstring w_filter;
|
||||
// for (const auto& filter_pair : params.m_Filter) {
|
||||
// w_filter += EncodingHelper::UTF8ToWchar(filter_pair.first.c_str());
|
||||
// w_filter += L'\0';
|
||||
// w_filter += EncodingHelper::UTF8ToWchar(filter_pair.second.c_str());
|
||||
// w_filter += L'\0';
|
||||
// }
|
||||
// // build title
|
||||
// std::wstring w_title(EncodingHelper::UTF8ToWchar(params.m_Title.c_str()));
|
||||
// // build default extension
|
||||
// std::wstring w_default_ext(EncodingHelper::UTF8ToWchar(params.m_DefaultExtension.c_str()));
|
||||
// // build initial directory
|
||||
// std::wstring w_init_dir(EncodingHelper::UTF8ToWchar(params.m_InitialDirectory.c_str()));
|
||||
// // prepare file name receiver and preset it as initial file name
|
||||
// std::wstring path_receiver(EncodingHelper::UTF8ToWchar(params.m_InitialFileName.c_str()));
|
||||
// path_receiver.resize(std::max(MAX_PATH, path_receiver.size()), L'\0');
|
||||
|
||||
// prepare the common part of file dialog struct
|
||||
OPENFILENAMEW dialog_param;
|
||||
ZeroMemory(&dialog_param, sizeof(OPENFILENAMEW));
|
||||
dialog_param.lStructSize = sizeof(OPENFILENAMEW);
|
||||
// dialog owner
|
||||
dialog_param.hwndOwner = params.m_Owner;
|
||||
// if no filter, we pass NULL
|
||||
dialog_param.lpstrFilter = w_filter.empty() ? NULL : w_filter.c_str();
|
||||
// no record to user selected filter
|
||||
dialog_param.lpstrCustomFilter = NULL;
|
||||
dialog_param.nMaxCustFilter = 0;
|
||||
dialog_param.nFilterIndex = 0;
|
||||
// path receiver, also is init filename
|
||||
dialog_param.lpstrFile = path_receiver.data();
|
||||
dialog_param.nMaxFile = static_caast<DWORD>(path_receiver.size());
|
||||
// no selected file infos
|
||||
dialog_param.lpstrFileTitle = NULL;
|
||||
dialog_param.nMaxFileTitle = 0;
|
||||
// initial directory
|
||||
dialog_param.lpstrInitialDir = w_init_dir.empty() ? NULL : w_init_dir.c_str();
|
||||
// dialog title
|
||||
dialog_param.lpstrTitle = w_title.empty() ? NULL : w_title.c_str();
|
||||
// setup basic flags
|
||||
dialog_param.Flags = OFN_EXPLORER;
|
||||
// default extension
|
||||
dialog_param.lpstrDefExt = w_default_ext.empty() ? NULL : w_default_ext.c_str();
|
||||
// // prepare the common part of file dialog struct
|
||||
// OPENFILENAMEW dialog_param;
|
||||
// ZeroMemory(&dialog_param, sizeof(OPENFILENAMEW));
|
||||
// dialog_param.lStructSize = sizeof(OPENFILENAMEW);
|
||||
// // dialog owner
|
||||
// dialog_param.hwndOwner = params.m_Owner;
|
||||
// // if no filter, we pass NULL
|
||||
// dialog_param.lpstrFilter = w_filter.empty() ? NULL : w_filter.c_str();
|
||||
// // no record to user selected filter
|
||||
// dialog_param.lpstrCustomFilter = NULL;
|
||||
// dialog_param.nMaxCustFilter = 0;
|
||||
// dialog_param.nFilterIndex = 0;
|
||||
// // path receiver, also is init filename
|
||||
// dialog_param.lpstrFile = path_receiver.data();
|
||||
// dialog_param.nMaxFile = static_caast<DWORD>(path_receiver.size());
|
||||
// // no selected file infos
|
||||
// dialog_param.lpstrFileTitle = NULL;
|
||||
// dialog_param.nMaxFileTitle = 0;
|
||||
// // initial directory
|
||||
// dialog_param.lpstrInitialDir = w_init_dir.empty() ? NULL : w_init_dir.c_str();
|
||||
// // dialog title
|
||||
// dialog_param.lpstrTitle = w_title.empty() ? NULL : w_title.c_str();
|
||||
// // setup basic flags
|
||||
// dialog_param.Flags = OFN_EXPLORER;
|
||||
// // default extension
|
||||
// dialog_param.lpstrDefExt = w_default_ext.empty() ? NULL : w_default_ext.c_str();
|
||||
|
||||
BOOL status;
|
||||
if constexpr (TIsOpen) {
|
||||
// multi-selection need add special multi-selection flags
|
||||
if constexpr (TMultiSelection) {
|
||||
dialog_param.Flags |= OFN_ALLOWMULTISELECT;
|
||||
}
|
||||
// BOOL status;
|
||||
// if constexpr (TIsOpen) {
|
||||
// // multi-selection need add special multi-selection flags
|
||||
// if constexpr (TMultiSelection) {
|
||||
// dialog_param.Flags |= OFN_ALLOWMULTISELECT;
|
||||
// }
|
||||
|
||||
// call browser
|
||||
status = GetOpenFileNameW(&dialog_param);
|
||||
// // call browser
|
||||
// status = GetOpenFileNameW(&dialog_param);
|
||||
|
||||
// only process result when success
|
||||
if (status) {
|
||||
if constexpr (TMultiSelection) {
|
||||
// get directory part, copy from start to dialog param specified offset
|
||||
std::wstring w_directory_part(path_receiver.c_str(), dialog_param.nFileOffset);
|
||||
// get file names part one by one
|
||||
size_t filename_cursor = dialog_param.nFileOffset;
|
||||
while (path_receiver[filename_cursor] != L'\0') {
|
||||
// init wstring from given offset
|
||||
std::wstring w_filename_part(path_receiver.c_str() + filename_cursor);
|
||||
// get eaten chars from result and increase to cursor
|
||||
filename_cursor += w_filename_part.size() + 1u;
|
||||
// combine 2 parts and insert into list
|
||||
ret.emplace_back(u8string(EncodingHelper::WcharToUTF8(w_directory_part + w_filename_part)));
|
||||
}
|
||||
} else {
|
||||
ret.emplace_back(u8string(EncodingHelper::WcharToUTF8(path_receiver.c_str())));
|
||||
}
|
||||
}
|
||||
// // only process result when success
|
||||
// if (status) {
|
||||
// if constexpr (TMultiSelection) {
|
||||
// // get directory part, copy from start to dialog param specified offset
|
||||
// std::wstring w_directory_part(path_receiver.c_str(), dialog_param.nFileOffset);
|
||||
// // get file names part one by one
|
||||
// size_t filename_cursor = dialog_param.nFileOffset;
|
||||
// while (path_receiver[filename_cursor] != L'\0') {
|
||||
// // init wstring from given offset
|
||||
// std::wstring w_filename_part(path_receiver.c_str() + filename_cursor);
|
||||
// // get eaten chars from result and increase to cursor
|
||||
// filename_cursor += w_filename_part.size() + 1u;
|
||||
// // combine 2 parts and insert into list
|
||||
// ret.emplace_back(std::string(EncodingHelper::WcharToUTF8(w_directory_part + w_filename_part)));
|
||||
// }
|
||||
// } else {
|
||||
// ret.emplace_back(std::string(EncodingHelper::WcharToUTF8(path_receiver.c_str())));
|
||||
// }
|
||||
// }
|
||||
|
||||
} else {
|
||||
// call browser
|
||||
status = GetSaveFileNameW(&dialog_param);
|
||||
// } else {
|
||||
// // call browser
|
||||
// status = GetSaveFileNameW(&dialog_param);
|
||||
|
||||
// only process result when success
|
||||
if (status) {
|
||||
ret.emplace_back(u8string(EncodingHelper::WcharToUTF8(path_receiver.c_str())));
|
||||
}
|
||||
// // only process result when success
|
||||
// if (status) {
|
||||
// ret.emplace_back(std::string(EncodingHelper::WcharToUTF8(path_receiver.c_str())));
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
// if failed, clear result
|
||||
// and return result
|
||||
if (!status) {
|
||||
ret.clear();
|
||||
}
|
||||
return status == TRUE;
|
||||
}
|
||||
// // if failed, clear result
|
||||
// // and return result
|
||||
// if (!status) {
|
||||
// ret.clear();
|
||||
// }
|
||||
// return status == TRUE;
|
||||
//}
|
||||
|
||||
bool OpenFileDialog(const FileDialogParameter& params, std::string& ret) {
|
||||
std::vector<std::string> cache;
|
||||
bool isok = GeneralFileDialog<true, false>(params, cache);
|
||||
if (isok) ret = cache.front();
|
||||
return isok;
|
||||
}
|
||||
bool OpenMultipleFileDialog(const FileDialogParameter& params, std::vector<std::string>& ret) {
|
||||
return GeneralFileDialog<true, true>(params, ret);
|
||||
}
|
||||
bool SaveFileDialog(const FileDialogParameter& params, std::string& ret) {
|
||||
std::vector<std::string> cache;
|
||||
bool isok = GeneralFileDialog<false, false>(params, cache);
|
||||
if (isok) ret = cache.front();
|
||||
return isok;
|
||||
}
|
||||
//bool OpenFileDialog(const FileDialogParameter& params, std::string& ret) {
|
||||
// std::vector<std::string> cache;
|
||||
// bool isok = GeneralFileDialog<true, false>(params, cache);
|
||||
// if (isok) ret = cache.front();
|
||||
// return isok;
|
||||
//}
|
||||
//bool OpenMultipleFileDialog(const FileDialogParameter& params, std::vector<std::string>& ret) {
|
||||
// return GeneralFileDialog<true, true>(params, ret);
|
||||
//}
|
||||
//bool SaveFileDialog(const FileDialogParameter& params, std::string& ret) {
|
||||
// std::vector<std::string> cache;
|
||||
// bool isok = GeneralFileDialog<false, false>(params, cache);
|
||||
// if (isok) ret = cache.front();
|
||||
// return isok;
|
||||
//}
|
||||
|
||||
bool OpenFolderDialog(const FolderDialogParameter& params, std::string& ret) {
|
||||
// create wchar string cache for windows W-tail function
|
||||
std::wstring w_title(EncodingHelper::UTF8ToWchar(params.m_Title.c_str()));
|
||||
// create buffer for receiving selected folder
|
||||
// initialize with MAX_PATH length and content is filled with zero.
|
||||
std::wstring path_receiver(MAX_PATH, L'\0');
|
||||
//bool OpenFolderDialog(const FolderDialogParameter& params, std::string& ret) {
|
||||
// // create wchar string cache for windows W-tail function
|
||||
// std::wstring w_title(EncodingHelper::UTF8ToWchar(params.m_Title.c_str()));
|
||||
// // create buffer for receiving selected folder
|
||||
// // initialize with MAX_PATH length and content is filled with zero.
|
||||
// std::wstring path_receiver(MAX_PATH, L'\0');
|
||||
|
||||
// prepare folder foalog struct
|
||||
BROWSEINFOW dialog_param = { 0 };
|
||||
dialog_param.hwndOwner = params.m_Owner;
|
||||
dialog_param.pidlRoot = nullptr;
|
||||
dialog_param.pszDisplayName = path_receiver.data();
|
||||
dialog_param.lpszTitle = w_title.c_str();
|
||||
dialog_param.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
|
||||
dialog_param.lpfn = nullptr;
|
||||
// // prepare folder foalog struct
|
||||
// BROWSEINFOW dialog_param = { 0 };
|
||||
// dialog_param.hwndOwner = params.m_Owner;
|
||||
// dialog_param.pidlRoot = nullptr;
|
||||
// dialog_param.pszDisplayName = path_receiver.data();
|
||||
// dialog_param.lpszTitle = w_title.c_str();
|
||||
// dialog_param.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
|
||||
// dialog_param.lpfn = nullptr;
|
||||
|
||||
// call browser
|
||||
PIDLIST_ABSOLUTE place = SHBrowseForFolderW(&dialog_param);
|
||||
// if browser failed, return.
|
||||
if (place == nullptr) {
|
||||
ret.clear();
|
||||
return false;
|
||||
}
|
||||
// // call browser
|
||||
// PIDLIST_ABSOLUTE place = SHBrowseForFolderW(&dialog_param);
|
||||
// // if browser failed, return.
|
||||
// if (place == nullptr) {
|
||||
// ret.clear();
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// get path from browser result
|
||||
BOOL status;
|
||||
if (status = SHGetPathFromIDListW(place, path_receiver.data())) {
|
||||
EncodingHelper::WcharToUTF8(path_receiver.c_str(), ret);
|
||||
} else {
|
||||
ret.clear();
|
||||
}
|
||||
// // get path from browser result
|
||||
// BOOL status;
|
||||
// if (status = SHGetPathFromIDListW(place, path_receiver.data())) {
|
||||
// EncodingHelper::WcharToUTF8(path_receiver.c_str(), ret);
|
||||
// } else {
|
||||
// ret.clear();
|
||||
// }
|
||||
|
||||
// clear browser result and return
|
||||
CoTaskMemFree(place);
|
||||
return status == TRUE;
|
||||
}
|
||||
// // clear browser result and return
|
||||
// CoTaskMemFree(place);
|
||||
// return status == TRUE;
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
|
@ -13,36 +13,36 @@
|
||||
|
||||
namespace YYCC::DialogHelper {
|
||||
|
||||
struct FileDialogParameter {
|
||||
FileDialogParameter() :
|
||||
m_Owner(nullptr),
|
||||
m_Filter(), m_SelectedFilter(0),
|
||||
m_Title(),
|
||||
m_DefaultExtension(), m_InitialDirectory(), m_InitialFileName() {}
|
||||
//struct FileDialogParameter {
|
||||
// FileDialogParameter() :
|
||||
// m_Owner(nullptr),
|
||||
// m_Filter(), m_SelectedFilter(0),
|
||||
// m_Title(),
|
||||
// m_DefaultExtension(), m_InitialDirectory(), m_InitialFileName() {}
|
||||
|
||||
HWND m_Owner;
|
||||
std::vector<std::pair<u8string, u8string>> m_Filter;
|
||||
size_t m_SelectedFilter;
|
||||
u8string m_Title;
|
||||
u8string m_DefaultExtension;
|
||||
u8string m_InitialFileName;
|
||||
u8string m_InitialDirectory;
|
||||
};
|
||||
// HWND m_Owner;
|
||||
// std::vector<std::pair<std::string, std::string>> m_Filter;
|
||||
// size_t m_SelectedFilter;
|
||||
// std::string m_Title;
|
||||
// std::string m_DefaultExtension;
|
||||
// std::string m_InitialFileName;
|
||||
// std::string m_InitialDirectory;
|
||||
//};
|
||||
|
||||
struct FolderDialogParameter {
|
||||
FolderDialogParameter() :
|
||||
m_Owner(nullptr),
|
||||
m_Title() {}
|
||||
//struct FolderDialogParameter {
|
||||
// FolderDialogParameter() :
|
||||
// m_Owner(nullptr),
|
||||
// m_Title() {}
|
||||
|
||||
HWND m_Owner;
|
||||
u8string m_Title;
|
||||
}
|
||||
// HWND m_Owner;
|
||||
// std::string m_Title;
|
||||
//};
|
||||
|
||||
bool OpenFileDialog(const FileDialogParameter& params, std::string& ret);
|
||||
bool OpenMultipleFileDialog(const FileDialogParameter& params, std::vector<std::string>& ret);
|
||||
bool SaveFileDialog(const FileDialogParameter& params, std::string& ret);
|
||||
//bool OpenFileDialog(const FileDialogParameter& params, std::string& ret);
|
||||
//bool OpenMultipleFileDialog(const FileDialogParameter& params, std::vector<std::string>& ret);
|
||||
//bool SaveFileDialog(const FileDialogParameter& params, std::string& ret);
|
||||
|
||||
bool OpenFolderDialog(const FolderDialogParameter& params, std::string& ret);
|
||||
//bool OpenFolderDialog(const FolderDialogParameter& params, std::string& ret);
|
||||
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
namespace YYCC::EncodingHelper {
|
||||
|
||||
bool WcharToChar(const wchar_t* src, u8string& dest, UINT codepage) {
|
||||
bool WcharToChar(const wchar_t* src, std::string& dest, UINT codepage) {
|
||||
int count, write_result;
|
||||
|
||||
//converter to CHAR
|
||||
@ -16,19 +16,19 @@ namespace YYCC::EncodingHelper {
|
||||
|
||||
return true;
|
||||
}
|
||||
bool WcharToUTF8(const wchar_t* src, u8string& dest) {
|
||||
bool WcharToUTF8(const wchar_t* src, std::string& dest) {
|
||||
return WcharToChar(src, dest, CP_UTF8);
|
||||
}
|
||||
u8string WcharToChar(const wchar_t* src, UINT codepage) {
|
||||
u8string ret;
|
||||
std::string WcharToChar(const wchar_t* src, UINT codepage) {
|
||||
std::string ret;
|
||||
if (!WcharToChar(src, ret, codepage)) ret.clear();
|
||||
return ret;
|
||||
}
|
||||
u8string WcharToUTF8(const wchar_t* src) {
|
||||
std::string WcharToUTF8(const wchar_t* src) {
|
||||
return WcharToChar(src, CP_UTF8);
|
||||
}
|
||||
|
||||
bool CharToWchar(const u8char* src, std::wstring& dest, UINT codepage) {
|
||||
bool CharToWchar(const char* src, std::wstring& dest, UINT codepage) {
|
||||
int wcount, write_result;
|
||||
|
||||
// convert to WCHAR
|
||||
@ -41,26 +41,26 @@ namespace YYCC::EncodingHelper {
|
||||
|
||||
return true;
|
||||
}
|
||||
bool UTF8ToWchar(const u8char* src, std::wstring& dest) {
|
||||
bool UTF8ToWchar(const char* src, std::wstring& dest) {
|
||||
return CharToWchar(src, dest, CP_UTF8);
|
||||
}
|
||||
std::wstring CharToWchar(const u8char* src, UINT codepage) {
|
||||
std::wstring CharToWchar(const char* src, UINT codepage) {
|
||||
std::wstring ret;
|
||||
if (!CharToWchar(src, ret, codepage)) ret.clear();
|
||||
return ret;
|
||||
}
|
||||
std::wstring UTF8ToWchar(const u8char* src) {
|
||||
std::wstring UTF8ToWchar(const char* src) {
|
||||
return CharToWchar(src, CP_UTF8);
|
||||
}
|
||||
|
||||
bool CharToChar(const u8char* src, u8string& dest, UINT src_codepage, UINT dest_codepage) {
|
||||
bool CharToChar(const char* src, std::string& dest, UINT src_codepage, UINT dest_codepage) {
|
||||
std::wstring intermediary;
|
||||
if (!CharToWchar(src, intermediary, src_codepage)) return false;
|
||||
if (!WcharToChar(intermediary.c_str(), dest, dest_codepage)) return false;
|
||||
return true;
|
||||
}
|
||||
u8string CharToChar(const u8char* src, UINT src_codepage, UINT dest_codepage) {
|
||||
u8string ret;
|
||||
std::string CharToChar(const char* src, UINT src_codepage, UINT dest_codepage) {
|
||||
std::string ret;
|
||||
if (!CharToChar(src, ret, src_codepage, dest_codepage)) ret.clear();
|
||||
return ret;
|
||||
}
|
||||
|
@ -10,18 +10,18 @@
|
||||
|
||||
namespace YYCC::EncodingHelper {
|
||||
|
||||
bool WcharToChar(const wchar_t* src, u8string& dest, UINT codepage);
|
||||
bool WcharToUTF8(const wchar_t* src, u8string& dest);
|
||||
u8string WcharToChar(const wchar_t* src, UINT codepage);
|
||||
u8string WcharToUTF8(const wchar_t* src);
|
||||
bool WcharToChar(const wchar_t* src, std::string& dest, UINT codepage);
|
||||
bool WcharToUTF8(const wchar_t* src, std::string& dest);
|
||||
std::string WcharToChar(const wchar_t* src, UINT codepage);
|
||||
std::string WcharToUTF8(const wchar_t* src);
|
||||
|
||||
bool CharToWchar(const u8char* src, std::wstring& dest, UINT codepage);
|
||||
bool UTF8ToWchar(const u8char* src, std::wstring& dest);
|
||||
std::wstring CharToWchar(const u8char* src, UINT codepage);
|
||||
std::wstring UTF8ToWchar(const u8char* src);
|
||||
bool CharToWchar(const char* src, std::wstring& dest, UINT codepage);
|
||||
bool UTF8ToWchar(const char* src, std::wstring& dest);
|
||||
std::wstring CharToWchar(const char* src, UINT codepage);
|
||||
std::wstring UTF8ToWchar(const char* src);
|
||||
|
||||
bool CharToChar(const u8char* src, u8string& dest, UINT src_codepage, UINT dest_codepage);
|
||||
u8string CharToChar(const u8char* src, UINT src_codepage, UINT dest_codepage);
|
||||
bool CharToChar(const char* src, std::string& dest, UINT src_codepage, UINT dest_codepage);
|
||||
std::string CharToChar(const char* src, UINT src_codepage, UINT dest_codepage);
|
||||
|
||||
}
|
||||
|
||||
|
@ -7,12 +7,6 @@
|
||||
|
||||
namespace YYCC::IOHelper {
|
||||
|
||||
void Gets(std::string& u8cmd) {
|
||||
std::wstring wcmd;
|
||||
std::getline(std::wcin, wcmd);
|
||||
YYCC::EncodingHelper::WcharToChar(wcmd, u8cmd, CP_UTF8);
|
||||
}
|
||||
|
||||
FILE* FOpen(const char* u8_filepath, const char* u8_mode) {
|
||||
std::wstring wmode, wpath;
|
||||
bool suc = YYCC::EncodingHelper::CharToWchar(u8_mode, wmode, CP_UTF8);
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
namespace YYCC::IOHelper {
|
||||
|
||||
void Gets(std::string& u8cmd);
|
||||
FILE* FOpen(const char* u8_filepath, const char* u8_mode);
|
||||
|
||||
}
|
||||
|
@ -10,6 +10,6 @@ namespace YYCC::StringHelper {
|
||||
std::string Printf(const char* format, ...);
|
||||
std::string VPrintf(const char* format, va_list argptr);
|
||||
|
||||
std::string Join(const char* decilmer);
|
||||
//std::string Join(const char* decilmer);
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
#include "TerminalHelper.hpp"
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
|
||||
#include "EncodingHelper.hpp"
|
||||
#include "StringHelper.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include "WinImportPrefix.hpp"
|
||||
#include <Windows.h>
|
||||
#include <io.h>
|
||||
@ -9,7 +13,7 @@
|
||||
|
||||
namespace YYCC::TerminalHelper {
|
||||
|
||||
bool ColorfulTerminal(FILE* fs) {
|
||||
bool EnsureTerminalColor(FILE* fs) {
|
||||
if (!_isatty(_fileno(fs))) return false;
|
||||
|
||||
HANDLE h_output;
|
||||
@ -22,15 +26,33 @@ namespace YYCC::TerminalHelper {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UTF8Terminal(FILE* fs) {
|
||||
bool EnsureTerminalUTF8(FILE* fs) {
|
||||
if (!SetConsoleCP(CP_UTF8)) return false;
|
||||
if (!SetConsoleOutputCP(CP_UTF8)) return false;
|
||||
|
||||
/*_setmode(_fileno(stdout), _O_U8TEXT);*/
|
||||
_setmode(_fileno(fs), _O_U16TEXT);
|
||||
int _ = _setmode(_fileno(fs), _O_U16TEXT);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FGets(std::string& u8_buf, FILE* stream) {
|
||||
std::wstring wcmd;
|
||||
if (std::getline(std::wcin, wcmd).fail()) return false;
|
||||
YYCC::EncodingHelper::WcharToChar(wcmd.c_str(), u8_buf, CP_UTF8);
|
||||
return true;
|
||||
}
|
||||
|
||||
void FPuts(const char* u8_buf, FILE* stream) {
|
||||
std::fputws(YYCC::EncodingHelper::UTF8ToWchar(u8_buf).c_str(), stream);
|
||||
}
|
||||
|
||||
void FPrintf(FILE* stream, const char* u8_fmt, ...) {
|
||||
va_list argptr;
|
||||
va_start(argptr, u8_fmt);
|
||||
FPuts(YYCC::StringHelper::VPrintf(u8_fmt, argptr).c_str(), stream);
|
||||
va_end(argptr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -3,6 +3,7 @@
|
||||
#if YYCC_OS == YYCC_OS_WINDOWS
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
namespace YYCC::TerminalHelper {
|
||||
|
||||
@ -45,10 +46,24 @@ namespace YYCC::TerminalHelper {
|
||||
#define YYCC_TERMCOL_LIGHT_CYAN(T) "\033[96m" T "\033[0m"
|
||||
#define YYCC_TERMCOL_LIGHT_WHITE(T) "\033[97m" T "\033[0m"
|
||||
|
||||
/**
|
||||
* @brief Try letting terminal support ASCII color schema.
|
||||
* @param fs[in] The stream to be set.
|
||||
* @return true if success, otherwise false.
|
||||
*/
|
||||
bool EnsureTerminalColor(FILE* fs);
|
||||
/**
|
||||
* @brief Try setting terminal to UTF8 encoding.
|
||||
* @param fs[in] The stream to be set.
|
||||
* @return true if success, otherwise false.
|
||||
* @remarks If you enable UTF8 for a stream, you must use stream functions provided in this namespace to operate that stream,
|
||||
* because after UTF8 modification, some old standard functions are not work and it may takes a little bit performance reduction.
|
||||
*/
|
||||
bool EnsureTerminalUTF8(FILE* fs);
|
||||
|
||||
bool ColorfulTerminal(FILE* fs);
|
||||
bool UTF8Terminal(FILE* fs);
|
||||
|
||||
bool FGets(std::string& u8_buf, FILE* stream);
|
||||
void FPuts(const char* u8_buf, FILE* stream);
|
||||
void FPrintf(FILE* stream, const char* u8_fmt, ...);
|
||||
}
|
||||
|
||||
#endif
|
@ -10,14 +10,14 @@
|
||||
#define YYCC_OS YYCC_OS_LINUX
|
||||
#endif
|
||||
|
||||
// Decide the char type we used
|
||||
#include <string>
|
||||
namespace YYCC {
|
||||
#if defined(__cpp_char8_t)
|
||||
using u8char = char8_t;
|
||||
using u8string = std::u8string
|
||||
#else
|
||||
using u8char = char;
|
||||
using u8string = std::string;
|
||||
#endif
|
||||
}
|
||||
//// Decide the char type we used
|
||||
//#include <string>
|
||||
//namespace YYCC {
|
||||
//#if defined(__cpp_char8_t)
|
||||
// using u8char = char8_t;
|
||||
// using u8string = std::std::string
|
||||
//#else
|
||||
// using u8char = char;
|
||||
// using u8string = std::string;
|
||||
//#endif
|
||||
//}
|
||||
|
@ -4,3 +4,4 @@
|
||||
|
||||
#include "StringHelper.hpp"
|
||||
#include "EncodingHelper.hpp"
|
||||
#include "TerminalHelper.hpp"
|
||||
|
@ -94,7 +94,7 @@
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
|
||||
@ -110,7 +110,7 @@
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
|
||||
@ -126,7 +126,7 @@
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
|
||||
@ -142,7 +142,7 @@
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
|
||||
|
@ -98,6 +98,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)src\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@ -116,6 +117,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)src\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@ -134,6 +136,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)src\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@ -152,6 +155,7 @@
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)src\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
|
@ -0,0 +1,25 @@
|
||||
#include <YYCCommonplace.hpp>
|
||||
#include <cstdio>
|
||||
|
||||
namespace Testbench {
|
||||
|
||||
static void TerminalTestbench() {
|
||||
YYCC::TerminalHelper::EnsureTerminalUTF8(stdout);
|
||||
YYCC::TerminalHelper::FPuts(u8"你好世界\n", stdout);
|
||||
YYCC::TerminalHelper::EnsureTerminalColor(stdout);
|
||||
YYCC::TerminalHelper::FPuts(YYCC_TERMCOL_LIGHT_CYAN(u8"Colorful Terminal\n"), stdout);
|
||||
}
|
||||
|
||||
static void StringTestbench() {
|
||||
YYCC::TerminalHelper::FPuts(
|
||||
YYCC::StringHelper::Printf(u8"Translation: %s == %s\n", u8"Hello World", u8"你好世界").c_str(),
|
||||
stdout
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char** args) {
|
||||
Testbench::TerminalTestbench();
|
||||
Testbench::StringTestbench();
|
||||
}
|
Loading…
Reference in New Issue
Block a user