endian_str.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * libnbt++ - A library for the Minecraft Named Binary Tag format.
  3. * Copyright (C) 2013, 2015 ljfa-ag
  4. *
  5. * This file is part of libnbt++.
  6. *
  7. * libnbt++ is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * libnbt++ is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef ENDIAN_STR_H_INCLUDED
  21. #define ENDIAN_STR_H_INCLUDED
  22. #include <cstdint>
  23. #include <iosfwd>
  24. #include "nbt_export.h"
  25. /**
  26. * @brief Reading and writing numbers from and to streams
  27. * in binary format with different byte orders.
  28. */
  29. namespace endian
  30. {
  31. enum endian { little, big };
  32. ///Reads number from stream in specified endian
  33. template<class T>
  34. void read(std::istream& is, T& x, endian e);
  35. ///Reads number from stream in little endian
  36. NBT_EXPORT void read_little(std::istream& is, uint8_t& x);
  37. NBT_EXPORT void read_little(std::istream& is, uint16_t& x);
  38. NBT_EXPORT void read_little(std::istream& is, uint32_t& x);
  39. NBT_EXPORT void read_little(std::istream& is, uint64_t& x);
  40. NBT_EXPORT void read_little(std::istream& is, int8_t& x);
  41. NBT_EXPORT void read_little(std::istream& is, int16_t& x);
  42. NBT_EXPORT void read_little(std::istream& is, int32_t& x);
  43. NBT_EXPORT void read_little(std::istream& is, int64_t& x);
  44. NBT_EXPORT void read_little(std::istream& is, float& x);
  45. NBT_EXPORT void read_little(std::istream& is, double& x);
  46. ///Reads number from stream in big endian
  47. NBT_EXPORT void read_big(std::istream& is, uint8_t& x);
  48. NBT_EXPORT void read_big(std::istream& is, uint16_t& x);
  49. NBT_EXPORT void read_big(std::istream& is, uint32_t& x);
  50. NBT_EXPORT void read_big(std::istream& is, uint64_t& x);
  51. NBT_EXPORT void read_big(std::istream& is, int8_t& x);
  52. NBT_EXPORT void read_big(std::istream& is, int16_t& x);
  53. NBT_EXPORT void read_big(std::istream& is, int32_t& x);
  54. NBT_EXPORT void read_big(std::istream& is, int64_t& x);
  55. NBT_EXPORT void read_big(std::istream& is, float& x);
  56. NBT_EXPORT void read_big(std::istream& is, double& x);
  57. ///Writes number to stream in specified endian
  58. template<class T>
  59. void write(std::ostream& os, T x, endian e);
  60. ///Writes number to stream in little endian
  61. NBT_EXPORT void write_little(std::ostream& os, uint8_t x);
  62. NBT_EXPORT void write_little(std::ostream& os, uint16_t x);
  63. NBT_EXPORT void write_little(std::ostream& os, uint32_t x);
  64. NBT_EXPORT void write_little(std::ostream& os, uint64_t x);
  65. NBT_EXPORT void write_little(std::ostream& os, int8_t x);
  66. NBT_EXPORT void write_little(std::ostream& os, int16_t x);
  67. NBT_EXPORT void write_little(std::ostream& os, int32_t x);
  68. NBT_EXPORT void write_little(std::ostream& os, int64_t x);
  69. NBT_EXPORT void write_little(std::ostream& os, float x);
  70. NBT_EXPORT void write_little(std::ostream& os, double x);
  71. ///Writes number to stream in big endian
  72. NBT_EXPORT void write_big(std::ostream& os, uint8_t x);
  73. NBT_EXPORT void write_big(std::ostream& os, uint16_t x);
  74. NBT_EXPORT void write_big(std::ostream& os, uint32_t x);
  75. NBT_EXPORT void write_big(std::ostream& os, uint64_t x);
  76. NBT_EXPORT void write_big(std::ostream& os, int8_t x);
  77. NBT_EXPORT void write_big(std::ostream& os, int16_t x);
  78. NBT_EXPORT void write_big(std::ostream& os, int32_t x);
  79. NBT_EXPORT void write_big(std::ostream& os, int64_t x);
  80. NBT_EXPORT void write_big(std::ostream& os, float x);
  81. NBT_EXPORT void write_big(std::ostream& os, double x);
  82. template<class T>
  83. void read(std::istream& is, T& x, endian e)
  84. {
  85. if(e == little)
  86. read_little(is, x);
  87. else
  88. read_big(is, x);
  89. }
  90. template<class T>
  91. void write(std::ostream& os, T x, endian e)
  92. {
  93. if(e == little)
  94. write_little(os, x);
  95. else
  96. write_big(os, x);
  97. }
  98. }
  99. #endif // ENDIAN_STR_H_INCLUDED