endian_str.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. endian_str.h - Stream input and output of binary numbers with different
  3. byte orders.
  4. Copyright (C) 2013 ljfa-ag
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #ifndef ENDIAN_STR_H_INCLUDED
  20. #define ENDIAN_STR_H_INCLUDED
  21. #include <cstdint>
  22. #include <iosfwd>
  23. #if !defined(BOOST_LITTLE_ENDIAN) && !defined(BOOST_BIG_ENDIAN)
  24. #include <boost/detail/endian.hpp>
  25. #endif
  26. /**
  27. * \brief Reading and writing numbers from and to streams
  28. * in binary format with different byte orders.
  29. */
  30. namespace endian
  31. {
  32. ///Read number from stream in native byte order.
  33. void read_native(std::istream& str, uint8_t& x);
  34. void read_native(std::istream& str, uint16_t& x);
  35. void read_native(std::istream& str, uint32_t& x);
  36. void read_native(std::istream& str, uint64_t& x);
  37. void read_native(std::istream& str, int8_t& x);
  38. void read_native(std::istream& str, int16_t& x);
  39. void read_native(std::istream& str, int32_t& x);
  40. void read_native(std::istream& str, int64_t& x);
  41. void read_native(std::istream& str, float& x);
  42. void read_native(std::istream& str, double& x);
  43. ///Write number to stream in native byte order.
  44. void write_native(std::ostream& str, uint8_t x);
  45. void write_native(std::ostream& str, uint16_t x);
  46. void write_native(std::ostream& str, uint32_t x);
  47. void write_native(std::ostream& str, uint64_t x);
  48. void write_native(std::ostream& str, int8_t x);
  49. void write_native(std::ostream& str, int16_t x);
  50. void write_native(std::ostream& str, int32_t x);
  51. void write_native(std::ostream& str, int64_t x);
  52. void write_native(std::ostream& str, float x);
  53. void write_native(std::ostream& str, double x);
  54. ///Read number from stream in anti-native byte order.
  55. void read_reorder(std::istream& str, uint8_t& x);
  56. void read_reorder(std::istream& str, uint16_t& x);
  57. void read_reorder(std::istream& str, uint32_t& x);
  58. void read_reorder(std::istream& str, uint64_t& x);
  59. void read_reorder(std::istream& str, int8_t& x);
  60. void read_reorder(std::istream& str, int16_t& x);
  61. void read_reorder(std::istream& str, int32_t& x);
  62. void read_reorder(std::istream& str, int64_t& x);
  63. void read_reorder(std::istream& str, float& x);
  64. void read_reorder(std::istream& str, double& x);
  65. ///Write number to stream in anti-native byte order.
  66. void write_reorder(std::ostream& str, uint8_t x);
  67. void write_reorder(std::ostream& str, uint16_t x);
  68. void write_reorder(std::ostream& str, uint32_t x);
  69. void write_reorder(std::ostream& str, uint64_t x);
  70. void write_reorder(std::ostream& str, int8_t x);
  71. void write_reorder(std::ostream& str, int16_t x);
  72. void write_reorder(std::ostream& str, int32_t x);
  73. void write_reorder(std::ostream& str, int64_t x);
  74. void write_reorder(std::ostream& str, float x);
  75. void write_reorder(std::ostream& str, double x);
  76. ///Read number from stream in big endian.
  77. template<class T> void read_big(std::istream& str, T& x);
  78. ///Read number from stream in little endian.
  79. template<class T> void read_little(std::istream& str, T& x);
  80. ///Write number to stream in big endian.
  81. template<class T> void write_big(std::ostream& str, T x);
  82. ///Write number to stream in little endian.
  83. template<class T> void write_little(std::ostream& str, T x);
  84. #if defined(BOOST_LITTLE_ENDIAN)
  85. template<class T> inline void read_big(std::istream& str, T& x) { read_reorder(str, x); }
  86. template<class T> inline void read_little(std::istream& str, T& x) { read_native(str, x); }
  87. template<class T> inline void write_big(std::ostream& str, T x) { write_reorder(str, x); }
  88. template<class T> inline void write_little(std::ostream& str, T x) { write_native(str, x); }
  89. #elif defined(BOOST_BIG_ENDIAN)
  90. template<class T> inline void read_big(std::istream& str, T& x) { read_native(str, x); }
  91. template<class T> inline void read_little(std::istream& str, T& x) { read_reorder(str, x); }
  92. template<class T> inline void write_big(std::ostream& str, T x) { write_native(str, x); }
  93. template<class T> inline void write_little(std::ostream& str, T x) { write_reorder(str, x); }
  94. #endif
  95. }
  96. #endif // ENDIAN_STR_H_INCLUDED