irrXML.cpp 3.2 KB

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