Changeset 11
- Timestamp:
- 04/23/08 13:20:39
- Files:
-
- trunk/src/ReadShowFile.cpp (modified) (2 diffs)
- trunk/src/ReadShowFile.h (modified) (1 diff)
- trunk/src/ReaderWriterXML.cpp (modified) (6 diffs)
- trunk/src/present3D.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/ReadShowFile.cpp
r1 r11 21 21 #include <osgDB/ReadFile> 22 22 #include <osgDB/FileNameUtils> 23 #include <osgDB/FileUtils> 24 25 #include <libxml/xmlmemory.h> 26 #include <libxml/parser.h> 23 27 24 28 bool p3d::getFileNames(osg::ArgumentParser& arguments, FileNameList& xmlFiles, FileNameList& normalFiles) … … 42 46 return (!xmlFiles.empty() || !normalFiles.empty()); 43 47 } 48 49 bool 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 72 bool 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 } 44 131 45 132 osg::Node* p3d::readHoldingSlide(const std::string& filename) trunk/src/ReadShowFile.h
r1 r11 23 23 bool getFileNames(osg::ArgumentParser& arguments, FileNameList& xmlFiles, FileNameList& normalFiles); 24 24 25 bool readEnvVars(osg::ArgumentParser& arguments); 26 27 bool readEnvVars(const std::string& filename); 28 25 29 osg::Node* readHoldingSlide(const std::string& filename); 26 30 trunk/src/ReaderWriterXML.cpp
r1 r11 180 180 typedef std::map<std::string, unsigned int> StringKeyMap; 181 181 182 std::string expandEnvVarsInFileName(const std::string& filename) const; 183 184 182 185 ColorMap _colorMap; 183 186 LayoutMap _layoutMap; … … 196 199 // Register with Registry to instantiate the above reader/writer. 197 200 osgDB::RegisterReaderWriterProxy<ReaderWriterP3DXML> g_readerWriter_P3DXML_Proxy; 201 202 std::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 } 198 234 199 235 bool ReaderWriterP3DXML::read(const char* str, float& value) const … … 539 575 value.absolute_path = false; 540 576 value.inverse_path = false; 541 value.path = str;577 value.path = expandEnvVarsInFileName(str); 542 578 543 579 osg::notify(_notifyLevel)<<"path read "<<str<<std::endl; … … 549 585 value.absolute_path = true; 550 586 value.inverse_path = true; 551 value.path = str;587 value.path = expandEnvVarsInFileName(str); 552 588 553 589 osg::notify(_notifyLevel)<<"camera path read "<<str<<std::endl; … … 1244 1280 while (cur != NULL) { 1245 1281 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 */ 1246 1294 if ((!xmlStrcmp(cur->name, (const xmlChar *)"name"))) 1247 1295 { … … 1263 1311 { 1264 1312 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))); 1266 1314 } 1267 1315 xmlFree(key); trunk/src/present3D.cpp
r9 r11 166 166 int main( int argc, char **argv ) 167 167 { 168 169 168 // use an ArgumentParser object to manage the program arguments. 170 169 osg::ArgumentParser arguments(&argc,argv); … … 199 198 } 200 199 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 201 209 #ifdef USE_SDL 202 210 SDLIntegration sdlIntegration;
