블로그 이미지
훅크선장

카테고리

분류 전체보기 (362)
사진이야기 (23)
펭귄컴퓨팅 (121)
컴퓨터보안 (84)
절름발이 프로그래머 (59)
하드웨어개조 (23)
멀알려줄까 (35)
홈베이킹&홈쿠킹 (2)
잡다한것들 (15)
Total
Today
Yesterday

달력

« » 2024.5
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

공지사항

태그목록

최근에 올라온 글

http://hugman.tistory.com/entry/C-String-Utilts
-----------------------------------------------

namespace SU   // SU represents 'String Utilities'
{
std::vector<std::string> split(std::string& text, std::string separators )
{
size_t n = text.length();
size_t start, stop;
std::vector<std::string> result;
start = text.find_first_not_of(separators);
while ((start >= 0) && (start < n)) {
stop = text.find_first_of(separators, start);
if ((stop < 0) || (stop > n)) stop = n;
result.push_back(text.substr(start, stop - start));
start = text.find_first_not_of(separators, stop+1);
}
return result;
}
std::string FindAndReplace (const std::string& source, const std::string target, const std::string replacement)
{
std::string str = source;
std::string::size_type pos = 0,  // where we are now
found;   // where the found data is
if (target.size () > 0)  // searching for nothing will cause a loop
{
while ((found = str.find (target, pos)) != std::string::npos)
{
str.replace (found, target.size (), replacement);
pos = found + replacement.size ();
}
}
return str;
};  // end of FindAndReplace

bool IsTherePattern(const std::string source, const std::string pattern)
{
// Source string 에 pattern 이 존재하는지 안하는지 boolean value를 리턴한다.
if( source.find(pattern) != std::string::npos) {  // exist?
return true;
}
else {
return false;
}
}

// Trim related methods
// Refer : http://www.codeproject.com/vcpp/stl/stdstringtrim.asp
// 약간 수정함
std::string TrimStart(std::string str, const char *delims)  // trim delims in the start of string
{
str.erase(0, str.find_first_not_of(delims));
return str;
}

std::string TrimEnd(std::string str, const char *delims)    // trim delims in the end of string
{
str.erase(str.find_last_not_of(delims) + 1);
return str;
}

std::string Trim(std::string str, const char *delims)    // trim delims in the start and end of string
{
str = TrimStart(str, delims); 
str = TrimEnd(str, delims); 
return str;
}

// 소문자 대문자 변환
int ToLower(int c)
{
if(c>='A' && c<='Z') return c-'A'+'a';
else return c;
}

std::string ToLowerStrings(std::string str)
{
std::string result = "";
for(size_t i=0; i < str.length(); i++)
result += ToLower(str[i]);
return result;
}

int ToUpper(int c)
{
if(c>='a' && c<='z') return c-'a'+'A';
else return c;
}

std::string ToUpperStrings(std::string str)
{
std::string result = "";
for(size_t i=0; i <str.length(); i++)
result += ToUpper(str[i]);
return result;
}

#ifdef FOR_WINDOWS_ONLY 
std::string CString2std_string(CString str)
{
return LPSTR(LPCTSTR(str));
}


char* CString2char_pointer(CString str)
{
return LPSTR(LPCTSTR(str));
}

CString GetCurrentPath()
{
HMODULE hModule;
hModule = ::GetModuleHandle(NULL); // handle of current module
ASSERT(hModule != 0);

CString strExeFileName;
VERIFY(::GetModuleFileName(hModule, strExeFileName.GetBuffer(_MAX_PATH), 
_MAX_PATH));
strExeFileName.ReleaseBuffer();

char Drive[_MAX_DRIVE];
char Path[_MAX_PATH];
char Filename[_MAX_FNAME];
char Ext[_MAX_EXT];

// suha, for VC++ 2005, convert to char array from CString
char CharPath[_MAX_PATH]; 
for (int it = 0 ; it < _MAX_PATH ; it++)
{
CharPath[it] = strExeFileName.GetAt(it);
if (CharPath[it] == '\0') break;
}

_splitpath(CharPath, Drive, Path, Filename, Ext);

return CString(Drive)+CString(Path); // has trailing backslash
}
#endif
}
Posted by 훅크선장
, |