📄 cstring.cpp
字号:
#include "CString.hpp"
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
// Constructors
CString::CString( )
{
string.ReSize(sizeof(TCHAR));
LPTSTR(string.Ptr())[0] = 0;
}
CString::CString( LPTSTR p )
{
UINT sz = _tcslen(p) + sizeof(TCHAR);
string.ReSize(sz);
_tcscpy(LPTSTR(string.Ptr()),p);
}
CString& CString::operator=( LPTSTR p )
{
UINT sz = _tcslen(p) + sizeof(TCHAR);
string.ReSize(sz);
_tcscpy(LPTSTR(string.Ptr()),p);
return *this;
}
CString& operator+=( CString& i, LPTSTR p )
{
i.string.ReSize(i.string.Size()+_tcslen(p));
_tcscat(i.Ptr(),p);
return i;
}
CString& operator+=( CString& i, CString& d )
{
i.string.ReSize( i.string.Size()+d.Len() );
_tcscat(i.Ptr(),d.Ptr());
return i;
}
CString operator+( LPTSTR p ,CString& s1)
{
CString ret(p); // Must return a new copy
ret += s1;
return ret;
}
CString operator+( CString& d ,CString& s1)
{
CString ret(d.Ptr()); // This forces to get a new copy (else we get a new ref)
ret += s1;
return ret;
}
CString operator+( CString& d, LPTSTR s1 )
{
CString ret(d.Ptr()); // This forces to get a new copy (else we get a new ref)
ret += s1;
return ret;
}
bool operator<(CString& s1, LPTSTR pusz )
{
return ( _tcscmp(s1.Ptr(),pusz) < 0 );
}
bool operator<( LPTSTR pusz ,CString& c2)
{
return ( _tcscmp(pusz,c2.Ptr()) < 0 );
}
bool operator<(CString& s1, CString& s2)
{
return ( _tcscmp(s1.Ptr(),s2.Ptr()) < 0 );
}
bool operator<=(CString& s1, LPTSTR pusz )
{
return ( _tcscmp(s1.Ptr(),pusz) <= 0 );
}
bool operator<=( LPTSTR pusz ,CString& c2)
{
return ( _tcscmp(pusz,c2.Ptr()) <= 0 );
}
bool operator<=(CString& s1, CString& s2)
{
return ( _tcscmp(s1.Ptr(),s2.Ptr()) <= 0 );
}
bool operator>(CString& s1, LPTSTR pusz )
{
return ( _tcscmp(s1.Ptr(),pusz) > 0 );
}
bool operator>( LPTSTR pusz ,CString& c2)
{
return ( _tcscmp(pusz,c2.Ptr()) > 0 );
}
bool operator>(CString& s1, CString& s2)
{
return ( _tcscmp(s1.Ptr(),s2.Ptr()) > 0 );
}
bool operator>=(CString& s1, LPTSTR pusz )
{
return ( _tcscmp(s1.Ptr(),pusz) >= 0 );
}
bool operator>=( LPTSTR pusz ,CString& c2)
{
return ( _tcscmp(pusz,c2.Ptr()) >= 0 );
}
bool operator>=(CString& s1, CString& s2)
{
return ( _tcscmp(s1.Ptr(),s2.Ptr()) >= 0 );
}
bool operator==(CString& s1, LPTSTR pusz )
{
return ( _tcscmp(s1.Ptr(),pusz) == 0 );
}
bool operator==( LPTSTR pusz ,CString& c2)
{
return ( _tcscmp(pusz,c2.Ptr()) == 0 );
}
bool operator==(CString& s1, CString& s2)
{
return ( _tcscmp(s1.Ptr(),s2.Ptr()) == 0 );
}
bool operator!=(CString& s1, LPTSTR pusz )
{
return ( _tcscmp(s1.Ptr(),pusz) != 0 );
}
bool operator!=( LPTSTR pusz ,CString& c2)
{
return ( _tcscmp(pusz,c2.Ptr()) != 0 );
}
bool operator!=(CString& s1, CString& s2)
{
return ( _tcscmp(s1.Ptr(),s2.Ptr()) != 0 );
}
bool CString::SetLen(const UINT v)
{
// Set length
UINT PrevLen = Len();
if (!string.ReSize(v+1))
return false;
// If growing, fill with ' '
if ( v > PrevLen)
memset(Ptr() + PrevLen,TCHAR(' '), v - PrevLen);
// End With 0
Ptr()[v] = 0;
return true;
}
CString AllTrim(CString& c)
{
CString ret(c.Ptr()); // Get a copy to work on!
UINT SLen = ret.Len(); if (!SLen) return ret;
LPTSTR PtrX;
LPTSTR PtrI = PtrX = ret.Ptr();
LPTSTR PtrF = PtrI + SLen;
// Get rid of the ending spaces
while(SLen && *(PtrF-1) == TCHAR(' ')) {
PtrF--; SLen--;
};
*PtrF=0; // End here!
// Search for the first char that磗 not an space
while (SLen && *PtrI==TCHAR(' ')) {
PtrI++; SLen--;
};
// Kill all the leading spaces
memmove( PtrX, PtrI, SLen+1);
// And adjust block size
ret.string.ReSize(1+SLen);
// Return the new string
return ret;
}
CString LeftTrim(CString& c)
{
CString ret(c.Ptr()); // Get a copy to work on!
UINT SLen = ret.Len(); if (!SLen) return ret;
LPTSTR PtrX;
LPTSTR PtrI= PtrX = ret.Ptr();
// Search for the first char that磗 not an space
while (SLen && *PtrI==TCHAR(' ')) {
PtrI++; SLen--;
};
// Kill all the leading spaces
memmove( PtrX, PtrI, SLen+1);
// And adjust block size
ret.string.ReSize(1+SLen);
// Return the new string
return ret;
}
CString RightTrim(CString& c)
{
CString ret(c.Ptr()); // Get a copy to work on!
UINT SLen = ret.Len(); if (!SLen) return ret;
LPTSTR PtrF= ret.Ptr() + SLen;
// Get rid of the ending spaces
while(SLen && *(PtrF-1)==' ') {
PtrF--; SLen--;
};
*PtrF=0; // End here!
// And adjust block size
ret.string.ReSize(1+SLen);
// Return the new string
return ret;
}
CString Left(CString& s,const UINT c)
{
CString ret(s.Ptr()); // Get a Copy to work on!
UINT SLen = ret.Len();
UINT Count = c;
if (Count > SLen) Count = SLen;
ret.SetLen(Count);
return ret;
}
CString Right(CString& s,const UINT c)
{
CString ret(s.Ptr()); // Get a Copy to work on!
LPTSTR Ptr = ret.Ptr();
UINT SLen = ret.Len();
UINT Count = c;
if (Count > SLen) Count = SLen;
memmove(Ptr,Ptr + SLen - Count, Count+1 );
ret.SetLen(Count);
return ret;
}
CString Mid(CString& s,const UINT p,const UINT c)
{
CString ret(s.Ptr()); // Get a Copy to work on!
UINT SLen = ret.Len();
UINT Pos = p;
UINT Count = c;
if (Pos >= SLen) {
ret.Zero(); // Nothing to get, empty return
} else {
// Ensure we can get it!
if ( Pos + Count > SLen) Count = SLen - Pos;
LPTSTR Ptr = ret.Ptr();
memmove(Ptr,Ptr + Pos, Count );
ret.SetLen(Count);
}
return ret;
}
CString Printf(LPTSTR string, ... )
{
va_list parameters;
CString ret;
ret.SetLen(4096); // Try with 4096 bytes!
va_start( parameters, string );
ret.SetLen( _vstprintf( ret.Ptr(),string, parameters ));
return ret;
}
CString LoadStringResource(HINSTANCE instance,UINT res)
{
CString ret;
ret.SetLen(4096); // Try with 4096 bytes!
UINT SLen = LoadString(instance,res,ret.Ptr(),4095);
ret.SetLen(SLen);
return ret;
}
int NCmp( CString& c2 ,LPTSTR p ,const UINT n)
{
return _tcsncmp(c2.Ptr(),p, n);
}
int NCmp( LPTSTR p ,CString& c2 ,const UINT n)
{
return _tcsncmp(p,c2.Ptr(), n);
}
int NCmp( CString& c1 ,CString& c2 ,const UINT n)
{
return _tcsncmp(c1.Ptr(),c2.Ptr(), n);
}
INT MessageBox(HWND hwnd,CString& Message,LPCTSTR caption,UINT type)
{
return MessageBox(hwnd,Message.Ptr(),caption,type);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -