1 /* 2 BStone: A Source port of 3 Blake Stone: Aliens of Gold and Blake Stone: Planet Strike 4 5 Copyright (c) 1992-2013 Apogee Entertainment, LLC 6 Copyright (c) 2013-2015 Boris I. Bendovsky (bibendovsky@hotmail.com) 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License 10 as published by the Free Software Foundation; either version 2 11 of the License, or (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the 20 Free Software Foundation, Inc., 21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 22 */ 23 24 25 #ifndef BSTONE_BINARY_WRITER_INCLUDED 26 #define BSTONE_BINARY_WRITER_INCLUDED 27 28 29 #include "bstone_istream.h" 30 31 32 namespace bstone { 33 34 35 class BinaryWriter { 36 public: 37 BinaryWriter( 38 IStream* stream = nullptr); 39 40 BinaryWriter( 41 const BinaryWriter& that) = delete; 42 43 BinaryWriter& operator=( 44 const BinaryWriter& that) = delete; 45 46 47 bool open( 48 IStream* stream); 49 50 // Closes the writer but stream. 51 void close(); 52 53 bool is_open() const; 54 55 // Writes a signed 8-bit integer value. 56 bool write_s8( 57 int8_t value); 58 59 // Writes an unsigned 8-bit integer value. 60 bool write_u8( 61 uint8_t value); 62 63 // Writes a signed 16-bit integer value. 64 bool write_s16( 65 int16_t value); 66 67 // Writes an unsigned 16-bit integer value. 68 bool write_u16( 69 uint16_t value); 70 71 // Writes a signed 32-bit integer value. 72 bool write_s32( 73 int32_t value); 74 75 // Writes an unsigned 32-bit integer value. 76 bool write_u32( 77 uint32_t value); 78 79 // Writes a signed 64-bit integer value. 80 bool write_s64( 81 int64_t value); 82 83 // Writes an unsigned 64-bit integer value. 84 bool write_u64( 85 uint64_t value); 86 87 // Writes a 32-bit float-point value. 88 bool write_r32( 89 float value); 90 91 // Writes a 64-bit float-point value. 92 bool write_r64( 93 double value); 94 95 bool write( 96 const void* buffer, 97 int count); 98 99 // Writes a string prepended with signed 32-bit (little-endian) length. 100 bool write( 101 const std::string& string); 102 103 // Skips a number of octets forward if count is positive or 104 // backward otherwise. 105 // Returns false on error. 106 bool skip( 107 int count); 108 109 // Returns a current position. 110 int64_t get_position() const; 111 112 // Sets a current position to a specified one. 113 bool set_position( 114 int64_t position); 115 116 template<typename T> write(const T & value)117 bool write( 118 const T& value) 119 { 120 if (!is_open()) { 121 return false; 122 } 123 124 return stream_->write(&value, sizeof(T)); 125 } 126 127 template<typename T,size_t N> write(const T (& value)[N])128 bool write( 129 const T (&value)[N]) 130 { 131 if (!is_open()) { 132 return false; 133 } 134 135 return stream_->write(value, N * sizeof(T)); 136 } 137 138 139 private: 140 IStream* stream_; 141 }; // BinaryWriter 142 143 144 } // bstone 145 146 147 #endif // BSTONE_BINARY_WRITER_INCLUDED 148