#pragma once // std::string Àº Å©±â°¡ 16ÀÌ»óÀÎ ¹®ÀÚ¿­Àº ÀüºÎ µ¿ÀûÇÒ´çÀ» ÇØ¹ö·Á¼­ // ÆÄÀϸíÀÇ ´ëºÎºÐÀÌ 16±æÀ̸¦ ³Ñ±â¹Ç·Î ÆÄÀϸí üũ½Ã ¸¶´ÙÀÇ ÀæÀº µ¿ÀûÇÒ´çÀÇ ºÎ´ãÀÌ Å©´Ù. // ÆÄÀÏ¸í ½ºÆ®¸µ¿¡´Â ¹öÆÛÅ©±â°¡ Àû´çÈ÷ Å« Ŭ·¡½º¸¦ Á÷Á¢ ¸¸µé¾î¼­ »ç¿ëÇϵµ·Ï ÇÑ´Ù. // typedef std::string CFileNameString; // »ç¿ë½Ã ¹®Á¦°¡ »ý°åÀ»¶§´Â À̰ÍÀ» Ų´Ù. class CFileNameString { public: enum { INTERNAL_BUFFER_SIZE = 50, // ´ëºÎºÐÀÇ ÆÄÀϸíÀÇ ±æÀ̰¡ 100ÀÌÇÏÀ̸ç, 100À̳Ѵ°͸¸ µ¿Àû ÇÒ´ç ÇÑ´Ù. }; typedef size_t size_type; private: char m_szInternalBuffer[ INTERNAL_BUFFER_SIZE + 1 ]; char *m_dynamicBuffer; size_type m_nSize; size_type m_nCapacity; private: void _Assign( const char *pString ) { size_type nLen = strlen( pString ); if( nLen <= INTERNAL_BUFFER_SIZE ) { if( nLen == 0 ) { m_szInternalBuffer[0]='\0'; } else { strcpy_s( m_szInternalBuffer, pString ); } } else { m_szInternalBuffer[ 0 ] = '\0'; if( m_nCapacity < nLen ) { if( m_nCapacity != 0 ) { delete [] m_dynamicBuffer; } m_dynamicBuffer = new char[ nLen + 1]; m_nCapacity = nLen; } strcpy_s( m_dynamicBuffer, nLen+1, pString ); } m_nSize = nLen; } void _Reset() { m_szInternalBuffer[ 0 ] = '\0'; m_dynamicBuffer = NULL; m_nSize = 0; m_nCapacity = 0; } void _Destroy() { if( m_dynamicBuffer ) { delete [] m_dynamicBuffer; m_dynamicBuffer = NULL; m_nCapacity = 0; } } public: CFileNameString() { _Reset(); } CFileNameString( const char *pString ) { _Reset(); _Assign( pString ); } CFileNameString( std::string &szString ) { _Reset(); _Assign( szString.c_str() ); } CFileNameString( const CFileNameString &szString ) { _Reset(); _Assign( szString.c_str() ); } ~CFileNameString() { _Destroy(); } public: void operator = ( const CFileNameString &rhs ) { _Assign( rhs.c_str() ); } void operator = ( const char *pString ) { _Assign( pString ); } void operator = ( char *pString ) { _Assign( pString ); } void operator = ( std::string &szString ) { _Assign( szString.c_str() ); } CFileNameString& operator += ( const CFileNameString& rhs ) { std::string szFullString = (char*)c_str(); szFullString += (char*)rhs.c_str(); _Assign( (char*)szFullString.c_str() ); return *this; } CFileNameString operator + ( const CFileNameString& rhs ) { std::string szFullString = (char*)c_str(); szFullString += (char*)rhs.c_str(); return CFileNameString( szFullString.c_str() ) ; } size_t size() { return m_nSize; } const char * c_str() const { return ( m_nSize <= INTERNAL_BUFFER_SIZE ) ? m_szInternalBuffer : m_dynamicBuffer; } char * begin() { return const_cast(c_str()); } char * end() { return const_cast(c_str()) + m_nSize; } const char & operator [] ( unsigned int index ) const { return c_str()[ index ]; } const bool operator < ( const CFileNameString & rhs ) const { return (strcmp( c_str(), rhs.c_str() ) < 0 ); } bool operator == ( char *rhs ) { return (strcmp( c_str(), rhs ) == 0 ); } bool operator == ( CFileNameString &rhs ) { return (strcmp( c_str(), rhs.c_str() ) == 0 ); } bool empty() { return ( m_nSize == 0 ); } }; template class StringPool { public: StringPool() : m_pBuffer( NULL ), m_nAllocSize(0), m_nSize(0) {} ~StringPool() { Clear(); } private: std::vector m_BufferList; T *m_pBuffer; size_t m_nAllocSize; size_t m_nSize; public: void Clear() { std::vector::iterator it = m_BufferList.begin(); while( it != m_BufferList.end() ) { delete [] (*it); ++it; } m_BufferList.clear(); } void Reserve( size_t nSize ) { if( m_pBuffer == NULL ) { m_pBuffer = new T[ _PoolSize ]; m_BufferList.push_back( m_pBuffer ); } else { if( m_nSize + nSize > _PoolSize ) { m_pBuffer = new T[ _PoolSize ]; m_BufferList.push_back( m_pBuffer ); m_nSize = 0; } } } T *Alloc( T *pSource , size_t nLength ) { ASSERT( (nLength+1 < _PoolSize) && (nLength >= 0)); if( nLength == 0 ) { static T cNullTerminate = '\0'; return &cNullTerminate; } Reserve( (nLength + 1) ); if( m_nSize > nLength+1 ) { if( memcmp( m_pBuffer + m_nSize - (nLength+1) , pSource, (nLength+1)*sizeof(T) ) == 0 ) { return m_pBuffer + m_nSize - (nLength+1); } } memcpy_s( m_pBuffer + m_nSize, (_PoolSize - m_nSize)*sizeof(T), pSource, sizeof(T) * nLength ); m_pBuffer[ m_nSize + nLength ] = 0; T *pResult = m_pBuffer + m_nSize; m_nSize += (nLength+1); return pResult; } }; bool ToMultiString(IN std::wstring& wstr, OUT std::string& str ); bool ToWideString(IN std::string& str, OUT std::wstring& wstr ); bool ToMultiString(IN WCHAR* wstr, OUT std::string& str ); bool ToWideString(IN CHAR* str, OUT std::wstring& wstr ); // ¼Ò¹®ÀÚ·Î º¯È¯ void ToLowerA(std::string& str); void ToLowerA( CFileNameString& str); void ToLowerW(std::wstring& str); // ´ë¹®ÀÚ·Î º¯È¯ void ToUpperA(std::string& str); void ToUpperW(std::wstring& str); std::wstring FormatW( const WCHAR* fmt, ... ); std::string FormatA( const CHAR* fmt, ... ); /* °ø¹éÀ¸·Î ¹®ÀÚ¸¦ ³ª´­ °æ¿ì.. ´ëÃæ ÀÌ·±½ÄÀ¸·Î ¾´´Ù. std::vector tokens; std::string str("-IP 127.0.0.1 -Port 5000", ); Tokenize( str, tokens, " "); */ void TokenizeA( const std::string& str, std::vector& tokens, const std::string& delimiters = " " ); void TokenizeW( const std::wstring& str, std::vector& tokens, const std::wstring& delimiters = L" " ); /* std::vector tokens; std::string str("{user_nick} {user_class}", ); Tokenize( str, tokens, "{", "}" ); ¸¶Áö¸· ÀÎÀÚ´Â °Ë»ö±¸ºÐÀÚ¸¦ Æ÷ÇÔÇÒ°ÍÀÎÁö ¿©ºÎ */ void TokenizeW( std::wstring& str, std::vector& tokens, std::wstring& szBegin, std::wstring& szEnd, bool bIncludeDelimiters ); void TokenizeA( std::string& str, std::vector& tokens, std::string& szBegin, std::string& szEnd, bool bIncludeDelimiters ); // ¹®ÀÚ¿­ÀÇ ¸ðµç °ø¹éÀ» ¾ø¾ÖÁØ´Ù. void RemoveSpaceA(std::string& str); void RemoveSpaceW(std::wstring& str); // ¹®ÀÚ¿­¿¡¼­ del À» Áö¿î´Ù. void RemoveStringA(std::string& str, const std::string& del); void RemoveStringW(std::wstring& str, const std::wstring& del); // str ¹®ÀÚ¿­ Áß¿¡¼­ szOld °¡ ÀÖÀ¸¸é ÀüºÎ szNew ·Î º¯°æÇÑ´Ù. void AllReplaceW( std::wstring& str, std::wstring& szOld, std::wstring& szNew ); void AllReplaceA( std::string& str, std::string& szOld, std::string& szNew ); // ¹®ÀÚ¿­ ¾ÕµÚ °ø¹é Á¦°Å void TrimString( char *pString );