iodevice.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
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 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Mark Page
28 */
29
30
31 #pragma once
32
33 #include "../System/cl_platform.h"
34 #include <memory>
35 #include <string>
36
37 namespace clan
38 {
41
42 class IODeviceProvider;
43 class IODevice_Impl;
44
50 class IODevice
51 {
54
55 public:
57 enum SeekMode
58 {
60 seek_set,
61
63 seek_cur,
64
66 seek_end
67 };
68
72
73 public:
75 IODevice();
76
80 IODevice(IODeviceProvider *provider);
81
82 ~IODevice();
83
87
88 public:
90 bool is_null() const { return !impl; }
91
93 void throw_if_null() const;
94
96
98 int get_size() const;
99
101
103 int get_position() const;
104
106
107 bool is_little_endian() const;
108
110 const IODeviceProvider *get_provider() const;
111
113 IODeviceProvider *get_provider();
114
118
119 public:
121
126 int send(const void *data, int len, bool send_all = true);
127
135 int receive(void *data, int len, bool receive_all = true);
136
142 int peek(void *data, int len);
143
149 bool seek(int position, SeekMode mode = seek_set);
150
158 int read(void *data, int len, bool receive_all = true);
159
167 int write(const void *data, int len, bool send_all = true);
168
170 void set_system_mode();
171
173 void set_big_endian_mode();
174
176 void set_little_endian_mode();
177
181 void write_int64(int64_t data);
182
186 void write_uint64(uint64_t data);
187
191 void write_int32(int32_t data);
192
196 void write_uint32(uint32_t data);
197
201 void write_int16(int16_t data);
202
206 void write_uint16(uint16_t data);
207
211 void write_int8(int8_t data);
212
216 void write_uint8(uint8_t data);
217
223 void write_float(float data);
224
231 void write_string_a(const std::string &str);
232
238 void write_string_nul(const std::string &str);
239
247 void write_string_text(const std::string &str);
248
250
251 int64_t read_int64();
252
254
255 uint64_t read_uint64();
256
258
259 int32_t read_int32();
260
262
263 uint32_t read_uint32();
264
266
267 int16_t read_int16();
268
270
271 uint16_t read_uint16();
272
274
275 int8_t read_int8();
276
278
279 uint8_t read_uint8();
280
282
284 float read_float();
285
287
290 std::string read_string_a();
291
293
296 std::string read_string_nul();
297
307 std::string read_string_text(const char *skip_initial_chars, const char *read_until_chars, bool allow_eof = true);
308
310 IODevice duplicate();
311
315
316 protected:
317 std::shared_ptr<IODevice_Impl> impl;
319 };
320
321 }
322
clan::IODevice::get_size
int get_size() const
Returns the size of data stream.
clan::IODevice::get_position
int get_position() const
Returns the position in the data stream.
clan
Definition: clanapp.h:35
clan::IODevice::write_int16
void write_int16(int16_t data)
Writes a signed 16 bit integer to output source.
clan::IODevice::write_float
void write_float(float data)
Writes a float to output source.
clan::IODevice::write_uint16
void write_uint16(uint16_t data)
Writes an unsigned 16 bit integer to output source.
clan::IODevice::send
int send(const void *data, int len, bool send_all=true)
Send data to device.
clan::IODevice::duplicate
IODevice duplicate()
Create a new IODevice referencing the same resource.
clan::IODevice::is_null
bool is_null() const
Returns true if this object is invalid.
Definition: iodevice.h:90
clan::IODevice::write_int64
void write_int64(int64_t data)
Writes a signed 64 bit integer to output source.
clan::IODevice::read_int8
int8_t read_int8()
Reads a signed 8 bit integer from input source.
clan::IODevice::write_string_text
void write_string_text(const std::string &str)
Writes a text string to the output source.
clan::IODevice::seek_end
Set to a specific value from the end (use negative position)
Definition: iodevice.h:66
clan::IODevice::~IODevice
~IODevice()
clan::IODevice
I/O Device interface.
Definition: iodevice.h:50
clan::IODevice::throw_if_null
void throw_if_null() const
Throw an exception if this object is invalid.
clan::IODevice::peek
int peek(void *data, int len)
Peek data from device (data is left in the buffer).
clan::IODevice::read_int64
int64_t read_int64()
Reads a signed 64 bit integer from input source.
clan::IODevice::write_string_a
void write_string_a(const std::string &str)
Writes a string to the output source.
clan::IODevice::read
int read(void *data, int len, bool receive_all=true)
Alias for receive(data, len, receive_all)
clan::IODevice::seek_set
Set to a specific value from the start.
Definition: iodevice.h:60
clan::IODevice::read_uint8
uint8_t read_uint8()
Reads an unsigned 8 bit integer from input source.
clan::IODevice::write_int32
void write_int32(int32_t data)
Writes a signed 32 bit integer to output source.
clan::IODevice::seek_cur
Set relative to the current position.
Definition: iodevice.h:63
clan::IODevice::read_string_a
std::string read_string_a()
Reads a string from the input source.
clan::StyleTokenType::string
@ keyword
clan::IODevice::write_uint32
void write_uint32(uint32_t data)
Writes an unsigned 32 bit integer to output source.
clan::IODevice::read_string_text
std::string read_string_text(const char *skip_initial_chars, const char *read_until_chars, bool allow_eof=true)
Reads a string from the input source where the source is a text file.
clan::IODevice::write_uint8
void write_uint8(uint8_t data)
Writes an unsigned 8 bit integer to output source.
clan::IODevice::seek
bool seek(int position, SeekMode mode=seek_set)
Seek in data stream.
clan::IODevice::read_int16
int16_t read_int16()
Reads a signed 16 bit integer from input source.
clan::IODevice::read_float
float read_float()
Reads a float from input source.
clan::IODeviceProvider
I/O device provider interface.
Definition: iodevice_provider.h:40
clan::IODevice::read_string_nul
std::string read_string_nul()
Reads a nul terminated string from the input source.
clan::IODevice::set_system_mode
void set_system_mode()
Changes input data endianess to the local systems mode.
clan::IODevice::IODevice
IODevice()
Constructs a null instance.
clan::IODevice::SeekMode
SeekMode
Seeking modes.
Definition: iodevice.h:57
clan::IODevice::impl
std::shared_ptr< IODevice_Impl > impl
Definition: iodevice.h:317
clan::IODevice::read_uint32
uint32_t read_uint32()
Reads an unsigned 32 bit integer from input source.
clan::IODevice::read_uint64
uint64_t read_uint64()
Reads an unsigned 64 bit integer from input source.
clan::IODevice::set_little_endian_mode
void set_little_endian_mode()
Changes input data endianess to little endian mode. This is the default setting.
clan::IODevice::read_int32
int32_t read_int32()
Reads a signed 32 bit integer from input source.
clan::IODevice::write_uint64
void write_uint64(uint64_t data)
Writes an unsigned 64 bit integer to output source.
clan::IODevice::get_provider
const IODeviceProvider * get_provider() const
Returns the provider for this object.
clan::IODevice::write_int8
void write_int8(int8_t data)
Writes a signed 8 bit integer to output source.
clan::IODevice::read_uint16
uint16_t read_uint16()
Reads an unsigned 16 bit integer from input source.
clan::IODevice::write_string_nul
void write_string_nul(const std::string &str)
Writes a nul terminated string to the output source.
clan::IODevice::receive
int receive(void *data, int len, bool receive_all=true)
Receive data from device.
clan::IODevice::set_big_endian_mode
void set_big_endian_mode()
Changes input data endianess to big endian mode. (Default is little endian)
clan::IODevice::write
int write(const void *data, int len, bool send_all=true)
Alias for send(data, len, send_all)
clan::IODevice::is_little_endian
bool is_little_endian() const
Returns true if the input source is in little endian mode.