[xiph-commits] r8699 - in trunk/oggdsf/src: lib/codecs/cmml/libCMMLParse tests/testCMMLParser

ozone at motherfish-iii.xiph.org ozone at motherfish-iii.xiph.org
Sat Jan 8 22:08:25 PST 2005


Author: ozone
Date: 2005-01-08 22:08:24 -0800 (Sat, 08 Jan 2005)
New Revision: 8699

Modified:
   trunk/oggdsf/src/lib/codecs/cmml/libCMMLParse/CMMLParser.cpp
   trunk/oggdsf/src/lib/codecs/cmml/libCMMLParse/CMMLParser.h
   trunk/oggdsf/src/tests/testCMMLParser/testCMMLParser.cpp
Log:
oggdsf:
 * Added "correctly failed" output to testCMMLParser
 * Rewrote public entry points to libCMMLParse/CMMLParser to be less sucky
 * Fixed memory leaks in libCMMLParse
 

Modified: trunk/oggdsf/src/lib/codecs/cmml/libCMMLParse/CMMLParser.cpp
===================================================================
--- trunk/oggdsf/src/lib/codecs/cmml/libCMMLParse/CMMLParser.cpp	2005-01-09 05:35:28 UTC (rev 8698)
+++ trunk/oggdsf/src/lib/codecs/cmml/libCMMLParse/CMMLParser.cpp	2005-01-09 06:08:24 UTC (rev 8699)
@@ -40,94 +40,13 @@
 #include <libilliCore/StringHelper.h>
 
 #include <fstream>
-#include <iostream>
 
+
 using namespace std;
 
 // TODO: Properly parse preamble
 // TODO: i18n?
-// TODO: Macrofy non-macrofied bits.  Woo macros!
-// TODO: Do actual memory management :}
 
-// Macros are evil, macros are evil, can't sleep, clown'll eat me ...
-
-#define XTAG_PARSE_INTO(tagParser, parseMethod, TagType, parentTagSetter, parentTag) \
-	{ \
-		TagType *locTag = new TagType; \
-		if (!parseMethod(tagParser, locTag)) { \
-			return false; \
-		} \
-		parentTag->parentTagSetter(locTag); \
-		/* delete locTag; */ \
-	};
-
-#define XTAG_SET_ATTRIBUTE(tagParser, attributeName, tag, attributeSetter) \
-	{ \
-		const char *locAttributeCString = xtag_get_attribute(tagParser, attributeName); \
-		if (locAttributeCString) { \
-			tag->attributeSetter(StringHelper::toWStr(locAttributeCString)); \
-			/* free((void *) locAttributeCString); */ \
-		} \
-	};
-
-#define XTAG_REQUIRED_ATTRIBUTE(tagParser, attributeName, tag) \
-	{ \
-		const char *locAttributeCString = xtag_get_attribute(tagParser, attributeName); \
-		if (!locAttributeCString) { \
-			cout << "Failed 3" << endl; \
-			return false; \
-		} else { \
-			/* free((void *) locAttributeCString); */ \
-		} \
-	};
-
-#define XTAG_PARSE_CHILD(parentParser, tagName, tagParser, tagType, setterMethod, parentTag) \
-	{ \
-		XTag *locParser = NULL; \
-		locParser = xtag_first_child(parentParser, tagName); \
-		if (locParser) { \
-			XTAG_PARSE_INTO(locParser, tagParser, tagType, setterMethod, parentTag); \
-		} \
-	};
-
-#define XTAG_EXACTLY_ONE_CHILD(parentParser, tagName) \
-	{ \
-		XTag *locParser = xtag_first_child(parentParser, tagName); \
-		if (locParser != NULL) { \
-			/* Found at least one child */ \
-			locParser = xtag_next_child(parentParser, tagName); \
-			if (locParser) { \
-				/* Danger will robinson, found more than one child */ \
-				cout << "Failed 1" << endl; \
-				return false; \
-			} \
-		} else { \
-			/* Found no child */ \
-			cout << "Failed 2" << endl; \
-			return false; \
-		} \
-	};
-
-#define XTAG_PARSE_LIST(TagType, listTagName, tagParser, parentParser, parentTag, parentGetListMethod) \
-	{ \
-		XTag *locTagListParser = NULL; \
-		for (	locTagListParser = xtag_first_child(parentParser, listTagName); \
-				locTagListParser != NULL; \
-				locTagListParser = xtag_next_child(parentParser, listTagName)) { \
-			XTAG_PARSE_INTO(locTagListParser, tagParser, TagType, addTag, parentTag->parentGetListMethod()); \
-		} \
-	};
-
-#define XTAG_SET_CDATA(tagParser, tag) \
-	{ \
-		const char *locCData = xtag_get_pcdata(tagParser); \
-		if (locCData) { \
-			tag->setText(StringHelper::toWStr(locCData)); \
-			/* free((void *) locCData); */ \
-		} \
-	};
-
-
 CMMLParser::CMMLParser(void)
 {
 }
@@ -157,9 +76,9 @@
 	// Look ma, the world's most portable file-size-getting-function-thing
 	locFile.seekg(0, ios::end);
 	size_t locCMMLFileSize = locFile.tellg();
+	locFile.clear();
 
 	// Read the entirety of the file into the buffer
-	locFile.clear();
 	locFile.seekg(0);
 
 	unsigned short BUFFER_SIZE = 8192;
@@ -188,8 +107,7 @@
 	}
 
 	// Clean up
-	//delete locRootTag;
-	//delete [] locBuffer;
+	delete [] locBuffer;
 
 	return locReturnValue;
 }
@@ -203,80 +121,25 @@
 
 	// Sanity check against a NULL output pointer
 	if (!outCMMLRoot) {
-		cout << "outCMMLRoot is NULL" << endl;
 		return false;
 	}
 
 	// Narrow the text given us, so we can pass it to XTag
 	string locCMMLRootText = StringHelper::toNarrowStr(inCMMLRootText);
 
-	// Look for a <cmml> tag
+	// Look for a tag, any tag
 	XTag *locRootParser = NULL;
 	locRootParser = xtag_new_parse(locCMMLRootText.c_str(), locCMMLRootText.size());
-	if (!locRootParser) {
-		// Couldn't find a tag at all
-		locReturnValue = false;
-		goto cleanup;
+	if (locRootParser) {
+		// Is it a <cmml> tag?
+		if (strcmp(xtag_get_name(locRootParser), "cmml") == 0) {
+			// Found a <cmml> tag
+			locReturnValue = parseRootTag(locRootParser, outCMMLRoot);
+		}
 	}
 
-	// Ensure we found a <cmml> tag
-
-	if (strcmp(xtag_get_name(locRootParser), "cmml") != 0) {
-		// This ain't no CMML tag!
-		locReturnValue = false;
-		goto cleanup;
-	}
-
-	XTag *locCMMLParser = locRootParser;
-
-	// Look for a <stream> tag
-	
-	XTag *locStreamParser = NULL;
-	locStreamParser = xtag_first_child(locCMMLParser, "stream");
-	if (locStreamParser) {
-		// We found a stream tag, so go parse it
-		XTAG_PARSE_INTO(locStreamParser, parseStreamTag, C_StreamTag, setStream, outCMMLRoot);
-	}
-
-	// Look for a <head> tag
-	
-	XTag *locHeadParser = NULL;
-	locHeadParser = xtag_first_child(locCMMLParser, "head");
-	if (locHeadParser) {
-		// We found a head tag, so go parse it
-		XTAG_PARSE_INTO(locHeadParser, parseHeadTag, C_HeadTag, setHead, outCMMLRoot);
-	} else {
-		// I have no head!  I am invalid!
-		locReturnValue = false;
-		goto cleanup;
-	}
-
-	// Look for multiple <clip> tags, and shove them into a clip list
-	
-	C_ClipTagList *locClipList = new C_ClipTagList;
-	XTag *locClipParser = NULL;
-	bool locFoundAtLeastOneClip = false;
-	for (	locClipParser = xtag_first_child(locCMMLParser, "clip");
-			locClipParser != NULL;
-			locClipParser = xtag_next_child(locCMMLParser, "clip")) {
-		locFoundAtLeastOneClip = true;
-		XTAG_PARSE_INTO(locClipParser, parseClipTag, C_ClipTag, addTag, locClipList);
-	}
-
-	if (locFoundAtLeastOneClip) {
-		outCMMLRoot->setClipList(locClipList);
-	}
-
-	//delete locClipList;
-
-	// If we got to here, we got a successful parse
-
-	locReturnValue = true;
-
-cleanup:
-
 	if (locRootParser) {
-		//xtag_free(locRootParser);
+		xtag_free(locRootParser);
 	}
 
 	return locReturnValue;
@@ -308,7 +171,9 @@
 		}
 	}
 
-	//xtag_free(locClipParser);
+	if (locClipParser) {
+		xtag_free(locClipParser);
+	}
 
 	return locReturnValue;
 }
@@ -337,13 +202,91 @@
 		}
 	}
 
-	//xtag_free(locTextParser);
+	if (locHeadParser) {
+		xtag_free(locHeadParser);
+	}
 
 	return locReturnValue;
 }
 
 
