Maelstrom: Moved the XML utility functions to UIArea so they can be used by panels

https://github.com/libsdl-org/Maelstrom/commit/8db39ac3952460a7e46c9fc07e411a57c395170b

From 8db39ac3952460a7e46c9fc07e411a57c395170b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 29 Oct 2011 02:00:22 -0400
Subject: [PATCH] Moved the XML utility functions to UIArea so they can be used
 by panels

---
 UIDialog.cpp            |  10 +--
 screenlib/UIArea.cpp    | 170 ++++++++++++++++++++++++++--------------
 screenlib/UIArea.h      |   6 ++
 screenlib/UIElement.cpp |  61 --------------
 screenlib/UIElement.h   |   6 --
 screenlib/UIPanel.cpp   |  30 +------
 6 files changed, 121 insertions(+), 162 deletions(-)

diff --git a/UIDialog.cpp b/UIDialog.cpp
index 80c8dca2..afafe96a 100644
--- a/UIDialog.cpp
+++ b/UIDialog.cpp
@@ -34,15 +34,7 @@ UIDialog::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
 		return false;
 	}
 
-	attr = node->first_attribute("expand", 0, false);
-	if (attr) {
-		const char *value = attr->value();
-		if (*value == '0' || *value == 'f' || *value == 'F') {
-			m_expand = false;
-		} else {
-			m_expand = true;
-		}
-	}
+	LoadBool(node, "expand", m_expand);
 
 	return true;
 }
diff --git a/screenlib/UIArea.cpp b/screenlib/UIArea.cpp
index 16d9118c..0f1fdefc 100644
--- a/screenlib/UIArea.cpp
+++ b/screenlib/UIArea.cpp
@@ -20,36 +20,10 @@
     slouken@libsdl.org
 */
 
+#include "SDL_FrameBuf.h"
 #include "UIArea.h"
 
 
-static AnchorLocation ParseAnchorLocation(const char *text)
-{
-	AnchorLocation value = TOPLEFT;
-
-	if (strcasecmp(text, "TOPLEFT") == 0) {
-		value = TOPLEFT;
-	} else if (strcasecmp(text, "TOP") == 0) {
-		value = TOP;
-	} else if (strcasecmp(text, "TOPRIGHT") == 0) {
-		value = TOPRIGHT;
-	} else if (strcasecmp(text, "LEFT") == 0) {
-		value = LEFT;
-	} else if (strcasecmp(text, "CENTER") == 0) {
-		value = CENTER;
-	} else if (strcasecmp(text, "RIGHT") == 0) {
-		value = RIGHT;
-	} else if (strcasecmp(text, "BOTTOMLEFT") == 0) {
-		value = BOTTOMLEFT;
-	} else if (strcasecmp(text, "BOTTOM") == 0) {
-		value = BOTTOM;
-	} else if (strcasecmp(text, "BOTTOMRIGHT") == 0) {
-		value = BOTTOMRIGHT;
-	}
-	return value;
-
-}
-
 UIArea::UIArea(FrameBuf *screen, UIArea *anchor, int w, int h) : ErrorBase()
 {
 	m_screen = screen;
@@ -75,26 +49,12 @@ UIArea::Load(rapidxml::xml_node<> *node)
 	rapidxml::xml_attribute<> *attr;
 	SDL_Rect rect = m_rect;
 
-	attr = node->first_attribute("show", 0, false);
-	if (attr) {
-		const char *value = attr->value();
-		if (*value == '0' || *value == 'f' || *value == 'F') {
-			m_shown = false;
-		} else {
-			m_shown = true;
-		}
-	}
+	LoadBool(node, "show", m_shown);
 
 	child = node->first_node("size", 0, false);
 	if (child) {
-		attr = child->first_attribute("w", 0, false);
-		if (attr) {
-			m_rect.w = SDL_atoi(attr->value());
-		}
-		attr = child->first_attribute("h", 0, false);
-		if (attr) {
-			m_rect.h = SDL_atoi(attr->value());
-		}
+		LoadNumber(child, "w", m_rect.w);
+		LoadNumber(child, "h", m_rect.h);
 	}
 
 	child = node->first_node("anchor", 0, false);
@@ -114,23 +74,11 @@ UIArea::Load(rapidxml::xml_node<> *node)
 			return false;
 		}
 
-		attr = child->first_attribute("anchorFrom", 0, false);
-		if (attr) {
-			m_anchor.anchorFrom = ParseAnchorLocation(attr->value());
-		}
-		attr = child->first_attribute("anchorTo", 0, false);
-		if (attr) {
-			m_anchor.anchorTo = ParseAnchorLocation(attr->value());
-		}
+		LoadAnchorLocation(child, "anchorFrom", m_anchor.anchorFrom);
+		LoadAnchorLocation(child, "anchorTo", m_anchor.anchorTo);
 
-		attr = child->first_attribute("x", 0, false);
-		if (attr) {
-			m_anchor.offsetX = SDL_atoi(attr->value());
-		}
-		attr = child->first_attribute("y", 0, false);
-		if (attr) {
-			m_anchor.offsetY = SDL_atoi(attr->value());
-		}
+		LoadNumber(child, "x", m_anchor.offsetX);
+		LoadNumber(child, "y", m_anchor.offsetY);
 	}
 
 	CalculateAnchor(false);
@@ -185,6 +133,108 @@ UIArea::SetHeight(int h)
 	}
 }
 
+bool
+UIArea::LoadBool(rapidxml::xml_node<> *node, const char *name, bool &value)
+{
+	rapidxml::xml_attribute<> *attr;
+
+	attr = node->first_attribute(name, 0, false);
+	if (attr) {
+		const char *text = attr->value();
+		if (*text == '\0' || *text == '0' ||
+		    *text == 'f' || *text == 'F') {
+			value = false;
+		} else {
+			value = true;
+		}
+		return true;
+	}
+	return false;
+}
+
+bool
+UIArea::LoadNumber(rapidxml::xml_node<> *node, const char *name, int &value)
+{
+	rapidxml::xml_attribute<> *attr;
+
+	attr = node->first_attribute(name, 0, false);
+	if (attr) {
+		value = (int)strtol(attr->value(), NULL, 0);
+		return true;
+	}
+	return false;
+}
+
+bool
+UIArea::LoadString(rapidxml::xml_node<> *node, const char *name, char *&value)
+{
+	rapidxml::xml_attribute<> *attr;
+
+	attr = node->first_attribute(name, 0, false);
+	if (attr) {
+		if (value) {
+			SDL_free(value);
+		}
+		value = SDL_strdup(attr->value());
+		return true;
+	}
+	return false;
+}
+
+bool
+UIArea::LoadAnchorLocation(rapidxml::xml_node<> *node, const char *name, AnchorLocation &value)
+{
+	rapidxml::xml_attribute<> *attr;
+
+	attr = node->first_attribute(name, 0, false);
+	if (attr) {
+		const char *text = attr->value();
+
+		if (strcasecmp(text, "TOPLEFT") == 0) {
+			value = TOPLEFT;
+		} else if (strcasecmp(text, "TOP") == 0) {
+			value = TOP;
+		} else if (strcasecmp(text, "TOPRIGHT") == 0) {
+			value = TOPRIGHT;
+		} else if (strcasecmp(text, "LEFT") == 0) {
+			value = LEFT;
+		} else if (strcasecmp(text, "CENTER") == 0) {
+			value = CENTER;
+		} else if (strcasecmp(text, "RIGHT") == 0) {
+			value = RIGHT;
+		} else if (strcasecmp(text, "BOTTOMLEFT") == 0) {
+			value = BOTTOMLEFT;
+		} else if (strcasecmp(text, "BOTTOM") == 0) {
+			value = BOTTOM;
+		} else if (strcasecmp(text, "BOTTOMRIGHT") == 0) {
+			value = BOTTOMRIGHT;
+		} else {
+			/* Failed to parse */
+			return false;
+		}
+		return true;
+	}
+	return false;
+}
+
+bool
+UIArea::LoadColor(rapidxml::xml_node<> *node, const char *name, Uint32 &value)
+{
+	rapidxml::xml_node<> *child;
+
+	child = node->first_node("color", 0, false);
+	if (child) {
+		rapidxml::xml_attribute<> *attr;
+		int r = 0xFF, g = 0xFF, b = 0xFF;
+
+		LoadNumber(child, "r", r);
+		LoadNumber(child, "g", g);
+		LoadNumber(child, "b", b);
+		value = m_screen->MapRGB(r, g, b);
+		return true;
+	}
+	return false;
+}
 void
 UIArea::GetAnchorLocation(AnchorLocation spot, int *x, int *y) const
 {
diff --git a/screenlib/UIArea.h b/screenlib/UIArea.h
index c0c8ec14..09489ef1 100644
--- a/screenlib/UIArea.h
+++ b/screenlib/UIArea.h
@@ -111,6 +111,12 @@ class UIArea : public ErrorBase
 	}
 
 protected:
+	bool LoadBool(rapidxml::xml_node<> *node, const char *name, bool &value);
+	bool LoadNumber(rapidxml::xml_node<> *node, const char *name, int &value);
+	bool LoadString(rapidxml::xml_node<> *node, const char *name, char *&value);
+	bool LoadAnchorLocation(rapidxml::xml_node<> *node, const char *name, AnchorLocation &value);
+	bool LoadColor(rapidxml::xml_node<> *node, const char *name, Uint32 &value);
+
 	void GetAnchorLocation(AnchorLocation spot, int *x, int *y) const;
 	void CalculateAnchor(bool triggerRectChanged = true);
 	virtual void OnRectChanged();
