Click here to Skip to main content
15,896,278 members
Articles / Desktop Programming / MFC

STL WebServer

Rate me:
Please Sign up or sign in to vote.
4.89/5 (5 votes)
8 May 2000 90.3K   2.5K   47  
A set of classes written in STL that implement a web server
#if !defined(StringUtil_H)
#define StringUtil_H


#include <string>
#include <algorithm>
#include <functional>
using namespace std;


/////////////////////////////////////////////////////////////////////////////////
// StringUtil
//
// Purpose:		provides common utility methods for strings


class StringUtil
{
public:
	StringUtil ()
	{}


	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<TCHAR>(),' ') );

		// 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() );
		*/
	}

	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<TCHAR>(),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() );
		*/
	}

	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<TCHAR>(),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() );
		*/
	}

	static
	void trimLeft ( string & str, TCHAR chTarget )
	{
		// setup buffer
		TCHAR buffer[2];
		buffer[0] = chTarget;
		buffer[1] = '\0';

		trimLeft( str, buffer );
	}

	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) ;
	}


	static
	void trimLeft ( string & str )
	{
		// trim spaces
		trimLeft( str, " " );
	}



	static
	int replace ( string & str, TCHAR chOld, TCHAR chNew )
	{
		// replace if found
		str.replace( str.begin(), str.end(), chOld, chNew );

		return 0;
	}

	static
	int replace ( string & str, LPCTSTR lpszOld, LPCTSTR lpszNew )
	{
		// replace if found
		str.replace( str.begin(), str.end(), lpszOld, lpszNew );

		return 0;
	}


	static
	string left ( const string & str, long pos )
	{
		return str.substr( 0, pos );
	}

	static
	string mid ( const string & str, long pos, long len = -1 )
	{
		if ( len == -1 )
			len = str.size() - 1;

		return str.substr( pos, len );
	}

	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 );
		}

	}


	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 );
		}

	}

	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;
	}


	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;
	}

	static
	bool loadString ( string & str, LPCTSTR loadStr )
	{
		if ( loadStr )
		{
			str = loadStr;
			return true;
		}
		else
			return false;
	}


	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);
	}

	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;
	}

	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;
	}

	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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions