Maelstrom: Refactored XML loading code out into utils

https://github.com/libsdl-org/Maelstrom/commit/90245ff48b1f15c84da818884cc84e94d280ed0a

From 90245ff48b1f15c84da818884cc84e94d280ed0a Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 27 Jul 2012 16:24:28 -0700
Subject: [PATCH] Refactored XML loading code out into utils

---
 screenlib/UIManager.cpp | 34 +++--------------------
 utils/Makefile.am       |  2 ++
 utils/Makefile.in       |  9 ++++---
 utils/loadxml.cpp       | 60 +++++++++++++++++++++++++++++++++++++++++
 utils/loadxml.h         | 28 +++++++++++++++++++
 5 files changed, 100 insertions(+), 33 deletions(-)
 create mode 100644 utils/loadxml.cpp
 create mode 100644 utils/loadxml.h

diff --git a/screenlib/UIManager.cpp b/screenlib/UIManager.cpp
index 0e6651d2..84b98f44 100644
--- a/screenlib/UIManager.cpp
+++ b/screenlib/UIManager.cpp
@@ -19,7 +19,7 @@
   3. This notice may not be removed or altered from any source distribution.
 */
 
-#include "physfs.h"
+#include "../utils/loadxml.h"
 
 #include "SDL_FrameBuf.h"
 #include "UIManager.h"
@@ -48,7 +48,7 @@ void
 UIManager::SetLoadPath(const char *path)
 {
 	delete[] m_loadPath;
-    size_t size = SDL_strlen(path)+1;
+	size_t size = SDL_strlen(path)+1;
 	m_loadPath = new char[size];
 	SDL_strlcpy(m_loadPath, path, size);
 }
@@ -70,37 +70,11 @@ UIManager::LoadPanel(const char *name)
 	panel = GetPanel(name, false);
 	if (!panel) {
 		char file[1024];
-		PHYSFS_File *fp;
-		PHYSFS_sint64 size;
 		char *buffer;
+		rapidxml::xml_document<> doc;
 
 		SDL_snprintf(file, sizeof(file), "%s/%s.xml", m_loadPath, name);
-		fp = PHYSFS_openRead(file);
-		if (!fp) {
-			fprintf(stderr, "Warning: Couldn't open %s: %s\n",
-						file, PHYSFS_getLastError());
-			return NULL;
-		}
-
-		size = PHYSFS_fileLength(fp);
-		buffer = new char[size+1];
-		if (PHYSFS_readBytes(fp, buffer, size) != size) {
-			fprintf(stderr, "Warning: Couldn't read from %s: %s\n",
-						file, PHYSFS_getLastError());
-			PHYSFS_close(fp);
-			delete[] buffer;
-			return NULL;
-		}
-		buffer[size] = '\0';
-		PHYSFS_close(fp);
-
-		rapidxml::xml_document<> doc;
-		try {
-			doc.parse<0>(buffer);
-		} catch (rapidxml::parse_error e) {
-			fprintf(stderr, "Warning: Couldn't parse %s: error: %s\n",
-						file, e.what());
-			delete[] buffer;
+		if (!LoadXML(file, buffer, doc)) {
 			return NULL;
 		}
 
diff --git a/utils/Makefile.am b/utils/Makefile.am
index 8ba49bc7..49eae800 100644
--- a/utils/Makefile.am
+++ b/utils/Makefile.am
@@ -6,6 +6,8 @@ libutils_a_SOURCES =	\
 	array.h		\
 	hashtable.c	\
 	hashtable.h	\
+	loadxml.cpp	\
+	loadxml.h	\
 	physfsrwops.c	\
 	physfsrwops.h	\
 	prefs.cpp	\
diff --git a/utils/Makefile.in b/utils/Makefile.in
index 388ff4b7..a05e2455 100644
--- a/utils/Makefile.in
+++ b/utils/Makefile.in
@@ -41,15 +41,15 @@ am__aclocal_m4_deps = $(top_srcdir)/acinclude/check_zlib.m4 \
 	$(top_srcdir)/configure.in
 am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
 	$(ACLOCAL_M4)
-mkinstalldirs = $(SHELL) $(top_srcdir)/build-scripts/mkinstalldirs
+mkinstalldirs = $(install_sh) -d
 CONFIG_CLEAN_FILES =
 LIBRARIES = $(noinst_LIBRARIES)
 AR = ar
 ARFLAGS = cru
 libutils_a_AR = $(AR) $(ARFLAGS)
 libutils_a_LIBADD =
-am_libutils_a_OBJECTS = hashtable.$(OBJEXT) physfsrwops.$(OBJEXT) \
-	prefs.$(OBJEXT)
+am_libutils_a_OBJECTS = hashtable.$(OBJEXT) loadxml.$(OBJEXT) \
+	physfsrwops.$(OBJEXT) prefs.$(OBJEXT)
 libutils_a_OBJECTS = $(am_libutils_a_OBJECTS)
 DEFAULT_INCLUDES = -I.@am__isrc@
 depcomp = $(SHELL) $(top_srcdir)/build-scripts/depcomp
@@ -183,6 +183,8 @@ libutils_a_SOURCES = \
 	array.h		\
 	hashtable.c	\
 	hashtable.h	\
+	loadxml.cpp	\
+	loadxml.h	\
 	physfsrwops.c	\
 	physfsrwops.h	\
 	prefs.cpp	\
@@ -238,6 +240,7 @@ distclean-compile:
 	-rm -f *.tab.c
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hashtable.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/loadxml.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/physfsrwops.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/prefs.Po@am__quote@
 
diff --git a/utils/loadxml.cpp b/utils/loadxml.cpp
new file mode 100644
index 00000000..3bb49796
--- /dev/null
+++ b/utils/loadxml.cpp
@@ -0,0 +1,60 @@
+/*
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+/* XML resource loading */
+
+#include <stdio.h>
+#include "physfs.h"
+#include "loadxml.h"
+
+bool
+LoadXML(const char *file, char *&buffer, rapidxml::xml_document<> &doc)
+{
+	PHYSFS_File *fp;
+	PHYSFS_sint64 size;
+
+	fp = PHYSFS_openRead(file);
+	if (!fp) {
+		fprintf(stderr, "Warning: Couldn't open %s: %s\n",
+					file, PHYSFS_getLastError());
+		return false;
+	}
+
+	size = PHYSFS_fileLength(fp);
+	buffer = new char[size+1];
+	if (PHYSFS_readBytes(fp, buffer, size) != size) {
+		fprintf(stderr, "Warning: Couldn't read from %s: %s\n",
+					file, PHYSFS_getLastError());
+		PHYSFS_close(fp);
+		delete[] buffer;
+		return false;
+	}
+	buffer[size] = '\0';
+	PHYSFS_close(fp);
+
+	try {
+		doc.parse<0>(buffer);
+	} catch (rapidxml::parse_error e) {
+		fprintf(stderr, "Warning: Couldn't parse %s: error: %s\n",
+					file, e.what());
+		delete[] buffer;
+		return false;
+	}
+
+	return true;
+}
diff --git a/utils/loadxml.h b/utils/loadxml.h
new file mode 100644
index 00000000..f459fdd9
--- /dev/null
+++ b/utils/loadxml.h
@@ -0,0 +1,28 @@
+/*
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef _loadxml_h
+#define _loadxml_h
+
+/* XML resource loading */
+
+#include "rapidxml.h"
+
+extern bool LoadXML(const char *file, char *&buffer, rapidxml::xml_document<> &doc);
+
+#endif /* _loadxml_h */