📄 stringa.cpp
字号:
// CStringA.cpp : implementation file
//
// CStringA Implementation
//
// Written by Paul E. Bible <pbible@littlefishsoftware.com>
// Copyright (c) 2000. All Rights Reserved.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name and all copyright
// notices remains intact. If the source code in this file is used in
// any commercial application then a statement along the lines of
// "Portions copyright (c) Paul E. Bible, 2000" must be included in
// the startup banner, "About" box -OR- printed documentation. An email
// letting me know that you are using it would be nice as well. That's
// not much to ask considering the amount of work that went into this.
// If even this small restriction is a problem send me an email.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
// Expect bugs!
//
// Please use and enjoy, and let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into
// this file.
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "StringA.h"
#include <atlbase.h>
#include <TCHAR.H>
#include <stdio.h>
#include <Winbase.h>
//////////////////////////////////////////////////////////////////////
//
// Constructors/Destructor
//
//////////////////////////////////////////////////////////////////////
CStringA::CStringA()
{
m_pszString = NULL;
AllocString(0);
}
CStringA::CStringA(CStringA& str)
{
m_pszString = NULL;
int nLen = str.GetLength();
AllocString(nLen);
strncpy(m_pszString, (LPCSTR) str, nLen);
}
CStringA::CStringA(LPCSTR pszString)
{
m_pszString = NULL;
int nLen = strlen(pszString);
AllocString(nLen);
strncpy(m_pszString, pszString, nLen);
}
CStringA::CStringA(BSTR bstrString)
{
USES_CONVERSION;
m_pszString = NULL;
int nLen = _tcslen(OLE2T(bstrString));
AllocString(nLen);
strncpy(m_pszString, W2A(bstrString), nLen);
}
CStringA::CStringA(char ch, int nRepeat)
{
m_pszString = NULL;
if (nRepeat > 0)
{
AllocString(nRepeat);
memset(m_pszString, ch, nRepeat);
}
}
CStringA::~CStringA()
{
free(m_pszString);
}
//////////////////////////////////////////////////////////////////////
//
// Memory Manipulation
//
//////////////////////////////////////////////////////////////////////
void CStringA::AllocString(int nLen)
{
ATLASSERT(nLen >= 0);
if (m_pszString != NULL)
free(m_pszString);
m_pszString = (char*) malloc((nLen+1) * sizeof(char));
ATLASSERT(m_pszString != NULL);
m_pszString[nLen] = '\0';
}
//DEL void CStringA::AllocString2(int nLen)
//DEL {
//DEL ATLASSERT(nLen >= 0);
//DEL if (m_pszString != NULL)
//DEL free(m_pszString);
//DEL m_pszString = (TCHAR*) malloc((nLen+1) * sizeof(TCHAR));
//DEL ATLASSERT(m_pszString != NULL);
//DEL m_pszString[nLen] = '\0';
//DEL }
void CStringA::ReAllocString(int nLen)
{
ATLASSERT(nLen >= 0);
m_pszString = (char*) realloc(m_pszString, (nLen+1) * sizeof(char));
ATLASSERT(m_pszString != NULL);
m_pszString[nLen] = '\0';
}
void CStringA::StringCopy(int nSrcLen, LPCSTR lpszSrcData)
{
AllocString(nSrcLen);
memcpy(m_pszString, lpszSrcData, nSrcLen * sizeof(char));
m_pszString[nSrcLen] = '\0';
}
void CStringA::StringCopy(CStringA &str, int nLen, int nIndex, int nExtra) const
{
int nNewLen = nLen + nExtra;
if (nNewLen != 0)
{
str.AllocString(nNewLen);
memcpy(str.GetString(), m_pszString+nIndex, nLen * sizeof(char));
}
}
void CStringA::ConcatCopy(LPCSTR lpszData)
{
ATLASSERT(lpszData != NULL);
// Save the existing string
int nLen = GetLength();
char* pszTemp = (char*) malloc((nLen+1) * sizeof(char));
memcpy(pszTemp, m_pszString, nLen * sizeof(char));
pszTemp[nLen] = '\0';
// Calculate the new string length and realloc memory
int nDataLen = strlen(lpszData);
int nNewLen = nLen + nDataLen;
ReAllocString(nNewLen);
// Copy the strings into the new buffer
memcpy(m_pszString, pszTemp, nLen * sizeof(char));
memcpy(m_pszString+nLen, lpszData, nDataLen * sizeof(char));
// Cleanup
free(pszTemp);
}
void CStringA::ConcatCopy(char ch)
{
// Save the existing string
int nLen = GetLength();
char* pszTemp = (char*) malloc((nLen+1) * sizeof(char));
memcpy(pszTemp, m_pszString, nLen * sizeof(char));
pszTemp[nLen] = '\0';
// Calculate the new string length and realloc memory
int nNewLen = nLen + 1;
ReAllocString(nNewLen);
// Copy the strings into the new buffer
memcpy(m_pszString, pszTemp, nLen * sizeof(char));
m_pszString[nNewLen-1] = ch;
// Cleanup
//free(pszTemp);
}
void CStringA::ConcatCopy(LPCSTR lpszData1, LPCSTR lpszData2)
{
ATLASSERT(lpszData1 != NULL);
ATLASSERT(lpszData2 != NULL);
int nLen1 = strlen(lpszData1);
int nLen2 = strlen(lpszData2);
int nLen = nLen1 + nLen2;
AllocString(nLen);
memcpy(m_pszString, lpszData1, nLen1 * sizeof(char));
memcpy(m_pszString+nLen1, lpszData2, nLen2 * sizeof(char));
}
BSTR CStringA::AllocSysString()
{
int nLen = MultiByteToWideChar(CP_ACP, 0, m_pszString, GetLength(), NULL, NULL);
BSTR bstr = ::SysAllocStringLen(NULL, nLen);
if (bstr == NULL)
{
ATLASSERT(0);
return NULL;
}
MultiByteToWideChar(CP_ACP, 0, m_pszString, GetLength(), bstr, nLen);
return bstr;
}
//////////////////////////////////////////////////////////////////////
//
// Accessors for the String as an Array
//
//////////////////////////////////////////////////////////////////////
void CStringA::Empty()
{
if (strlen(m_pszString) > 0)
m_pszString[0] = '\0';
}
char CStringA::GetAt(int nIndex)
{
int nLen = strlen(m_pszString);
ATLASSERT(nIndex >= 0);
ATLASSERT(nIndex < nLen);
return m_pszString[nIndex];
}
char CStringA::operator[] (int nIndex)
{
int nLen = strlen(m_pszString);
ATLASSERT(nIndex >= 0);
ATLASSERT(nIndex < nLen);
return m_pszString[nIndex];
}
void CStringA::SetAt(int nIndex, char ch)
{
int nLen = strlen(m_pszString);
ATLASSERT(nIndex >= 0);
ATLASSERT(nIndex < nLen);
m_pszString[nIndex] = ch;
}
int CStringA::GetLength() const
{
return strlen(m_pszString);
}
bool CStringA::IsEmpty() const
{
return (GetLength() > 0) ? false : true;
}
//////////////////////////////////////////////////////////////////////
//
// Conversions
//
//////////////////////////////////////////////////////////////////////
void CStringA::MakeUpper()
{
_strupr(m_pszString);
}
void CStringA::MakeLower()
{
_strlwr(m_pszString);
}
void CStringA::MakeReverse()
{
_strrev(m_pszString);
}
void CStringA::TrimLeft()
{
LPSTR lpsz = m_pszString;
while (_istspace(*lpsz))
lpsz ++;//= _tcsinc(lpsz);
if (lpsz != m_pszString)
{
memmove(m_pszString, lpsz, (GetLength()+1) * sizeof(char));
}
}
void CStringA::TrimLeft(LPCSTR lpszTargets)
{
ATLASSERT(strlen(lpszTargets) > 0);
LPSTR lpsz = m_pszString;
while (*lpsz != '\0')
{
if (strchr(lpszTargets, *lpsz) == NULL)
break;
lpsz++;// = _tcsinc(lpsz);
}
if (lpsz != m_pszString)
{
int nDataLength = GetLength() - (lpsz - m_pszString);
memmove(m_pszString, lpsz, (nDataLength + 1) * sizeof(char));
}
}
void CStringA::TrimRight()
{
LPSTR lpsz = m_pszString;
LPSTR lpszLast = NULL;
while (*lpsz != '\0')
{
if (_istspace(*lpsz))
{
if (lpszLast == NULL)
lpszLast = lpsz;
}
else
lpszLast = NULL;
lpsz ++;//= _tcsinc(lpsz);
}
if (lpszLast != NULL)
*lpszLast = '\0';
}
void CStringA::TrimRight(LPCSTR lpszTargets)
{
ATLASSERT(strlen(lpszTargets) > 0);
LPSTR lpsz = m_pszString;
LPSTR lpszLast = NULL;
while (*lpsz != '\0')
{
if (strchr(lpszTargets, *lpsz) != NULL)
{
if (lpszLast == NULL)
lpszLast = lpsz;
}
else
lpszLast = NULL;
lpsz ++;//= _tcsinc(lpsz);
}
if (lpszLast != NULL)
{
// truncate at left-most matching character
*lpszLast = '\0';
}
}
//////////////////////////////////////////////////////////////////////
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -