Added string to keyword map parser.

This commit is contained in:
Robert Osfield
2007-08-14 17:04:27 +00:00
parent 16d31fb85c
commit 17521f338a

View File

@@ -11,6 +11,8 @@
#include <osgDB/Input>
#include <osgDB/Output>
#include <osgTerrain/Terrain>
class ReaderWriterTerrain : public osgDB::ReaderWriter
{
public:
@@ -29,6 +31,22 @@ class ReaderWriterTerrain : public osgDB::ReaderWriter
virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* opt) const
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (osgDB::equalCaseInsensitive(ext,"terrain"))
{
KeywordValueMap keywordValueMap;
parseTerrainString(osgDB::getNameLessExtension(file), keywordValueMap);
for(KeywordValueMap::iterator itr = keywordValueMap.begin();
itr != keywordValueMap.end();
++itr)
{
osg::notify(osg::NOTICE)<<"["<<itr->first<<"] = "<<"["<<itr->second<<"]"<<std::endl;
}
return 0;
}
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile( file, opt );
@@ -90,6 +108,89 @@ class ReaderWriterTerrain : public osgDB::ReaderWriter
else return 0;
}
typedef std::map<std::string, std::string> KeywordValueMap;
bool parseTerrainString(const std::string& str, KeywordValueMap& keywordValueMap) const
{
bool success = false;
std::string::size_type pos = 0;
while(pos != std::string::npos)
{
pos = str.find_first_not_of(' ',pos);
if (pos == std::string::npos) break;
std::string::size_type semicolon = str.find_first_of(';', pos);
std::string::size_type startstatement = pos;
std::string::size_type endstatement = std::string::npos;
if (semicolon!=std::string::npos)
{
endstatement = semicolon-1;
pos = semicolon+1;
}
else
{
endstatement = str.length()-1;
pos = std::string::npos;
}
if (startstatement<endstatement) endstatement = str.find_last_not_of(' ',endstatement);
if (startstatement<=endstatement)
{
// osg::notify(osg::NOTICE)<<"statement = ["<<str.substr(startstatement, endstatement-startstatement+1)<<"]"<<std::endl;
std::string::size_type assignment = str.find_first_of('=', startstatement);
if (assignment!=std::string::npos && assignment>endstatement)
{
assignment = std::string::npos;
}
std::string::size_type startvariable = startstatement;
std::string::size_type endvariable = startstatement;
std::string::size_type startvalue = startstatement;
std::string::size_type endvalue = endstatement;
if (assignment!=std::string::npos)
{
endvariable = assignment-1;
startvalue = assignment+1;
if (startvariable<=endvariable)
{
endvariable = str.find_last_not_of(' ',endvariable);
}
if (startvariable<=endvariable)
{
++endvariable;
}
}
if (startvalue<=endvalue)
{
startvalue = str.find_first_not_of(' ',startvalue);
}
if (startvalue<=endvalue)
{
if (startvariable<endvariable)
{
keywordValueMap[str.substr(startvariable, endvariable-startvariable)] = str.substr(startvalue, endvalue-startvalue+1);
success = true;
}
else
{
keywordValueMap[""] = str.substr(startvalue, endvalue-startvalue+1);
success = true;
}
}
}
}
return success;
}
};
// now register with Registry to instantiate the above