⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlpullparseres.c

📁 xml解析器(在niosII环境下用C语言开发的)
💻 C
📖 第 1 页 / 共 5 页
字号:
		pos -= bufStart;
		posStart -= bufStart;
		posEnd -= bufStart;
		bufAbsoluteStart += bufStart;
		bufStart = 0;
	}
	// at least one charcter must be read or error
	 len = strlen(buf) - bufEnd > READ_CHUNK_SIZE ? READ_CHUNK_SIZE : 
strlen(buf) - bufEnd;
	 ret = read((unsigned char*)buf, bufEnd, len);
	if (ret > 0) {
		bufEnd += ret;
		return true;
	}
	if (ret == -1) {
		if (bufAbsoluteStart == 0 && pos == 0) {
			fprintf(stderr,"input contained no data");
			return -1;
		} else {			
			if (level > 0) {				
				strcat(expectedTagStack," - expected end tag");
				if (level > 1) {
					strcat(expectedTagStack,"s"); //more than one end tag
				}
				strcat(expectedTagStack," ");				
				for (i = level; i > 0; i--) {
         tagName=(char*)malloc(elRawNameEnd[i]);
        stringcopy(elRawName[i], 0,tagName,0, elRawNameEnd[i]);				
					strcat(expectedTagStack,"</");
					strcat(expectedTagStack,tagName);
					strcat(expectedTagStack,">");
				}
				strcat(expectedTagStack," to close");
				for (i = level; i > 0; i--) {
					if (i != level) {
						strcat(expectedTagStack," and"); //more than one end tag
					}
          tagName=(char*)malloc(elRawNameEnd[i]);
          stringcopy(elRawName[i], 0,tagName,0, elRawNameEnd[i]);					
					strcat(expectedTagStack," start tag <");
					strcat(expectedTagStack,tagName);
					strcat(expectedTagStack,">");
					strcat(expectedTagStack," from line  ");
					strcat(expectedTagStack,elRawNameLine[i]);	
					}
				strcat(expectedTagStack,", parser stopped on");
			}

			fprintf(stderr,"no more data available\n");
			return -1;
		}
	} else {
		fprintf(stderr,"error reading input, returned ");
		return -1;
	}
}

char more() 
{
	char ch ;
	if (pos >= bufEnd) fillBuf();
	ch = buf[pos++];
	//line/columnNumber
	if (ch == '\n') {
		++lineNumber;
		columnNumber = 1;
	} else {
		++columnNumber;
	}
	return ch;
}

int checkOutOfBound(int index, int attributeCount)
{
	if (index < 0 || index >= attributeCount){
fprintf(stderr,"Attribut Position must be 0..%d,but not %d",(attributeCount-1),index);
	 return false;
		}
   return true;
}

/** Unknown properties are always returned as false */
int getFeature(char* name) 
{
	if (!name) {
		 fprintf(stderr,"feature name should not be NULL!");
		 return false;
	}
	if (strcmp(name,FEATURE_PROCESS_NAMESPACES)==0) {
		return Namespacesprocess;
	}  
	fprintf(stderr,"unsupported feature %s", name);
	return false;
}

int setProperty(char* name, char* value)
{
	fprintf(stderr,  "unsupported property: %s", name);
	return false;
}


char * getProperty(char* name) 
{
	if (name == NULL) {
		fprintf(stderr, "property name should not be NULL!");
		return NULL;
	}
	if (strcmp(name,PROPERTY_XMLDECL_VERSION)==0) {
		return xmlDeclVersion;
	} else if (strcmp(name,PROPERTY_XMLDECL_STANDALONE)==0) {
		return NULL;  //Note: xmlDeclStandalone;
	} else if (strcmp(name,PROPERTY_XMLDECL_CONTENT)==0) {
		return xmlDeclContent;
	}
	return NULL;
}


