diff --git a/include/osgDB/Output b/include/osgDB/Output index f92e23305..0c43d22bc 100644 --- a/include/osgDB/Output +++ b/include/osgDB/Output @@ -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; } diff --git a/src/osgDB/FieldReader.cpp b/src/osgDB/FieldReader.cpp index cc7b3f428..77ae35f50 100644 --- a/src/osgDB/FieldReader.cpp +++ b/src/osgDB/FieldReader.cpp @@ -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); } diff --git a/src/osgDB/Output.cpp b/src/osgDB/Output.cpp index c494421c9..2667233be 100644 --- a/src/osgDB/Output.cpp +++ b/src/osgDB/Output.cpp @@ -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;iwriteObject(obj,*this); diff --git a/src/osgPlugins/osg/Node.cpp b/src/osgPlugins/osg/Node.cpp index 0db91a658..66e70d0db 100644 --- a/src/osgPlugins/osg/Node.cpp +++ b/src/osgPlugins/osg/Node.cpp @@ -103,7 +103,7 @@ bool Node_writeLocalData(const Object& obj, Output& fw) { const Node& node = static_cast(obj); - if (!node.getName().empty()) fw.indent() << "name "<<'"'<getFileName().empty())) { - fw.indent() << "file \""<getFileName())<<"\""<< std::endl; + fw.indent() << "file "<getFileName()))<< std::endl; } return true; diff --git a/src/osgPlugins/osg/Texture2D.cpp b/src/osgPlugins/osg/Texture2D.cpp index c0628b1f0..1794dbbcc 100644 --- a/src/osgPlugins/osg/Texture2D.cpp +++ b/src/osgPlugins/osg/Texture2D.cpp @@ -72,7 +72,7 @@ bool Texture2D_writeLocalData(const Object& obj, Output& fw) if (texture.getImage() && !(texture.getImage()->getFileName().empty())) { - fw.indent() << "file \""<getFileName())<<"\""<< std::endl; + fw.indent() << "file "<getFileName()))<< std::endl; } return true; diff --git a/src/osgPlugins/osg/Texture3D.cpp b/src/osgPlugins/osg/Texture3D.cpp index 5912d519e..b725d83f0 100644 --- a/src/osgPlugins/osg/Texture3D.cpp +++ b/src/osgPlugins/osg/Texture3D.cpp @@ -63,7 +63,7 @@ bool Texture3D_writeLocalData(const Object& obj, Output& fw) if (texture.getImage() && !(texture.getImage()->getFileName().empty())) { - fw.indent() << "file \""<getFileName())<<"\""<< std::endl; + fw.indent() << "file "<getFileName()))<< std::endl; } return true;