Exemple #1
0
bool ExtractStyle( LPCTSTR pcszStyle, UINT uLength, Style &style, CStaticString &strValue )
{
	LPTSTR pcszStyleEnd = _tcschr( pcszStyle, _T(':') );
	if( pcszStyleEnd )
	{
		CStaticString strStyleName( pcszStyle, pcszStyleEnd - pcszStyle );
		strStyleName.TrimBoth();
		TRACE( _T("Style Name: %s\n"), (LPCTSTR)CSimpleString( strStyleName, strStyleName.GetLength() ) );
		Style *pStyle = g_mapStyle.Lookup( strStyleName );
		if( pStyle )
		{
			style = *pStyle;
			pcszStyleEnd++;
			strValue.Set( pcszStyleEnd, uLength - ( pcszStyleEnd - pcszStyle ) );
			strValue.TrimBoth();
			TRACE( _T("Style Value: %s\n"), (LPCTSTR)CSimpleString( strValue, strValue.GetLength() ) );
			return true;
		}
	}
	return false;
}
COLORREF GetColourFromString( const CStaticString &strColour, COLORREF crDefault)
//
//	Return the colour from the string passed.
//
//	Does a quick lookup to see if the string is oe of the standard colours
//	if not it simple assumes the colour is in hex.
{
	if( !strColour.GetLength() )
		return crDefault;	// black

	LPCTSTR pcszColour = strColour.GetData();

	if( *pcszColour == '#' )
	{
		pcszColour++;

		//
		//	This bit of code adapted from the mozilla source for the same purpose.
		const int nLength = strColour.GetLength() - 1;
		int nBytesPerColour = min( nLength / 3, 2 );
		int rgb[3] = { 0, 0, 0 };
		for( int nColour = 0; nColour < 3; nColour++ )
		{
			int val = 0;
			for( int nByte = 0; nByte < nBytesPerColour; nByte++ )
			{
				int c = 0;
				if( *pcszColour )
				{
					c = tolower( (TCHAR) *pcszColour );
					if( (c >= '0') && (c <= '9'))
					{
						c = c - '0';
					}
					else if( (c >= 'a') && ( c <= 'f') )
					{
						c = c - 'a' + 10;
					}
					else
					{
						c = 0;
					}
					val = (val << 4) + c;
					pcszColour++;
				}
			}
			rgb[ nColour ] = val;
		}

		return RGB( rgb[0], rgb[1], rgb[2] );
	}

	COLORREF *pcr = g_mapColour.Lookup( strColour );
	if( pcr )
	{
		return *pcr;
	}

	if( *pcszColour  )
	{
		//
		//	It's safe to use _tcstoul because the assumption that the data originally passed to us is zero terminated.
		//	That way even if the colour is *the* last item in the file _tcstoul will properly terminate!
		TCHAR *endptr;
		return _tcstoul( pcszColour, &endptr, 16 );
	}
	return crDefault;
}