適合於Unix與Win32下的字串處理類,可以以此為基類進行擴充套件 (轉)

worldblog發表於2007-12-13
適合於Unix與Win32下的字串處理類,可以以此為基類進行擴充套件 (轉)[@more@]

/****************************CString.h******************************/

#ifndef MyStringH
#define MyStringH

#ifndef
typedef int bool;
#define false 0
#define true !false
#endif

class CString{
public:
  CString();
  CString(const char *str);
  CString(CString &str);
  virtual ~CString();

 
  const char * c_str()const;
  CString& sprintf(const char* format, ...);
  int Length() const;
  CString  Trim() const;
  CString  TrimLeft() const;
  CString  TrimRight() const;
  CString  UpperCase() const;
  CString  LowerCase() const;
  CString  StringOfChar(char ch, int count)const;//StringOfChar('A',4)returns "AAAA" 

  CString  operator+(const CString &str); return reference
  CString  operator +(const char* lhs);
  CString &operator +=(const CString& rhs);
  CString &operator=(const CString &str);

  char &operator [](const int idx);
  bool  operator !=(const CString& rhs) const;
  bool  operator   bool  operator <=(const CString& rhs) const;
  bool  operator ==(const CString& rhs) const;
  bool  operator >(const CString& rhs) const;
  bool  operator >=(const CString& rhs) const;
  回被分隔符分割的字串,如CString("ab|cd")::GetDelimitedString("|",1)返回"cd"
  CString GetDelimitedString(const char *delimiter,int index);
private:
  char cTmp;//when operator[] over range,use this var
  char *_data;//store the data
};

#endif

/****************************CString.cpp******************************/

#include "CString.h"
#include
#include
#include


CString::CString():_data(0)
{
  _data=new char[1];
  _data[0]='';
}

CString::CString(const char *str):_data(0)
{
  if(str)
  {
  _data=new char[strlen(str)+1];
  (_data,0x00,strlen(str)+1);
  strcpy(_data,str);
  }
  else
  {
  _data=new char[1];
  _data[0]=''; 
  }
}

CString::CString(CString &str):_data(0)
{
  須在複製中為動態分配空間的指標分配空間
  _data=new char[str.Length()+1];
  strcpy(_data,str._data);
}

CString::~CString()
{
  delete []_data;
}

the length of this string,not counting the null-tenating character.
int CString::Length() const
{
  return strlen(_data);
}

CString & CString::operator=(const CString &str)

  if(this==&str) be "this==str"
  {
  return *this;
  }
  delete []_data;//for the previous 'new'
  _data=new char[str.Length()+1];
  strcpy(_data,str._data);
  return *this;
}

value returned by c_str points to the internal character
referenced by the _data property
const char * CString::c_str()const
{
  return _data;
}

CString&  CString::sprintf(const char* format, ...)
{
#ifdef WIN32
  delete []_data;
  va_list paramList;
  va_start(paramList, format);
  int size = vsnprintf(NULL, 0, format, paramList);
  _data=new char[size+1];
  memset(_data,0x00,size+1);
  vsnprintf(_data, size, format, paramList);//Only for WIN32
  va_end(paramList);
  return *this;
#else
  delete []_data;
  _data=new char[1024*5];//maxsize:5k,may cause overflow
  va_list paramList;
  va_start(paramList, format);
  vsprintf(_data,format, paramList);
  va_end(paramList);
  return *this;
#endif
}

CString CString::operator+(const CString &str)
{
  CString tmp;
  tmp=*this;
  delete []_data;
  _data=new char[tmp.Length()+str.Length()+1];
  memset(_data,0x00,tmp.Length()+str.Length()+1);
  strcpy(_data,tmp.c_str());
  strcat(_data,str.c_str());
  return *this;
}

CString  CString::operator +(const char* str)
{
  CString tmp;
  tmp=*this;
  delete []_data;
  _data=new char[tmp.Length()+strlen(str)+1];
  memset(_data,0x00,tmp.Length()+strlen(str)+1);
  strcpy(_data,tmp.c_str());
  strcat(_data,str);
  return *this;
}

CString &CString::operator +=(const CString& rhs)
{
  CString tmp;
  tmp=*this;
  delete []_data;
  _data=new char[tmp.Length()+rhs.Length()+1];
  memset(_data,0x00,tmp.Length()+rhs.Length()+1);
  strcpy(_data,tmp.c_str());
  strcat(_data,rhs.c_str());
  return *this;
}

char &CString::operator [](const int idx)
{
  if( (idx<0) || (idx>Length()-1) )
  return cTmp;//if over range,return the reference of cTmp
  else
  return _data[idx];
}

bool  CString::operator !=(const CString& rhs) const
{
  if( strcmp(_data,rhs.c_str())!=0)
  return true;
  else
  return false;
}

