示例#1
0
文件: SML.cpp 项目: baritone/openMVS
bool SML::ParseValues(MemFile& valuesMemFile)
{
	if (valuesMemFile.getSize() == 0)
		return true;
	// loop through all name/value pairs
	MemFile memLine;
	TokenIStream filterLine(&valuesMemFile);
	filterLine.setTrimTokens(SML_TOKEN_IGNORECHARS);
	filterLine.setToken(SML_TOKEN_VALUETOKEN);
	MemFile memValue;
	TokenIStream filterValue(&memLine);
	filterValue.setTrimTokens(SML_TOKEN_IGNORECHARS);
	filterValue.setToken(SML_TOKEN_NAMETOKEN);
	do {
		// parse value name and value
		filterLine.readLine(memLine);
		filterLine.trimBackLine(memLine);
		filterLine.trimFrontLine(memLine);
		if (memLine.isEOF())
			continue; // empty line, return
		// parse value name
		const size_t lenNameValueReal = (size_t)memLine.getSizeLeft();
		const size_t lenName = filterValue.readLine(memValue);
		#if SML_AUTOVALUES == SML_AUTOVALUES_ON
		String szName;
		if (filterValue.trimBackLine(memValue) == lenName || lenNameValueReal == lenName) {
			// no name found, auto generate the name
			szName = _T("Item") + String::ToString(size());
		} else {
			// read the name
			szName = (LPCTSTR)memValue.getData();
			memValue.setSize(0);
		}
		#else
		filterValue.trimBackLine(memValue);
		String szName = (LPCTSTR)memValue.getData();
		ASSERT(!filterValue.isEOS() && !szName.IsEmpty());
		if (filterValue.isEOS() || szName.IsEmpty()) {
			memValue.setSize(0);
			memLine.setSize(0);
			filterValue.setPos(0);
			continue; // Parse Error: invalid syntax ('=' not found)
		}
		ASSERT(szName.size() == memValue.getSizeLeft());
		memValue.setSize(0);
		#endif
		SMLVALUE& val = operator[](szName);
		// parse value
		filterValue.read(memValue);
		LPCTSTR szValue = filterValue.trimFrontLine(memValue);
		val.val = szValue;
		ASSERT(_tcslen(szValue) == memValue.getSizeLeft());
		memValue.setSize(0);
		memLine.setSize(0);
		filterValue.setPos(0);
	} while (!filterLine.isEOS());
	// all file processed, safe to reset it to 0
	valuesMemFile.setSize(0);
	return true;
}
示例#2
0
文件: SML.cpp 项目: baritone/openMVS
bool SML::ParseSection(TokenIStream& filter, MemFile& memFile)
{
	while (true) {
		// find first section start (NameOpenBracket)
		filter.setToken(SML_TOKEN_NAMEOPENBRACKET);
		filter.readLine(memFile);

		// parse values before the new section or end of section
		const size_f_t posMemFile = memFile.getPos();
		MemFile valuesMemFile;
		TokenIStream sectionFilter(&memFile, SML_TOKEN_SECTIONCLOSEBRACKET);
		const size_t lenValues = sectionFilter.readLine(valuesMemFile);
		if (ParseValues(valuesMemFile) == false)
			return false; // Parse Error: invalid values
		ASSERT(valuesMemFile.getSize() == 0);

		// if end of section found, return
		if (!sectionFilter.isEOS()) {
			// if a name open bracket was found before the EOS,
			// then restore it for the parent next ParseSection()
			memFile.setPos(posMemFile+(lenValues+1)*sizeof(TCHAR));
			if (!filter.isEOS())
				filter.restoreToken();
			break;
		}
		else {
			ASSERT(memFile.getSize()-posMemFile == lenValues*sizeof(TCHAR));
			ASSERT(memFile.getSize()-posMemFile == memFile.getSize() ||
				memFile.getSize()-posMemFile == memFile.getSizeLeft() ||
				memFile.getSizeLeft() == 0);
			memFile.setSize(0);
		}

		// if no more data, return
		if (filter.isEOS())
			break;

		// parse child section name
		filter.setToken(SML_TOKEN_NAMECLOSEBRACKET);
		const size_t lenName = filter.readLine(memFile);
		ASSERT(!filter.isEOS());
		if (lenName == 0)
			return false; // Parse Error: invalid section name
		const String strChildName((LPCTSTR)memFile.getData(), (UINT)lenName);
		memFile.growSize(-((size_f_t)(lenName*sizeof(TCHAR))));
		ASSERT(memFile.getSize() == 0);
		// create the child with the given name
		const IDX idxChild = CreateChildUnique(strChildName);
		LPSML const pChild = m_arrChildren[idxChild];
		pChild->SetFncItem(m_fncInitItem, m_fncSaveItem, m_fncReleaseItem, m_fncItemData);

		// parse child section
		filter.setToken(SML_TOKEN_SECTIONOPENBRACKET);
		filter.readLine(memFile);
		filter.trimBackLine(memFile);
		ASSERT(memFile.getSize() == 0);
		if (pChild->ParseSection(filter, memFile) == false)
			return false;
	}
	return true;
}