add:添加字符串分割。
This commit is contained in:
parent
0f7a8d3c92
commit
1427bf4bca
@ -11,15 +11,14 @@ public:
|
||||
~COfPath();
|
||||
|
||||
public:
|
||||
static bool isSamePath(const ofString& pa, const ofString& pb);
|
||||
static ofString normalizePath(const ofString& path);
|
||||
static ofString replaceStr(const ofString& str, const ofString& from, const ofString& to);
|
||||
static ofString getFullRunPath();
|
||||
static ofString getHome();
|
||||
static ofString getConfigDir(const ofString& sub_dir, bool create = false);
|
||||
static ofString getFull(const ofString& path, const ofString& sub_file_path);
|
||||
static bool isExist(const ofString& path);
|
||||
static bool writeBin(const ofString& path, const char* data, int len);
|
||||
static bool is_same_path(const ofString& pa, const ofString& pb);
|
||||
static ofString normalize(const ofString& path);
|
||||
static ofString get_full_path();
|
||||
static ofString get_home();
|
||||
static ofString get_config_dir(const ofString& sub_dir, bool create = false);
|
||||
static ofString get_full(const ofString& path, const ofString& sub_file_path);
|
||||
static bool exist(const ofString& path);
|
||||
static bool write(const ofString& path, const char* data, int len);
|
||||
};
|
||||
}; // namespace ofen
|
||||
#endif
|
@ -2,6 +2,7 @@
|
||||
#define OFEN_STRING_HEADER
|
||||
|
||||
#include "of_def.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace ofen {
|
||||
class COfStr
|
||||
@ -9,6 +10,9 @@ class COfStr
|
||||
public:
|
||||
COfStr();
|
||||
~COfStr();
|
||||
public:
|
||||
static ofString replace(const ofString& str, const ofString& from, const ofString& to);
|
||||
static std::vector<ofString> split(const ofString& input, const ofString& delimiter);
|
||||
};
|
||||
}; // namespace ofen
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "of_path.h"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include "of_str.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@ -25,34 +26,20 @@ COfPath::~COfPath()
|
||||
{
|
||||
}
|
||||
|
||||
bool COfPath::isSamePath(const ofString& pa, const ofString& pb)
|
||||
bool COfPath::is_same_path(const ofString& pa, const ofString& pb)
|
||||
{
|
||||
return normalizePath(pa) == normalizePath(pb);
|
||||
return normalize(pa) == normalize(pb);
|
||||
}
|
||||
|
||||
ofString COfPath::normalizePath(const ofString& path)
|
||||
ofString COfPath::normalize(const ofString& path)
|
||||
{
|
||||
ofString normalized = replaceStr(path, ofT("\\"), ofT("/"));
|
||||
ofString normalized = COfStr::replace(path, ofT("\\"), ofT("/"));
|
||||
if (!normalized.empty() && normalized.back() == ofT('/')) {
|
||||
normalized.pop_back();
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
ofString COfPath::replaceStr(const ofString& str, const ofString& from, const ofString& to)
|
||||
{
|
||||
if (from.empty()) {
|
||||
return str;
|
||||
}
|
||||
ofString result = str;
|
||||
size_t startPos = 0;
|
||||
while ((startPos = result.find(from, startPos)) != ofString::npos) {
|
||||
result.replace(startPos, from.length(), to);
|
||||
startPos += to.length();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
ofString COfPath::getFullRunPath()
|
||||
ofString COfPath::get_full_path()
|
||||
{
|
||||
ofString path;
|
||||
#ifdef _WIN32
|
||||
@ -81,7 +68,7 @@ ofString COfPath::getFullRunPath()
|
||||
#endif
|
||||
}
|
||||
|
||||
ofString COfPath::getHome()
|
||||
ofString COfPath::get_home()
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
ofChar* value = nullptr;
|
||||
@ -107,9 +94,9 @@ ofString COfPath::getHome()
|
||||
#endif
|
||||
}
|
||||
|
||||
ofString COfPath::getConfigDir(const ofString& sub_dir, bool create)
|
||||
ofString COfPath::get_config_dir(const ofString& sub_dir, bool create)
|
||||
{
|
||||
fs::path userHome = fs::path(getHome());
|
||||
fs::path userHome = fs::path(get_home());
|
||||
userHome.append(".config");
|
||||
userHome.append(sub_dir);
|
||||
if (create) {
|
||||
@ -124,7 +111,7 @@ ofString COfPath::getConfigDir(const ofString& sub_dir, bool create)
|
||||
#endif
|
||||
}
|
||||
|
||||
ofString COfPath::getFull(const ofString& path, const ofString& sub_file_path)
|
||||
ofString COfPath::get_full(const ofString& path, const ofString& sub_file_path)
|
||||
{
|
||||
fs::path p(path);
|
||||
p.append(sub_file_path);
|
||||
@ -135,13 +122,13 @@ ofString COfPath::getFull(const ofString& path, const ofString& sub_file_path)
|
||||
#endif
|
||||
}
|
||||
|
||||
bool COfPath::isExist(const ofString& path)
|
||||
bool COfPath::exist(const ofString& path)
|
||||
{
|
||||
fs::path p(path);
|
||||
return fs::exists(p);
|
||||
}
|
||||
|
||||
bool COfPath::writeBin(const ofString& path, const char* data, int len)
|
||||
bool COfPath::write(const ofString& path, const char* data, int len)
|
||||
{
|
||||
std::ofstream file(path, std::ios::binary);
|
||||
if (!file.is_open()) {
|
||||
|
@ -7,4 +7,28 @@ COfStr::COfStr()
|
||||
COfStr::~COfStr()
|
||||
{
|
||||
}
|
||||
ofString COfStr::replace(const ofString& str, const ofString& from, const ofString& to)
|
||||
{
|
||||
if (from.empty()) {
|
||||
return str;
|
||||
}
|
||||
ofString result = str;
|
||||
size_t startPos = 0;
|
||||
while ((startPos = result.find(from, startPos)) != ofString::npos) {
|
||||
result.replace(startPos, from.length(), to);
|
||||
startPos += to.length();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::vector<ofString> COfStr::split(const ofString& input, const ofString& delimiter)
|
||||
{
|
||||
std::vector<ofString> result;
|
||||
size_t pos = 0, prev = 0;
|
||||
while ((pos = input.find(delimiter, prev)) != ofString::npos) {
|
||||
result.push_back(input.substr(prev, pos - prev));
|
||||
prev = pos + delimiter.size(); // Move past the delimiter
|
||||
}
|
||||
result.push_back(input.substr(prev));
|
||||
return result;
|
||||
}
|
||||
} // namespace ofen
|
||||
|
@ -6,15 +6,15 @@ using namespace ofen;
|
||||
|
||||
void testA()
|
||||
{
|
||||
assert(COfPath::isSamePath(ofT("D:/Java"), ofT("D:\\Java\\")));
|
||||
assert(COfPath::is_same_path(ofT("D:/Java"), ofT("D:\\Java\\")));
|
||||
}
|
||||
|
||||
void testB()
|
||||
{
|
||||
auto home = COfPath::getHome();
|
||||
auto home = COfPath::get_home();
|
||||
assert(!home.empty());
|
||||
|
||||
auto rp = COfPath::replaceStr(ofT("cpp/z"), ofT("p/"), ofT("Ni"));
|
||||
auto rp = COfPath::replace(ofT("cpp/z"), ofT("p/"), ofT("Ni"));
|
||||
assert(rp == ofT("cpNiz"));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user