前几日用bcb,发现它有个函数是Format,可以直接返回字符串,感觉非常好用,小我略想了一下,呵呵,用vc写了个相同功能的函数,代码很简单,仅供大家参考:
__inline bool TrimRight(LPTSTR d)
{
if(d==NULL)
return false;
LPTSTR pstr=NULL;
char* p=d;
while(*p!='\0')
{
if(_istspace(*p)&&
pstr==NULL)
{
pstr=p;
}
else
pstr=NULL;
p=_tcsinc(p);
}
if(pstr!=NULL)
{
*pstr='\0';
}
return true;
}
__inline bool TrimLeft(LPTSTR d)
{
if(d==NULL)
return false;
LPTSTR pstr=NULL;
char* p=d;
while(*p!='\0')
{
if(!_istspace(*p))
{
if(pstr==NULL)
{
pstr=p;
break;
}
}
p=_tcsinc(p);
}
if(pstr!=NULL&&
pstr!=d)
{
MoveMemory(d,pstr,lstrlen(pstr));
}
return true;
}
struct fstring
{
public:
fstring()
{
memset(str,0,sizeof(TCHAR)*5024);
}
fstring(char* src)
{
memset(str,0,sizeof(TCHAR)*5024);
lstrcpy(str,src);
}
fstring(const char* src)
{
memset(str,0,sizeof(TCHAR)*5024);
lstrcpy(str,src);
}
fstring(fstring& src)
{
memset(str,0,sizeof(TCHAR)*5024);
lstrcpy(str,src.str);
}
bool CopyFromBuffer(PBYTE pBuf,UINT nSize)
{
if(nSize+1>5024)
return false;
memset(str,0,sizeof(TCHAR)*5024);
CopyMemory(str,pBuf,nSize);
str[nSize+1]='\0';
return true;
}
bool TrimLeft()
{
return __farfhy::string::TrimLeft(str);
}
bool TrimRight()
{
return __farfhy::string::TrimRight(str);
}
bool EmptyBuffer()
{
memset(str,0,5024);
return true;
}
bool ReadStrFromList(LPTSTR dst)
{
return __farfhy::string::ReadStrFromList(dst,str);
}
fstring& Format(LPCTSTR lpszText,...)
{
va_list list;
va_start(list,lpszText);
memset(str,0,5024);
Format(lpszText,list);
va_end(list);
return *this;
}
fstring& operator=(LPCTSTR src);
fstring& operator=(char* src);
fstring& operator=(const fstring s);
__inline operator char*( );
__inline operator const char*();
friend __inline fstring Format(LPCTSTR lpszText,...);
private:
fstring& Format(LPTSTR lpszText,va_list& list)
{
memset(str,0,sizeof(TCHAR)*5024);
vsprintf(str,lpszText,list);
return *this;
}
TCHAR str[5024];
};
fstring::operator char *()
{
return str;
}
fstring::operator const char *()
{
return str;
}
__inline fstring Format(LPCTSTR lpszText,...)
{
fstring fs;
va_list list;
va_start(list,lpszText);
char teststr[5024],str[5024];
memset(str,0,5024);
memset(teststr,0,5024);
sprintf(teststr,"%s\n",lpszText);
fs.Format(teststr,list);
va_end(list);
return fs;
}