void reset() 
{ int i;

 elRawNameLine=(int*)malloc(sizeof(int)*4);
 elNamespaceCount=(int*)malloc(sizeof(int)*4);
  elRawNameEnd=(int*)malloc(sizeof(int)*4);
 elRawName=(char*)malloc(sizeof(char*)*5);
 elName=(char*)malloc(sizeof(char*)*5);
 elPrefix=(char*)malloc(sizeof(char*)*5);
 elUri=(char*)malloc(sizeof(char*)*5);
 namespacePrefix=(char*)malloc(sizeof(char*)*5);
 namespaceUri=(char*)malloc(sizeof(char*)*5);
 attributeName=(char*)malloc(sizeof(char*)*5);
 attributePrefix=(char*)malloc(sizeof(char*)*5);
 attributeUri=(char*)malloc(sizeof(char*)*5);
 attributeValue=(char*)malloc(sizeof(char*)*5);
 entityName=(char*)malloc(sizeof(char*)*5);
entityReplacement=(char*)malloc(sizeof(char*)*5);
entityNameBuf=(char*)malloc(sizeof(char*)*5);
entityReplacementBuf=(char*)malloc(sizeof(char*)*5);
 for(i=0;i<4;i++) elNamespaceCount[i]=0;
  for(i=0;i<10;i++) elRawName[i]=NULL;
  elStackSize = 0;
	attrPosSize = 0;
	namespaceSize = 0;
	entitySize	  = 0;
	lineNumber = 1;
	columnNumber = 0;
	seenRoot = false;
	reachedEnd = false;
	eventType = START_DOCUMENT;
	emptyElementTag = false;

	level = 0;

	attributeCount = 0;

	namespaceEnd = 0;

	entityEnd = 0;

	reader_ptr= NULL;
	reader_size=0;
	inputEncoding = NULL;

	preventBufferCompaction = false;
	bufAbsoluteStart = 0;
	bufEnd = bufStart = 0;
	pos = posStart = posEnd = 0;

	pcEnd = pcStart = 0;

	usePC = false;

	seenStartTag = false;
	seenEndTag = false;
	pastEndTag = false;
	seenAmpersand = false;
	seenMarkup = false;
	seenDocdecl = false;

	xmlDeclVersion = NULL;
	xmlDeclStandalone = false;
	xmlDeclContent = NULL;
}




char* getInputEncoding() 
{
	return inputEncoding;
}

int getNamespaceCount(int depth_)
{
	if (!Namespacesprocess || depth_== 0) {
		return 0;
	}
	if (depth_< 0 || depth_> level)
	{
		fprintf(stderr,  "Namespace count may be for level 0..%d not %d", level, 
depth_);
		return 0;
	}
	return elNamespaceCount[depth_];
}

char* getNamespacePrefix(int pos_)
{
	if (pos_ < namespaceEnd) {
		return namespacePrefix[pos_];
	} else {
		fprintf(stderr,  "position %d exceeded number of available namespaces %d",
pos_, namespaceEnd);
		return NULL;
	}
}

char* getNamespaceUri(int pos_)
{
	if (pos_< namespaceEnd) {printf("namespaceUri[1]=%s",namespaceUri[1]);
		return namespaceUri[pos_];
	} else {
		fprintf(stderr, "position %d exceeded number of available namespaces %d",
pos_, namespaceEnd);
		return NULL;
	}
}

char* getNamespace_from_pre(char* prefix_)
{
	if (prefix_!= NULL) {
    int i;
		for (i = namespaceEnd - 1; i >= 0; i--) {
			if (strcmp(prefix_,namespacePrefix[i])==0) {
				return namespaceUri[i];
			}
		}
		if (strcmp(prefix_,"xml")==0) {
			return XML_URI;
		} else if (strcmp(prefix_,"xmlns")==0) {
			return XMLNS_URI;
		}
	} else {
   int i;
		for (i = namespaceEnd ; i >= 0; i--) {
      //namespacePrefix[2]=NULL;
			if (namespacePrefix[i+1] == NULL) { 
        //printf("\n&&&namespaceUri[%d]=%s",i-1,namespaceUri[i-1]);
				return namespaceUri[i-1];
			}
		}
	}
	return NULL;
}

int   getLevel() {
	return level;
}

int getLineNumber() 
{
	return lineNumber;
}

int getColumnNumber() 
{
	return columnNumber;
}

int isWhitespace()
{ 
  int i;
	if (eventType == TEXT || eventType == CDSECT) {
		if (usePC) {      
			for (i = pcStart; i < pcEnd; i++) {
				if (!isS(pc[i])) return false;
			}
			return true;
		} else {
			for (i = posStart; i < posEnd; i++) {
				if (!isS(buf[i])) return false;
			}
			return true;
		}
	} else if (eventType == IGNORABLE_WHITESPACE) {
		return true;
	}
	fprintf(stderr,"no content available to check for whitespaces");
	return false;
}

char* getText() 
{
	if (eventType == START_DOCUMENT || eventType == END_DOCUMENT) {
		return NULL;
	} else if (eventType == ENTITY_REF) {
		return text;
	}
	if (text == NULL) {
		if (!usePC || eventType == START_TAG || eventType == END_TAG) {
      text=(char*)malloc(posEnd - posStart);
     stringcopy(buf, posStart,text,0, posEnd - posStart);		
		} else {
      text=(char*)malloc(posEnd - posStart);
      stringcopy(pc, pcStart,text,0,pcEnd - pcStart);		
		}
	}
	return text;
}

