From b4562b0393977f9641788af10501e63369fcc006 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 27 Nov 2009 15:39:07 +0000 Subject: [PATCH] From Jean-Sebastien Guay, Explanation: Currently osg2cpp removes "\n" line endings to replace them with a textual equivalent ("\\n") in order for the string representing the shader to contain line endings in the string. But if the file that was read contained Windows line endings ("\r\n"), the resulting file looked really weird (the \r were left there and editors interpreted that as an additional newline). Also, I can imagine that if the shader file that was read had Mac line endings ("\r") then the output shader would all end up in one long line since there are no "\n"... What I've done: I've added a search and replace of "\r\n" to "\n", and then "\r" to "\n" (note that the order is important). I've also changed the filename handling so that the output file will be put in the same directory as the input file in case it was specified with a path. Previous functionality is retained for files specified with the filename only."" --- examples/osg2cpp/osg2cpp.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/osg2cpp/osg2cpp.cpp b/examples/osg2cpp/osg2cpp.cpp index d7d4e7d3f..9cb4592c0 100644 --- a/examples/osg2cpp/osg2cpp.cpp +++ b/examples/osg2cpp/osg2cpp.cpp @@ -7,6 +7,16 @@ #include +// Search in str for all occurences of spat and replace them with rpat. +void searchAndReplace(std::string& str, const std::string& spat, const std::string& rpat) +{ + std::string::size_type pos = 0; + while ((pos = str.find(spat, pos)) != std::string::npos) + { + str.replace(pos, spat.length(), rpat); + } +} + void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName) { osgDB::ofstream fout(cppFileName.c_str()); @@ -16,6 +26,8 @@ void writeShader(osg::Shader* shader, const std::string& cppFileName, const std: } std::string shaderSource = shader->getShaderSource(); + searchAndReplace(shaderSource, "\r\n", "\n"); + searchAndReplace(shaderSource, "\r", "\n"); std::string variableString = std::string("char ")+variableName+std::string("[] = "); @@ -71,6 +83,7 @@ int main( int argc, char **argv ) if (shader.valid()) { std::string name = osgDB::getStrippedName(filename); + std::string path = osgDB::getFilePath(filename); std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\""; std::string numbericCharacters = "0123456789"; std::string::size_type pos = name.find_first_of(invalidCharacters); @@ -81,7 +94,7 @@ int main( int argc, char **argv ) } std::string ext = osgDB::getFileExtension(filename); - std::string cppFileName = name + "_" + ext + ".cpp"; + std::string cppFileName = osgDB::concatPaths(path, name + "_" + ext + ".cpp"); std::string variableName = name + "_" + ext; writeShader(shader.get(), cppFileName, variableName);