bool  CString::operator {
  if( strcmp(_data,rhs.c_str())<0)
  return true;
  else
  return false;
}

bool  CString::operator <=(const CString& rhs) const
{
  if( strcmp(_data,rhs.c_str())<=0)
  return true;
  else
  return false;
}

bool  CString::operator ==(const CString& rhs) const
{
  if( strcmp(_data,rhs.c_str())==0)
  return true;
  else
  return false;
}

bool  CString::operator >(const CString& rhs) const
{
  if( strcmp(_data,rhs.c_str())>0)
  return true;
  else
  return false;
}

bool  CString::operator >=(const CString& rhs) const
{
  if( strcmp(_data,rhs.c_str())>=0)
  return true;
  else
  return false;
}

CString  CString::TrimLeft() const
{
  CString Result;
  char *pDest,*pTmp;
  pTmp=new char[Length()+1];
  memset(pTmp,0x00,Length()+1);
  strcpy(pTmp,_data);
  pDest=pTmp;

  for(int i=0;i  {
  if( (*pDest==' ') || (*pDest=='n') || (*pDest=='t') )
  pDest++;
  else
  break;
  }
  Result=pDest;
  delete []pTmp;
  return Result;
}

CString  CString::TrimRight() const
{
  CString Result;
  char *pDest,*pTmp;
  pTmp=new char[Length()+1];
  memset(pTmp,0x00,Length()+1);
  strcpy(pTmp,_data);
  pDest=pTmp+Length()-1;

  for(int i=Length();i>0;i--)
  {
  if( (*pDest==' ') || (*pDest=='n') || (*pDest=='t') )
  pDest--;
  else
  break;
  }
  *(++pDest)=0;//Cut to here
  Result=pTmp;
  delete []pTmp;
  return Result;
}

CString  CString::Trim() const
{
  CString Result;
  char *pDest,*pTmp,*p;
  pTmp=new char[Length()+1];
  memset(pTmp,0x00,Length()+1);
  strcpy(pTmp,_data);
  pDest=pTmp;

  for(int i=0;i  {
  if( (*pDest==' ') || (*pDest=='n') || (*pDest=='t') )
  pDest++;
  else
  break;
  }
  p=pTmp+Length()-1;

  for(int j=Length();j>0;j--)
  {
  if( (*p==' ') || (*p=='n') || (*p=='t') )
  p--;
  else
  break;
  }
  *(++p)=0;//Cut to here

  Result=pDest;
  delete []pTmp;
  return Result;
}

CString  CString::UpperCase() const
{
  CString Result;
  char *pTmp;
  pTmp=new char[Length()+1];
  memset(pTmp,0x00,Length()+1);
  strcpy(pTmp,_data);
  for(int i=0;i  {
  if( (pTmp[i]>='a') && (pTmp[i]<='z') )
  {
  pTmp[i]-=('a'-'A');
  }
  }
  Result=pTmp;
  delete []pTmp;
  return Result;
}

CString  CString::LowerCase() const
{
  CString Result;
  char *pTmp;
  pTmp=new char[Length()+1];
  memset(pTmp,0x00,Length()+1);
  strcpy(pTmp,_data);
  for(int i=0;i  {
  if( (pTmp[i]>='A') && (pTmp[i]<='Z') )
  {
  pTmp[i]+=('a'-'A');
  }
  }
  Result=pTmp;
  delete []pTmp;
  return Result;
}

CString  CString::StringOfChar(char ch, int count)const
{
  CString str;
  char *pTmp;
  pTmp=new char[count+1];
  memset(pTmp,0x00,count+1);
  for(int i=0;i  {
  pTmp[i]=ch;
  }
  str=pTmp;
  delete []pTmp;
  return str;
}

回被分隔符分割的字串,如
("ab,cd,ef",",",1) 返回"cd"
("ab,,ef",",",1) 返回"ef"
CString CString::GetDelimitedString(const char *delimiter,int index)
{
  CString strResult;
  int iIndex=0;
  int iLength=Length()+1;
  char *p=new char[iLength];
  char *p=NULL;
  memset(pSource,0,iLength);
  memcpy(pSource,_data,iLength);
  p=strtok(pSource,delimiter);
  while(p)
  {
  if(iIndex==index)
  {
  strResult=CString(p);
  break;
  }
  iIndex++;
  p=strtok(NULL,delimiter);
  }
  delete []pSource;
  pSource=NULL;
  return strResult;
}

#ifdef TEST
main()
{
  char *p="test";
  CString strA("1234");
  CString strB="123456";
  strA=strB.GetDelimitedString("/",1);
  CString strF;
  strF.sprintf("[%s]",strA.c_str());
  strF=strA.StringOfChar('A',3);
  printf("[%s]n",strF.c_str());
}   
#endif

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992885/,如需轉載,請註明出處,否則將追究法律責任。

相關文章