sources/network/bytestream.h

branch
protocol5
changeset 106
7b156b764d11
parent 98
4f0f0b1b8e0b
child 109
e4966d7e615d
equal deleted inserted replaced
104:a76af67a3a4b 106:7b156b764d11
29 */ 29 */
30 30
31 #pragma once 31 #pragma once
32 #include <stdexcept> 32 #include <stdexcept>
33 #include "../main.h" 33 #include "../main.h"
34 BEGIN_ZFC_NAMESPACE
34 35
35 class String; 36 class String;
36 37
37 enum { MAX_NETWORK_STRING = 0x800 }; 38 enum
39 {
40 MAX_NETWORK_STRING = 0x800
41 };
38 42
39 // TODO: Make able to handle big-endian too 43 // TODO: Make able to handle big-endian too
40 class Bytestream 44 class Bytestream
41 { 45 {
42 public: 46 public:
46 50
47 public: 51 public:
48 IOError (String message) : 52 IOError (String message) :
49 m_message (message) {} 53 m_message (message) {}
50 54
51 inline METHOD 55 const char* what() const throw()
52 what() const throw() -> const char*
53 { 56 {
54 return m_message.chars(); 57 return m_message;
55 } 58 }
56 }; 59 };
57 60
58 Bytestream (unsigned long length = 0x800); 61 Bytestream (unsigned long length = 0x800);
59 Bytestream (const unsigned char* data, unsigned long length); 62 Bytestream (const unsigned char* data, unsigned long length);
60 Bytestream (const Vector<unsigned char>& bytes); 63 Bytestream (const Vector<unsigned char>& bytes);
61 Bytestream (const Bytestream& other); 64 Bytestream (const Bytestream& other);
62 ~Bytestream(); 65 ~Bytestream();
63 66
64 inline METHOD allocated_size() const -> unsigned long; 67 void clear();
65 inline METHOD bytes_left() const -> unsigned long; 68 void grow_to_fit (unsigned long bytes);
66 METHOD clear() -> void; 69 void read (unsigned char* buffer, unsigned long length);
67 inline METHOD data() -> unsigned char*; 70 int8_t read_byte();
68 inline METHOD data() const -> const unsigned char*; 71 int32_t read_long();
69 METHOD grow_to_fit (unsigned long bytes) -> void; 72 int16_t read_short();
70 inline METHOD position() const -> unsigned long; 73 String read_string();
71 METHOD read (unsigned char* buffer, unsigned long length) -> void; 74 float read_float();
72 METHOD read_byte() -> char; 75 void resize (unsigned long length);
73 METHOD read_short() -> short int; 76 void write (const unsigned char* val, unsigned int length);
74 METHOD read_long() -> long int; 77 void write_buffer (const Bytestream& other);
75 METHOD read_string() -> String; 78 void write_buffer (const Vector<unsigned char>& other);
76 METHOD read_float() -> float; 79 void write_byte (int8_t val);
77 METHOD resize (unsigned long length) -> void; 80 void write_double (double val);
78 inline METHOD rewind() -> void; 81 void write_float (float val);
79 inline METHOD seek (unsigned long pos) -> void; 82 void write_long (int32_t val);
80 inline METHOD to_vector() const -> Vector<unsigned char>; 83 void write_short (int16_t val);
81 METHOD write (const unsigned char* val, unsigned int length) -> void; 84 void write_string (const String& val);
82 METHOD write_buffer (const Bytestream& other) -> void;
83 METHOD write_buffer (const Vector<unsigned char>& other) -> void;
84 METHOD write_byte (char val) -> void;
85 METHOD write_double (double val) -> void;
86 METHOD write_float (float val) -> void;
87 METHOD write_long (long int val) -> void;
88 METHOD write_short (short int val) -> void;
89 METHOD write_string (const String& val) -> void;
90 inline METHOD written_length() const -> unsigned long;
91 85
92 inline METHOD operator[] (unsigned long idx) -> unsigned char&; 86 Bytestream& operator= (const Bytestream& other);
93 inline METHOD operator[] (unsigned long idx) const -> unsigned char; 87
94 METHOD operator= (const Bytestream& other) -> Bytestream&; 88 unsigned long allocated_size() const
89 {
90 return m_allocatedSize;
91 }
92
93 unsigned long bytes_left() const
94 {
95 return m_writtenLength - (m_cursor - &m_data[0]);
96 }
97
98 inline unsigned char* data()
99 {
100 return m_data;
101 }
102 inline const unsigned char* data() const
103 {
104 return m_data;
105 }
106
107 unsigned long position() const
108 {
109 return m_cursor - m_data;
110 }
111
112 void rewind()
113 {
114 m_cursor = m_data;
115 }
116
117 void seek (unsigned long pos)
118 {
119 m_cursor = m_data + pos;
120 }
121
122 Vector<unsigned char> to_vector() const
123 {
124 return Vector<unsigned char> (m_data, m_writtenLength);
125 }
126
127 unsigned long written_length() const
128 {
129 return m_writtenLength;
130 }
131
132 unsigned char& operator[] (unsigned long idx)
133 {
134 return m_data[idx];
135 }
136
137 unsigned char operator[] (unsigned long idx) const
138 {
139 return m_data[idx];
140 }
95 141
96 private: 142 private:
97 unsigned char* m_data; 143 unsigned char* m_data;
98 unsigned char* m_cursor; 144 unsigned char* m_cursor;
99 unsigned long m_allocatedSize; 145 unsigned long m_allocatedSize;
100 unsigned long m_writtenLength; 146 unsigned long m_writtenLength;
101 147
102 METHOD init (const unsigned char* data, unsigned long length) -> void; 148 void init (const unsigned char* data, unsigned long length);
103 METHOD write (unsigned char val) -> void; 149 void write (unsigned char val);
104 METHOD ensure_read_space (unsigned int bytes) -> void; 150 void ensure_read_space (unsigned int bytes);
105 inline METHOD space_left() const -> unsigned long; 151
152 unsigned long space_left() const
153 {
154 return m_allocatedSize - m_writtenLength;
155 }
106 }; 156 };
107 157
108 // ------------------------------------------------------------------------------------------------- 158 END_ZFC_NAMESPACE
109 //
110 inline METHOD
111 Bytestream::allocated_size() const -> unsigned long
112 {
113 return m_allocatedSize;
114 }
115
116 // -------------------------------------------------------------------------------------------------
117 //
118 inline METHOD
119 Bytestream::written_length() const -> unsigned long
120 {
121 return m_writtenLength;
122 }
123
124 // -------------------------------------------------------------------------------------------------
125 //
126 inline METHOD
127 Bytestream::operator[] (unsigned long idx) -> unsigned char&
128 {
129 return m_data[idx];
130 }
131
132 // -------------------------------------------------------------------------------------------------
133 //
134 inline METHOD
135 Bytestream::operator[] (unsigned long idx) const -> unsigned char
136 {
137 return m_data[idx];
138 }
139
140 // -------------------------------------------------------------------------------------------------
141 //
142 inline METHOD
143 Bytestream::position() const -> unsigned long
144 {
145 return m_cursor - m_data;
146 }
147
148 // -------------------------------------------------------------------------------------------------
149 //
150 inline METHOD
151 Bytestream::seek (unsigned long pos) -> void
152 {
153 m_cursor = m_data + pos;
154 }
155
156 // -------------------------------------------------------------------------------------------------
157 //
158 inline METHOD
159 Bytestream::rewind() -> void
160 {
161 m_cursor = m_data;
162 }
163
164 // -------------------------------------------------------------------------------------------------
165 //
166 inline METHOD
167 Bytestream::bytes_left() const -> unsigned long
168 {
169 return (m_writtenLength - (m_cursor - &m_data[0]));
170 }
171
172 // -------------------------------------------------------------------------------------------------
173 //
174 inline METHOD
175 Bytestream::space_left() const -> unsigned long
176 {
177 return (m_allocatedSize - m_writtenLength);
178 }
179
180 // -------------------------------------------------------------------------------------------------
181 //
182 inline METHOD
183 Bytestream::data() -> unsigned char*
184 {
185 return m_data;
186 }
187
188 // -------------------------------------------------------------------------------------------------
189 //
190 inline METHOD
191 Bytestream::data() const -> const unsigned char*
192 {
193 return m_data;
194 }
195
196 // -------------------------------------------------------------------------------------------------
197 //
198 inline METHOD
199 Bytestream::to_vector() const -> Vector<unsigned char>
200 {
201 return Vector<unsigned char> (m_data, m_writtenLength);
202 }

mercurial