www.pudn.com > acdx.rar > StringUtil.h


#if !defined(StringUtil_H) 
#define StringUtil_H 
 
 
#include  
#include  
#include  
using namespace std; 
 
 
///////////////////////////////////////////////////////////////////////////////// 
// StringUtil 
// 
// Purpose:		provides common utility methods for strings 
 
 
//##ModelId=424BB64102C6 
class StringUtil 
{ 
public: 
	//##ModelId=424BB64102C7 
	StringUtil () 
	{} 
 
 
	//##ModelId=424BB64102D6 
	static 
	void trimRight ( string & str ) 
	{ 
		// trim spaces right 
		trimRight( str, " " ); 
 
		/* 
		// find last that is not a space 
		string::reverse_iterator last =  
		std::find_if( str.rbegin(), str.rend(), std::bind2nd(std::not_equal_to(),' ') ); 
 
		// remove spaces from end (here we use the knowledge that a reverse_terator  
		// is based on an iterator refer include file xstring 
		str.erase( (string::iterator &) last, str.end() ); 
		*/ 
	} 
 
	//##ModelId=424BB64102D9 
	static 
	void trimRight( string & str, TCHAR chTarget ) 
	{ 
		// setup buffer 
		TCHAR buffer[2]; 
		buffer[0] = chTarget; 
		buffer[1] = '\0'; 
 
		trimRight( str, buffer ); 
 
		/* 
		// find last that is not a space 
		string::reverse_iterator last =  
		std::find_if( str.rbegin(), str.rend(),  
		              std::bind2nd(std::not_equal_to(),chTarget) ); 
 
		// remove spaces from end (here we use the knowledge that a reverse_terator  
		// is based on an iterator refer include file xstring 
		str.erase( (string::iterator &) last, str.end() ); 
		*/ 
	} 
 
	//##ModelId=424BB64102E7 
	static 
	void trimRight ( string & str, LPCTSTR lpszTargets ) 
	{ 
		// find first that is not space 
		long pos  = str.find_last_not_of(lpszTargets); 
 
		// if start then nothing found 
		if ( pos == str.size() || pos == -1 ) 
			return; 
 
		// remove from string 
		str.erase(0,pos) ; 
		/* 
		// find last that is not a space 
		string::reverse_iterator last =  
		std::find_if( str.rbegin(), str.rend(),  
		              std::bind2nd(std::not_equal_to(),lpszTargets) ); 
 
		// remove spaces from end (here we use the knowledge that a reverse_terator  
		// is based on an iterator refer include file xstring 
		str.erase( (string::iterator &) last, str.end() ); 
		*/ 
	} 
 
	//##ModelId=424BB64102F5 
	static 
	void trimLeft ( string & str, TCHAR chTarget ) 
	{ 
		// setup buffer 
		TCHAR buffer[2]; 
		buffer[0] = chTarget; 
		buffer[1] = '\0'; 
 
		trimLeft( str, buffer ); 
	} 
 
	//##ModelId=424BB64102F9 
	static 
	void trimLeft ( string & str, LPCTSTR lpszTargets ) 
	{ 
		// find first that is not space 
		long pos  = str.find_first_not_of(lpszTargets); 
 
		// if start then nothing found 
		if ( pos == 0  || pos == -1 ) 
			return; 
 
		// remove from string 
		str.erase(0,pos) ; 
	} 
 
 
	//##ModelId=424BB6410308 
	static 
	void trimLeft ( string & str ) 
	{ 
		// trim spaces 
		trimLeft( str, " " ); 
	} 
 
 
 
	//##ModelId=424BB6410315 
	static 
	int replace ( string & str, TCHAR chOld, TCHAR chNew ) 
	{ 
		// replace if found 
		str.replace( str.begin(), str.end(), chOld, chNew ); 
 
		return 0; 
	} 
 
	//##ModelId=424BB641031A 
	static 
	int replace ( string & str, LPCTSTR lpszOld, LPCTSTR lpszNew ) 
	{ 
		// replace if found 
		str.replace( str.begin(), str.end(), lpszOld, lpszNew ); 
 
		return 0; 
	} 
 
 
	//##ModelId=424BB6410328 
	static 
	string left ( const string & str, long pos ) 
	{ 
		return str.substr( 0, pos ); 
	} 
 