+// Macros are evil, macros are evil, can't sleep, clown'll eat me ...
 
+#define XTAG_PARSE_INTO(tagParser, parseMethod, TagType, parentTagSetter, parentTag) \
+	{ \
+		TagType *locTag = new TagType; \
+		if (!parseMethod(tagParser, locTag)) { \
+			return false; \
+		} \
+		parentTag->parentTagSetter(locTag); \
+	};
+
+#define XTAG_SET_ATTRIBUTE(tagParser, attributeName, tag, attributeSetter) \
+	{ \
+		const char *locAttributeCString = xtag_get_attribute(tagParser, attributeName); \
+		if (locAttributeCString) { \
+			tag->attributeSetter(StringHelper::toWStr(locAttributeCString)); \
+			/* free((void *) locAttributeCString); */ \
+		} \
+	};
+
+#define XTAG_REQUIRED_ATTRIBUTE(tagParser, attributeName, tag) \
+	{ \
+		const char *locAttributeCString = xtag_get_attribute(tagParser, attributeName); \
+		if (!locAttributeCString) { \
+			return false; \
+		} else { \
+			/* free((void *) locAttributeCString); */ \
+		} \
+	};
+
+#define XTAG_PARSE_CHILD(parentParser, tagName, tagParser, tagType, setterMethod, parentTag) \
+	{ \
+		XTag *locParser = NULL; \
+		locParser = xtag_first_child(parentParser, tagName); \
+		if (locParser) { \
+			XTAG_PARSE_INTO(locParser, tagParser, tagType, setterMethod, parentTag); \
+		} \
+	};
+
+#define XTAG_EXACTLY_ONE_CHILD(parentParser, tagName) \
+	{ \
+		XTag *locParser = xtag_first_child(parentParser, tagName); \
+		if (locParser != NULL) { \
+			/* Found at least one child */ \
+			locParser = xtag_next_child(parentParser, tagName); \
+			if (locParser) { \
+				/* Danger will robinson, found more than one child */ \
+				return false; \
+			} \
+		} else { \
+			/* Found no child */ \
+			return false; \
+		} \
+	};
+
+#define XTAG_PARSE_LIST(TagType, listTagName, tagParser, parentParser, parentTag, parentGetListMethod) \
+	{ \
+		XTag *locTagListParser = NULL; \
+		for (	locTagListParser = xtag_first_child(parentParser, listTagName); \
+				locTagListParser != NULL; \
+				locTagListParser = xtag_next_child(parentParser, listTagName)) { \
+			XTAG_PARSE_INTO(locTagListParser, tagParser, TagType, addTag, parentTag->parentGetListMethod()); \
+		} \
+	};
+
+#define XTAG_SET_CDATA(tagParser, tag) \
+	{ \
+		const char *locCData = xtag_get_pcdata(tagParser); \
+		if (locCData) { \
+			tag->setText(StringHelper::toWStr(locCData)); \
+			/* free((void *) locCData); */ \
+		} \
+	};
+
+
+// Look ma, it's declarative programming!
+
 bool CMMLParser::parseStreamTag(XTag* inStreamParser, C_StreamTag* outStream)
 {
 	XTAG_SET_ATTRIBUTE(inStreamParser, "id", outStream, setId);
@@ -357,6 +300,23 @@
 }
 
 
