Example #1
0
bool CfgParser::ReadValue(const char *&p, string &value)
{
	SkipWhites(p);

	bool quoted = (*p=='"');
	if(quoted) p++;

	value.clear();
	while(1)
	{
		if(quoted)
		{
			if(*p=='\n') return false;
			if(*p=='"')
			{
				p++;
				break;
			}
		}
		else
			if(*p==' ' || *p=='\t' || *p=='\r' || *p=='\n' || *p==';')
				break;

		if(*p=='\r' || *p=='\n')
			return false;

		if(*p!='\\')
			value.push_back(*p++);
		else
		{
			p++;
			if(*p=='\r') p++; // CRLF version

			if(*p=='\n') { } // do nothing 
			else if(*p=='n') value.push_back('\n');
			else if(*p=='t') value.push_back('\t');
			else if(*p=='"') value.push_back('"');
			else return false;
		}
	}

	SkipWhites(p);
	if(*p!=';') return false;
	p++;

	return true;
}
Example #2
0
bool CfgParser::ReadName(const char *&p, string &name, int &index)
{
	SkipWhitesNL(p);

	const char *n_begin, *n_end;
	int _index = 0;

	n_begin = p;
	while(IsAlnum(*p) || *p == '_' || *p=='@' || *p=='$')
		p++;
	n_end = p;

	SkipWhites(p);
	if(*p=='[')
	{
		p++;
		SkipWhites(p);

        _index = ParseInt(p);
		SkipWhites(p);
		if(*p!=']') return false;
		p++;
		SkipWhites(p);
	}
	else if(*p=='*')
	{
		p++;
		_index = auto_index++;
		SkipWhites(p);
	}

	if(*p==':' || *p=='=')
		p++;
	else
	{
		SkipWhitesNL(p);
		if(*p!='{') return false;
	}

	name.assign(n_begin, n_end-n_begin);
	index = _index;

	return true;
}
Example #3
0
void XmlParser::Next()
{
	nattr.Clear();
	nattr1 = nattrval1 = Null;
	if(empty_tag) {
		empty_tag = false;
		type = XML_END;
		return;
	}

	type = Null;
	StringBuffer raw_text;
	for(;;) {
		if(*term == '\0') {
			type = XML_EOF;
			return;
		}
		if(*term == '<') {
			if(term[1] == '!' && term[2] == '[' &&
			   term[3] == 'C' && term[4] == 'D' && term[5] == 'A' && term[6] == 'T' && term[7] == 'A' &&
			   term[8] == '[') { // ![CDATA[
				term += 9;
				for(;;) {
					if(*term == '\0')
						throw XmlError("Unterminated CDATA");
					if(term[0] == ']' && term[1] == ']' && term[2] == '>') { // ]]>
						term += 3;
						break;
					}
					if(*term == '\n')
						line++;
					raw_text.Cat(*term++);
				}
				type = XML_TEXT;
				continue;
			}
			else
				break;
		}
		if(*term == '\n')
			line++;
		if(*term == '&') {
			Ent(raw_text);
			type = XML_TEXT;
		}
		else {
			if((byte)*term > ' ')
				type = XML_TEXT;
			raw_text.Cat(*term++);
		}
	}
	cdata = FromUtf8(String(raw_text)).ToString();

	if(cdata.GetCount() && (npreserve || preserveall))
		type = XML_TEXT;
	
	if(type == XML_TEXT)
		return;
	
	term++;
	if(*term == '!') {
		type = XML_DECL;
		term++;
		if(term[0] == '-' && term[1] == '-') {
			type = XML_COMMENT;
			term += 2;
			for(;;) {
				if(term[0] == '-' && term[1] == '-' && term[2] == '>')
					break;
				if(*term == '\0')
					throw XmlError("Unterminated comment");
				if(*term == '\n')
					line++;
				tagtext.Cat(*term++);
			}
			term += 3;
			return;
		}
		bool intdt = false;
		for(;;) {
			if (*term == '[')
				intdt = true;
			if(*term == '>' && intdt == false) {
				term++;
				break;
			}
			if(intdt == true && term[0] == ']' && term[1] == '>') {
				tagtext.Cat(*term++);
				term++;
				break;
			}
			if(*term == '\0')
				throw XmlError("Unterminated declaration");
			if(*term == '\n')
				line++;
			tagtext.Cat(*term++);
		}
	}
	else
	if(*term == '?') {
		type = XML_PI;
		term++;
		for(;;) {
			if(term[0] == '?' && term[1] == '>') {
				term += 2;
				return;
			}
			if(*term == '\0')
				throw XmlError("Unterminated processing info");
			if(*term == '\n')
				line++;
			tagtext.Cat(*term++);
		}
	}
	else
	if(*term == '/') {
		type = XML_END;
		term++;
		const char *t = term;
		while(IsXmlNameChar(*term))
			term++;
		tagtext = String(t, term);
		if(*term != '>')
			throw XmlError("Unterminated end-tag");
		term++;
	}
	else {
		type = XML_TAG;
		const char *t = term;
		while(IsXmlNameChar(*term))
			term++;
		tagtext = String(t, term);
		for(;;) {
			SkipWhites();
			if(*term == '>') {
				term++;
				break;
			}
			if(term[0] == '/' && term[1] == '>') {
				cdata.Clear();
				empty_tag = true;
				term += 2;
				break;
			}
			if(*term == '\0')
				throw XmlError("Unterminated tag");
			const char *t = term++;
			while((byte)*term > ' ' && *term != '=' && *term != '>')
				term++;
			String attr(t, term);
			SkipWhites();
			if(*term == '=') {
				term++;
				SkipWhites();
				StringBuffer attrval;
				if(*term == '\"')
					ReadAttr(attrval, '\"');
				else
				if(*term == '\'')
					ReadAttr(attrval, '\'');
				else
					while((byte)*term > ' ' && *term != '>' && *term != '/')
						if(*term == '&')
							Ent(attrval);
						else {
							const char *e = term;
							while((byte)*++e > ' ' && *e != '>' && *e != '&')
								;
							attrval.Cat(term,(int) (e - term));
							term = e;
						}
				if(attr == "xml:space" && attrval.GetLength() == 8 && !memcmp(~attrval, "preserve", 8))
					npreserve = true;
				String aval = FromUtf8(~attrval, attrval.GetLength()).ToString();
				if(IsNull(nattr1)) {
					nattr1 = attr;
					nattrval1 = aval;
				}
				else
					nattr.Add(attr, aval);
			}
		}
	}
}