Example #1
0
	//	Load colours from the XML file.
	//
	void Colour_Container::load_xml(void)
	{
		char const *error_prefix = __PRETTY_FUNCTION__;
		std::string file_name = "named_colours.xml";

		if (!el_file_exists_anywhere(file_name.c_str()))
			return;

		xmlDocPtr doc;
		xmlNodePtr cur;

		if ((doc = xmlReadFile(file_name.c_str(), NULL, 0)) == NULL)
		{
			LOG_ERROR("%s : Can't open file [%s]\n", error_prefix, file_name.c_str() );
			return;
		}

		if ((cur = xmlDocGetRootElement (doc)) == NULL)
		{
			LOG_ERROR("%s : Empty xml document\n", error_prefix );
			xmlFreeDoc(doc);
			return;
		}

		if (xmlStrcasecmp (cur->name, (const xmlChar *) "named_colours"))
		{
			LOG_ERROR("%s : no named_colours element\n", error_prefix );
			xmlFreeDoc(doc);
			return;
		}

		for (cur = cur->xmlChildrenNode; cur; cur = cur->next)
		{
			if (!xmlStrcasecmp(cur->name, (const xmlChar *)"colour"))
			{
				const int NUM_STR = 5;
				char *read_strings[NUM_STR] = {NULL, NULL, NULL, NULL, NULL};
				const char *names[NUM_STR] = {"name", "r", "g", "b", "a"};
				for (int i=0; i<NUM_STR; i++)
				{
					char *tmp = (char*)xmlGetProp(cur, (xmlChar *)names[i]);
					if (!tmp)
						continue;
					MY_XMLSTRCPY(&read_strings[i], tmp);
					xmlFree(tmp);
				}
				if (read_strings[0])
				{
					GLfloat col_vals[NUM_STR-1] = { -1.0f, -1.0f, -1.0f, 1.0f};
					for (int i=1; i<NUM_STR; i++)
					{
						if (read_strings[i])
						{
							GLfloat tmpcol = -1.0f;
							std::stringstream ss(read_strings[i]);
							if( (ss >> tmpcol).fail() )
								continue;
							col_vals[i-1] = tmpcol;
						}
					}
					bool valid = true;
					for (int i=0; i<NUM_STR-1; i++)
						if (col_vals[i] < 0)
							valid = false;
					if (valid && (NUM_STR == 5))
						add(read_strings[0], Colour_Tuple(col_vals[0],col_vals[1],col_vals[2],col_vals[3]));
				}
				for (int i=0; i<NUM_STR; i++)
					if (read_strings[i])
						free(read_strings[i]);
			}
		}
		xmlFreeDoc(doc);
	}
Example #2
0
	//	Read the icon xml file, constructing icon objects as we go.
	//
	bool Container::read_xml(icon_window_mode icon_mode)
	{
		char const *error_prefix = __PRETTY_FUNCTION__;
		std::string file_name;
		if (icon_mode == NEW_CHARACTER_ICONS)
			file_name = "new_character_icon_window.xml";
		else if (icon_mode == MAIN_WINDOW_ICONS)
			file_name = "main_icon_window.xml";
		else
		{
			LOG_ERROR("%s : invalid icon mode\n", error_prefix );
			return false;
		}

		// shame but xmlFileMatch gives an additional error message
		if (!el_file_exists_anywhere(file_name.c_str()))
			return false;

		xmlDocPtr doc;
		xmlNodePtr cur;

		if ((doc = xmlReadFile(file_name.c_str(), NULL, 0)) == NULL)
		{
			LOG_ERROR("%s : Can't open file [%s]\n", error_prefix, file_name.c_str() );
			return false;
		}

		if ((cur = xmlDocGetRootElement (doc)) == NULL)
		{
			LOG_ERROR("%s : Empty xml document\n", error_prefix );
			xmlFreeDoc(doc);
			return false;
		}

		if (xmlStrcasecmp (cur->name, (const xmlChar *) "icon_window"))
		{
			LOG_ERROR("%s : Not icon_window file\n", error_prefix );
			xmlFreeDoc(doc);
			return false;
		}

		for (cur = cur->xmlChildrenNode; cur; cur = cur->next)
		{
			if (!xmlStrcasecmp(cur->name, (const xmlChar *)"icon"))
			{
				Virtual_Icon *new_icon = icon_xml_factory(cur);
				if (new_icon)
					icon_list.push_back(new_icon);
			}
			else if (!xmlStrcasecmp(cur->name, (const xmlChar *)"multi_icon"))
			{
				std::string control_name;
				get_xml_field_string(control_name, "control_name", cur);
				if (control_name.empty())
					LOG_ERROR("%s : invalid multi icon control_name\n", error_prefix );
				else
				{
					std::vector<Virtual_Icon *> multi_icons;
					for (xmlNodePtr multi_cur = cur->xmlChildrenNode; multi_cur; multi_cur = multi_cur->next)
					{
						if (!xmlStrcasecmp(multi_cur->name, (const xmlChar *)"icon"))
						{
							Virtual_Icon *new_icon = icon_xml_factory(multi_cur);
							if (new_icon)
								multi_icons.push_back(new_icon);
						}
					}
					icon_list.push_back(new Multi_Icon(control_name.c_str(), multi_icons));
				}
			}
			else if (!xmlStrcasecmp(cur->name, (const xmlChar *)"image_settings"))
				get_xml_field_int(&display_icon_size, "display_size", cur);
		}
		xmlFreeDoc(doc);
		return true;
	}