+bool CMMLParser::parseRootTag(XTag* inCMMLRootParser, C_CMMLRootTag* outCMMLRoot)
+{
+	XTAG_SET_ATTRIBUTE(inCMMLRootParser, "id", outCMMLRoot, setId);
+
+	XTAG_EXACTLY_ONE_CHILD(inCMMLRootParser, "head");
+	XTAG_PARSE_CHILD(inCMMLRootParser, "head", parseHeadTag, C_HeadTag, setHead, outCMMLRoot);
+	XTAG_PARSE_CHILD(inCMMLRootParser, "stream", parseStreamTag, C_StreamTag, setStream, outCMMLRoot);
+
+	XTAG_PARSE_LIST(C_ClipTag, "clip", parseClipTag, inCMMLRootParser, outCMMLRoot, clipList);
+
+	// i18n
+	XTAG_SET_ATTRIBUTE(inCMMLRootParser, "lang", outCMMLRoot, setLang);
+	XTAG_SET_ATTRIBUTE(inCMMLRootParser, "dir", outCMMLRoot, setDirn);
+
+	return true;
+}
+
 bool CMMLParser::parseHeadTag(XTag* inHeadParser, C_HeadTag* outHead)
 {
 	XTAG_SET_ATTRIBUTE(inHeadParser, "id", outHead, setId);

Modified: trunk/oggdsf/src/lib/codecs/cmml/libCMMLParse/CMMLParser.h
===================================================================
--- trunk/oggdsf/src/lib/codecs/cmml/libCMMLParse/CMMLParser.h	2005-01-09 05:35:28 UTC (rev 8698)
+++ trunk/oggdsf/src/lib/codecs/cmml/libCMMLParse/CMMLParser.h	2005-01-09 06:08:24 UTC (rev 8699)
@@ -50,16 +50,13 @@
 	CMMLParser(void);
 	~CMMLParser(void);
 
-	//C_CMMLTag* genericParseTag(string inCMMLText);
-	//bool parseCMMLDoc(string inCMMLDocText, C_CMMLDoc* outDoc);
 	bool parseClipTag(wstring inClipText, C_ClipTag* outClip);
 	bool parseHeadTag(wstring inHeadText, C_HeadTag* outHead);
 	bool parseCMMLRootTag(wstring inCMMLRootText, C_CMMLRootTag* outCMMLRoot);
 	bool parseDocFromFile(wstring inFilename, C_CMMLDoc* outCMMLDoc);
 
-
-
 protected:
+	bool parseRootTag(XTag* inCMMLRootParser, C_CMMLRootTag* outCMMLRoot);
 	bool parseStreamTag(XTag* inStreamParser, C_StreamTag* outStream);
 	bool parseHeadTag(XTag* inHeadParser, C_HeadTag* outHead);
 	bool parseClipTag(XTag* inClipParser, C_ClipTag* outClip);

Modified: trunk/oggdsf/src/tests/testCMMLParser/testCMMLParser.cpp
===================================================================
--- trunk/oggdsf/src/tests/testCMMLParser/testCMMLParser.cpp	2005-01-09 05:35:28 UTC (rev 8698)
+++ trunk/oggdsf/src/tests/testCMMLParser/testCMMLParser.cpp	2005-01-09 06:08:24 UTC (rev 8699)
@@ -3,7 +3,7 @@
 
 #include "stdafx.h"
 
-#include "libCMMLTags/libCMMLTags.h"
+#include <libCMMLTags/libCMMLTags.h>
 
 #if 1
 #include <libCMMLParse/libCMMLParse.h>
@@ -29,11 +29,12 @@
 
 	if (locWasOK && inShouldPass) {
 		// We correctly passed: print it out for verification
-		cout << "+++ Correctly passed:" << " " << headTestNumber << endl;
+		cout << "+++ Correctly passed (Head):" << " " << headTestNumber << endl;
 		wcout << L"Original: " << endl << inHeadString << endl;
 		wcout << L"Parsed output:" << endl << locHead.toString() << endl << endl;
 	} else if (!locWasOK && !inShouldPass) {
 		// We correctly failed
+		cout << "+++ Correctly failed (Head):" << " " << headTestNumber << endl;
 	} else if (locWasOK) {
 		// We incorrectly passed
 		cout << "*** INCORRECTLY PASSED (Head) ***" << " " << headTestNumber << endl;
@@ -56,11 +57,12 @@
 
 	if (locWasOK && inShouldPass) {
 		// We correctly passed: print it out for verification
-		cout << "+++ Correctly passed:" << " " << clipTestNumber << endl;
+		cout << "+++ Correctly passed (Clip):" << " " << clipTestNumber << endl;
 		wcout << L"Original: " << endl << inClipString << endl;
 		wcout << L"Parsed output:" << endl << locClip.toString() << endl << endl;
 	} else if (!locWasOK && !inShouldPass) {
 		// We correctly failed
+		cout << "+++ Correctly failed (Clip):" << " " << clipTestNumber << endl;
 	} else if (locWasOK) {
 		// We incorrectly passed
 		cout << "*** INCORRECTLY PASSED (Clip) ***" << " " << clipTestNumber << endl;
@@ -85,11 +87,12 @@
 
 	if (locWasOK && inShouldPass) {
 		// We correctly passed: print it out for verification
-		cout << "+++ Correctly passed:" << " " << rootTestNumber << endl;
+		cout << "+++ Correctly passed (Root):" << " " << rootTestNumber << endl;
 		wcout << L"Original: " << endl << inCMMLRootString << endl;
 		wcout << L"Parsed output:" << endl << locCMMLRoot.toString() << endl << endl;
 	} else if (!locWasOK && !inShouldPass) {
 		// We correctly failed
+		cout << "+++ Correctly failed (Root):" << " " << rootTestNumber << endl;
 	} else if (locWasOK) {
 		// We incorrectly passed
 		cout << "*** INCORRECTLY PASSED (Root) ***" << " " << rootTestNumber << endl;



More information about the commits mailing list