Show
Ignore:
Timestamp:
11/27/09 16:39:07 (5 years ago)
Author:
robert
Message:

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.""

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osg2cpp/osg2cpp.cpp

    r9692 r10840  
    77 
    88#include <iostream> 
     9 
     10// Search in str for all occurences of spat and replace them with rpat. 
     11void searchAndReplace(std::string& str, const std::string& spat, const std::string& rpat) 
     12{ 
     13    std::string::size_type pos = 0; 
     14    while ((pos = str.find(spat, pos)) != std::string::npos) 
     15    { 
     16        str.replace(pos, spat.length(), rpat); 
     17    } 
     18} 
    919 
    1020void writeShader(osg::Shader* shader, const std::string& cppFileName, const std::string& variableName) 
     
    1727 
    1828    std::string shaderSource = shader->getShaderSource(); 
     29    searchAndReplace(shaderSource, "\r\n", "\n"); 
     30    searchAndReplace(shaderSource, "\r", "\n"); 
    1931 
    2032    std::string variableString = std::string("char ")+variableName+std::string("[] = "); 
     
    7284        { 
    7385            std::string name = osgDB::getStrippedName(filename); 
     86            std::string path = osgDB::getFilePath(filename); 
    7487            std::string invalidCharacters = "-+/\\*=(){}[]:;<>,.?@'~#`!\""; 
    7588            std::string numbericCharacters = "0123456789"; 
     
    8295             
    8396            std::string ext = osgDB::getFileExtension(filename); 
    84             std::string cppFileName = name + "_" + ext + ".cpp"; 
     97            std::string cppFileName = osgDB::concatPaths(path, name + "_" + ext + ".cpp"); 
    8598            std::string variableName = name + "_" + ext; 
    8699            writeShader(shader.get(), cppFileName, variableName);