	//##ModelId=424BB6410337 
	static 
	string mid ( const string & str, long pos, long len = -1 ) 
	{ 
		if ( len == -1 ) 
			len = str.size() - 1; 
 
		return str.substr( pos, len ); 
	} 
 
	//##ModelId=424BB6410345 
	static 
	void makeUpper ( string & str ) 
	{ 
		// reverse case of string 
		for( string::iterator p = str.begin(); p != str.end(); p++ ) 
		{ 
			if( islower( *p ) ) 
				*p = _toupper( *p ); 
		} 
 
	} 
 
 
	//##ModelId=424BB6410353 
	static 
	void makeLower ( string & str ) 
	{ 
		// reverse case of string 
		for( string::iterator p = str.begin(); p != str.end(); p++ ) 
		{ 
		  if( isupper( *p ) ) 
			 *p = _tolower( *p ); 
		} 
 
	} 
 
	//##ModelId=424BB6410356 
	static 
	bool compareNoCase ( const string & strIn1, LPTSTR strIn2 ) 
	{ 
		// come back later and do compare with  
		// conversion inline 
		string str1 = strIn1; 
		makeLower( str1 ); 
 
		string str2 = strIn2; 
		makeLower( str2 ); 
 
		if ( str1 == str2 ) 
			return true; 
		else 
			return false; 
	} 
 
 
	//##ModelId=424BB6410364 
	static 
	bool compareNoCase ( const string & strIn1, const string & strIn2 ) 
	{ 
		// come back later and do compare with  
		// conversion inline 
		string str1 = strIn1; 
		makeLower( str1 ); 
 
		string str2 = strIn2; 
		makeLower( str2 ); 
 
		if ( str1 == str2 ) 
			return true; 
		else 
			return false; 
	} 
 
	//##ModelId=424BB6410372 
	static 
	bool loadString ( string & str, LPCTSTR loadStr ) 
	{ 
		if ( loadStr ) 
		{ 
			str = loadStr; 
			return true; 
		} 
		else 
			return false; 
	} 
 
 
	//##ModelId=424BB6410376 
	static 
	void formatString ( string & str, LPCTSTR lpszFormat, ... ) 
	{ 
		va_list args; 
		va_start(args, lpszFormat); 
 
		// buffer for format 
		int   len; 
		TCHAR buffer[512] = _T("\0"); 
 
		// perform format 
		len = _vsntprintf(buffer, 512, lpszFormat, args); 
 
		// if there was no error 
		if ( len >= 0 ) 
			str = buffer; 
 
		va_end(args); 
	} 
 
	//##ModelId=424BB6410385 
	static 
	LPTSTR getBufferString ( string & str, long length ) 
	{ 
		 
		//string::pointer ptr = str.get_allocator().allocate( length, NULL ); 
		//str.assign( ptr ); 
 
		 
		// get allocator for string 
		//string::allocator_type allocator =  
		//str.get_allocator(); 
 
		// setup buffer for copy 
		str.resize( length ); 
 
		// copy data 
		LPTSTR ptr = (LPTSTR) str.c_str(); 
		 
 
 
		return ptr; 
	} 
 
	//##ModelId=424BB6410395 
	static 
	int intVal ( string strVal ) 
	{ 
		int nVal = 0; 
		trimLeft(strVal); 
		for( int index = 0; index < strVal.size(); ++index ) 
			nVal = nVal*10 + strVal[index] - '0'; 
 
		return nVal; 
	} 
 
	//##ModelId=424BB64103A1 
	static 
	int monthFromStr ( const string& str ) 
	{ 
		LPSTR aMonths[] =  
		{ 
			"xxx", "jan", "feb", "mar", "apr", "may", "jun", 
			"jul", "aug", "sep", "oct", "nov", "dec"  
		}; 
 
		for( int nMonth=1; nMonth <= 12; ++nMonth ) 
		{ 
			if ( compareNoCase( str, aMonths[nMonth] ) ) 
				break; 
		} 
 
		return nMonth; 
	} 
 
 
}; 
 
#endif