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.""
This commit is contained in:
Robert Osfield 2009-11-27 15:39:07 +00:00
parent 6ff21888f3
commit b4562b0393

View File

@ -7,6 +7,16 @@
#include <iostream> #include <iostream>
// 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) void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName)
{ {
osgDB::ofstream fout(cppFileName.c_str()); 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(); std::string shaderSource = shader->getShaderSource();
searchAndReplace(shaderSource, "\r\n", "\n");
searchAndReplace(shaderSource, "\r", "\n");
std::string variableString = std::string("char ")+variableName+std::string("[] = "); std::string variableString = std::string("char ")+variableName+std::string("[] = ");
@ -71,6 +83,7 @@ int main( int argc, char **argv )
if (shader.valid()) if (shader.valid())
{ {
std::string name = osgDB::getStrippedName(filename); std::string name = osgDB::getStrippedName(filename);
std::string path = osgDB::getFilePath(filename);
std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\""; std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\"";
std::string numbericCharacters = "0123456789"; std::string numbericCharacters = "0123456789";
std::string::size_type pos = name.find_first_of(invalidCharacters); 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 ext = osgDB::getFileExtension(filename);
std::string cppFileName = name + "_" + ext + ".cpp"; std::string cppFileName = osgDB::concatPaths(path, name + "_" + ext + ".cpp");
std::string variableName = name + "_" + ext; std::string variableName = name + "_" + ext;
writeShader(shader.get(), cppFileName, variableName); writeShader(shader.get(), cppFileName, variableName);