1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSGDB_INPUT
15#define OSGDB_INPUT 1
16
17#include <osg/Image>
18#include <osg/Shader>
19#include <osg/Node>
20#include <osg/Drawable>
21#include <osg/StateAttribute>
22#include <osg/ArgumentParser>
23
24#include <osgDB/ReaderWriter>
25#include <osgDB/Options>
26
27#include <map>
28#include <string>
29
30namespace osgDB {
31
32/** basic structure for custom runtime inheritance checking */
33struct basic_type_wrapper {
34    virtual ~basic_type_wrapper() {}
35    virtual bool matches(const osg::Object *proto) const = 0;
36};
37
38/** a class template that checks inheritance between a given
39    Object's class and a class defined at compile time through
40        the template parameter T.
41        This is used in conjunction with readObjectOfType() to
42        specify an abstract class as reference type.
43**/
44template<class T>
45struct type_wrapper: basic_type_wrapper {
46    bool matches(const osg::Object *proto) const
47    {
48        return dynamic_cast<const T*>(proto) != 0;
49    }
50};
51
52/** deprecated. */
53class OSGDB_EXPORT Field
54{
55    public:
56
57        enum {
58            MIN_CACHE_SIZE = 256
59        };
60
61        Field();
62        Field(const Field& field);
63        virtual ~Field();
64
65        virtual Field& operator = (const Field& ic);
66
67        void reset();
68        void addChar(char c);
69        int getNoCharacters() const { return _fieldCacheSize; }
70
71        void setWithinQuotes(bool withinQuotes=true);
72        bool getWithinQuotes();
73
74        void setNoNestedBrackets(int no);
75        int getNoNestedBrackets();
76
77        enum FieldType
78        {
79            OPEN_BRACKET,
80            CLOSE_BRACKET,
81            STRING,
82            WORD,
83            REAL,
84            INTEGER,
85            BLANK,
86            UNINITIALISED
87        };
88
89        FieldType getFieldType() const;
90
91        bool isValid() const;
92
93        bool isOpenBracket() const;
94        bool isCloseBracket() const;
95
96        bool isWord() const;
97        bool matchWord(const char* str) const;
98        bool matchWord(const char* str,int noCharacters) const;
99
100        bool isString() const;
101        bool matchString(const char* str) const;
102        bool matchString(const char* str,int noCharacters) const;
103        bool isQuotedString() const;
104
105        const char* getStr() const;
106        char* takeStr();
107
108        bool isInt() const;
109        bool matchInt(int i) const;
110        bool getInt(int& i) const;
111
112        bool isUInt() const;
113        bool matchUInt(unsigned int i) const;
114        bool getUInt(unsigned int& i) const;
115
116        bool isFloat() const;
117        bool matchFloat(float f) const;
118        bool getFloat(float& f) const;
119        bool getFloat(double& f) const;
120
121        static FieldType calculateFieldType(const char* str,bool withinQuotes=false);
122
123    protected:
124
125        void _init();
126        void _free();
127        void _copy(const Field& ic);
128
129        int _fieldCacheCapacity;
130        int _fieldCacheSize;
131        char* _fieldCache;
132
133        mutable FieldType _fieldType;
134
135        bool _withinQuotes;
136
137        int _noNestedBrackets;
138
139};
140
141/** deprecated. */
142class OSGDB_EXPORT FieldReader
143{
144    public:
145
146        FieldReader();
147        FieldReader(const FieldReader& ic);
148        virtual ~FieldReader();
149
150        virtual FieldReader& operator = (const FieldReader& ic);
151
152        void attach(std::istream* input);
153        void detach();
154
155        virtual bool eof() const;
156
157        bool readField(Field& fieldPtr);
158        void ignoreField();
159
160        /** no of unmatched `{' encountered so far in file*/
161        int getNoNestedBrackets() const;
162
163    private:
164
165        bool _readField(Field* fieldPtr);
166
167        void _init();
168        void _free();
169        void _copy(const FieldReader& ic);
170
171        std::istream* _fin;
172        bool _eof;
173
174        bool findStartOfNextField();
175
176        int _noNestedBrackets;
177
178        bool _delimiterEatLookUp[256];
179        bool _delimiterKeepLookUp[256];
180
181};
182
183/** deprecated. */
184class OSGDB_EXPORT FieldReaderIterator
185{
186    public:
187
188        enum {
189            MINIMUM_FIELD_READER_QUEUE_SIZE = 10
190        };
191
192        FieldReaderIterator();
193        FieldReaderIterator(const FieldReaderIterator& ic);
194        virtual ~FieldReaderIterator();
195
196        FieldReaderIterator& operator = (const FieldReaderIterator& ic);
197
198        void attach(std::istream* input);
199        void detach();
200
201        virtual bool eof() const;
202
203        FieldReader& getFieldReader() { return _reader; }
204
205        void insert(int pos,Field* field);
206        void insert(int pos,const char* str);
207
208        Field& operator [] (int pos);
209        Field& field (int pos);
210
211        FieldReaderIterator& operator ++ ();
212        FieldReaderIterator& operator += (int no);
213
214        /** increments the iterator of the next simple field or
215          * whole block if the current field[0] is an open bracket */
216        void advanceOverCurrentFieldOrBlock();
217        void advanceToEndOfCurrentBlock();
218        void advanceToEndOfBlock(int noNestBrackets);
219
220        bool matchSequence(const char* str);
221
222        bool readSequence(const char* keyword,std::string& value);
223        bool readSequence(const char* keyword,unsigned int& value);
224        bool readSequence(const char* keyword,int& value);
225        bool readSequence(const char* keyword,float& value);
226        bool readSequence(const char* keyword,osg::Vec2f& value);
227        bool readSequence(const char* keyword,osg::Vec3f& value);
228        bool readSequence(const char* keyword,osg::Vec4f& value);
229        bool readSequence(const char* keyword,osg::Vec2d& value);
230        bool readSequence(const char* keyword,osg::Vec3d& value);
231        bool readSequence(const char* keyword,osg::Vec4d& value);
232
233        bool readSequence(std::string& value);
234        bool readSequence(unsigned int& value);
235        bool readSequence(int& value);
236        bool readSequence(float& value);
237        bool readSequence(osg::Vec2f& value);
238        bool readSequence(osg::Vec3f& value);
239        bool readSequence(osg::Vec4f& value);
240        bool readSequence(osg::Vec2d& value);
241        bool readSequence(osg::Vec3d& value);
242        bool readSequence(osg::Vec4d& value);
243
244    private:
245
246        void _init();
247        void _free();
248        void _copy(const FieldReaderIterator& ic);
249
250        FieldReader _reader;
251
252        Field _blank;
253
254        Field* _previousField;
255
256        Field** _fieldQueue;
257        int _fieldQueueSize;
258        int _fieldQueueCapacity;
259
260};
261
262/** deprecated. */
263class OSGDB_EXPORT Input : public FieldReaderIterator
264{
265    public:
266
267        Input();
268        virtual ~Input();
269
270        void setOptions(const Options* options) { _options = options; }
271        const Options* getOptions() const { return _options.get(); }
272
273        virtual osg::Object*         readObjectOfType(const osg::Object& compObj);
274        virtual osg::Object*         readObjectOfType(const basic_type_wrapper &btw);
275
276
277        template<typename T>
278        inline T* readObjectOfType()
279        {
280            return dynamic_cast<T*>(readObjectOfType(osgDB::type_wrapper<T>()));
281        }
282
283        virtual osg::Object*         readObject();
284        virtual osg::Image*          readImage();
285        virtual osg::Drawable*       readDrawable();
286        virtual osg::StateAttribute* readStateAttribute();
287        virtual osg::Uniform*        readUniform();
288        virtual osg::Node*           readNode();
289        virtual osg::Shader*         readShader();
290
291        virtual osg::Object*         readObject(const std::string& fileName);
292        virtual osg::Image*          readImage(const std::string& fileName);
293        virtual osg::Node*           readNode(const std::string& fileName);
294        virtual osg::Shader*         readShader(const std::string& fileName);
295
296        virtual osg::Object*         getObjectForUniqueID(const std::string& uniqueID);
297        virtual void                 registerUniqueIDForObject(const std::string& uniqueID,osg::Object* obj);
298
299        typedef osg::ArgumentParser::Parameter Parameter;
300
301        bool read(Parameter value1);
302        bool read(Parameter value1, Parameter value2);
303        bool read(Parameter value1, Parameter value2, Parameter value3);
304        bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4);
305        bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
306        bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
307        bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
308        bool read(Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
309
310        bool read(const char* str);
311        bool read(const char* str, Parameter value1);
312        bool read(const char* str, Parameter value1, Parameter value2);
313        bool read(const char* str, Parameter value1, Parameter value2, Parameter value3);
314        bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4);
315        bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5);
316        bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6);
317        bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7);
318        bool read(const char* str, Parameter value1, Parameter value2, Parameter value3, Parameter value4, Parameter value5, Parameter value6, Parameter value7, Parameter value8);
319
320    private:
321
322        typedef std::map< std::string, osg::ref_ptr<osg::Object> > UniqueIDToObjectMapping;
323        UniqueIDToObjectMapping _uniqueIDToObjectMap;
324
325        osg::ref_ptr<const Options> _options;
326
327};
328
329}
330
331#endif                                            // __SG_INPUT_H
332