Changeset 11

Show
Ignore:
Timestamp:
04/23/08 13:20:39
Author:
robert
Message:

Introduced support for parsing ${env_var} as part of path setup.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/ReadShowFile.cpp

    r1 r11  
    2121#include <osgDB/ReadFile> 
    2222#include <osgDB/FileNameUtils> 
     23#include <osgDB/FileUtils> 
     24 
     25#include <libxml/xmlmemory.h> 
     26#include <libxml/parser.h> 
    2327 
    2428bool p3d::getFileNames(osg::ArgumentParser& arguments, FileNameList& xmlFiles, FileNameList& normalFiles) 
     
    4246    return (!xmlFiles.empty() || !normalFiles.empty()); 
    4347}    
     48 
     49bool p3d::readEnvVars(osg::ArgumentParser& arguments) 
     50{ 
     51    bool readVars = false; 
     52 
     53    for(unsigned int i=1; i<arguments.argc(); ++i) 
     54    { 
     55        if (!arguments.isOption(i)) 
     56        { 
     57            std::string ext = osgDB::getLowerCaseFileExtension(arguments[i]); 
     58            if (ext=="xml" || ext=="p3d") 
     59            { 
     60                std::string file = osgDB::findDataFile(arguments[i]); 
     61                if (!file.empty()) 
     62                { 
     63                    if (p3d::readEnvVars(file)) readVars = true; 
     64                } 
     65            } 
     66        } 
     67    } 
     68     
     69    return readVars; 
     70} 
     71 
     72bool p3d::readEnvVars(const std::string& fileName) 
     73{ 
     74    std::string ext = osgDB::getFileExtension(fileName); 
     75    if (!osgDB::equalCaseInsensitive(ext,"xml") &&  
     76        !osgDB::equalCaseInsensitive(ext,"p3d")) return false; 
     77         
     78         
     79    xmlDocPtr doc; 
     80    xmlNodePtr cur; 
     81 
     82    doc = xmlParseFile(fileName.c_str()); 
     83 
     84    if (doc == NULL ) 
     85    { 
     86            fprintf(stderr,"Document not parsed successfully. \n"); 
     87            return false; 
     88    } 
     89 
     90     cur = xmlDocGetRootElement(doc); 
     91 
     92     if (cur == NULL) 
     93     { 
     94            fprintf(stderr,"empty document\n"); 
     95            xmlFreeDoc(doc); 
     96            return false; 
     97    } 
     98 
     99     if (xmlStrcmp(cur->name, (const xmlChar *) "presentation")) 
     100     { 
     101            fprintf(stderr,"document of the wrong type, root node != presentation"); 
     102            xmlFreeDoc(doc); 
     103            return false; 
     104    } 
     105     
     106    bool readVars = false; 
     107     
     108    xmlChar *key; 
     109    cur = cur->xmlChildrenNode; 
     110    while (cur != NULL) { 
     111 
     112        if ((!xmlStrcmp(cur->name, (const xmlChar *)"env"))) 
     113        { 
     114            key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 
     115            if (key) 
     116            { 
     117                char* str = strdup((char*)key); 
     118                osg::notify(osg::INFO)<<"putenv("<<str<<")"<<std::endl; 
     119                putenv(str); 
     120                readVars = true; 
     121            } 
     122            xmlFree(key); 
     123        } 
     124        cur = cur->next; 
     125    } 
     126 
     127    xmlFreeDoc(doc); 
     128     
     129    return readVars; 
     130} 
    44131 
    45132osg::Node* p3d::readHoldingSlide(const std::string& filename) 
  • trunk/src/ReadShowFile.h

    r1 r11  
    2323bool getFileNames(osg::ArgumentParser& arguments, FileNameList& xmlFiles, FileNameList& normalFiles); 
    2424 
     25bool readEnvVars(osg::ArgumentParser& arguments); 
     26 
     27bool readEnvVars(const std::string& filename); 
     28 
    2529osg::Node* readHoldingSlide(const std::string& filename); 
    2630 
  • trunk/src/ReaderWriterXML.cpp

    r1 r11  
    180180    typedef std::map<std::string, unsigned int> StringKeyMap; 
    181181 
     182    std::string expandEnvVarsInFileName(const std::string& filename) const;  
     183 
     184 
    182185    ColorMap            _colorMap; 
    183186    LayoutMap           _layoutMap; 
     
    196199// Register with Registry to instantiate the above reader/writer. 
    197200osgDB::RegisterReaderWriterProxy<ReaderWriterP3DXML> g_readerWriter_P3DXML_Proxy; 
     201 
     202std::string ReaderWriterP3DXML::expandEnvVarsInFileName(const std::string& filename) const 
     203{ 
     204    std::string argument(filename); 
     205    std::string::size_type start_pos = argument.find("${"); 
     206     
     207    while (start_pos != std::string::npos) 
     208    {         
     209        std::string::size_type end_pos = argument.find("}",start_pos); 
     210        if (start_pos != std::string::npos) 
     211        { 
     212            std::string var = argument.substr(start_pos+2, end_pos-start_pos-2); 
     213            const char* str = getenv(var.c_str()); 
     214 
     215            std::cout<<"for argument = ["<<argument<<"]"<<std::endl; 
     216 
     217            std::cout<<"Found ["<<var<<"] = "<<str<<std::endl; 
     218            argument.erase(start_pos, end_pos-start_pos+1); 
     219            argument.insert(start_pos, str); 
     220 
     221            std::cout<<"new argument = ["<<argument<<"]"<<std::endl; 
     222 
     223            start_pos = argument.find("${",start_pos+strlen(str)); 
     224        } 
     225        else 
     226        { 
     227            start_pos = std::string::npos; 
     228        } 
     229         
     230    } 
     231     
     232    return argument; 
     233} 
    198234 
    199235bool ReaderWriterP3DXML::read(const char* str, float& value) const 
     
    539575        value.absolute_path = false; 
    540576        value.inverse_path = false; 
    541         value.path = str
     577        value.path = expandEnvVarsInFileName(str)
    542578 
    543579        osg::notify(_notifyLevel)<<"path read "<<str<<std::endl; 
     
    549585        value.absolute_path = true; 
    550586        value.inverse_path = true; 
    551         value.path = str
     587        value.path = expandEnvVarsInFileName(str)
    552588 
    553589        osg::notify(_notifyLevel)<<"camera path read "<<str<<std::endl; 
     
    12441280    while (cur != NULL) { 
    12451281 
     1282/* 
     1283        if ((!xmlStrcmp(cur->name, (const xmlChar *)"env"))) 
     1284        { 
     1285            key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1); 
     1286            if (key) 
     1287            { 
     1288                osg::notify(osg::NOTICE)<<"putenv("<<(char*)key<<")"<<std::endl; 
     1289                putenv((char*)key); 
     1290            } 
     1291            xmlFree(key); 
     1292        } 
     1293*/ 
    12461294        if ((!xmlStrcmp(cur->name, (const xmlChar *)"name"))) 
    12471295        { 
     
    12631311            { 
    12641312                osg::notify(osg::INFO)<<"Appending search path "<<(char*)key<<std::endl; 
    1265                 osgDB::getDataFilePathList().push_front(std::string((char*)key)); 
     1313                osgDB::getDataFilePathList().push_front(expandEnvVarsInFileName(std::string((char*)key))); 
    12661314            } 
    12671315            xmlFree(key); 
  • trunk/src/present3D.cpp

    r9 r11  
    166166int main( int argc, char **argv ) 
    167167{ 
    168  
    169168    // use an ArgumentParser object to manage the program arguments. 
    170169    osg::ArgumentParser arguments(&argc,argv); 
     
    199198    } 
    200199 
     200 
     201    // read any env vars from presentations before we create viewer to make sure the viewer 
     202    // utilises these env vars 
     203    if (p3d::readEnvVars(arguments)) 
     204    { 
     205        osg::DisplaySettings::instance()->readEnvironmentalVariables(); 
     206    } 
     207 
     208 
    201209#ifdef USE_SDL 
    202210    SDLIntegration sdlIntegration;