irrXML.cpp 3.3 KB

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