209 lines
4.8 KiB
C++
209 lines
4.8 KiB
C++
#include "LString.h"
|
|
#include <sstream>
|
|
|
|
extern int StrToInt(const String& str)
|
|
{
|
|
std::stringstream ss(str);
|
|
int val;
|
|
ss >> val;
|
|
return val;
|
|
}
|
|
|
|
extern String GetFileName(const String& path)
|
|
{
|
|
int a = path.find_last_of(PATH_SPLITTER);
|
|
if(a == -1)
|
|
return path;
|
|
int length = path.length();
|
|
return path.substr(a+1, length - a);
|
|
}
|
|
|
|
extern String GetFileName(const String& path, const String& pathSplitter)
|
|
{
|
|
int a = path.find_last_of(pathSplitter.c_str());
|
|
if(a == -1)
|
|
return path;
|
|
int length = path.length();
|
|
return path.substr(a+1, length - a);
|
|
}
|
|
|
|
extern String GetFilePath(const String& path, const String& pathSplitter)
|
|
{
|
|
int a = path.find_last_of(pathSplitter.c_str());
|
|
if(a == -1)
|
|
return path;
|
|
return path.substr(0, a);
|
|
}
|
|
|
|
extern String GetFileNameWithoutExtention(const String& path)
|
|
{
|
|
int a = path.find_last_of(_text("/"));
|
|
int b = path.find_last_of(_text("."));
|
|
if(a == -1 && b == -1)
|
|
return path;
|
|
if(b == -1)
|
|
{
|
|
int length = path.length();
|
|
return path.substr(a+1, length - a);
|
|
}
|
|
return path.substr(a+1, b-a - 1);
|
|
}
|
|
|
|
extern void GetRootFolderS(const String& path, String& folderName, String& resultPath)
|
|
{
|
|
int start = path.find_first_not_of(_text("/"));
|
|
|
|
int offset = path.find_first_of(_text("/"),start);
|
|
if(start < 0)
|
|
{
|
|
folderName = path;
|
|
resultPath = String(_text(""));
|
|
return;
|
|
}
|
|
if(offset < 0)
|
|
{
|
|
folderName = path.substr(start, path.length());
|
|
resultPath = String(_text(""));
|
|
return;
|
|
}
|
|
folderName = path.substr(start, offset - start);
|
|
resultPath = path.substr(offset, path.length() - offset);
|
|
}
|
|
|
|
extern void GetRootFolderS(const String& path, String& folderName)
|
|
{
|
|
int start = path.find_first_not_of(_text("/"));
|
|
int offset = path.find_first_of(_text("/"),start);
|
|
folderName = path.substr(start, offset - start);
|
|
}
|
|
|
|
extern String GetDirectoryName(const String& path)
|
|
{
|
|
String ret = path;
|
|
if(ret.length()<2) return ret;
|
|
while(ret.length() > 1 && (ret.c_str()[ret.length()-1]) == L'\\')
|
|
ret = ret.substr(0,ret.length()-1);
|
|
|
|
int a = ret.find_last_of(PATH_SPLITTER);
|
|
if(a == -1)
|
|
return ret;
|
|
int length = ret.length();
|
|
return ret.substr(a+1, length - a);
|
|
}
|
|
|
|
extern void SplitString(const String& text, std::vector<String>* collection)
|
|
{
|
|
int pos = 0;
|
|
int counter = 0;
|
|
const char* str = text.c_str();
|
|
while(str[counter])
|
|
{
|
|
if(str[counter] == '\n')
|
|
{
|
|
(*collection).push_back(text.substr(pos, counter - pos));
|
|
pos = counter + 1;
|
|
}
|
|
counter++;
|
|
}
|
|
int diff = text.size() - pos;
|
|
if(diff)
|
|
(*collection).push_back(text.substr(pos, diff));
|
|
}
|
|
|
|
extern void SplitString(const String& text, std::vector<String>* collection, char splitter)
|
|
{
|
|
int pos = 0;
|
|
int counter = 0;
|
|
const char* str = text.c_str();
|
|
while(str[counter])
|
|
{
|
|
if(str[counter] == splitter)
|
|
{
|
|
(*collection).push_back(text.substr(pos, counter - pos));
|
|
pos = ++counter;
|
|
//counter++;
|
|
}
|
|
counter++;
|
|
}
|
|
int diff = text.size() - pos;
|
|
if(diff)
|
|
(*collection).push_back(text.substr(pos, diff));
|
|
}
|
|
|
|
extern void SplitStringW(const std::wstring& text, std::vector<std::wstring>* collection)
|
|
{
|
|
int pos = 0;
|
|
int counter = 0;
|
|
const wchar_t* str = text.c_str();
|
|
while(str[counter])
|
|
{
|
|
if(str[counter] == L'\n')
|
|
{
|
|
(*collection).push_back(text.substr(pos, counter - pos));
|
|
pos = counter + 1;
|
|
}
|
|
counter++;
|
|
}
|
|
int diff = text.size() - pos;
|
|
if(diff)
|
|
(*collection).push_back(text.substr(pos, diff));
|
|
}
|
|
|
|
extern void SplitStringW(const std::wstring& text, std::vector<std::wstring>* collection, wchar_t splitter)
|
|
{
|
|
int pos = 0;
|
|
int counter = 0;
|
|
const wchar_t* str = text.c_str();
|
|
while(str[counter])
|
|
{
|
|
if(str[counter] == splitter)
|
|
{
|
|
(*collection).push_back(text.substr(pos, counter - pos));
|
|
pos = counter;
|
|
}
|
|
counter++;
|
|
}
|
|
int diff = text.size() - pos;
|
|
if(diff)
|
|
(*collection).push_back(text.substr(pos, diff));
|
|
}
|
|
|
|
|
|
extern void StringSerialize(const String& src, LifeCore::IMemoryStream* stm)
|
|
{
|
|
//HACK Multi byte string may be not only one byte per character
|
|
String::size_type size = src.length();
|
|
stm->Write(size);
|
|
stm->Write(src.c_str(), size);
|
|
}
|
|
extern String StringDeserialize(LifeCore::IMemoryStream* stm)
|
|
{
|
|
String::size_type size;
|
|
stm->Read(&size, sizeof(size));
|
|
char* buffer = new char[size + 1];
|
|
buffer[size] = 0;
|
|
stm->Read(buffer, size);
|
|
String val = String(buffer);
|
|
delete[] buffer;
|
|
return val;
|
|
}
|
|
|
|
extern void WCSTMBS(const wchar_t* src, String& dst)
|
|
{
|
|
int length = wcslen(src);
|
|
int len = WideCharToMultiByte(CP_ACP, 0, src, length, 0, 0,0,0);
|
|
char* buf = new char[len+1];
|
|
memset(buf,0,len+1);
|
|
WideCharToMultiByte(CP_ACP, 0, src, length, buf, len+1,0,0);
|
|
dst = String(buf);
|
|
delete[] buf;
|
|
}
|
|
|
|
extern void MBSTWCS(const String& src, std::wstring& dst)
|
|
{
|
|
int strLen = MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, NULL, 0);
|
|
wchar_t* widePath = new wchar_t[strLen];
|
|
MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, widePath, strLen);
|
|
dst = std::wstring(widePath);
|
|
delete[]widePath;
|
|
} |