Bstring.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <windows.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include <tchar.h>
  6. #ifndef __JONES__STRING__
  7. #define __JONES__STRING__
  8. typedef const BYTE FAR * LPBTSTR;
  9. struct BStringData
  10. {
  11. long nRefs; //引用记数
  12. int nDataLength; //字符使用长度
  13. int nAllocLength; //分配长度
  14. BYTE* data() { return (BYTE*)(this+1); } //存放字符串的地方
  15. //this+1 相当与是BStringData[1];所以TCHAR* data()指的是BStringData[1]的地址
  16. };
  17. //基本和网络通讯的数据包差不多
  18. typedef struct tagAnsMarketData //统一的应答结构
  19. {
  20. WORD wStkNum; //数目
  21. BYTE iData[1]; //数据
  22. }ANS_MARKET_DATA,*PANS_MARKET_DATA;
  23. //下面是代码了
  24. class BString
  25. {
  26. public:
  27. //构造函数
  28. BString();
  29. BString(const BString& stringSrc);
  30. BString(BYTE ch, int nLength =1);
  31. BString(LPBTSTR lpsz); // BString(LPCSTR lpsz); ANSI下版本
  32. //BString(LPCWSTR lpsz);UNICODE下版本
  33. BString(LPBTSTR lpch, int nLength); //BString(LPCSTR lpch, int nLength);ANSI下版本
  34. //BString(LPCWSTR lpch, int nLength);//UNICODE下版本
  35. //BString(const BYTE* psz);
  36. ~BString();
  37. //BStringData的属性
  38. int GetLength() const; //得到字符长度
  39. int GetAllocLength() const; //得到分配的内存长度
  40. BOOL IsEmpty() const; //判断字符长度是否为0
  41. operator LPBTSTR() const; //类型转换
  42. void Empty(); //清空BStringData
  43. //操作符重载
  44. const BString& operator=(const BString& stringSrc);
  45. const BString& operator=(LPBTSTR lpsz);
  46. const BString& operator=(BYTE ch);
  47. const BString& operator+=(const BString& string);
  48. const BString& operator+=(BYTE ch);
  49. const BString& operator+=(LPBTSTR lpsz);
  50. BYTE operator[](int nIndex) const;
  51. friend BString operator+(const BString& string1,const BString& string2);
  52. friend BString operator+(const BString& string, BYTE ch);
  53. friend BString operator+(BYTE ch, const BString& string);
  54. friend BString operator+(const BString& string, LPBTSTR lpsz);
  55. friend BString operator+(LPBTSTR lpsz, const BString& string);
  56. //操作,脱离共享数据块
  57. int Delete(int nIndex, int nCount = 1);//删除从nIndex开始长度为nCount的数据
  58. int Insert(int nIndex, BYTE ch); //插入一个字符
  59. int Insert(int nIndex, LPBTSTR pstr); //插入一个字符串
  60. int Replace(LPBTSTR lpszOld, LPBTSTR lpszNew); //替换数据
  61. int Replace(BYTE chOld, BYTE chNew); //替换数据
  62. int Remove(BYTE chRemove); //移除一个字符
  63. void TrimRight(LPBTSTR lpszTargetList);
  64. void TrimRight(BYTE chTarget);//去掉右边chTarget
  65. void TrimRight(); //去掉右边空格
  66. void TrimLeft(LPBTSTR lpszTargets);
  67. void TrimLeft(BYTE chTarget); //去掉左边chTarget
  68. void TrimLeft(); //去掉左边空格
  69. //取某段字符串
  70. void SetAt(int nIndex, BYTE ch);
  71. BYTE GetAt(int nIndex) const;
  72. BString Mid(int nFirst) const; //取某段字符串
  73. BString Mid(int nFirst, int nCount) const; //取某段字符串
  74. BString Right(int nCount) const; //取右边字符串
  75. BString Left(int nCount) const; //取左边字符串
  76. void BString::MakeUpper(); //大写
  77. void BString::MakeLower(); //小写
  78. void BString::MakeReverse(); //????不知道干什么的 strrev
  79. //查找
  80. int Find(BYTE ch) const;
  81. int Find(BYTE ch, int nStart) const;
  82. int ReverseFind(BYTE ch) const;
  83. int Find(LPBTSTR lpszSub) const;
  84. int Find(LPBTSTR lpszSub, int nStart) const;
  85. int FindOneOf(LPBTSTR lpszCharSet) const;//得到第一个匹配lpszCharSet中其中一个字符的位置 调用_tcspbrk
  86. //高级操作
  87. BYTE* GetBuffer(int nMinBufLength); //重新分配内存,在拷贝原来的数据
  88. void ReleaseBuffer(int nNewLength=-1); //在[nNewLength]='\0',对内存大小没有改变
  89. BYTE* GetBufferSetLength(int nNewLength); //重新分配内存,在拷贝原来的数据
  90. void FreeExtra(); //深拷贝自己,然后--原来的引用记数器
  91. BYTE* LockBuffer(); //引用计数器=-1,加锁
  92. void UnlockBuffer(); //解锁,引用计数器=1
  93. //比较
  94. int Compare(LPBTSTR lpsz) const; //区分大小写比较
  95. int CompareNoCase(LPBTSTR lpsz) const; //不区分大小写比较
  96. //比较速度没有Compare快
  97. int Collate(LPBTSTR lpsz) const; //区分大小写比较
  98. int CollateNoCase(LPBTSTR lpsz) const; //不区分大小写比较
  99. //格式化字符串
  100. void Format(LPBTSTR lpszFormat, ...);//CSting中最长的函数了,完全是自己分析的(牛啊)
  101. private:
  102. void Init();
  103. BStringData* GetData() const; //通过m_pchData-1 得到BStringData
  104. void AllocBuffer(int nLen); //给BStringData分配内存,不带记数器
  105. void CopyBeforeWrite(); //带引用记数的复制自己深拷贝
  106. void AllocBeforeWrite(int nLen); //给BStringData分配内存,带记数器
  107. void AssignCopy(int nSrcLen, LPBTSTR lpszSrcData);//分配内存,并拷贝lpszSrcData内容
  108. //把nCopyIndex开始的nCopyLen长度的数据拷贝给dest,nExtraLen扩充的长度,次函数好像没下面用
  109. void AllocCopy(BString& dest, int nCopyLen, int nCopyIndex,int nExtraLen) const;
  110. void Release(); //--引用记数器并判断是否删除内存,如删除并初始化
  111. void FormatV(LPBTSTR lpszFormat, va_list argList);//格式化字符串
  112. void ConcatCopy(int nSrc1Len, LPBTSTR lpszSrc1Data,
  113. int nSrc2Len, LPBTSTR lpszSrc2Data);//连接数据lpszSrc1Data+lpszSrc2Data
  114. void ConcatInPlace(int nSrcLen, LPBTSTR lpszSrcData); //连接字符串
  115. static void Release(BStringData* pData); //--引用记数器并判断是否删除内存
  116. static void FreeData(BStringData* pData); //释放内存
  117. static int SafeStrlen(LPBTSTR lpsz); //得到长度
  118. BYTE* m_pchData; //指向BStringData的数据区
  119. };
  120. /*调用BString::Compare比较大小,如果比较中有CStirng的话用
  121. 调用operator LPBTSTR()转化类型为LPBTSTR
  122. */
  123. bool operator==(const BString& s1, const BString& s2);
  124. bool operator==(const BString& s1, LPBTSTR s2);
  125. bool operator==(LPBTSTR s1, const BString& s2);
  126. bool operator!=(const BString& s1, const BString& s2);
  127. bool operator!=(const BString& s1, LPBTSTR s2);
  128. bool operator!=(LPBTSTR s1, const BString& s2);
  129. bool operator<(const BString& s1, const BString& s2);
  130. bool operator<(const BString& s1, LPBTSTR s2);
  131. bool operator<(LPBTSTR s1, const BString& s2);
  132. bool operator>(const BString& s1, const BString& s2);
  133. bool operator>(const BString& s1, LPBTSTR s2);
  134. bool operator>(LPBTSTR s1, const BString& s2);
  135. bool operator<=(const BString& s1, const BString& s2);
  136. bool operator<=(const BString& s1, LPBTSTR s2);
  137. bool operator<=(LPBTSTR s1, const BString& s2);
  138. bool operator>=(const BString& s1, const BString& s2);
  139. bool operator>=(const BString& s1, LPBTSTR s2);
  140. bool operator>=(LPBTSTR s1, const BString& s2);
  141. //////////////////////////////////////////////////////////////////////
  142. //检测lpsz是否有效,调用了IsBadStringPtr
  143. BOOL AfxIsValidString(LPBTSTR lpsz, int nLength = -1);
  144. //检测lp是否能读写权限,调用了IsBadReadPtr,IsBadStringPtr
  145. BOOL AfxIsValidAddress(const void* lp,UINT nBytes, BOOL bReadWrite = TRUE);
  146. //CStirng数组操作
  147. void ConstructElements(BString* pElements, int nCount); //初始化CStirng数组
  148. void DestructElements(BString* pElements, int nCount); //删除CStirng数组
  149. void CopyElements(BString* pDest, const BString* pSrc, int nCount); //BString数组拷贝
  150. #endif