char* getTextCharacters(int* holderForStartAndLength) 
{
	if (eventType == TEXT) {
		if (usePC) {
			holderForStartAndLength[0] = pcStart;
			holderForStartAndLength[1] = pcEnd - pcStart;
			return pc;
		} else {
			holderForStartAndLength[0] = posStart;
			holderForStartAndLength[1] = posEnd - posStart;
			return buf;
		}
	} else if (eventType == START_TAG
			|| eventType == END_TAG
			|| eventType == CDSECT
			|| eventType == COMMENT
			|| eventType == ENTITY_REF
			|| eventType == PROCESSING_INSTRUCTION
			|| eventType == IGNORABLE_WHITESPACE
			|| eventType == DOCDECL) {
		holderForStartAndLength[0] = posStart;
		holderForStartAndLength[1] = posEnd - posStart;
		return buf;
	} else if (eventType == START_DOCUMENT
		|| eventType == END_DOCUMENT) {
		holderForStartAndLength[0] = holderForStartAndLength[1] = -1;
		return NULL;
	} else {
		fprintf(stderr,"unknown text eventType: %d", eventType);
		return NULL;
	}
}

char* getNamespace() 
{
	if ((eventType == START_TAG)|| (eventType == END_TAG)) {
    
		return Namespacesprocess ? elUri[level] : NO_NAMESPACE;
	} 
 // if ((eventType == START_TAG)&&*namespacePrefix[level-1]=="\0"){
   // return namespaceUri[level-1];}
	return NULL;
}

char* getName() 
{
	if ((eventType == START_TAG)||(eventType == END_TAG)) {
		//printf("\n ^^elName[level]=%s",elName[level]);
    return elName[level];
    
	} else if (eventType == ENTITY_REF) {
		if (entityRefName == NULL) {
      entityRefName=(char*)malloc(posEnd - posStart);
      stringcopy(buf, posStart,entityRefName,0, posEnd - posStart);			
		}
		return entityRefName;
	} else {
    //printf("\n NULL elName[level]=%s",elName[level]);
		return NULL;
	}
}

char* getPrefix() 
{
	if ((eventType == START_TAG)||(eventType == END_TAG)){
 		return elPrefix[level];
	} 
	return NULL;
}


int isEmptyElementTag()
{
	if (eventType != START_TAG){
fprintf(stderr,"parser must be on START_TAG to check for empty element,but %s", TYPES[eventType]);
	       return false;
	      }
	return emptyElementTag;
}

int getAttributeCount() 
{
	if (eventType != START_TAG) return -1;
	return attributeCount;
}

char* getAttributeNamespace(int index) 
{
	if (eventType != START_TAG){
		fprintf(stderr,"only START_TAG can have attributes");
		return NULL;
	      }
	   
	if (!Namespacesprocess) return NO_NAMESPACE;
  
	checkOutOfBound(index, attributeCount);
	return attributeUri[index];
}

char* getAttributeName(int index) 
{
	if (eventType != START_TAG){
		fprintf(stderr,"only START_TAG can have attributes");
		return NULL;
		}
	checkOutOfBound(index, attributeCount);
	return attributeName[index];
}

char* getAttributePrefix(int index) 
{
	if (eventType != START_TAG){
		fprintf(stderr,"only START_TAG can have attributes");
		return NULL;
		}
	if (!Namespacesprocess) return NULL;
	checkOutOfBound(index, attributeCount);
	return attributePrefix[index];
}

char* getAttributeType(int index) 
{
	if (eventType != START_TAG){
		fprintf(stderr,"only START_TAG can have attributes");
		return NULL;
		}
	checkOutOfBound(index, attributeCount);
	return AttributeType;
}

int isAttributeDefault(int index) 
{
	if (eventType != START_TAG){
		fprintf(stderr,"only START_TAG can have attributes");
		return false;
		}
	checkOutOfBound(index, attributeCount);
	return false;
}

char* getAttributeValue(int index) 
{
	if (eventType != START_TAG){
		fprintf(stderr,	"only START_TAG can have attributes");
		return NULL;
		}
	checkOutOfBound(index, attributeCount);
	return attributeValue[index];
}

char* getAttrVal_from_name(char* ns, char* name) 
{
   int i;
	if (eventType != START_TAG)
	{
		fprintf(stderr, "only START_TAG can have attributes ");
		return NULL;
	}
	if (name == NULL) {
		fprintf(stderr,"attribute name can not be NULL");
		return NULL;
	}	
	if (Namespacesprocess) {
		if (ns == NULL) {
			ns = emptyString;
		}
		for (i = 0; i < attributeCount; ++i) {
			if ((strcmp(ns,attributeUri[i])==0) && (strcmp(name,attributeName[i])==0)) {
				return attributeValue[i];
			}
		}
	} else {
		if (ns != NULL && strlen(ns)== 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -