irrXML.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (C) 2002-2005 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine" and the "irrXML" project.
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h and/or irrXML.h
  4. // Need to include Assimp, too. We're using Assimp's version of fast_atof
  5. // so we need stdint.h
  6. #include "./../../include/aiTypes.h"
  7. #include "irrXML.h"
  8. #include "irrString.h"
  9. #include "irrArray.h"
  10. #include "./../fast_atof.h"
  11. #include "CXMLReaderImpl.h"
  12. namespace irr
  13. {
  14. namespace io
  15. {
  16. //! Implementation of the file read callback for ordinary files
  17. class CFileReadCallBack : public IFileReadCallBack
  18. {
  19. public:
  20. //! construct from filename
  21. CFileReadCallBack(const char* filename)
  22. : File(0), Size(0), Close(true)
  23. {
  24. // open file
  25. File = fopen(filename, "rb");
  26. if (File)
  27. getFileSize();
  28. }
  29. //! construct from FILE pointer
  30. CFileReadCallBack(FILE* file)
  31. : File(file), Size(0), Close(false)
  32. {
  33. if (File)
  34. getFileSize();
  35. }
  36. //! destructor
  37. virtual ~CFileReadCallBack()
  38. {
  39. if (Close && File)
  40. fclose(File);
  41. }
  42. //! Reads an amount of bytes from the file.
  43. virtual int read(void* buffer, int sizeToRead)
  44. {
  45. if (!File)
  46. return 0;
  47. return (int)fread(buffer, 1, sizeToRead, File);
  48. }
  49. //! Returns size of file in bytes
  50. virtual int getSize()
  51. {
  52. return Size;
  53. }
  54. private:
  55. //! retrieves the file size of the open file
  56. void getFileSize()
  57. {
  58. fseek(File, 0, SEEK_END);
  59. Size = ftell(File);
  60. fseek(File, 0, SEEK_SET);
  61. }
  62. FILE* File;
  63. int Size;
  64. bool Close;
  65. }; // end class CFileReadCallBack
  66. // FACTORY FUNCTIONS:
  67. //! Creates an instance of an UFT-8 or ASCII character xml parser.
  68. IrrXMLReader* createIrrXMLReader(const char* filename)
  69. {
  70. return new CXMLReaderImpl<char, IXMLBase>(new CFileReadCallBack(filename));
  71. }
  72. //! Creates an instance of an UFT-8 or ASCII character xml parser.
  73. IrrXMLReader* createIrrXMLReader(FILE* file)
  74. {
  75. return new CXMLReaderImpl<char, IXMLBase>(new CFileReadCallBack(file));
  76. }
  77. //! Creates an instance of an UFT-8 or ASCII character xml parser.
  78. IrrXMLReader* createIrrXMLReader(IFileReadCallBack* callback)
  79. {
  80. return new CXMLReaderImpl<char, IXMLBase>(callback, false);
  81. }
  82. //! Creates an instance of an UTF-16 xml parser.
  83. IrrXMLReaderUTF16* createIrrXMLReaderUTF16(const char* filename)
  84. {
  85. return new CXMLReaderImpl<char16, IXMLBase>(new CFileReadCallBack(filename));
  86. }
  87. //! Creates an instance of an UTF-16 xml parser.
  88. IrrXMLReaderUTF16* createIrrXMLReaderUTF16(FILE* file)
  89. {
  90. return new CXMLReaderImpl<char16, IXMLBase>(new CFileReadCallBack(file));
  91. }
  92. //! Creates an instance of an UTF-16 xml parser.
  93. IrrXMLReaderUTF16* createIrrXMLReaderUTF16(IFileReadCallBack* callback)
  94. {
  95. return new CXMLReaderImpl<char16, IXMLBase>(callback, false);
  96. }
  97. //! Creates an instance of an UTF-32 xml parser.
  98. IrrXMLReaderUTF32* createIrrXMLReaderUTF32(const char* filename)
  99. {
  100. return new CXMLReaderImpl<char32, IXMLBase>(new CFileReadCallBack(filename));
  101. }
  102. //! Creates an instance of an UTF-32 xml parser.
  103. IrrXMLReaderUTF32* createIrrXMLReaderUTF32(FILE* file)
  104. {
  105. return new CXMLReaderImpl<char32, IXMLBase>(new CFileReadCallBack(file));
  106. }
  107. //! Creates an instance of an UTF-32 xml parser.
  108. IrrXMLReaderUTF32* createIrrXMLReaderUTF32(IFileReadCallBack* callback)
  109. {
  110. return new CXMLReaderImpl<char32, IXMLBase>(callback, false);
  111. }
  112. } // end namespace io
  113. } // end namespace irr