332 lines
8.3 KiB
C++
332 lines
8.3 KiB
C++
#include "Inc\toolkit\StringConvert.h"
|
|
#include <iostream>
|
|
#include <codecvt>
|
|
#include <memory>
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <chrono>
|
|
#include <filesystem>
|
|
|
|
//namespace fs = std::experimental::filesystem;
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace dp {
|
|
|
|
StringConvert::StringConvert()
|
|
{
|
|
}
|
|
|
|
StringConvert::~StringConvert()
|
|
{
|
|
}
|
|
|
|
std::string StringConvert::UnicodeToUTF8(const std::wstring & wstr)
|
|
{
|
|
std::string ret;
|
|
try{
|
|
std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
|
|
ret = wcv.to_bytes(wstr);
|
|
}
|
|
catch (const std::exception & e){
|
|
std::cerr << e.what() << std::endl;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::wstring StringConvert::UTF8ToUnicode(const std::string & str)
|
|
{
|
|
std::wstring ret;
|
|
try{
|
|
std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;
|
|
ret = wcv.from_bytes(str);
|
|
}
|
|
catch (const std::exception & e){
|
|
std::cerr << e.what() << std::endl;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::string StringConvert::UnicodeToANSI(const std::wstring & wstr)
|
|
{
|
|
std::string ret;
|
|
std::mbstate_t state = {};
|
|
const wchar_t *src = wstr.data();
|
|
size_t len = std::wcsrtombs(nullptr, &src, 0, &state);
|
|
if (static_cast<size_t>(-1) != len) {
|
|
std::unique_ptr< char[] > buff(new char[len + 1]);
|
|
len = std::wcsrtombs(buff.get(), &src, len, &state);
|
|
if (static_cast<size_t>(-1) != len) {
|
|
ret.assign(buff.get(), len);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::wstring StringConvert::ANSIToUnicode(const std::string & str)
|
|
{
|
|
std::wstring ret;
|
|
std::mbstate_t state = {};
|
|
const char *src = str.data();
|
|
size_t len = std::mbsrtowcs(nullptr, &src, 0, &state);
|
|
if (static_cast<size_t>(-1) != len) {
|
|
std::unique_ptr< wchar_t[] > buff(new wchar_t[len + 1]);
|
|
len = std::mbsrtowcs(buff.get(), &src, len, &state);
|
|
if (static_cast<size_t>(-1) != len) {
|
|
ret.assign(buff.get(), len);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::string StringConvert::DeleteChar(std::string str, char c)
|
|
{
|
|
str.erase(std::remove(str.begin(), str.end(), c), str.end());
|
|
return str;
|
|
}
|
|
|
|
std::wstring StringConvert::DeleteWchar(std::wstring wstr, wchar_t c)
|
|
{
|
|
wstr.erase(std::remove(wstr.begin(), wstr.end(), c), wstr.end());
|
|
return wstr;
|
|
}
|
|
|
|
std::string StringConvert::StringTrim(std::string str)
|
|
{
|
|
if (str.empty()) {
|
|
return str;
|
|
}
|
|
str.erase(0, str.find_first_not_of(" "));
|
|
str.erase(str.find_last_not_of(" ") + 1);
|
|
return str;
|
|
}
|
|
|
|
std::wstring StringConvert::WStringTrim(std::wstring wstr)
|
|
{
|
|
if (wstr.empty()) {
|
|
return wstr;
|
|
}
|
|
wstr.erase(0, wstr.find_first_not_of(L" "));
|
|
wstr.erase(wstr.find_last_not_of(L" ") + 1);
|
|
return wstr;
|
|
}
|
|
|
|
int StringConvert::StringSplit(std::vector<std::string>& dst, const std::string& src, const std::string& separator)
|
|
{
|
|
if (src.empty() || separator.empty())
|
|
return 0;
|
|
|
|
int nCount = 0;
|
|
std::string temp;
|
|
size_t pos = 0, offset = 0;
|
|
|
|
// 分割第1~n-1个
|
|
while ((pos = src.find_first_of(separator, offset)) != std::string::npos)
|
|
{
|
|
temp = src.substr(offset, pos - offset);
|
|
if (temp.length() > 0) {
|
|
dst.push_back(temp);
|
|
nCount++;
|
|
}
|
|
offset = pos + 1;
|
|
}
|
|
|
|
// 分割第n个
|
|
temp = src.substr(offset, src.length() - offset);
|
|
if (temp.length() > 0) {
|
|
dst.push_back(temp);
|
|
nCount++;
|
|
}
|
|
return nCount;
|
|
}
|
|
|
|
int StringConvert::WStringSplit(std::vector<std::wstring>& dst, const std::wstring& src, const std::wstring& separator)
|
|
{
|
|
if (src.empty() || separator.empty())
|
|
return 0;
|
|
|
|
int nCount = 0;
|
|
std::wstring temp;
|
|
size_t pos = 0, offset = 0;
|
|
|
|
// 分割第1~n-1个
|
|
while ((pos = src.find_first_of(separator, offset)) != std::wstring::npos)
|
|
{
|
|
temp = src.substr(offset, pos - offset);
|
|
if (temp.length() > 0) {
|
|
dst.push_back(temp);
|
|
nCount++;
|
|
}
|
|
offset = pos + 1;
|
|
}
|
|
|
|
// 分割第n个
|
|
temp = src.substr(offset, src.length() - offset);
|
|
if (temp.length() > 0) {
|
|
dst.push_back(temp);
|
|
nCount++;
|
|
}
|
|
return nCount;
|
|
}
|
|
|
|
std::string StringConvert::StringTolower(std::string str)
|
|
{
|
|
std::transform(str.begin(), str.end(),
|
|
str.begin(),
|
|
[](unsigned char ch) { return tolower(ch); });
|
|
return std::move(str);
|
|
}
|
|
|
|
|
|
std::string StringConvert::StringToupper(std::string str)
|
|
{
|
|
std::transform(str.begin(), str.end(),
|
|
str.begin(),
|
|
[](unsigned char ch) { return toupper(ch); });
|
|
return std::move(str);
|
|
}
|
|
|
|
std::string CurrDir()
|
|
{
|
|
return fs::current_path().generic_u8string();
|
|
}
|
|
|
|
std::string ReadFile(const std::string& file_path)
|
|
{
|
|
std::string file_data;
|
|
std::ifstream ifs(file_path, std::ios::binary);
|
|
if (ifs.is_open())
|
|
{
|
|
//std::stringstream buf;
|
|
//buf << ifs.rdbuf();
|
|
|
|
file_data = std::string((std::istreambuf_iterator<char>(ifs)),
|
|
std::istreambuf_iterator<char>());
|
|
ifs.close();
|
|
}
|
|
return std::move(file_data);
|
|
}
|
|
|
|
/************************************************************************/
|
|
/* 获取日期时间 */
|
|
/************************************************************************/
|
|
std::string TimeString::GetDateString(DATE_STYLE style)
|
|
{
|
|
auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
|
struct tm* ptm = localtime(&tt);
|
|
char date[60] = { 0 };
|
|
|
|
switch (style)
|
|
{
|
|
case dp::YMD_1:
|
|
sprintf(date, "%d/%02d/%02d",
|
|
(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday);
|
|
break;
|
|
case dp::YMD_2:
|
|
sprintf(date, "%d-%02d-%02d",
|
|
(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return std::string(date);
|
|
}
|
|
|
|
std::string TimeString::GetTimeString(TIME_STYLE style)
|
|
{
|
|
auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
|
|
struct tm* ptm = localtime(&tt);
|
|
char date_time[60] = { 0 };
|
|
|
|
switch (style)
|
|
{
|
|
case dp::HMS_1:
|
|
{
|
|
sprintf(date_time, "%02d时%02d分%02d秒",
|
|
(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
|
|
break;
|
|
}
|
|
case dp::HMS_2:
|
|
{
|
|
sprintf(date_time, "%02d:%02d:%02d",
|
|
(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
|
|
break;
|
|
}
|
|
case dp::HMS_3:
|
|
{
|
|
sprintf(date_time, "%02d-%02d-%02d",
|
|
(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
return std::string(date_time);
|
|
}
|
|
|
|
std::string TimeString::GetTimestamp(TIME_STYLE style)
|
|
{
|
|
std::time_t timestamp = 0;
|
|
switch (style)
|
|
{
|
|
case dp::TIMESTAMP_SECOND:
|
|
{
|
|
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> tp = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now());
|
|
auto tmp = std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch());
|
|
timestamp = tmp.count();
|
|
break;
|
|
}
|
|
case dp::TIMESTAMP_MILLISECOND:
|
|
{
|
|
std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
|
|
auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
|
|
timestamp = tmp.count();
|
|
break;
|
|
}
|
|
case dp::TIMESTAMP_MICROSECOND:
|
|
{
|
|
std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds> tp = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
|
|
auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch());
|
|
timestamp = tmp.count();
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
return std::to_string(timestamp);
|
|
}
|
|
|
|
std::string TimeString::TimestampToStdTime(int64_t timestamp)
|
|
{
|
|
#if(0)
|
|
int64_t milli = timestamp + (int64_t)8 * 60 * 60 * 1000;//此处转化为东八区北京时间,如果是其它时区需要按需求修改
|
|
auto mTime = std::chrono::milliseconds(milli);
|
|
auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime);
|
|
auto tt = std::chrono::system_clock::to_time_t(tp);
|
|
std::tm* now = std::gmtime(&tt);
|
|
char str_out[128] = { 0 };
|
|
sprintf(str_out, "%4d-%02d-02d %02d:%02d:%02d", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
|
|
return std::string(str_out);
|
|
#else
|
|
|
|
//std::time_t timestamp = std::time(NULL);
|
|
std::tm tm = *std::localtime(×tamp);
|
|
std::stringstream ss;
|
|
ss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
|
|
|
|
return ss.str();
|
|
#endif
|
|
}
|
|
|
|
std::string TimeString::StdTimeToTimestamp(std::string strTime)
|
|
{
|
|
std::tm tm = {};
|
|
std::stringstream ss(strTime);
|
|
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
|
|
time_t timestamp = std::mktime(&tm);
|
|
return std::to_string(timestamp);
|
|
}
|
|
}
|