diff --git a/screenlib/UIElement.cpp b/screenlib/UIElement.cpp
index be4f4089..b2396dc7 100644
--- a/screenlib/UIElement.cpp
+++ b/screenlib/UIElement.cpp
@@ -20,8 +20,6 @@
     slouken@libsdl.org
 */
 
-#include "SDL_FrameBuf.h"
-#include "UIPanel.h"
 #include "UIElement.h"
 
 UIElementType UIElement::s_elementType;
@@ -45,62 +43,3 @@ UIElement::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
 
 	return true;
 }
-
-bool
-UIElement::LoadBool(rapidxml::xml_node<> *node, const char *name, bool &value)
-{
-	rapidxml::xml_attribute<> *attr;
-
-	attr = node->first_attribute(name, 0, false);
-	if (attr) {
-		const char *text = attr->value();
-		if (*text == '\0' || *text == '0' ||
-		    *text == 'f' || *text == 'F') {
-			value = false;
-		} else {
-			value = true;
-		}
-	}
-}
-
-bool
-UIElement::LoadNumber(rapidxml::xml_node<> *node, const char *name, int &value)
-{
-	rapidxml::xml_attribute<> *attr;
-
-	attr = node->first_attribute(name, 0, false);
-	if (attr) {
-		value = (int)strtol(attr->value(), NULL, 0);
-	}
-}
-
-bool
-UIElement::LoadString(rapidxml::xml_node<> *node, const char *name, char *&value)
-{
-	rapidxml::xml_attribute<> *attr;
-
-	attr = node->first_attribute(name, 0, false);
-	if (attr) {
-		if (value) {
-			SDL_free(value);
-		}
-		value = SDL_strdup(attr->value());
-	}
-}
-
-bool
-UIElement::LoadColor(rapidxml::xml_node<> *node, const char *name, Uint32 &value)
-{
-	rapidxml::xml_node<> *child;
-
-	child = node->first_node("color", 0, false);
-	if (child) {
-		rapidxml::xml_attribute<> *attr;
-		int r = 0xFF, g = 0xFF, b = 0xFF;
-
-		LoadNumber(child, "r", r);
-		LoadNumber(child, "g", g);
-		LoadNumber(child, "b", b);
-		value = m_screen->MapRGB(r, g, b);
-	}
-}
diff --git a/screenlib/UIElement.h b/screenlib/UIElement.h
index 5192ecdd..8a6d2b49 100644
--- a/screenlib/UIElement.h
+++ b/screenlib/UIElement.h
@@ -39,12 +39,6 @@ class UIElement : public UIBaseElement
 
 	virtual bool Load(rapidxml::xml_node<> *node, const UITemplates *templates);
 
-protected:
-	bool LoadBool(rapidxml::xml_node<> *node, const char *name, bool &value);
-	bool LoadNumber(rapidxml::xml_node<> *node, const char *name, int &value);
-	bool LoadString(rapidxml::xml_node<> *node, const char *name, char *&value);
-	bool LoadColor(rapidxml::xml_node<> *node, const char *name, Uint32 &value);
-
 protected:
 	static UIElementType s_elementType;
 
diff --git a/screenlib/UIPanel.cpp b/screenlib/UIPanel.cpp
index 800d000a..87e3502f 100644
--- a/screenlib/UIPanel.cpp
+++ b/screenlib/UIPanel.cpp
@@ -57,32 +57,10 @@ UIPanel::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
 		return false;
 	}
 
-	attr = node->first_attribute("fullscreen", 0, false);
-	if (attr) {
-		const char *value = attr->value();
-		if (*value == '0' || *value == 'f' || *value == 'F') {
-			m_fullscreen = false;
-		} else {
-			m_fullscreen = true;
-		}
-	}
-	attr = node->first_attribute("cursor", 0, false);
-	if (attr) {
-		const char *value = attr->value();
-		if (*value == '0' || *value == 'f' || *value == 'F') {
-			m_cursorVisible = false;
-		} else {
-			m_cursorVisible = true;
-		}
-	}
-	attr = node->first_attribute("enterSound", 0, false);
-	if (attr) {
-		m_enterSound = atoi(attr->value());
-	}
-	attr = node->first_attribute("leaveSound", 0, false);
-	if (attr) {
-		m_leaveSound = atoi(attr->value());
-	}
+	LoadBool(node, "fullscreen", m_fullscreen);
+	LoadBool(node, "cursor", m_cursorVisible);
+	LoadNumber(node, "enterSound", m_enterSound);
+	LoadNumber(node, "leaveSound", m_leaveSound);
 
 	return true;
 }