CString linux 實現

/********************************************************************

** STL HEAH FILE USE FOR 徐巖柏

** Copyright(c) 2001 -2003

**-------------------------------------------------------------

Filename:  stl.h

Author : 徐巖柏

Email  [email protected]  

Date:  2001-4-29

If you find any bug or you want to add some useful function 

please email to me.Redistribution and use in source are permitted

provided that: source distributions retain this entire copyright

notice and comment.

如果你在使用中發現BUG或添加了功能,請Email我。你可以使用與發佈該文件

,但不得進行商業用途。你的發佈要包含完整的本文件頭。

*********************************************************************/

#ifndef STL_H_BAIKELKEY

#define STL_H_BAIKELKEY

#pragma warning(disable:4786) 

#pragma warning(disable:4275) 

#include <iostream>

#include <iterator>

#include <list>

#include <deque>

#include <map>

#include <numeric>     //accumulate

#include <set>

#include <stack>

#include <vector>

#include <algorithm>

#include <functional>

#include <string>

#include <fstream>

#include <memory>

#include <queue>

#include <complex>

//#include <sstream>

#include <cctype>   //isalpha()

#include <new>

#include <cstdarg>  //number not knowing arguments.such as :pringf

#include <utility>

#include <cstdio>

#include <cstdlib>

#include <cassert>


#define _min(a,b) ((a)<(b)?(a):(b))

#define _max(a,b) ((a)>(b)?(a):(b))

#define bzero(ptr,n) memset(ptr,0,n)

//////////以下是一個string的包裝類/////////////////////

class XString : public std::string  

public:

int Delete( int nIndex, int nCount = 1 )

{

this->erase(nIndex,nCount);

return this->GetLength();

}

int Insert( int nIndex, const char * pstr )

{

this->insert(nIndex,pstr);

return this->GetLength(); 

}

int Insert( int nIndex, char ch )

XString strTmp(ch);

this->insert(nIndex,strTmp);

strTmp.Empty();

return this->GetLength();

}

int Remove( char ch )

{

XString::iterator iter;

int count = 0;

for(iter = this->begin(); iter != this->end();iter ++)

{

if(*iter == ch)

{

this->erase(iter);count++;

}

}

return count;

}

void MakeReverse( )

{

XString strTmp;

XString::iterator iter;

iter=this->end();

iter--;

for(; iter != this->begin(); iter--)

{

strTmp += *iter;

}

strTmp += *iter;

*this = strTmp;

strTmp.Empty();

}

int Find( char ch ) const

{

return this->find(ch);

}

int Find( const char * lpszSub ) const

{

return this->find(lpszSub);

}

int Find( char ch, int nStart ) const

{

return this->find(ch,nStart);

}

int Find( const char * pstr, int nStart ) const

{

return this->find(pstr,nStart);

}

int ReverseFind( char ch ) const

{

return this->find_last_of(ch); 

}

int FindOneOf( const char * lpszCharSet ) const

{

return this->find_first_of(lpszCharSet);

}

int Format(const char* pstrFormat, ... )

{///本函數僅僅支持ANSI標準字符集 '%[flags] [width] [.precision] [{h | l | I64 | L}]type'   

assert(pstrFormat!=NULL);

va_list argList;

va_start(argList,pstrFormat); 

int nMaxLen = 0;

for (const char * p = pstrFormat; *p != '\0';p++ )

{

if (*p != '%' || *(++p) == '%')

{// 如果不是'%'就直接累計長度,如果是'%%'也使長度加1

nMaxLen += 1;

continue;

}

int nItemLen = 0; //用來保存每個參數的長度

int nWidth = 0; //用來保存每一項的寬度

for (; *p != '\0'; p ++)

{

if (*p == '#')

nMaxLen += 2;   // 處理 '0x'

else if (*p == '*')

nWidth = va_arg(argList, int);  //如:'%5f' 中的5

else if (*p == '-' || *p == '+' || *p == '0'|| *p == ' ')

;  //忽略該符號

else // 不是標誌字符就退出循環

break;

}

if (nWidth == 0)

{ //提取寬度

nWidth = atoi(p);

for (; *p != '\0' && isdigit(*p); p ++)

;

}

assert(nWidth >= 0);//有效寬度

int nPrecision = 0; //精度位數

if (*p == '.')

{

p++;// 跳過 '.'字符 (寬度.精度)   

if (*p == '*')

{ //有參數給出

nPrecision = va_arg(argList, int);

p ++;// 取得精度,跳過字符

}

else

{ //在格式串中有寬度

nPrecision = atoi(p);

for (; *p != '\0' && isdigit(*p); p ++)

;

}

assert(nPrecision >= 0);//有效寬度

}

switch (*p)

{

case 'h':     //short int 型

p ++;

break;

case 'l':  //long double 型

p ++;

break;

case 'F':  //近指針

case 'N': //遠指針 

case 'L': //long double 型

p++;

break;

}

switch (*p)

{

case 'c':   //// 單個字符

case 'C':

nItemLen = 2;

va_arg(argList, char);

break;

case 's': //// 字符串

case 'S':

nItemLen = strlen(va_arg(argList, const char*));

nItemLen = ((1) > (nItemLen)) ? (1) : (nItemLen);//如果是空串就使用1 即保存'\0'

break;

}

if (nItemLen != 0)  

{

nItemLen = ((nItemLen) > (nWidth)) ? (nItemLen) : (nWidth);//使用大者

if (nPrecision != 0)

nItemLen = ((nItemLen) < (nPrecision)) ? (nItemLen) : (nPrecision);

}

else

{

switch (*p)

{

case 'd':    //整數的處理

case 'i':

case 'u':

case 'x':

case 'X':

case 'o':

va_arg(argList, int);

nItemLen = 32;  //四字節

nItemLen = ((nItemLen) > (nWidth+nPrecision)) ? (nItemLen) : (nWidth+nPrecision);//使用大者

break;

case 'e': //浮點數

case 'f':

case 'g':

case 'G':

va_arg(argList, double);

nItemLen = 32;//四字節

nItemLen = ((nItemLen) > (nWidth+nPrecision)) ? (nItemLen) : (nWidth+nPrecision);//使用大者;

break;

case 'p': //指針

va_arg(argList, void*);

nItemLen = 32;

nItemLen = ((nItemLen) > (nWidth+nPrecision)) ? (nItemLen) : (nWidth+nPrecision);//使用大者;

break;

case 'n':

va_arg(argList, int*); //指向整數的指針,見BorlanderC++3.1庫函數P352

break;

default:

assert(false);  //不能處理的格式,給出警告

}

}

nMaxLen += nItemLen;//把該項的長度累計

}

va_end(argList);

va_start(argList, pstrFormat);  // 重新開始提取參數

char* ch = new char[nMaxLen+1]; //分配內存

vsprintf(ch, pstrFormat, argList);

//assert(vsprintf(ch, pstrFormat, argList) <= nMaxLen);

this->append(ch); //加到string的尾部

delete[] ch; //釋放內存

va_end(argList);

return nMaxLen;

}

