123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #pragma once
- #include "stdafx.h"
- #include <string>
- #include <vector>
- #include <sstream>
- #include <Windows.h>
- #pragma warning(disable:4996)
- namespace StrUtil
- {
- using namespace std;
- inline std::wstring AnsiToUnicode(const char* buf);
- inline std::string AnsiToUtf8(const char* buf);
- inline std::string UnicodeToAnsi(const wchar_t* buf);
- inline std::wstring Utf8ToUnicode(const char* buf);
- inline std::string UnicodeToUtf8(const wchar_t* buf);
- inline std::string TrimLeft(const std::string& str);
- inline std::string TrimRight(const std::string& str);
- inline std::string Trim(const std::string& str);
- inline std::vector<std::string> Split(std::string& str, const char* c);
- inline std::string GetModulePath();
- inline void Replace(std::string& srcstr, const std::string& oldstr, const std::string& newstr);
- inline HMODULE GetCurrentModule();
-
- };
- namespace StrUtil {
- void Replace(std::string& srcstr, const std::string& oldstr, const std::string& newstr)
- {
- string::size_type pos = 0;
- string::size_type a = oldstr.size();
- string::size_type b = newstr.size();
- while ((pos = srcstr.find(oldstr, pos)) != string::npos)
- {
- srcstr.replace(pos, a, newstr);
- pos += b;
- }
- }
- std::vector<std::string> Split(std::string& str, const char* c)
- {
- char* cstr, * p;
- std::vector<std::string> res;
- cstr = new char[str.size() + 1];
- strcpy(cstr, str.c_str());
- p = strtok(cstr, c);
- while (p != NULL)
- {
- res.push_back(p);
- p = strtok(NULL, c);
- }
- return res;
- }
- std::wstring AnsiToUnicode(const char* buf)
- {
- int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
- if (len == 0) return L"";
- std::vector<wchar_t> unicode(len);
- ::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
- return &unicode[0];
- }
- std::string AnsiToUtf8(const char* buf)
- {
- return UnicodeToUtf8(AnsiToUnicode(buf).c_str());
- }
- std::string UnicodeToAnsi(const wchar_t* buf)
- {
- int len = ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
- if (len == 0) return "";
- std::vector<char> utf8(len);
- ::WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);
- return &utf8[0];
- }
- std::wstring Utf8ToUnicode(const char* buf)
- {
- int len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
- if (len == 0) return L"";
- std::vector<wchar_t> unicode(len);
- ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
- return &unicode[0];
- }
- std::string UnicodeToUtf8(const wchar_t* buf)
- {
- int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
- if (len == 0) return "";
- std::vector<char> utf8(len);
- ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
- return &utf8[0];
- }
- std::string TrimLeft(const std::string& str) {
- std::string t = str;
- for (std::string::iterator i = t.begin(); i != t.end(); i++) {
- if (!isspace(*i)) {
- t.erase(t.begin(), i);
- break;
- }
- }
- return t;
- }
- std::string TrimRight(const std::string& str) {
- if (str.begin() == str.end()) {
- return str;
- }
- std::string t = str;
- for (std::string::iterator i = t.end() - 1; i != t.begin(); i--) {
- if (!isspace(*i)) {
- t.erase(i + 1, t.end());
- break;
- }
- }
- return t;
- }
- std::string Trim(const std::string& str) {
- std::string t = str;
- std::string::iterator i;
- for (i = t.begin(); i != t.end(); i++) {
- if (!isspace(*i)) {
- t.erase(t.begin(), i);
- break;
- }
- }
- if (i == t.end()) {
- return t;
- }
- for (i = t.end() - 1; i != t.begin(); i--) {
- if (!isspace(*i)) {
- t.erase(i + 1, t.end());
- break;
- }
- }
- return t;
- }
- HMODULE GetCurrentModule()
- { // NB: XP+ solution!
- HMODULE hModule = NULL;
- GetModuleHandleEx(
- GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
- (LPCTSTR)GetCurrentModule,
- &hModule);
- return hModule;
- }
- std::string GetModulePath()
- {
- char fullPath[MAX_PATH];
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- HMODULE hModule = GetCurrentModule();
- if (0 == GetModuleFileNameA(hModule, fullPath, MAX_PATH))
- return "";
- _splitpath(fullPath, drive, dir, NULL, NULL);
- std::string str = std::string(drive) + dir;
- return str;
- }
-
- }
|