Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

BinWriter.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "BinWriter.h"
  19. template<class T>
  20. void writePrimitive(BinWriter& writer, T value)
  21. {
  22. writer.writeRaw(&value, sizeof(value));
  23. }
  24. BinWriter::BinWriter() : data_{nullptr}, size_{0}, position_{0}
  25. {}
  26. BinWriter::BinWriter(void* data, size_t size) : data_{data}, size_{size}, position_{0}
  27. {}
  28. void BinWriter::writeRaw(const void* data, size_t size)
  29. {
  30. assert(size <= remaining());
  31. uint8_t* ptr = reinterpret_cast<uint8_t*>(data_) + position_;
  32. memcpy(ptr, data, size);
  33. position_ += size;
  34. }
  35. void BinWriter::writeByte(uint8_t value)
  36. {
  37. writePrimitive(*this, value);
  38. }
  39. void BinWriter::writeShort(uint16_t value)
  40. {
  41. writePrimitive(*this, value);
  42. }
  43. void BinWriter::writeInt(uint32_t value)
  44. {
  45. writePrimitive(*this, value);
  46. }
  47. void BinWriter::writeLong(uint64_t value)
  48. {
  49. writePrimitive(*this, value);
  50. }
  51. void BinWriter::writeFloat(float value)
  52. {
  53. writePrimitive(*this, value);
  54. }
  55. void BinWriter::writeDouble(double value)
  56. {
  57. writePrimitive(*this, value);
  58. }
  59. void BinWriter::writeString(const string& value)
  60. {
  61. if(value.size() >= 0xFFFF)
  62. {
  63. writeShort(0xFFFF);
  64. writeInt(static_cast<uint32_t>(value.size()));
  65. }
  66. else
  67. {
  68. writeShort(static_cast<uint16_t>(value.size()));
  69. }
  70. writeRaw(value.data(), value.size());
  71. align();
  72. }
  73. void BinWriter::skip(size_t amount)
  74. {
  75. assert(amount <= remaining());
  76. position_ += amount;
  77. }
  78. void BinWriter::seek(size_t position)
  79. {
  80. assert(position <= size_);
  81. position_ = position;
  82. }
  83. void BinWriter::align()
  84. {
  85. position_ = (position_ + 3) & ~3;
  86. }
  87. size_t BinWriter::position() const
  88. {
  89. return position_;
  90. }
  91. size_t BinWriter::remaining() const
  92. {
  93. return size_ - position_;
  94. }