int GetLength() const

{

return this->length();

}

XString Left(int nCount) const

{

if (nCount <=0) 

return XString(""); 

XString strTmp;

strTmp = this->substr(0,nCount);

return strTmp;

}

XString Right(int nCount) const

{

if (nCount <=0) 

return XString("");

XString strTmp;

if (nCount > GetLength())

strTmp = this->substr(0);

else

strTmp = this->substr(GetLength()-nCount);

return strTmp;

}

XString Mid(int nFirst) const

{

XString strTmp;

if (nFirst >= GetLength())

return XString("");

if (nFirst <= 0)

strTmp = this->substr(0);

else

strTmp = this->substr(nFirst);

return strTmp;  

}

XString Mid( int nFirst, int nCount) const

{

if (nCount <= 0) 

return XString("");

if (nFirst >= GetLength())

return XString("");

XString strTmp;

if (nFirst <= 0)

strTmp = this->substr(0,nCount);

else

strTmp = this->substr(nFirst,nCount);

return strTmp;    

}

XString& operator=(const std::string str)

{

if (this->compare(str) == 0) return *this;

this->assign(str);

return *this;

}

XString& operator=(char ch)

{

this->Empty();

this->insert(this->begin(),ch);

return *this;

}

XString& operator =( const char * lpsz )

{

this->Empty();

this->append(lpsz);

return *this;

}

void MakeUpper()

{

std::transform(this->begin (),

this->end (),this->begin (),

toupper);

}

void MakeLower()

{

std::transform(this->begin (),

this->end (),this->begin (),

tolower);

}

bool IsEmpty( ) const

{

return this->empty(); 

}

void Empty( )

{//清除

this->erase(this->begin(),this->end());

}

char GetAt( int nIndex ) const

{

return this->at(nIndex);

}

char operator []( int nIndex ) const

{

return this->at(nIndex);

}

void SetAt( int nIndex, char ch )

{

this->at(nIndex) = ch;

}

operator const char * ( ) const

{

return this->c_str();

}

friend XString operator + (const XString& string1, const XString& string2)

{

XString str;

str.append(string1);

str.append(string2);

return str;

}

friend XString operator + ( const XString& string1, char ch )

{

XString str;

str.append(string1);

str.insert(str.end(),ch);

return str;

}

friend XString operator + ( const XString& string1, char* ch )

{

XString str;

str.append(string1);

str.append(ch);

return str;

}

int Compare( const char * lpsz ) const

{

XString str;

str.append(lpsz);

return this->compare(str);

}

int Compare( const XString& string1 ) const

{

return this->compare(string1);

}

int CompareNoCase( const char * lpsz ) const

{

XString str,strThis;

str.append(lpsz);

strThis = (*this);

str.MakeLower();

strThis.MakeLower();

return strThis.compare(str);

}

int CompareNoCase( const XString& string1 ) const

{

XString str,strThis;

str = string1;

strThis = (*this);

str.MakeLower();

strThis.MakeLower();

return strThis.compare(str);

}

void TrimRight( )

{

TrimRight (' ');

}

void TrimLeft( )

{   

TrimLeft(' ');

}

void TrimLeft( char chTarget )

{

std::string::size_type pos;

pos = this->find_first_not_of(chTarget);

if (pos == 0) return; 

this->erase(this->begin(),this->begin()+pos);  

}

void TrimRight( char chTarget )

{

std::string::size_type pos;

pos = this->find_last_not_of(chTarget);

++pos;

if (pos == this->GetLength())

return; 

this->erase(this->begin()+pos,this->end());  

}

void Replace( char chOld, char chNew )

{

for(int i=0;i<this->GetLength();i++)

{

if (this->at(i) == chOld)

this->at(i) = chNew;

}  

}

void Replace(const char* chOld,const char* chNew )

{

int index = this->find(chOld);

while (index > -1)

{

this->erase(index,strlen(chOld));

this->insert(index,chNew);

index = this->find(chOld);

}

}  

//char * GetBuffer( int nMinBufLength )

//{

// this->resize(nMinBufLength);

// return this->begin();

//}

//void ReleaseBuffer( int nNewLength = -1 )

//{

// this->TrimRight('\0');

//}

XString(const XString& string1)

{

this->append(string1);

}

XString(const char *ch)

{

this->append(ch);

}

XString(const char ch)

{

*this += ch;

}

XString()

{}

};

#endif


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章