1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSGDB_XML_PARSER
15#define OSGDB_XML_PARSER 1
17#include <osgDB/Registry>
24/** read an Xml file, find the file in Options DataFilePathList.*/
25extern OSGDB_EXPORT XmlNode* readXmlFile(const std::string& filename,const Options* options);
27/** read an Xml file, find the file in osgDB::Registry's eaderWriter::Options DataFilePathList.*/
28inline XmlNode* readXmlFile(const std::string& filename)
30 return readXmlFile(filename, osgDB::Registry::instance()->getOptions());
33/** read an Xml from from an istream.*/
34extern OSGDB_EXPORT XmlNode* readXmlStream(std::istream& fin);
36extern OSGDB_EXPORT std::string trimEnclosingSpaces(const std::string& str);
38/** XmlNode class for very basic reading and writing of xml files.*/
39class OSGDB_EXPORT XmlNode : public osg::Referenced
56 typedef std::map< std::string, std::string > Properties;
57 typedef std::vector< osg::ref_ptr<XmlNode> > Children;
62 Properties properties;
65 std::string getTrimmedContents() const { return trimEnclosingSpaces(contents); }
69 class OSGDB_EXPORT ControlMap
74 typedef std::map< std::string, int > ControlToCharacterMap;
75 typedef std::map< int, std::string> CharacterToControlMap;
77 void addControlToCharacter(const std::string& control, int c);
79 ControlToCharacterMap _controlToCharacterMap;
80 CharacterToControlMap _characterToControlMap;
84 void setUpControlMappings();
88 class OSGDB_EXPORT Input : public ControlMap
97 typedef std::string::size_type size_type;
99 void open(const std::string& filename);
100 void attach(std::istream& istream);
102 void readAllDataIntoBuffer();
104 operator bool () const { return _currentPos<_buffer.size(); }
106 size_type currentPosition() const { return _currentPos; }
108 int get() { if (_currentPos<_buffer.size()) return static_cast<unsigned char>(_buffer[_currentPos++]); else return -1; }
110 int operator [] (size_type i) const { if ((_currentPos+i)<_buffer.size()) return static_cast<unsigned char>(_buffer[_currentPos+i]); else return -1; }
112 void operator ++ () { if (_currentPos<_buffer.size()) ++_currentPos; }
114 void operator += (size_type n) { if ((_currentPos+n)<_buffer.size()) _currentPos+=n; else _currentPos = _buffer.size(); }
116 void skipWhiteSpace();
118 std::string substr(size_type pos, size_type n=std::string::npos) { return (_currentPos<_buffer.size()) ? _buffer.substr(_currentPos+pos,n) : std::string(); }
120 size_type find(const std::string& str)
122 if (_currentPos<_buffer.size())
124 size_type pos = _buffer.find(str, _currentPos);
125 if (pos==std::string::npos) return std::string::npos;
126 else return pos-_currentPos;
127 } else return std::string::npos;
130 bool match(const std::string& str) { return (_currentPos<_buffer.size()) ? _buffer.compare(_currentPos, str.size(), str)==0 : false; }
138 void setEncoding(Encoding encoding) { _encoding = encoding; }
139 Encoding getEncoding() const { return _encoding; }
141 inline void copyCharacterToString(std::string& str)
143 if (_currentPos>=_buffer.size()) return;
146 case(ENCODING_UTF8) :
148 int char0 = static_cast<unsigned char>(_buffer[_currentPos]); ++_currentPos;
149 str.push_back(char0);
151 if (char0 < 0x80 || _currentPos>=_buffer.size()) break; // 1-byte character
153 str.push_back(_buffer[_currentPos]); ++_currentPos;
154 if (char0<0xe0 || _currentPos<_buffer.size()) break; // 2-byte character
156 str.push_back(_buffer[_currentPos]); ++_currentPos;
157 if (char0<0xf0 || _currentPos>=_buffer.size()) break; // 3-byte character
159 str.push_back(_buffer[_currentPos]); ++_currentPos;
160 if (char0<0xf8 || _currentPos>=_buffer.size()) break; // 4-byte character
162 if (_currentPos>=_buffer.size()) break;
163 str.push_back(_buffer[_currentPos]); ++_currentPos; // 5-byte character?
167 case(ENCODING_ASCII) :
169 str.push_back(_buffer[_currentPos]);
177 size_type _currentPos;
185 bool read(Input& input);
186 bool write(std::ostream& fout, const std::string& indent = "") const;
188 bool write(const ControlMap& controlMap, std::ostream& fout, const std::string& indent = "") const;
189 bool writeString(const ControlMap& controlMap, std::ostream& fout, const std::string& str) const;
193 bool writeChildren(const ControlMap& controlMap, std::ostream& fout, const std::string& indent) const;
194 bool writeProperties(const ControlMap& controlMap, std::ostream& fout) const;
196 bool readAndReplaceControl(std::string& in_contents, Input& input) const;