Input 10 KB

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