📄 xmlpullparseres.c
字号:
ns = NULL;
}
if (ns != NULL)
fprintf(stderr,"when namespaces processing is disabled attribute namespace must be NULL");
for (i = 0; i < attributeCount; ++i) {
if (strcmp(name,attributeName[i])==0) {
return attributeValue[i];
}
}
}
return emptyString;
}
char* nextText()
{
if (getEventType() != START_TAG) {
fprintf(stderr,"parser must be on START_TAG to read next text");
return NULL;
}
int eventType = next();
if (eventType == TEXT) {
char* result = getText();
eventType = next();
if (eventType != END_TAG)
{
fprintf(stderr,"TEXT must be immediately followed by END_TAG and not %s",
TYPES[getEventType()]);
return NULL;
}
return result;
} else if (eventType == END_TAG) {
return emptyString;
} else {
fprintf(stderr,"parser must be on START_TAG or TEXT to read text");
return NULL;
}
}
int nextTag()
{
next();
if (eventType == TEXT && isWhitespace()) { // skip whitespace
next();
}
if (eventType != START_TAG && eventType != END_TAG) {
fprintf(stderr, "expected START_TAG or END_TAG not %s", TYPES[getEventType()
]);
return -1;
}
return eventType;
}
void ensureElementsCapacity()
{
int i;
const int newSize = (level >= 11 ? 2 * level : 16) + 2;
const int needsCopying = level>elStackSize ? 1:0;
int elStackSize2;
// iarr1=iarr;
elStackSize2=elStackSize+1;
if(needsCopying) arr=(char*)malloc(sizeof(char*)*newSize);
if(needsCopying) arr2=(char*)malloc(sizeof(char*)*newSize);
if(needsCopying) arr3=(char*)malloc(sizeof(char*)*newSize);
if (needsCopying) carr=(char*)malloc(sizeof(char*)*newSize);
if (needsCopying) iarr1=(int*)malloc(sizeof(int)*newSize);
if (needsCopying) iarr2=(int*)malloc(sizeof(int)*newSize);
if (needsCopying) iarr=(int*)malloc(sizeof(int)*newSize);
if ((level+1) >=elStackSize) {
for(i=0;i<elStackSize2;i++)
if (needsCopying) {
arr[i]=(char*)malloc(strlen(elName[i]));
strcpy( arr[i],elName[i]);
// free (elName[i]);
}
if(needsCopying) {//free (elName);
elName = arr;}
for(i=0;i<elStackSize2;i++)
if (needsCopying) {
arr2[i]=(char*)malloc(strlen(elPrefix[i]));
strcpy(arr2[i],elPrefix[i]);
// free (elPrefix[i]);
}
if (needsCopying) {//free (elPrefix);
elPrefix = arr2;}
for(i=0;i<elStackSize2;i++)
if (needsCopying) {
arr3[i]=(char*)malloc(strlen(elUri[i]));
strcpy(arr3[i],elUri[i]);
// free (elUri[i]);
}
if (needsCopying) {//free (elUri);
elUri = arr3;}
for(i=0;i<elStackSize2;i++)
if (needsCopying) {
iarr1[i]=elNamespaceCount[i];
//free (elNamespaceCount[i]);
} else {// special initialization
iarr1[0] = 0;
}
if (needsCopying) {//free(elNamespaceCount);
elNamespaceCount = iarr1;}
for(i=0;i<elStackSize2;i++)
if (needsCopying) { iarr2[i]=elRawNameEnd[i] ; }
if (needsCopying) {//free(elRawNameEnd);
elRawNameEnd = iarr2; }
for(i=0;i<elStackSize2;i++)
if (needsCopying) { iarr[i]=elRawNameLine[i]; }
if (needsCopying) {//free(elRawNameLine);
elRawNameLine = iarr;}
for(i=0;i<level;i++)
if (needsCopying) {
carr[i]=(char*)malloc(strlen(elRawName[i]));
strcpy( carr[i],elRawName[i]);
// free (elRawName[i]);
}
if (needsCopying) { //free (elRawName);
elRawName = carr; }
elRawName[level]=NULL;
elStackSize++;
}
arr=NULL;
arr2=NULL;
arr3=NULL;
carr=NULL;
iarr=NULL;
iarr1=NULL;
iarr2=NULL;
}
/*
* Make sure that in attributes temporary array is enough space.
*/
void ensureAttributesCapacity(int size)
{
int i;
const int newSize = size > 11 ? 2 * size : 16;
const int needsCopying = size>attrPosSize?1: 0;
int attrPosSize2;
attrPosSize2=attrPosSize+1;
//char* arr[newSize];
if(needsCopying) arr=(char*)malloc(sizeof(char*)*newSize);
if(needsCopying) arr2=(char*)malloc(sizeof(char*)*newSize);
if(needsCopying) arr3=(char*)malloc(sizeof(char*)*newSize);
if(needsCopying) carr=(char*)malloc(sizeof(char*)*newSize);
if (size >= attrPosSize) {
for(i=0;i<attrPosSize;i++)
if (needsCopying) {
arr[i]=(char*)malloc(strlen(attributeName[i]));
strcpy(arr[i],attributeName[i]);}
if (needsCopying){free(attributeName); attributeName = arr;}
for(i=0;i<attrPosSize;i++)
if (needsCopying) {
arr2[i]=(char*)malloc(strlen(attributePrefix[i]));
strcpy(arr2[i],attributePrefix[i]);}
if (needsCopying) {free(attributePrefix);attributePrefix = arr2;}
for(i=0;i<attrPosSize;i++)
if (needsCopying){
arr3[i]=(char*)malloc(strlen(attributeUri[i]));
strcpy( arr3[i],attributeUri[i]);}
if (needsCopying) {free(attributeUri);attributeUri = arr3;}
for(i=0;i<attrPosSize;i++)
if (needsCopying){
carr[i]=(char*)malloc(strlen(attributeValue[i]));
strcpy(carr[i], attributeValue[i]);}
if (needsCopying) {free (attributeValue);attributeValue = carr; }
++attrPosSize;
}
if (needsCopying){arr=NULL;
arr2=NULL;
arr3=NULL;
carr=NULL; }
}
void ensureNamespacesCapacity(int size)
{
int i;
const int newSize = size > 11 ? 2 * size : 16;
// char* newNamespacePrefix[newSize];
// char* newNamespaceUri [newSize];
newNamespacePrefix=(char*)malloc(sizeof(char*)*newSize);
newNamespaceUri=(char*)malloc(sizeof(char*)*newSize);
if (size >= namespaceSize) {
for(i=0;i<namespaceEnd;i++)
if (strlen(namespacePrefix[i]) != 0) {
newNamespacePrefix[i]=(char*)malloc(strlen(namespacePrefix[i]));
newNamespaceUri[i]=(char*)malloc(strlen(namespaceUri[i]));
strcpy(newNamespacePrefix[i],namespacePrefix[i]);
strcpy(newNamespaceUri[i],namespaceUri[i]);
//arraycopy(namespacePrefix[i], 0, newNamespacePrefix[i], 0, strlen(namespacePrefix[i]));
//arraycopy(namespaceUri[i], 0, newNamespaceUri[i], 0, strlen(namespaceUri[i]));
}
namespacePrefix = newNamespacePrefix;
namespaceUri = newNamespaceUri;
++namespaceSize;
}
}
void ensureEntityCapacity()
{
int i;
const int newSize = entityEnd > 11 ? 2 * entityEnd : 16;
newEntityName=(char*)malloc(sizeof(char*)*newSize);
newEntityNameBuf=(char*)malloc(sizeof(char*)*newSize);
newEntityReplacement=(char*)malloc(sizeof(char*)*newSize);
newEntityReplacementBuf=(char*)malloc(sizeof(char*)*newSize);
if (entityEnd >= entitySize) {
for(i=0;i<entityEnd;i++)
if (entityName != NULL) {
newEntityName[i]=(char*)malloc(strlen(entityName[i]));
newEntityNameBuf[i]=(char*)malloc(strlen(entityNameBuf[i]));
newEntityReplacement[i]=(char*)malloc(strlen(entityReplacement[i]));
newEntityReplacementBuf[i]=(char*)malloc(strlen(entityReplacementBuf[i]));
strcpy(newEntityName[i],entityName[i]);
strcpy(newEntityNameBuf[i],entityNameBuf[i]);
strcpy(newEntityReplacement[i],entityReplacement[i]);
strcpy(newEntityReplacementBuf[i],entityReplacementBuf[i]);
}
entityName = newEntityName;
entityNameBuf = newEntityNameBuf;
entityReplacement = newEntityReplacement;
entityReplacementBuf = newEntityReplacementBuf;
++entitySize;
}
}
void ensurePC(int end) //把扩充之后的PC代替之前的PC
{
const int newSize = end > READ_CHUNK_SIZE ? 2 * end : 2 * READ_CHUNK_SIZE;
char newPC[newSize];
arraycopy(pc, 0, newPC, 0, pcEnd);
pc = newPC;
}
void joinPC() //buf到pc
{
const int len = posEnd - posStart;
const int newEnd = pcEnd + len + 1;
if ((unsigned)newEnd >= strlen(pc)) ensurePC(newEnd);
// add 1 for extra space for one char
arraycopy(buf, posStart, pc, pcEnd, len);
pcEnd += len;
usePC = 1;
}
char requireInput(char ch, const char* input) //match input
{
unsigned int i ;
for (i = 0; i < strlen(input); i++) {
if (ch != input[i]) {
fprintf(stderr,"expected %s in %s and not %s!" , printable(input[i]) ,
input, printable(ch) );
return 0;
}
ch = more();
}
return ch;
}
char requireNextS()
{
const char ch = more();
if (!isS(ch)) {
fprintf(stderr, "white space is required and not %s" , printable(ch));
return 0;
}
return skipS(ch);
}
char skipS(char ch)
{
while (isS(ch)) {
ch = more();
} // skip additional spaces
return ch;
}
int isNameStartChar(char ch)
{
return ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A)
|| (ch >= 0xC0 && ch <= 0xFF)|| ch == '_' || ch == ':');
}
int isNameChar(char ch)
{
return ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A)
|| (ch >= 0xC0 && ch <= 0xFF)|| (ch >= '0' && ch <= '9')
|| ch == '.' || ch == '-' || ch == '_' || ch == ':');
}
int isS(char ch) //判断是否是空白字符
{
return (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t');
}
char* printable(char ch)
{
if (ch == '\n') {
return print_char;
} else if ((ch == '\r')||(ch == '\t')||(ch == '\''))
{
print_char[1]=ch;
return print_char;
}
print_char[0]=ch; print_char[1]='\0';
return print_char;
}
void defineEntityReplacementText(char* entityName_, char* replacementText_)
{
ensureEntityCapacity();
entityName[entityEnd]=(char*)malloc(strlen(entityName_));
entityNameBuf[entityEnd]=(char*)malloc(strlen(entityName_));
stringcopy(entityName_, 0,entityName[entityEnd],0, strlen(entityName_));
strcpy(entityNameBuf[entityEnd],entityName_);
entityReplacement[entityEnd] = replacementText_;
entityReplacementBuf[entityEnd]=(char*)malloc(strlen(replacementText_));
strcpy(entityReplacementBuf[entityEnd],replacementText_);
++entityEnd;
}
char* parseEntityRef()
{
// ASSUMPTION just after &
char ch = more();
int len;
char tempstr[2]={'\0','\0'};
entityRefName = NULL;
posStart = pos;
if (ch == '#') {
// parse character reference
char charRef = 0;
ch = more();
if (ch == 'x') {
//encoded in hex
while (true) {
ch = more();
if (ch >= '0' && ch <= '9') {
charRef = (char) (charRef * 16 + (ch - '0'));
} else if (ch >= 'a' && ch <= 'f') {
charRef = (char) (charRef * 16 + (ch - ('a' - 10)));
} else if (ch >= 'A' && ch <= 'F') {
charRef = (char) (charRef * 16 + (ch - ('A' - 10)));
} else if (ch == ';') {
break;
} else {
fprintf(stderr,"character reference (with hex value) may not contain %s"
, printable(ch));
return NULL;
}
}
} else {
// encoded in decimal
while (true) {
if (ch >= '0' && ch <= '9') {
charRef = (char) (charRef * 10 + (ch - '0'));
} else if (ch == ';') {
break;
} else {
fprintf(stderr, "character reference (with hex value) may not contain %s"
,printable(ch));
return NULL;
}
ch = more();
}
}
posEnd = pos - 1;
charRefOneCharBuf[0] = charRef;
if (tokenize) {
text=(char*)malloc(1);
stringcopy(charRefOneCharBuf, 0,text,0, 1);
}
return charRefOneCharBuf;
} else {
// name reference
// scan until ;
do {
ch = more();
} while (ch != ';');
posEnd = pos - 1;
// determine what name maps to
len = posEnd - posStart;
if (len == 2 && buf[posStart] == 'l' && buf[posStart + 1] == 't') {
if (tokenize) {
tempstr[0]='<';
text = tempstr;
}
charRefOneCharBuf[0] = '<';
return charRefOneCharBuf;
} else if (len == 3 && buf[posStart] == 'a'
&& buf[posStart + 1] == 'm' && buf[posStart + 2] == 'p') {
if (tokenize) { tempstr[0]='&';
text = tempstr;
}
charRefOneCharBuf[0] = '&';
return charRefOneCharBuf;
} else if (len == 2 && buf[posStart] == 'g' && buf[posStart + 1] == 't') {
if (tokenize) {tempstr[0]='>';
text = tempstr;
}
charRefOneCharBuf[0] = '>';
return charRefOneCharBuf;
} else if (len == 4 && buf[posStart] == 'a' && buf[posStart + 1] == 'p'
&& buf[posStart + 2] == 'o' && buf[posStart + 3] == 's') {
if (tokenize) {tempstr[0]='\'';
text = tempstr;
}
charRefOneCharBuf[0] = '\'';
return charRefOneCharBuf;
} else if (len == 4 && buf[posStart] == 'q' && buf[posStart + 1] == 'u'
&& buf[posStart + 2] == 'o' && buf[posStart + 3] == 't') {
if (tokenize) {tempstr[0]='"';
text = tempstr;
}
charRefOneCharBuf[0] = '"';
return charRefOneCharBuf;
} else {
char* result = lookuEntityReplacement(len);
if (result != NULL) {
return result;
}
}
if (tokenize) text = NULL;
return NULL;
}
}
char* lookuEntityReplacement(int entitNameLen)
{
int i;
entityRefName=(char*)malloc(posEnd - posStart);
stringcopy(buf, posStart,entityRefName,0, posEnd - posStart);
for (i = entityEnd - 1; i >= 0; --i) {
if (entityRefName == entityName[i]) {
if (tokenize) text = entityReplacement[i];
return entityReplacementBuf[i];
}
}
return NULL;
}
void initialParser()
{
char charbuf[1];
char pc_buf[pc_buf_size];
char Input_buf[input_buf_size];
elStackSize = 0;
attrPosSize = 0;
namespaceSize = 0;
entitySize = 0;
eventType = 0;
bufLoadFactor = 95;
charRefOneCharBuf = charbuf;
pc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -