BinaryReader.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Types.h"
  24. namespace crown
  25. {
  26. class File;
  27. /// A reader that offers a convenient way to read from a File
  28. class BinaryReader
  29. {
  30. public:
  31. //-----------------------------------------------------------------------------
  32. BinaryReader(File& file) : m_file(file) {}
  33. //-----------------------------------------------------------------------------
  34. int8_t read_byte()
  35. {
  36. int8_t buffer;
  37. m_file.read(&buffer, sizeof(int8_t));
  38. return buffer;
  39. }
  40. //-----------------------------------------------------------------------------
  41. int16_t read_int16()
  42. {
  43. int16_t buffer;
  44. m_file.read(&buffer, sizeof(int16_t));
  45. return buffer;
  46. }
  47. //-----------------------------------------------------------------------------
  48. uint16_t read_uint16()
  49. {
  50. uint16_t buffer;
  51. m_file.read(&buffer, sizeof(uint16_t));
  52. return buffer;
  53. }
  54. //-----------------------------------------------------------------------------
  55. int32_t read_int32()
  56. {
  57. int32_t buffer;
  58. m_file.read(&buffer, sizeof(int32_t));
  59. return buffer;
  60. }
  61. //-----------------------------------------------------------------------------
  62. uint32_t read_uint32()
  63. {
  64. uint32_t buffer;
  65. m_file.read(&buffer, sizeof(uint32_t));
  66. return buffer;
  67. }
  68. //-----------------------------------------------------------------------------
  69. int64_t read_int64()
  70. {
  71. int64_t buffer;
  72. m_file.read(&buffer, sizeof(int64_t));
  73. return buffer;
  74. }
  75. //-----------------------------------------------------------------------------
  76. double read_double()
  77. {
  78. double buffer;
  79. m_file.read(&buffer, sizeof(double));
  80. return buffer;
  81. }
  82. //-----------------------------------------------------------------------------
  83. float read_float()
  84. {
  85. float buffer;
  86. m_file.read(&buffer, sizeof(float));
  87. return buffer;
  88. }
  89. private:
  90. File& m_file;
  91. };
  92. } // namespace crown