mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-10-21 14:53:44 +03:00
first implementation for | support Daniel
* pattern.c: first implementation for | support Daniel
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
Sat Feb 5 18:36:56 CET 2005 Daniel Veillard <daniel@veillard.com>
|
||||
|
||||
* pattern.c: first implementation for | support
|
||||
|
||||
Sat Feb 5 14:58:46 CET 2005 Daniel Veillard <daniel@veillard.com>
|
||||
|
||||
* pattern.c: fixed the namespaces support
|
||||
|
131
pattern.c
131
pattern.c
@@ -66,6 +66,7 @@ struct _xmlStreamComp {
|
||||
};
|
||||
|
||||
struct _xmlStreamCtxt {
|
||||
struct _xmlStreamCtxt *next;/* link to next sub pattern if | */
|
||||
xmlStreamCompPtr comp; /* the compiled stream */
|
||||
int nbState; /* number of state in the automata */
|
||||
int maxState; /* allocated number of state */
|
||||
@@ -118,7 +119,7 @@ struct _xmlStepOp {
|
||||
struct _xmlPattern {
|
||||
void *data; /* the associated template */
|
||||
xmlDictPtr dict; /* the optional dictionnary */
|
||||
struct _xmlPattern *next; /* siblings */
|
||||
struct _xmlPattern *next; /* next pattern if | is used */
|
||||
const xmlChar *pattern; /* the pattern */
|
||||
|
||||
int nbStep;
|
||||
@@ -188,6 +189,8 @@ xmlFreePattern(xmlPatternPtr comp) {
|
||||
|
||||
if (comp == NULL)
|
||||
return;
|
||||
if (comp->next != NULL)
|
||||
xmlFreePattern(comp->next);
|
||||
if (comp->stream != NULL)
|
||||
xmlFreeStreamComp(comp->stream);
|
||||
if (comp->pattern != NULL)
|
||||
@@ -1362,11 +1365,13 @@ xmlStreamCtxtAddState(xmlStreamCtxtPtr comp, int idx, int level) {
|
||||
int
|
||||
xmlStreamPush(xmlStreamCtxtPtr stream,
|
||||
const xmlChar *name, const xmlChar *ns) {
|
||||
int ret = 0, tmp, i, m, match, step, desc, final;
|
||||
int ret = 0, err = 0, tmp, i, m, match, step, desc, final;
|
||||
xmlStreamCompPtr comp;
|
||||
|
||||
if ((stream == NULL) || (stream->nbState < 0))
|
||||
return(-1);
|
||||
|
||||
while (stream != NULL) {
|
||||
comp = stream->comp;
|
||||
if ((name == NULL) && (ns == NULL)) {
|
||||
stream->nbState = 0;
|
||||
@@ -1374,11 +1379,12 @@ xmlStreamPush(xmlStreamCtxtPtr stream,
|
||||
if (comp->steps[0].flags & XML_STREAM_STEP_ROOT) {
|
||||
tmp = xmlStreamCtxtAddState(stream, 0, 0);
|
||||
if (tmp < 0)
|
||||
return(-1);
|
||||
err++;
|
||||
if (comp->steps[tmp].flags & XML_STREAM_STEP_FINAL)
|
||||
return(1);
|
||||
ret = 1;
|
||||
continue; /* while */
|
||||
}
|
||||
return(0);
|
||||
continue; /* while */
|
||||
}
|
||||
/*
|
||||
* Check evolution of existing states
|
||||
@@ -1390,10 +1396,12 @@ xmlStreamPush(xmlStreamCtxtPtr stream,
|
||||
/* dead states */
|
||||
if (step < 0) continue;
|
||||
/* skip new states just added */
|
||||
if (stream->states[(2 * i) + 1] > stream->level) continue;
|
||||
if (stream->states[(2 * i) + 1] > stream->level)
|
||||
continue;
|
||||
/* skip continuations */
|
||||
desc = comp->steps[step].flags & XML_STREAM_STEP_DESC;
|
||||
if ((stream->states[(2 * i) + 1] < stream->level) && (!desc))continue;
|
||||
if ((stream->states[(2 * i) + 1] < stream->level) && (!desc))
|
||||
continue;
|
||||
|
||||
/* discard old states */
|
||||
/* something needed about old level discarded */
|
||||
@@ -1426,27 +1434,17 @@ xmlStreamPush(xmlStreamCtxtPtr stream,
|
||||
ret = 1;
|
||||
} else {
|
||||
/* descending match create a new state */
|
||||
xmlStreamCtxtAddState(stream, step + 1, stream->level + 1);
|
||||
xmlStreamCtxtAddState(stream, step + 1,
|
||||
stream->level + 1);
|
||||
}
|
||||
} else {
|
||||
if (final) {
|
||||
ret = 1;
|
||||
#if 0
|
||||
stream->states[2 * i] = -1;
|
||||
#endif
|
||||
} else {
|
||||
#if 0
|
||||
stream->states[2 * i] = step + 1;
|
||||
stream->states[2 * i + 1] = stream->level + 1;
|
||||
#endif
|
||||
xmlStreamCtxtAddState(stream, step + 1, stream->level + 1);
|
||||
xmlStreamCtxtAddState(stream, step + 1,
|
||||
stream->level + 1);
|
||||
}
|
||||
}
|
||||
#if 0
|
||||
} else if (!desc) {
|
||||
/* didn't match, discard */
|
||||
stream->states[2 * i] = -1;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1485,9 +1483,14 @@ xmlStreamPush(xmlStreamCtxtPtr stream,
|
||||
}
|
||||
}
|
||||
|
||||
stream = stream->next;
|
||||
} /* while stream != NULL */
|
||||
|
||||
#ifdef DEBUG_STREAMING
|
||||
xmlDebugStreamCtxt(stream, ret);
|
||||
#endif
|
||||
if (err > 0)
|
||||
ret = -1;
|
||||
return(ret);
|
||||
}
|
||||
|
||||
@@ -1502,12 +1505,15 @@ xmlStreamPush(xmlStreamCtxtPtr stream,
|
||||
int
|
||||
xmlStreamPop(xmlStreamCtxtPtr stream) {
|
||||
int i, m;
|
||||
int ret;
|
||||
|
||||
if (stream == NULL)
|
||||
return(-1);
|
||||
ret = 0;
|
||||
while (stream != NULL) {
|
||||
stream->level--;
|
||||
if (stream->level < 0)
|
||||
return(-1);
|
||||
ret = -1;
|
||||
|
||||
/*
|
||||
* Check evolution of existing states
|
||||
@@ -1519,6 +1525,8 @@ xmlStreamPop(xmlStreamCtxtPtr stream) {
|
||||
if (stream->states[(2 * i) + 1] > stream->level)
|
||||
stream->states[(2 * i)] = -1;
|
||||
}
|
||||
stream = stream->next;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
@@ -1543,25 +1551,57 @@ xmlPatternPtr
|
||||
xmlPatterncompile(const xmlChar *pattern, xmlDict *dict,
|
||||
int flags ATTRIBUTE_UNUSED,
|
||||
const xmlChar **namespaces) {
|
||||
xmlPatternPtr ret = NULL;
|
||||
xmlPatternPtr ret = NULL, cur;
|
||||
xmlPatParserContextPtr ctxt = NULL;
|
||||
const xmlChar *or, *start;
|
||||
xmlChar *tmp = NULL;
|
||||
|
||||
ctxt = xmlNewPatParserContext(pattern, dict, namespaces);
|
||||
if (pattern == NULL)
|
||||
return(NULL);
|
||||
|
||||
start = pattern;
|
||||
while (*or != 0) {
|
||||
or = start;
|
||||
tmp = NULL;
|
||||
while ((*or != 0) && (*or != '|')) or++;
|
||||
if (*or == 0)
|
||||
ctxt = xmlNewPatParserContext(start, dict, namespaces);
|
||||
else {
|
||||
tmp = xmlStrndup(start, or - start);
|
||||
if (tmp != NULL) {
|
||||
ctxt = xmlNewPatParserContext(tmp, dict, namespaces);
|
||||
}
|
||||
or++;
|
||||
}
|
||||
if (ctxt == NULL) goto error;
|
||||
ret = xmlNewPattern();
|
||||
if (ret == NULL) goto error;
|
||||
ctxt->comp = ret;
|
||||
cur = xmlNewPattern();
|
||||
if (cur == NULL) goto error;
|
||||
if (ret == NULL)
|
||||
ret = cur;
|
||||
else {
|
||||
cur->next = ret->next;
|
||||
ret->next = cur;
|
||||
}
|
||||
ctxt->comp = cur;
|
||||
|
||||
xmlCompilePathPattern(ctxt);
|
||||
xmlFreePatParserContext(ctxt);
|
||||
|
||||
xmlStreamCompile(ret);
|
||||
if (xmlReversePattern(ret) < 0)
|
||||
|
||||
xmlStreamCompile(cur);
|
||||
if (xmlReversePattern(cur) < 0)
|
||||
goto error;
|
||||
start = or;
|
||||
if (tmp != NULL) {
|
||||
xmlFree(tmp);
|
||||
tmp = NULL;
|
||||
}
|
||||
}
|
||||
return(ret);
|
||||
error:
|
||||
if (ctxt != NULL) xmlFreePatParserContext(ctxt);
|
||||
if (ret != NULL) xmlFreePattern(ret);
|
||||
if (tmp != NULL) xmlFree(tmp);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
@@ -1577,9 +1617,18 @@ error:
|
||||
int
|
||||
xmlPatternMatch(xmlPatternPtr comp, xmlNodePtr node)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if ((comp == NULL) || (node == NULL))
|
||||
return(-1);
|
||||
return(xmlPatMatch(comp, node));
|
||||
|
||||
while (comp != NULL) {
|
||||
ret = xmlPatMatch(comp, node);
|
||||
if (ret != 0)
|
||||
return(ret);
|
||||
comp = comp->next;
|
||||
}
|
||||
return(ret);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1594,9 +1643,29 @@ xmlPatternMatch(xmlPatternPtr comp, xmlNodePtr node)
|
||||
xmlStreamCtxtPtr
|
||||
xmlPatternGetStreamCtxt(xmlPatternPtr comp)
|
||||
{
|
||||
xmlStreamCtxtPtr ret = NULL, cur;
|
||||
|
||||
if ((comp == NULL) || (comp->stream == NULL))
|
||||
return(NULL);
|
||||
return(xmlNewStreamCtxt(comp->stream));
|
||||
|
||||
while (comp != NULL) {
|
||||
if (comp->stream == NULL)
|
||||
goto failed;
|
||||
cur = xmlNewStreamCtxt(comp->stream);
|
||||
if (cur == NULL)
|
||||
goto failed;
|
||||
if (ret == NULL)
|
||||
ret = cur;
|
||||
else {
|
||||
cur->next = ret->next;
|
||||
ret->next = cur;
|
||||
}
|
||||
comp = comp->next;
|
||||
}
|
||||
return(ret);
|
||||
failed:
|
||||
xmlFreeStreamCtxt(ret);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
#endif /* LIBXML_PATTERN_ENABLED */
|
||||
|
Reference in New Issue
Block a user