#ifndef __YOUNG__BUFFER
#define __YOUNG__BUFFER
#include <stdio h="">
class YoungBuffer
{
protected:
char * m_pData;
public:
YoungBuffer();
};
#endif
#include "YoungBuffer.h"
YoungBuffer::YoungBuffer():m_pData(NULL)
{
}
</stdio>
#ifndef __YOUNG__STRING
#define __YOUNG__STRING
#include <stdarg.h>
#include <string>
#define MAX_LOCAL_STRING_LEN 256
using namespace std;
class YoungString:public YoungBuffer
{
public:
virtual void clear() = 0;
virtual int length() = 0;
virtual void assign(const char* p_data, int iLen = -1) = 0;
virtual void append(const char* p_data) = 0;
virtual void upper() =0;
virtual void lower() = 0;
virtual int __cdecl Format(char* pstrFormat, ...) = 0;
};
class YoungStringA:public YoungString
{
protected:
bool m_bWideChar;
char m_Buffer[MAX_LOCAL_STRING_LEN + 1];//因为需要\0结尾,所以长度+1
protected:
char* GetPtr(const char *p_data,bool bAppend);
public:
int __cdecl Format(char* pstrFormat, ...);
void upper();
void lower();
void clear();
int length();
void assign(const char* p_data, int iLen = -1);
void append(const char *p_data);
YoungStringA(const char * p_data = NULL);
YoungStringA(const char * p_data,int iLen);
YoungStringA(YoungStringA& src);
operator char *() ;
operator const char *() ;
YoungStringA& operator=(const char * p_data);
YoungStringA& operator=(string str);//YoungString operator=(string str); 区别 是否调用拷贝构造函数
};
#endif
// YoungString.cpp : Defines the entry point for the console application.
//
#include <tchar.h>
#include "YoungBuffer.h"
#include "YoungString.h"
int YoungStringA::Format(char* pstrFormat, ...)
{
YoungStringA sFormat = pstrFormat;
// Do ordinary printf replacements
// NOTE: Documented max-length of wvsprintf() is 1024
char szBuffer[1025] = { 0 };
va_list argList;
va_start(argList, pstrFormat);
int iRet = vsprintf(szBuffer, sFormat, argList);
va_end(argList);
assign(szBuffer);
return iRet;
}
void YoungStringA::clear()
{
m_pData = NULL;
memset(m_Buffer, 0, MAX_LOCAL_STRING_LEN+1);
}
void YoungStringA::append(const char *p_data)
{
int iNewLength = length() + (int) strlen(p_data);
if( iNewLength >= MAX_LOCAL_STRING_LEN )
{
if( m_pData == m_Buffer )
{
m_pData = static_cast<char*>(malloc((iNewLength + 1) * sizeof(char)));
strcpy(m_pData, m_Buffer);
strcat(m_pData, p_data);
}
else
{
m_pData = static_cast<char*>(realloc(m_pData, (iNewLength + 1) * sizeof(char)));
strcat(m_pData, p_data);
}
}
else
{
if( m_pData != m_Buffer )
{
free(m_pData);
m_pData = m_Buffer;
}
strcat(m_Buffer, p_data);
}
}
void YoungStringA::upper()
{
strupr(m_pData);
}
void YoungStringA::lower()
{
strlwr(m_pData);
}
int YoungStringA::length()
{
if (m_bWideChar)
{
return wcslen((const wchar_t*)m_pData);
}
else
{
return strlen((const char*)m_pData);
}
//return strlen()
}
void YoungStringA::assign(const char* p_data, int iLen)
{
if( p_data == NULL ) p_data = "";
iLen = (iLen < 0 ? (int) strlen(p_data) : iLen);
if( iLen < MAX_LOCAL_STRING_LEN )
{
if( m_pData != m_Buffer )
{
free(m_pData);
m_pData = m_Buffer;
}
}
else if( iLen > length() || m_pData == m_Buffer ) //
{
if( m_pData == m_Buffer )
m_pData = NULL;
m_pData = (char*)realloc(m_pData, (iLen + 1) * sizeof(char));
}
memset(m_pData,0, iLen);
strncpy(m_pData, p_data, iLen);
}
YoungStringA::YoungStringA(const char * sz)
{
clear();
assign(sz);
}
YoungStringA::YoungStringA(const char * sz,int iLen)
{
clear();
assign(sz,iLen);
}
YoungStringA::YoungStringA(YoungStringA& src)//拷贝构造函数
{
assign(src.m_pData);
}
YoungStringA& YoungStringA::operator=(const char* p_data)
{
if (p_data == NULL)
{
clear();
}
else
{
assign(p_data);
}
return *this;
}
YoungStringA& YoungStringA::operator=(string str)
{
assign(str.c_str());
return *this;
}
YoungStringA::operator char*()
{
return m_pData;
}
YoungStringA::operator const char*()
{
return m_pData;
}
//待扩充!!!!!
https://code.google.com/p/power-younglib