设为首页 收藏本站
查看: 948|回复: 0

[经验分享] CString linux 实现

[复制链接]

尚未签到

发表于 2018-5-18 08:14:24 | 显示全部楼层 |阅读模式
  /********************************************************************
  ** STL HEAH FILE USE FOR 徐岩柏
  ** Copyright(c) 2001 -2003
  **-------------------------------------------------------------
  Filename:  stl.h
  Author : 徐岩柏
  Email  baikeley@hotmail.com
  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
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-461516-1-1.html 上篇帖子: linux bash特性(20170419) 下篇帖子: 权限列表(linux)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表