pedrodom.cpp revision 99de207f0f009471ba7e392233d62f88ad8f1739
/*
* Implementation of the Pedro mini-DOM parser and tree
*
* Authors:
* Bob Jamison
*
* Copyright (C) 2005 Bob Jamison
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "pedrodom.h"
namespace Pedro
{
//########################################################################
//# E L E M E N T
//########################################################################
{
{
}
return elem;
}
{
{
}
}
{
return res;
}
{
for (unsigned int i=0 ; i<attributes.size() ; i++)
return attributes[i].getValue();
return "";
}
{
return "";
return res;
}
{
return "";
return res;
}
{
if (!child)
return;
}
{
}
{
}
{
int i;
if (!f)
return;
//Opening tag, and attributes
for (i=0;i<indent;i++)
fputc(' ',f);
for (unsigned int i=0 ; i<attributes.size() ; i++)
{
fprintf(f," %s=\"%s\"",
}
for (unsigned int i=0 ; i<namespaces.size() ; i++)
{
fprintf(f," xmlns:%s=\"%s\"",
}
fprintf(f,">\n");
//Between the tags
{
for (int i=0;i<indent;i++)
fputc(' ', f);
}
//Closing tag
for (int i=0; i<indent; i++)
fputc(' ',f);
}
{
writeIndentedRecursive(f, 0);
}
{
}
//########################################################################
//# P A R S E R
//########################################################################
typedef struct
{
char *escaped;
char value;
} EntityEntry;
static EntityEntry entities[] =
{
{ "&" , '&' },
{ "<" , '<' },
{ ">" , '>' },
{ "'", '\'' },
{ """, '"' },
{ NULL , '\0' }
};
{
long line = 1;
long col = 1;
for (long i=0 ; i<pos ; i++)
{
{
col = 0;
line ++;
}
else
col++;
}
}
{
long lineNr;
long colNr;
}
{
return -1;
//printf("ch:%c\n", ch);
return ch;
}
{
{
if (ch == '&')
else if (ch == '<')
else if (ch == '>')
else if (ch == '\'')
else if (ch == '"')
else
}
return ret;
}
{
int p = p0;
while (*text)
{
return p0;
p++; text++;
}
return p;
}
{
while (p<parselen)
{
if (p2 > p)
{
p = p2;
while (p<parselen)
{
if (p2 > p)
{
p = p2;
break;
}
p++;
}
}
if (!isspace(b))
break;
p++;
}
return p;
}
/* modify this to allow all chars for an element or attribute name*/
{
int p = p0;
while (p<parselen)
{
if (b<=' ' || b=='/' || b=='>' || b=='=')
break;
p++;
}
return p;
}
{
int p = p0;
return p0;
p++;
while ( p<parselen )
{
if (b=='"' || b=='\'')
break;
if (b=='&' && do_i_parse)
{
bool found = false;
{
if (p2>p)
{
p = p2;
found = true;
break;
}
}
if (!found)
{
error("unterminated entity");
return false;
}
}
else
{
p++;
}
}
return p;
}
{
//printf("### parseVersion: %d\n", p0);
int p = p0;
if (peek(p) != '<')
return p0;
p++;
return p0;
p++;
while (p<parselen)
{
if (ch=='?')
{
p++;
break;
}
p++;
}
if (peek(p) != '>')
return p0;
p++;
//printf("Got version:%s\n",buf.c_str());
return p;
}
{
//printf("### parseDoctype: %d\n", p0);
int p = p0;
p = skipwhite(p);
return p0;
p++;
return p0;
p++;
while (p<parselen)
{
if (ch=='>')
{
p++;
break;
}
p++;
}
//printf("Got doctype:%s\n",buf.c_str());
return p;
}
{
int p = p0;
int p2 = p;
p = skipwhite(p);
//## Get open tag
if (ch!='<')
return p0;
p++;
p = skipwhite(p);
p = getWord(p, openTagName);
//printf("####tag :%s\n", openTagName.c_str());
p = skipwhite(p);
//Add element to tree
// Get attributes
if (peek(p) != '>')
{
while (p<parselen)
{
p = skipwhite(p);
//printf("ch:%c\n",ch);
if (ch=='>')
break;
{
p++;
p = skipwhite(p);
if (ch=='>')
{
p++;
//printf("quick close\n");
return p;
}
}
if (p2==p)
break;
//printf("name:%s",buf);
p=p2;
p = skipwhite(p);
//printf("ch:%c\n",ch);
if (ch!='=')
break;
p++;
p = skipwhite(p);
// ch = parsebuf[p];
// printf("ch:%c\n",ch);
p=p2+1;
//printf("name:'%s' value:'%s'\n",attrName.c_str(),attrVal.c_str());
else
}
}
bool cdata = false;
p++;
// ### Get intervening data ### */
while (p<parselen)
{
//# COMMENT
{
p = p2;
while (p<parselen)
{
if (p2 > p)
{
p = p2;
break;
}
p++;
}
}
//# END TAG
{
break;
}
//# CDATA
if (p2 > p)
{
cdata = true;
p = p2;
continue;
}
//# CHILD ELEMENT
if (ch == '<')
{
if (p2 == p)
{
/*
printf("problem on element:%s. p2:%d p:%d\n",
openTagName.c_str(), p2, p);
*/
return p0;
}
p = p2;
continue;
}
//# ENTITY
{
bool found = false;
{
if (p2>p)
{
p = p2;
found = true;
break;
}
}
if (!found)
{
error("unterminated entity");
return -1;
}
continue;
}
//# NONE OF THE ABOVE
p++;
}/*while*/
//printf("%d : data:%s\n",p,data.c_str());
//## Get close tag
p = skipwhite(p);
if (ch != '<')
{
error("no < for end tag\n");
return p0;
}
p++;
if (ch != '/')
{
error("no / on end tag");
return p0;
}
p++;
p = skipwhite(p);
p = getWord(p, closeTagName);
if (openTagName != closeTagName)
{
error("Mismatched closing tag. Expected </%S>. Got '%S'.",
return p0;
}
p = skipwhite(p);
if (peek(p) != '>')
{
return p0;
}
p++;
// printf("close element:%s\n",closeTagName.c_str());
p = skipwhite(p);
return p;
}
{
return rootNode;
}
{
long i = 0;
while (i< len)
{
i++;
}
charbuf[i] = '\0';
return n;
}
{
long i = 0;
while (i< len)
{
i++;
}
charbuf[i] = '\0';
return n;
}
{
//##### LOAD INTO A CHAR BUF, THEN CONVERT TO XMLCh
if (!fileName)
return NULL;
if (!f)
return NULL;
{
fclose(f);
return NULL;
}
//printf("length:%d\n",filelen);
{
}
fclose(f);
/*
printf("nrbytes:%d\n",wc_count);
printf("buf:%ls\n======\n",charbuf);
*/
return n;
}
}//namespace Pedro
#if 0
//########################################################################
//# T E S T
//########################################################################
{
if (!elem)
{
printf("Parsing failed\n");
return false;
}
delete elem;
return true;
}
{
if (argc != 2)
{
return 1;
}
return 1;
return 0;
}
#endif
//########################################################################
//# E N D O F F I L E
//########################################################################