Updated the handling of string in the .osg reading and writing so that it

handles the use of " quotes inside the string.
This commit is contained in:
Robert Osfield
2002-10-28 12:55:19 +00:00
parent 91c73c6da1
commit 32c6a7809a
7 changed files with 76 additions and 15 deletions

View File

@@ -34,6 +34,9 @@ class OSGDB_EXPORT Output : public std::ofstream
Output& indent();
/** wrap a string with "" quotes and use \" for any internal quotes.*/
std::string wrapString(const std::string& str);
inline void setIndentStep(int step) { _indentStep = step; }
inline int getIndentStep() const { return _indentStep; }

View File

@@ -162,6 +162,7 @@ bool FieldReader::_readField(Field* fieldPtr)
}
_fin->ignore(1);
char c;
bool escape = false; // use the escape character sequence \" to allow " to included in strings.
while (true)
{
ch = _fin->peek();
@@ -170,15 +171,34 @@ bool FieldReader::_readField(Field* fieldPtr)
_eof = true;
return fieldPtr && fieldPtr->getNoCharacters()!=0;
}
c = ch;
if (ch=='"')
{
c = ch;
if (ch=='\\' && !escape)
{
escape = true;
_fin->ignore(1);
//return fieldPtr && fieldPtr->getNoCharacters()!=0;
return fieldPtr!=NULL;
}
else if (ch=='"')
{
if (escape)
{
escape = false;
_fin->get(c);
if (fieldPtr) fieldPtr->addChar(c);
}
else
{
_fin->ignore(1);
//return fieldPtr && fieldPtr->getNoCharacters()!=0;
return fieldPtr!=NULL;
}
}
else
{
if (escape)
{
escape = false;
if (fieldPtr) fieldPtr->addChar('\\');
}
_fin->get(c);
if (fieldPtr) fieldPtr->addChar(c);
}
@@ -193,6 +213,7 @@ bool FieldReader::_readField(Field* fieldPtr)
}
_fin->ignore(1);
char c;
bool escape = false; // use the escape character sequence \' to allow ' to included in strings.
while (true)
{
ch = _fin->peek();
@@ -202,14 +223,33 @@ bool FieldReader::_readField(Field* fieldPtr)
return fieldPtr && fieldPtr->getNoCharacters()!=0;
}
c = ch;
if (ch=='\'')
{
if (ch=='\\' && !escape)
{
escape = true;
_fin->ignore(1);
//return fieldPtr && fieldPtr->getNoCharacters()!=0;
return fieldPtr!=NULL;
}
else if (ch=='\'')
{
if (escape)
{
escape = false;
_fin->get(c);
if (fieldPtr) fieldPtr->addChar(c);
}
else
{
_fin->ignore(1);
//return fieldPtr && fieldPtr->getNoCharacters()!=0;
return fieldPtr!=NULL;
}
}
else
{
if (escape)
{
escape = false;
if (fieldPtr) fieldPtr->addChar('\\');
}
_fin->get(c);
if (fieldPtr) fieldPtr->addChar(c);
}

View File

@@ -62,6 +62,7 @@ Output& Output::indent()
}
void Output::moveIn()
{
_indent += _indentStep;
@@ -74,6 +75,23 @@ void Output::moveOut()
if (_indent<0) _indent=0;
}
std::string Output::wrapString(const std::string& str)
{
std::string newstring;
newstring.push_back('"');
for(unsigned int i=0;i<str.size();++i)
{
if (str[i]=='"')
{
newstring.push_back('\\');
newstring.push_back('"');
}
else newstring.push_back(str[i]);
}
newstring.push_back('"');
return newstring;
}
bool Output::writeObject(const osg::Object& obj)
{
return Registry::instance()->writeObject(obj,*this);

View File

@@ -103,7 +103,7 @@ bool Node_writeLocalData(const Object& obj, Output& fw)
{
const Node& node = static_cast<const Node&>(obj);
if (!node.getName().empty()) fw.indent() << "name "<<'"'<<node.getName()<<'"'<< std::endl;
if (!node.getName().empty()) fw.indent() << "name "<<fw.wrapString(node.getName())<< std::endl;
fw.indent() << "cullingActive ";
if (node.getCullingActive()) fw << "TRUE"<< std::endl;
@@ -126,7 +126,7 @@ bool Node_writeLocalData(const Object& obj, Output& fw)
{
if (node.getDescriptions().size()==1)
{
fw.indent() << "description "<<'"'<<node.getDescriptions().front()<<'"'<< std::endl;
fw.indent() << "description "<<fw.wrapString(node.getDescriptions().front())<< std::endl;
}
else
{
@@ -136,7 +136,7 @@ bool Node_writeLocalData(const Object& obj, Output& fw)
ditr!=node.getDescriptions().end();
++ditr)
{
fw.indent() << '"'<<*ditr<<'"'<< std::endl;
fw.indent() << fw.wrapString(*ditr)<< std::endl;
}
fw.moveOut();
fw.indent() << "}"<< std::endl;

View File

@@ -63,7 +63,7 @@ bool Texture1D_writeLocalData(const Object& obj, Output& fw)
if (texture.getImage() && !(texture.getImage()->getFileName().empty()))
{
fw.indent() << "file \""<<fw.getFileNameForOutput(texture.getImage()->getFileName())<<"\""<< std::endl;
fw.indent() << "file "<<fw.wrapString(fw.getFileNameForOutput(texture.getImage()->getFileName()))<< std::endl;
}
return true;

View File

@@ -72,7 +72,7 @@ bool Texture2D_writeLocalData(const Object& obj, Output& fw)
if (texture.getImage() && !(texture.getImage()->getFileName().empty()))
{
fw.indent() << "file \""<<fw.getFileNameForOutput(texture.getImage()->getFileName())<<"\""<< std::endl;
fw.indent() << "file "<<fw.wrapString(fw.getFileNameForOutput(texture.getImage()->getFileName()))<< std::endl;
}
return true;

View File

@@ -63,7 +63,7 @@ bool Texture3D_writeLocalData(const Object& obj, Output& fw)
if (texture.getImage() && !(texture.getImage()->getFileName().empty()))
{
fw.indent() << "file \""<<fw.getFileNameForOutput(texture.getImage()->getFileName())<<"\""<< std::endl;
fw.indent() << "file "<<fw.wrapString(fw.getFileNameForOutput(texture.getImage()->getFileName()))<< std::endl;
}
return true;