- Timestamp:
- 03/21/12 18:36:20 (15 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/osg/XmlStreamOperator.h
r12808 r13041 19 19 TEXT_LINE // A text line, e.g. recording array elements 20 20 }; 21 21 22 22 XmlOutputIterator( std::ostream* ostream, int precision ) 23 23 : _readLineType(FIRST_LINE), _prevReadLineType(FIRST_LINE), _hasSubProperty(false) … … 28 28 _root->type = osgDB::XmlNode::GROUP; 29 29 } 30 30 31 31 virtual ~XmlOutputIterator() {} 32 32 33 33 virtual bool isBinary() const { return false; } 34 34 35 35 virtual void writeBool( bool b ) 36 36 { addToCurrentNode( b ? std::string("TRUE") : std::string("FALSE") ); } 37 37 38 38 virtual void writeChar( char c ) 39 39 { _sstream << (short)c; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 40 40 41 41 virtual void writeUChar( unsigned char c ) 42 42 { _sstream << (unsigned short)c; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 43 43 44 44 virtual void writeShort( short s ) 45 45 { _sstream << s; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 46 46 47 47 virtual void writeUShort( unsigned short s ) 48 48 { _sstream << s; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 49 49 50 50 virtual void writeInt( int i ) 51 51 { _sstream << i; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 52 52 53 53 virtual void writeUInt( unsigned int i ) 54 54 { _sstream << i; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 55 55 56 56 virtual void writeLong( long l ) 57 57 { _sstream << l; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 58 58 59 59 virtual void writeULong( unsigned long l ) 60 60 { _sstream << l; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 61 61 62 62 virtual void writeFloat( float f ) 63 63 { _sstream << f; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 64 64 65 65 virtual void writeDouble( double d ) 66 66 { _sstream << d; addToCurrentNode( _sstream.str() ); _sstream.str(""); } 67 67 68 68 virtual void writeString( const std::string& s ) 69 69 { addToCurrentNode( s, true ); } … … 90 90 else if ( _readLineType==TEXT_LINE ) 91 91 addToCurrentNode( fn ); 92 92 93 93 setLineType( NEW_LINE ); 94 94 } … … 96 96 addToCurrentNode( fn ); 97 97 } 98 98 99 99 virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) ) 100 100 { 101 101 _sstream << fn; 102 102 } 103 103 104 104 virtual void writeGLenum( const osgDB::ObjectGLenum& value ) 105 105 { 106 GLenum e = value.get(); 106 GLenum e = value.get(); 107 107 const std::string& enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString("GL", e); 108 108 addToCurrentNode( enumString, true ); 109 109 } 110 110 111 111 virtual void writeProperty( const osgDB::ObjectProperty& prop ) 112 112 { … … 137 137 } 138 138 } 139 139 140 140 virtual void writeMark( const osgDB::ObjectMark& mark ) 141 141 { … … 150 150 } 151 151 } 152 152 153 153 virtual void writeCharArray( const char* s, unsigned int size ) {} 154 154 155 155 virtual void writeWrappedString( const std::string& str ) 156 156 { … … 167 167 addToCurrentNode( realStr ); 168 168 } 169 169 170 170 virtual void flush() 171 171 { … … 175 175 xmlRoot->write( *_out ); 176 176 } 177 177 178 178 protected: 179 179 void addToCurrentNode( const std::string& str, bool isString=false ) … … 184 184 return; 185 185 } 186 186 187 187 if ( _readLineType==NEW_LINE ) 188 188 { … … 196 196 setLineType( TEXT_LINE ); 197 197 } 198 198 199 199 if ( _readLineType==TEXT_LINE ) 200 200 { … … 214 214 } 215 215 } 216 216 217 217 void addToCurrentNode( std::ostream& (*fn)(std::ostream&) ) 218 218 { … … 226 226 } 227 227 } 228 228 229 229 osgDB::XmlNode* pushNode( const std::string& name ) 230 230 { 231 231 osg::ref_ptr<osgDB::XmlNode> node = new osgDB::XmlNode; 232 232 node->type = osgDB::XmlNode::ATOM; 233 233 234 234 // Set element name without '#' and '::' characters 235 235 std::string realName; … … 239 239 { 240 240 realName = name; 241 241 242 242 std::string::size_type pos = realName.find("::"); 243 243 if ( pos!=std::string::npos ) … … 245 245 } 246 246 node->name = realName; 247 247 248 248 if ( _nodePath.size()>0 ) 249 249 { … … 253 253 else 254 254 _root->children.push_back(node); 255 255 256 256 _nodePath.push_back( node.get() ); 257 257 return node.get(); 258 258 } 259 259 260 260 osgDB::XmlNode* popNode() 261 261 { … … 270 270 return node; 271 271 } 272 272 273 273 void trimEndMarkers( osgDB::XmlNode* node, const std::string& name ) 274 274 { 275 275 osgDB::XmlNode::Properties::iterator itr = node->properties.find(name); 276 276 if ( itr==node->properties.end() ) return; 277 277 278 278 std::string& str = itr->second; 279 279 if ( !str.empty() ) … … 283 283 str.erase( end+1 ); 284 284 } 285 285 286 286 if ( str.empty() ) 287 287 node->properties.erase(itr); 288 288 } 289 289 290 290 void setLineType( ReadLineType type ) 291 291 { … … 293 293 _readLineType = type; 294 294 } 295 295 296 296 typedef std::vector<osgDB::XmlNode*> XmlNodePath; 297 297 XmlNodePath _nodePath; 298 298 299 299 osg::ref_ptr<osgDB::XmlNode> _root; 300 300 std::stringstream _sstream; 301 301 302 302 ReadLineType _readLineType; 303 303 ReadLineType _prevReadLineType; … … 312 312 _in = istream; 313 313 _root = osgDB::readXmlStream( *istream ); 314 314 315 315 if ( _root.valid() && _root->children.size()>0 ) 316 316 _nodePath.push_back( _root->children[0] ); 317 317 } 318 318 319 319 virtual ~XmlInputIterator() {} 320 320 321 321 virtual bool isBinary() const { return false; } 322 322 323 323 virtual void readBool( bool& b ) 324 324 { … … 328 328 else b = false; 329 329 } 330 330 331 331 virtual void readChar( char& c ) 332 332 { … … 335 335 c = (char)s; 336 336 } 337 337 338 338 virtual void readSChar( signed char& c ) 339 339 { … … 342 342 c = (signed char)s; 343 343 } 344 344 345 345 virtual void readUChar( unsigned char& c ) 346 346 { … … 349 349 c = (unsigned char)s; 350 350 } 351 351 352 352 virtual void readShort( short& s ) 353 353 { std::string str; if (prepareStream()) _sstream >> str; s = static_cast<short>(strtol(str.c_str(), NULL, 0)); } 354 354 355 355 virtual void readUShort( unsigned short& s ) 356 356 { std::string str; if (prepareStream()) _sstream >> str; s = static_cast<unsigned short>(strtoul(str.c_str(), NULL, 0)); } 357 357 358 358 virtual void readInt( int& i ) 359 359 { std::string str; if (prepareStream()) _sstream >> str; i = static_cast<int>(strtol(str.c_str(), NULL, 0)); } 360 360 361 361 virtual void readUInt( unsigned int& i ) 362 362 { std::string str; if (prepareStream()) _sstream >> str; i = static_cast<unsigned int>(strtoul(str.c_str(), NULL, 0)); } 363 363 364 364 virtual void readLong( long& l ) 365 365 { std::string str; if (prepareStream()) _sstream >> str; l = strtol(str.c_str(), NULL, 0); } 366 366 367 367 virtual void readULong( unsigned long& l ) 368 368 { std::string str; if (prepareStream()) _sstream >> str; l = strtoul(str.c_str(), NULL, 0); } 369 369 370 370 virtual void readFloat( float& f ) 371 371 { std::string str; if (prepareStream()) _sstream >> str; f = osg::asciiToFloat(str.c_str()); } 372 372 373 373 virtual void readDouble( double& d ) 374 374 { std::string str; if (prepareStream()) _sstream >> str; d = osg::asciiToDouble(str.c_str()); } 375 375 376 376 virtual void readString( std::string& s ) 377 377 { 378 378 if ( prepareStream() ) _sstream >> s; 379 379 380 380 // Replace '--' to '::' to get correct wrapper class 381 381 std::string::size_type pos = s.find("--"); … … 383 383 s.replace( pos, 2, "::" ); 384 384 } 385 385 386 386 virtual void readStream( std::istream& (*fn)(std::istream&) ) 387 387 { if ( prepareStream() ) _sstream >> fn; } 388 388 389 389 virtual void readBase( std::ios_base& (*fn)(std::ios_base&) ) 390 390 { _sstream >> fn; } 391 391 392 392 virtual void readGLenum( osgDB::ObjectGLenum& value ) 393 393 { … … 398 398 value.set( e ); 399 399 } 400 400 401 401 virtual void readProperty( osgDB::ObjectProperty& prop ) 402 402 { … … 414 414 if ( pos!=std::string::npos ) 415 415 enumString.replace( pos, 2, "::" ); 416 416 417 417 if ( prop._name!=enumString ) 418 418 { … … 429 429 prop.set( value ); 430 430 } 431 431 432 432 virtual void readMark( osgDB::ObjectMark& mark ) {} 433 433 434 434 virtual void readCharArray( char* s, unsigned int size ) {} 435 435 436 436 virtual void readWrappedString( std::string& str ) 437 437 { 438 438 if ( !prepareStream() ) return; 439 439 440 440 // Read available string in the stream buffer 441 441 unsigned int availSize = _sstream.rdbuf()->in_avail(); 442 442 std::string realStr = _sstream.str(); 443 443 _sstream.str(""); 444 444 445 445 // Find the first quot or valid character 446 446 bool hasQuot = false; … … 480 480 } 481 481 } 482 482 483 483 virtual bool matchString( const std::string& str ) 484 484 { … … 492 492 return false; 493 493 } 494 494 495 495 virtual void advanceToCurrentEndBracket() {} 496 496 497 497 protected: 498 498 bool isReadable() const { return _sstream.rdbuf()->in_avail()>0; } 499 499 500 500 bool prepareStream() 501 501 { … … 503 503 if ( isReadable() ) return true; 504 504 _sstream.clear(); 505 505 506 506 osgDB::XmlNode* current = _nodePath.back().get(); 507 507 if ( current->type!=osgDB::XmlNode::COMMENT ) … … 513 513 return true; 514 514 } 515 515 516 516 if ( current->properties.size()>0 ) 517 517 { … … 519 519 else if ( applyPropertyToStream(current, "text") ) return true; 520 520 } 521 521 522 522 if ( current->children.size()>0 ) 523 523 { … … 530 530 return prepareStream(); 531 531 } 532 532 533 533 bool applyPropertyToStream( osgDB::XmlNode* node, const std::string& name ) 534 534 { … … 542 542 return false; 543 543 } 544 544 545 545 typedef std::vector< osg::ref_ptr<osgDB::XmlNode> > XmlNodePath; 546 546 XmlNodePath _nodePath; 547 547 548 548 osg::ref_ptr<osgDB::XmlNode> _root; 549 549 std::stringstream _sstream;
