コード例 #1
0
ファイル: stats.c プロジェクト: CoolBalance/vtd-xml
int main(){

	exception e;
	FILE *f = NULL;
	int i = 0,count=0,par_count=0,v=0;
	
	char* filename = "./bioinfo.xml";
	struct stat s;
	UByte *xml = NULL; // this is the buffer containing the XML content, UByte means unsigned byte
	VTDGen *vg = NULL; // This is the VTDGen that parses XML
	VTDNav *vn = NULL; // This is the VTDNav that navigates the VTD records
	AutoPilot *ap = NULL;

	// allocate a piece of buffer then reads in the document content
	// assume "c:\soap2.xml" is the name of the file
	f = fopen(filename,"r");

	stat(filename,&s);

	i = (int) s.st_size;	
	wprintf(L"size of the file is %d \n",i);

	xml = (UByte *)malloc(sizeof(UByte) *i);
	i = fread(xml,sizeof(UByte),i,f);
	Try{
		vg = createVTDGen();
		setDoc(vg,xml,i);
		parse(vg,TRUE);
		vn = getNav(vg);
		ap = createAutoPilot2();
		bind(ap,vn);
		if (selectXPath(ap,L"/bix/package/command/parlist")){
			while(evalXPath(ap)!= -1){
				count++;
			}
		}
		
		if (selectXPath(ap,L"/bix/package/command/parlist/par")){
			while(evalXPath(ap)!= -1){
				par_count++;
			}
		}
		wprintf(L"count ==> %d \n",count);
		wprintf(L"par_count ==> %d \n",par_count);

		toElement(vn,ROOT);
		selectElement(ap,L"par");
		while(iterateAP(ap)){
			if (getCurrentDepth(vn) == 4){
				v++;
			}
		}
		wprintf(L"verify ==> %d \n",v);
		fclose(f);
		// remember C has no automatic garbage collector
		// needs to deallocate manually.
		freeVTDNav(vn);
		freeVTDGen(vg);
		freeAutoPilot(ap);

	}
	Catch (e) {
		if (e.et == parse_exception)
			printf("parse exception e ==> %s \n %s\n", e.msg, e.sub_msg);	
		// manual garbage collection here
		freeVTDGen(vg);
	}
  return 0;
}	
コード例 #2
0
ファイル: DataTree.cpp プロジェクト: bsy6766/SimpleDataTree
bool DataTree::parseData(const std::string& fileData, int& curIndex, int curDepth)
{
	// Success flag
	bool success = true;

	// Return if current index is bigger than file size
	if (curIndex >= fileData.size()) { return true; }

	// Get index where next line starts
	int nextLineIndex = this->findNewLineIndex(fileData, curIndex) + 1;

	// Return if next line index is current index
	if (nextLineIndex == curIndex){ return true; }

	// Find hwere comment index starts
	int commentIndex = findCommentSymbolOrNewLineIndex(fileData, curIndex);
	// Find the first index where non whitespace starts
	int nonWhiteSpaceIndex = findFirstNonWhitespaceIndex(fileData, curIndex);

	// If comment index is before the first non whitesapce character, the entire line is comment
	if (commentIndex <= nonWhiteSpaceIndex)
	{
		// Skip to next line
		curIndex = nextLineIndex;
		return parseData(fileData, curIndex, curDepth);
	}
	
	// If current line isn't comment, parse it.
	int parsingDepth = getCurrentDepth(fileData, curIndex);
	// Get the size of data
	const int size = fileData.size();

	//Key and Value
	std::string newKey;
	std::string newValue;

	// Check depth
	if (parsingDepth > curDepth)
	{
		// Parsing dpeth is deeper than current depth. So advance.
		curDepth = parsingDepth;

		// Run while recursion ends with same depth
		while (curDepth <= parsingDepth)
		{
			// Get index where next line starts
			nextLineIndex = findNewLineIndex(fileData, curIndex) + 1;
			// Parse the key
			newKey = parseKey(fileData, size, parsingDepth + curIndex, nextLineIndex, curIndex);

			// Only if key is valid
			if (newKey.size() > 0 && newKey.empty() == false)
			{
				// Parse value
				newValue = parseValue(fileData, size, curIndex);
			}

			// Add new key and value to children.
			this->children[newKey] = new DataTree(newKey, newValue);
			// Advance current index to next line
			curIndex = nextLineIndex;
			// Recursively parse data on new child
			success = this->children[newKey]->parseData(fileData, curIndex, parsingDepth);

			// Update parsing depth
			parsingDepth = getCurrentDepth(fileData, curIndex);
		}
	}

	// Really no point of using bool as return. Used for debugging.
	return success;
}