#pragma once


#pragma once

template<int INTERNAL_BUF_SIZE = 256>
class CWorkspaceString
{
public:
	CWorkspaceString(void)
	{
		InternalInit();
	}

	CWorkspaceString(const CWorkspaceString &other)
	{
		InternalInit();

		*this = other;
	}

	CWorkspaceString(const CString &other)
	{
		InternalInit();

		*this = other;
	}

	CWorkspaceString(const LPSTR other)
	{
		InternalInit();

		*this = other;
	}

	virtual ~CWorkspaceString(void)
	{
		//	開放しておく
		if(m_heapBuf)
			delete m_heapBuf;

		InternalInit();
	}

	/*!
		内部初期化
	*/
	void InternalInit()
	{
		m_strLen = 0;
		m_heapLen = 0;
		m_heapBuf = NULL;

		//	空文字列へ
		GetString()[0] = 0;
	}

	//////////////////////////////////////////////////////////////////////////////////////////
	//	取得
	//////////////////////////////////////////////////////////////////////////////////////////
	//!	LPCSTR
	operator LPCSTR() const
	{
		return const_cast<CWorkspaceString *>(this)->GetString();
	}

	//!	LPSTR
	operator LPSTR()
	{
		return GetString();
	}

	//!	サイズ取得
	int GetLength() const
	{
		return m_strLen;
	}

	//////////////////////////////////////////////////////////////////////////////////////////
	//	コピー
	//////////////////////////////////////////////////////////////////////////////////////////
	//!	CWorkspaceString
	void operator=(const CWorkspaceString &_other)
	{
		CWorkspaceString &other = const_cast<CWorkspaceString&>(_other);
		Copy(other, other.GetLength());
	}

	//!	CString
	void operator=(const CString &_other)
	{
		CString &other = const_cast<CString&>(_other);
		Copy(other, other.GetLength());
	}

	//!	LPCSTR
	void operator=(LPCSTR other)
	{
		Copy(other, strlen(other));
	}

	//!	char
	void operator=(LPSTR other)
	{
		Copy(other, strlen(other));
	}


	//////////////////////////////////////////////////////////////////////////////////////////
	//	比較
	//////////////////////////////////////////////////////////////////////////////////////////
	//!	CWorkspaceString
	int operator==(const CWorkspaceString &_other)
	{
		CWorkspaceString &other = const_cast<CWorkspaceString&>(_other);
		if(_mbscmp((unsigned char*)GetString(), (unsigned char*)((LPCSTR)other)) == 0)
			return(TRUE);
		return(FALSE);
	}

	//!	CString
	int operator==(const CString &_other)
	{
		CString &other = const_cast<CString&>(_other);
		if(_mbscmp((unsigned char*)GetString(), (unsigned char*)((LPCSTR)other)) == 0)
			return(TRUE);
		return(FALSE);
	}

	//!	LPCSTR
	int operator==(LPCSTR other)
	{
		if(_mbscmp((unsigned char*)GetString(), (unsigned char*)((LPCSTR)other)) == 0)
			return(TRUE);
		return(FALSE);
	}

	//!	LPCSTR
	int operator==(LPSTR other)
	{
		if(_mbscmp((unsigned char*)GetString(), (unsigned char*)((LPCSTR)other)) == 0)
			return(TRUE);
		return(FALSE);
	}

	//!	普通に比較
	int Compare(LPCSTR &other)
	{
		return(_mbscmp((unsigned char*)GetString(), (unsigned char*)((LPCSTR)other)));
	}

	//!	比較
	int CompareNoCase(LPCSTR &other)
	{
		return(_mbsicmp((unsigned char*)GetString(), (unsigned char*)((LPCSTR)other)));
	}


protected:
	//////////////////////////////////////////////////////////////////////////////////////////
	//	補助
	//////////////////////////////////////////////////////////////////////////////////////////
	/*!
		メモリ確保
	*/
	void ReSize(int size)
	{
		size ++;

		//	未確保？
		if(m_heapBuf == NULL && size <= INTERNAL_BUF_SIZE)
			return;

		//	確保不要？
		if(m_heapBuf != NULL && size <= m_heapLen)
			return;

		//	確保
		if(m_heapBuf)
			delete m_heapBuf;
		m_heapBuf = new char[size];
		m_heapLen = size;
	}


	/*!
		内部コピー
	*/
	void Copy(LPCSTR src, int len)
	{
		ReSize(len);

		//	コピー
		memcpy(GetString(), src, len + 1);
		m_strLen = len;
	}

	//!	文字列の取得
	LPSTR GetString()
	{
		if(m_heapBuf != NULL)
			return m_heapBuf;
		return m_stackBuf;
	}

protected:
	//////////////////////////////////////////////////////////////////////////////////////////
	//	メンバ変数
	//////////////////////////////////////////////////////////////////////////////////////////
	//!	文字列の長さ
	int			m_strLen;

	//!	非確保メモリ
	char		m_stackBuf[INTERNAL_BUF_SIZE];

	//!	ヒープメモリ
	char		*m_heapBuf;

	//!	ヒープの長さ
	int			m_heapLen;
};


//!	標準サイズ
typedef CWorkspaceString<256> CWorkspaceString255;
