[sword-cvs] sword/src/utilfuns utilxml.cpp,NONE,1.1 Makefile.am,1.5,1.6

sword@www.crosswire.org sword@www.crosswire.org
Sun, 25 May 2003 21:32:47 -0700


Update of /usr/local/cvsroot/sword/src/utilfuns
In directory www:/tmp/cvs-serv8807/src/utilfuns

Modified Files:
	Makefile.am 
Added Files:
	utilxml.cpp 
Log Message:
	Added utilfuns/utilxml.cpp and test/xmltest which
		include basic xml utility classes
	Added comparison operators to SWBuf so it plays
		nicely with stl containers



--- NEW FILE: utilxml.cpp ---

#include <utilxml.h>
#include <ctype.h>
#include <utilstr.h>

SWORD_NAMESPACE_START

void XMLTag::parse() const {
	int i;
	int start;
	char *name = 0;
	char *value = 0;
	for (i = 0; ((buf[i]) && (!isalpha(buf[i]))); i++);
	for (; buf[i]; i++) {
		if (buf[i] == ' ') {
			for (; ((buf[i]) && (!isalpha(buf[i]))); i++) {
				if (buf[i] == '/')
					empty = true;
			}
			if (buf[i]) {		// we have an attribute name
				start = i;
				for (; ((buf[i]) && (!strchr(" =", buf[i]))); i++);
				if (i-start) {
					if (name)
						delete [] name;
					name = new char [ (i-start) + 1 ];
					strncpy(name, buf+start, i-start);
					name[i-start] = 0;
				}
				for (; ((buf[i]) && (strchr(" =\"", buf[i]))); i++);
				if (buf[i]) {	// we have attribute value
					start = i;
					for (; ((buf[i]) && (buf[i] != '\"')); i++);
					if (i-start) {
						if (value)
							delete [] value;
						value = new char [ (i-start) + 1 ];
						strncpy(value, buf+start, i-start);
						value[i-start] = 0;
						attributes[name] = value;
					}
				}
			}
		}
	}
	parsed = true;
	if (name) delete [] name;
	if (value) delete [] value;
}


XMLTag::XMLTag(const char *tagString) {

	name   = 0;
	buf    = 0;
	parsed = false;
	empty  = false;
	endTag = false;

	stdstr(&buf, tagString);

	int start = 0;
	int i;
	for (i = 0; ((tagString[i]) && (!isalpha(tagString[i]))); i++) {
		if (tagString[i] == '/')
			endTag = true;
	}
	start = i;
	for (; ((tagString[i]) && (!strchr(" />", tagString[i]))); i++);
	if (i-start) {
		name = new char [ (i-start) + 1 ];
		strncpy(name, tagString+start, i-start);
		name[i-start] = 0;
	}
}

XMLTag::~XMLTag() {
	if (buf)
		delete [] buf;
	if (name)
		delete [] name;
}

const ListString XMLTag::getAttributeNames() const {
	ListString retVal;

	if (!parsed)
		parse();

	for (MapStringPair::iterator it = attributes.begin(); it != attributes.end(); it++)
		retVal.push_back(it->first.c_str());

	return retVal;
}


const char *XMLTag::getAttribute(const char *attribName) const {
	if (!parsed)
		parse();

	MapStringPair::iterator it = attributes.find(attribName);
	if (it == attributes.end()) {
		return 0;
	}
	return (it == attributes.end()) ? 0 : it->second.c_str();
}


const char *XMLTag::toString() const {
	SWBuf tag = "<";
	if (!parsed)
		parse();

	if (isEndTag())
		tag += "/";

	tag += getName();
	for (MapStringPair::iterator it = attributes.begin(); it != attributes.end(); it++) {
		tag.appendFormatted(" %s=\"%s\"", it->first.c_str(), it->second.c_str());
	}

	if (isEmpty())
		tag += "/";

	tag += ">";


	if (buf)
		delete [] buf;
	buf = new char [ tag.length() + 1 ];
	strcpy(buf, tag.c_str());

	return buf;
}


SWORD_NAMESPACE_END

Index: Makefile.am
===================================================================
RCS file: /usr/local/cvsroot/sword/src/utilfuns/Makefile.am,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** Makefile.am	20 Feb 2003 03:12:37 -0000	1.5
--- Makefile.am	26 May 2003 04:32:45 -0000	1.6
***************
*** 3,6 ****
--- 3,7 ----
  libsword_la_SOURCES += $(utilfunsdir)/Greek2Greek.cpp
  libsword_la_SOURCES += $(utilfunsdir)/utilstr.cpp
+ libsword_la_SOURCES += $(utilfunsdir)/utilxml.cpp
  libsword_la_SOURCES += $(utilfunsdir)/unixstr.cpp
  libsword_la_SOURCES += $(utilfunsdir)/swunicod.cpp