static bool ResourceConfig_AddToMap(const char *theName, StringParser &theVal, Map &theMap, int theParseCount, Type* &theType)
{
	std::string aKey;
	theVal.ReadValue(aKey,true);
	if(aKey.empty())
		throw ConfigObjectException("Invalid resource key.");
	
	std::pair<Map::iterator,bool> aRet;
	aRet = theMap.insert(Map::value_type(aKey,Map::referent_type(Type(),theParseCount)));

	int &aParseCount = aRet.first->second.second;

	if(aRet.second)
	{
		theType = &aRet.first->second.first;		
		return true;
	}
	else if(aParseCount==theParseCount)
	{
		char aBuf[512];
		sprintf(aBuf,"%s resource already exists: %s",theName,aKey.c_str());
		throw ConfigObjectException(aBuf);
	}

	return false;
}
FontPtr ResourceConfig::SafeReadFont(StringParser &theVal)
{
	std::string aFace;
	theVal.ReadString(aFace); EnsureComma(theVal);
	int aStyle = SafeReadFontStyle(theVal); EnsureComma(theVal);
	
	int aSize;
	if(!theVal.ReadValue(aSize) || aSize<=0)
		throw ConfigObjectException("Invalid font size.");

	return Window::GetDefaultWindow()->GetFont(FontDescriptor(aFace,aStyle,aSize));
}
bool ResourceConfig::HandleInt(StringParser &theVal)
{
	int *anInt;
	std::string aRefName;
	if(ResourceConfig_AddToMap("Int",theVal,mIntMap,mParseCount,anInt))
	{
		ResourceConfig *aRef = CheckResourceReference(theVal,aRefName);
		if(aRef!=NULL)
			*anInt = aRef->SafeGetInt(aRefName);
		else
		{
			if(!theVal.ReadValue(*anInt))
				throw ConfigObjectException("Invalid integer value.");
		}
	}

	return true;
}
bool ResourceConfig::HandleString(StringParser &theVal)
{
	GUIString *aString;
	std::string aRefName;
	if(ResourceConfig_AddToMap("String",theVal,mStringMap,mParseCount,aString))
	{

		ResourceConfig *aRef = CheckResourceReference(theVal,aRefName);
		if(aRef!=NULL)
			*aString = aRef->SafeGetString(aRefName);
		else
		{
			std::wstring aVal;
			theVal.ReadValue(aVal,false);
			*aString = aVal;
		}
	}

	return true;
}
int ResourceConfig::SafeReadColor(StringParser &theVal)
{
	theVal.SkipWhitespace();
	if(theVal.GetChar()=='(')
	{
		// special color (std or dec)
		std::string aType;
		theVal.IncrementPos();
		theVal.ReadString(aType);
		int aColor = 0;
		if(stricmp(aType.c_str(),"STD")==0) // standard color
		{
			EnsureComma(theVal);
			std::string aName;
			theVal.ReadString(aName); 
			const char *aPtr = aName.c_str();

			if(stricmp(aPtr,"3DDarkShadow")==0) aColor = ColorScheme::GetColorRef(StandardColor_3DDarkShadow); 
			else if(stricmp(aPtr,"3DFace")==0) aColor = ColorScheme::GetColorRef(StandardColor_3DFace); 
			else if(stricmp(aPtr,"3DHilight")==0) aColor = ColorScheme::GetColorRef(StandardColor_3DHilight); 
			else if(stricmp(aPtr,"3DShadow")==0) aColor = ColorScheme::GetColorRef(StandardColor_3DShadow); 
			else if(stricmp(aPtr,"Scrollbar")==0) aColor = ColorScheme::GetColorRef(StandardColor_Scrollbar); 
			else if(stricmp(aPtr,"ButtonText")==0) aColor = ColorScheme::GetColorRef(StandardColor_ButtonText); 
			else if(stricmp(aPtr,"GrayText")==0) aColor = ColorScheme::GetColorRef(StandardColor_GrayText); 
			else if(stricmp(aPtr,"Hilight")==0) aColor = ColorScheme::GetColorRef(StandardColor_Hilight); 
			else if(stricmp(aPtr,"HilightText")==0) aColor = ColorScheme::GetColorRef(StandardColor_HilightText); 
			else if(stricmp(aPtr,"ToolTipBack")==0) aColor = ColorScheme::GetColorRef(StandardColor_ToolTipBack); 
			else if(stricmp(aPtr,"ToolTipText")==0) aColor = ColorScheme::GetColorRef(StandardColor_ToolTipText); 
			else if(stricmp(aPtr,"MenuBack")==0) aColor = ColorScheme::GetColorRef(StandardColor_MenuBack); 
			else if(stricmp(aPtr,"MenuText")==0) aColor = ColorScheme::GetColorRef(StandardColor_MenuText); 
			else if(stricmp(aPtr,"Back")==0) aColor = ColorScheme::GetColorRef(StandardColor_Back); 
			else if(stricmp(aPtr,"Text")==0) aColor = ColorScheme::GetColorRef(StandardColor_Text); 
			else if(stricmp(aPtr,"Link")==0) aColor = ColorScheme::GetColorRef(StandardColor_Link); 
			else if(stricmp(aPtr,"LinkDown")==0) aColor = ColorScheme::GetColorRef(StandardColor_LinkDown); 
			else
				throw ConfigObjectException("Invalid standard color: " + aType);
		}
		else if(stricmp(aType.c_str(),"DEC")==0)
		{
			EnsureComma(theVal); 
			int r,g,b;
			theVal.ReadValue(r); EnsureComma(theVal);
			theVal.ReadValue(g); EnsureComma(theVal);
			theVal.ReadValue(b); 
			aColor = ((r&0xff)<<16) | ((g&0xff)<<8) | (b&0xff);
		}

		EnsureCloseParen(theVal);
		return aColor;
	}

	int aColor = 0;
	for(int i=0; i<6; i++)
	{
		aColor<<=4;
		int aDigit = ResourceConfig_GetHexDigit(theVal.GetChar());
		if(aDigit<0)
			throw ConfigObjectException("Invalid color specification.");

		aColor |= aDigit;
		theVal.IncrementPos();
	}
	
	return aColor;
}