#include "StdAfx.h"
#include "kstring.h"
#include <wchar.h>
#include <windows.h>
#include "KAssert.h"
#include "KException.h"
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define FORCE_ANSI 0x10000
#define FORCE_UNICODE 0x20000
#define FORCE_INT64 0x40000
#define TCHAR_ARG TCHAR
#define WCHAR_ARG WCHAR
#define CHAR_ARG char
#ifdef _X86_
#define DOUBLE_ARG double/*_AFX_DOUBLE*/
#else
#define DOUBLE_ARG double
#endif
namespace kbasic
{
/****************************************************************************
*内部类的函数实现
****************************************************************************/
kstring::kstringData::kstringData(int nMaxLen)
{
_nLen = 0;
_nMaxLen = nMaxLen;//实际允许的最大字节数,\0的字节数不计算在其中
_nRef = 1;
_szV = new wchar_t[nMaxLen + 1];
_szV[0] = L'\0';
}
kstring::kstringData::kstringData(long nMaxLen)
{
_nLen = 0;
_nMaxLen = nMaxLen;//实际允许的最大字节数,\0的字节数不计算在其中
_nRef = 1;
_szV = new wchar_t[nMaxLen + 1];
_szV[0] = L'\0';
}
kstring::kstringData::kstringData(wchar_t* pszData, int nLen)
{
_nLen = nLen;
_nMaxLen = _nLen;//实际允许的最大字节数,\0的字节数不计算在其中
_nRef = 1;
_szV = pszData;
}
void kstring::kstringData::AddRef()
{
++_nRef;
}
void kstring::kstringData::Release()
{
--_nRef;
if (_nRef == 0)
delete this;
}
/****************************************************************************
*接口函数
****************************************************************************/
//构造与析构函数
kstring::kstring(const char* p) : m_pMBCSStr(NULL)
{
if (p == NULL)
{
m_pStr = allocBuf(0);
m_pStr->_szV[0] = L'\0';
}
else
{
long nLen = strlen(p);
long nNeed = MultiByteToWideChar(CP_ACP, 0, p, nLen, NULL, 0);
m_pStr = allocBuf(nNeed);
MultiByteToWideChar(CP_ACP, 0, p, nLen, m_pStr->_szV, nNeed);
m_pStr->_szV[nNeed] = L'\0';
}
}
kstring::kstring(const wchar_t* p) : m_pMBCSStr(NULL)
{
if (p == NULL)
{
m_pStr = allocBuf(0);
m_pStr->_szV[0] = L'\0';
}
else
{
long nLen = wcslen(p);
m_pStr = allocBuf(nLen);
wcscpy(m_pStr->_szV, p);
}
}
kstring::kstring(const char* p, int nLen) : m_pMBCSStr(NULL)
{
ASSERT(p != NULL);
long nNeed = MultiByteToWideChar(CP_ACP, 0, p, nLen, NULL, 0);
m_pStr = allocBuf(nNeed);
MultiByteToWideChar(CP_ACP, 0, p, nLen, m_pStr->_szV, nNeed);
m_pStr->_szV[nNeed] = L'\0';
}
kstring::kstring(const wchar_t* p, int nLen) : m_pMBCSStr(NULL)
{
ASSERT(p != NULL);
m_pStr = allocBuf(nLen);
wcsncpy(m_pStr->_szV, p, nLen);
m_pStr->_szV[nLen] = L'\0';
}
kstring::kstring(const kstring& old) : m_pMBCSStr(NULL)
{
ASSERT(old.m_pStr != NULL);
m_pStr = old.m_pStr;
m_pStr->AddRef();
}
kstring::kstring(int nReservedLen) : m_pMBCSStr(NULL)
{
m_pStr = new kstringData(nReservedLen);
}
kstring::~kstring(void)
{
m_pStr->Release();
clearMBCSBuf();
}
//=号重载函数
kstring& kstring::operator=(const char* p)
{
ASSERT(p != NULL);
clearMBCSBuf();
long nLen = strlen(p);
long nNeed = MultiByteToWideChar(CP_ACP, 0, p, nLen, NULL, 0);
if (m_pStr->getRef() == 1 && nNeed <= m_pStr->getMemLen())
{
MultiByteToWideChar(CP_ACP, 0, p, nLen, m_pStr->_szV, nNeed);
m_pStr->_szV[nNeed] = L'\0';
m_pStr->_nLen = nNeed;
}
else
{
m_pStr->Release();
m_pStr = allocBuf(nNeed);
MultiByteToWideChar(CP_ACP, 0, p, nLen, m_pStr->_szV, nNeed);
m_pStr->_szV[nNeed] = L'\0';
}
return *this;
}
kstring& kstring::operator=(const wchar_t* p)
{
ASSERT(p != NULL);
clearMBCSBuf();
long nLen = wcslen(p);
if (m_pStr->getRef() == 1 && nLen <= m_pStr->getMemLen())
{
wcscpy(m_pStr->_szV, p);
m_pStr->_nLen = nLen;
}
else
{
m_pStr->Release();
m_pStr = allocBuf(nLen);
wcscpy(m_pStr->_szV, p);
}
return *this;
}
kstring& kstring::operator=(const kstring& rhv)
{
if (this == &rhv)
return *this;
ASSERT(rhv.m_pStr != NULL);
clearMBCSBuf();
m_pStr->Release();
m_pStr = rhv.m_pStr;
m_pStr->AddRef();
return *this;
}
//+号重载函数
kstring operator+(const kstring& lhv, wchar_t ch)
{
if (ch == L'\0')
return lhv;
long nNeed = 1;
long nTotal = nNeed + lhv.getLength();
kstring strRet(nTotal);
wcscpy(strRet.m_pStr->_szV, (wchar_t *) lhv);
strRet.m_pStr->_szV[lhv.getLength()] = ch;
strRet.m_pStr->_szV[nTotal] = L'\0';
strRet.m_pStr->_nLen = nTotal;
return strRet;
}
kstring operator+(wchar_t ch, const kstring& rhv)
{
if (ch == L'\0')
return rhv;
long nNeed = 1;
long nTotal = nNeed + rhv.getLength();
kstring strRet(nTotal);
strRet.m_pStr->_szV[0] = ch;
wcscpy(strRet.m_pStr->_szV + 1, (wchar_t *) rhv);
strRet.m_pStr->_nLen = nTotal;
return strRet;
}
kstring operator+(const kstring& lhv, const kstring& rhv)
{
ASSERT(lhv.m_pStr != NULL && rhv.m_pStr != NULL);
long nTotal = lhv.getLength() + rhv.getLength();
kstring strRet(nTotal);
wcscpy(strRet.m_pStr->_szV, (const wchar_t *) lhv);
wcscpy(strRet.m_pStr->_szV + lhv.getLength(), (const wchar_t *) rhv);
strRet.m_pStr->_nLen = nTotal;
return strRet;
}
//+=号重载函数
kstring& kstring::operator+=(wchar_t ch)
{
if (ch == L'\0')//如果是字符串结事符,�
return *this;
clearMBCSBuf();
long nNeed = 1;
long nTotal = nNeed + m_pStr->_nLen;
if (m_pStr->getRef() == 1 && nTotal <= m_pStr->getMemLen())
{
m_pStr->_szV[m_pStr->_nLen] = ch;
m_pStr->_szV[nTotal] = L'\0';
m_pStr->_nLen = nTotal;
}
else
{
kstringData* pStr = allocBuf(nTotal);
wcscpy(pStr->_szV, m_pStr->_szV);
pStr->_szV[m_pStr->_nLen] = ch;
pStr->_szV[nTotal] = L'\0';
m_pStr->Release();
m_pStr = pStr;
}
return *this;
}
kstring& kstring::operator+=(const char* p)
{
ASSERT(p != NULL);
clearMBCSBuf();
long nLen = strlen(p);
long nNeed = MultiByteToWideChar(CP_ACP, 0, p, nLen, NULL, 0);
long nTotal = nNeed + m_pStr->_nLen;
if (m_pStr->getRef() == 1 && nTotal <= m_pStr->getMemLen())
{
MultiByteToWideChar(CP_ACP, 0, p, nLen, m_pStr->_szV + m_pStr->_nLen,
nNeed);
m_pStr->_szV[nTotal] = L'\0';
m_pStr->_nLen = nTotal;
}
else
{
kstringData* pStr = allocBuf(nTotal);
wcscpy(pStr->_szV, m_pStr->_szV);
MultiByteToWideChar(CP_ACP, 0, p, nLen, pStr->_szV + m_pStr->_nLen,
nNeed);
pStr->_szV[nTotal] = L'\0';
m_pStr->Release();
m_pStr = pStr;
}
return *this;
}
kstring& kstring::operator+=(const wchar_t* p)
{
ASSERT(p != NULL);
clearMBCSBuf();
long nNeed = wcslen(p);
long nTotal = nNeed + m_pStr->_nLen;
if (m_pStr->getRef() == 1 && nTotal <= m_pStr->getMemLen())
{
wcscpy(m_pStr->_szV + m_pStr->_nLen, p);
m_pStr->_nLen = nTotal;
}
else
{
kstringData* pStr = allocBuf(nTotal);
wcscpy(pStr->_szV, m_pStr->_szV);
wcscpy(pStr->_szV + m_pStr->_nLen, p);
m_pStr->Release();
m_pStr = pStr;
}
return *this;
}
kstring& kstring::operator+=(const kstring& rhv)
{
ASSERT(rhv.m_pStr != NULL);
clearMBCSBuf();
long nTotal = m_pStr->_nLen + rhv.m_pStr->_nLen;;
if (m_pStr->getRef() == 1 && nTotal <= m_pStr->getMemLen())
{
wcsncpy(m_pStr->_szV + m_pStr->_nLen, rhv.m_pStr->_szV,
rhv.m_pStr->_nLen);//避免加等自已造成的结束标记被覆盖的问题
m_pStr->_szV[nTotal] = L'\0';
m_pStr->_nLen = nTotal;
}
else
{
kstringData* pStr = allocBuf(nTotal);
wcscpy(pStr->_szV, m_pStr->_szV);
wcscpy(pStr->_szV + m_pStr->_nLen, rhv.m_pStr->_szV);
m_pStr->Release();
m_pStr = pStr;
}
return *this;
}
//比较函数:0:相等,<0:小于,>0:大于
long kstring::compare(const char* p)
{
ASSERT(p != NULL);
if (m_pMBCSStr == NULL)
{
//进行转换
int nLen = WideCharToMultiByte(CP_ACP, 0, m_pStr->_szV, m_pStr->_nLen,
NULL, 0, NULL, NULL);
m_pMBCSStr = new char[nLen + 1];
WideCharToMultiByte(CP_ACP, 0, m_pStr->_szV, m_pStr->_nLen,
m_pMBCSStr, nLen, NULL, NULL);
m_pMBCSStr[nLen] = '\0';
}
return
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
klib.rar (33个子文件)
klib
kxmlElement.cpp 16KB
klib.ncb 113KB
kxmlElement.h 3KB
kxmlNode.h 6KB
kxmlTextNode.h 2KB
klib.dsp 4KB
kxml.h 385B
kxmlEntities.cpp 4KB
kexception.cpp 140B
stdafx.h 157B
kxmlDeclaration.h 1KB
klib.opt 49KB
kencrypte.cpp 13KB
kencoding.cpp 10KB
kxmlEntities.h 1KB
kstringBuilder.h 438B
kxmlAttribute.h 2KB
kstring.cpp 23KB
kxmlTextNode.cpp 4KB
kencoding.h 6KB
kbuffer.h 1KB
kxmlNode.cpp 2KB
kxmlAttribute.cpp 5KB
klib.plg 3KB
Debug
vc60.pdb 36KB
kstringBuilder.cpp 727B
klib.dsw 533B
kobject.h 199B
kexception.h 975B
kobject.cpp 275B
kxmlDeclaration.cpp 5KB
kxmlDocument.h 2KB
kconfig.h 142B
共 33 条
- 1
资源评论
- ljy6912016-01-19能参考学习的一个库
- iloveyouysr2015-04-23正准备封装一个基础库,正好学习中,很不错!
疾似云流
- 粉丝: 5
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功