www.pudn.com > truecrypt-4.2-source-code.zip > Xml.c


/*  
Copyright (c) 2004-2006 TrueCrypt Foundation. All rights reserved.  
 
Covered by TrueCrypt License 2.0 the full text of which is contained in the file 
License.txt included in TrueCrypt binary and source code distribution archives.  
*/ 
 
#include  
#include  
#include "Xml.h" 
 
 
static BOOL BeginsWith (char *string, char *subString) 
{ 
	while (*string++ == *subString++) 
	{ 
		if (*subString == 0) return TRUE; 
		if (*string == 0) return FALSE; 
	} 
 
	return FALSE; 
} 
 
 
char *XmlNextNode (char *xmlNode) 
{ 
	char *t = xmlNode + 1; 
	while ((t = strchr (t, '<')) != NULL) 
	{ 
		if (t[1] != '/') 
			return t; 
 
		t++; 
	} 
 
	return NULL; 
} 
 
 
char *XmlFindElement (char *xmlNode, char *nodeName) 
{ 
	char *t = xmlNode; 
	size_t nameLen = strlen (nodeName); 
 
	do 
	{ 
		if (BeginsWith (t + 1, nodeName) 
			&& (t[nameLen + 1] == '>' 
			|| t[nameLen + 1] == ' ')) return t; 
 
	} while (t = XmlNextNode (t)); 
 
	return NULL; 
} 
 
 
char *XmlFindElementByAttributeValue (char *xml, char *nodeName, char *attrName, char *attrValue) 
{ 
	char attr[2048]; 
 
	while (xml = XmlFindElement (xml, nodeName)) 
	{ 
		XmlAttribute (xml, attrName, attr, sizeof (attr)); 
		if (strcmp (attr, attrValue) == 0) 
			return xml; 
 
		xml++; 
	} 
 
	return NULL; 
} 
 
 
char *XmlAttribute (char *xmlNode, char *xmlAttrName, char *xmlAttrValue, int xmlAttrValueSize) 
{ 
	char *t = xmlNode; 
	char *e = xmlNode; 
	int l = 0; 
 
	xmlAttrValue[0] = 0; 
	if (t[0] != '<') return NULL; 
 
	e = strchr (e, '>'); 
	if (e == NULL) return NULL; 
 
	while ((t = strstr (t, xmlAttrName)) && t < e) 
	{ 
		char *o = t + strlen (xmlAttrName); 
		if (t[-1] == ' ' 
			&& 
			(BeginsWith (o, "=\"") 
			|| BeginsWith (o, "= \"") 
			|| BeginsWith (o, " =\"") 
			|| BeginsWith (o, " = \"")) 
			) 
			break; 
 
		t++; 
	} 
 
	if (t == NULL || t > e) return NULL; 
 
	t = strchr (t, '"') + 1; 
	e = strchr (t, '"'); 
	l = (int)(e - t); 
	if (e == NULL || l > xmlAttrValueSize) return NULL; 
 
	memcpy (xmlAttrValue, t, l); 
	xmlAttrValue[l] = 0; 
 
	return xmlAttrValue; 
} 
 
 
char *XmlNodeText (char *xmlNode, char *xmlText, int xmlTextSize) 
{ 
	char *t = xmlNode; 
	char *e = xmlNode + 1; 
	int l = 0, i = 0, j = 0; 
 
	xmlText[0] = 0; 
 
	if (t[0] != '<') 
		return NULL; 
 
	t = strchr (t, '>') + 1; 
	if (t == (char *)1) return NULL; 
 
	e = strchr (e, '<'); 
	if (e == NULL) return NULL; 
 
	l = (int)(e - t); 
	if (e == NULL || l > xmlTextSize) return NULL; 
 
	while (i < l) 
	{ 
		if (BeginsWith (&t[i], "<")) 
		{ 
			xmlText[j++] = '<'; 
			i += 4; 
			continue; 
		} 
		if (BeginsWith (&t[i], ">")) 
		{ 
			xmlText[j++] = '>'; 
			i += 4; 
			continue; 
		} 
		if (BeginsWith (&t[i], "&")) 
		{ 
			xmlText[j++] = '&'; 
			i += 5; 
			continue; 
		} 
		xmlText[j++] = t[i++]; 
	} 
	xmlText[j] = 0; 
 
	return t; 
} 
 
 
int XmlWriteHeader (FILE *file) 
{ 
	return fputs ("\n", file); 
} 
 
 
int XmlWriteFooter (FILE *file) 
{ 
	return fputs ("\n", file); 
}