iodevice.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 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 #pragma once
31 
32 #include "../System/cl_platform.h"
33 #include <memory>
34 #include <string>
35 
36 namespace clan
37 {
40 
41  class IODeviceProvider;
42  class IODevice_Impl;
43 
49  class IODevice
50  {
51  public:
53  enum SeekMode
54  {
57 
60 
63  };
64 
66  IODevice();
67 
71  IODevice(IODeviceProvider *provider);
72 
73  ~IODevice();
74 
76  bool is_null() const { return !impl; }
77 
79  void throw_if_null() const;
80 
82 
84  int get_size() const;
85 
87 
89  int get_position() const;
90 
92 
93  bool is_little_endian() const;
94 
96  const IODeviceProvider *get_provider() const;
97 
100 
102 
107  int send(const void *data, int len, bool send_all = true);
108 
116  int receive(void *data, int len, bool receive_all = true);
117 
123  int peek(void *data, int len);
124 
130  bool seek(int position, SeekMode mode = seek_set);
131 
139  int read(void *data, int len, bool receive_all = true);
140 
148  int write(const void *data, int len, bool send_all = true);
149 
151  void set_system_mode();
152 
154  void set_big_endian_mode();
155 
157  void set_little_endian_mode();
158 
162  void write_int64(int64_t data);
163 
167  void write_uint64(uint64_t data);
168 
172  void write_int32(int32_t data);
173 
177  void write_uint32(uint32_t data);
178 
182  void write_int16(int16_t data);
183 
187  void write_uint16(uint16_t data);
188 
192  void write_int8(int8_t data);
193 
197  void write_uint8(uint8_t data);
198 
204  void write_float(float data);
205 
212  void write_string_a(const std::string &str);
213 
219  void write_string_nul(const std::string &str);
220 
228  void write_string_text(const std::string &str);
229 
231 
232  int64_t read_int64();
233 
235 
236  uint64_t read_uint64();
237 
239 
240  int32_t read_int32();
241 
243 
244  uint32_t read_uint32();
245 
247 
248  int16_t read_int16();
249 
251 
252  uint16_t read_uint16();
253 
255 
256  int8_t read_int8();
257 
259 
260  uint8_t read_uint8();
261 
263 
265  float read_float();
266 
268 
272 
274 
278 
288  std::string read_string_text(const char *skip_initial_chars, const char *read_until_chars, bool allow_eof = true);
289 
292 
293  protected:
294  std::shared_ptr<IODevice_Impl> impl;
295  };
296 
298 }
Definition: clanapp.h:35
bool is_null() const
Returns true if this object is invalid.
Definition: iodevice.h:76
Set to a specific value from the end (use negative position)
Definition: iodevice.h:62
int peek(void *data, int len)
Peek data from device (data is left in the buffer).
void set_big_endian_mode()
Changes input data endianess to big endian mode. (Default is little endian)
int get_position() const
Returns the position in the data stream.
I/O Device interface.
Definition: iodevice.h:49
uint8_t read_uint8()
Reads an unsigned 8 bit integer from input source.
int send(const void *data, int len, bool send_all=true)
Send data to device.
int get_size() const
Returns the size of data stream.
bool is_little_endian() const
Returns true if the input source is in little endian mode.
std::string read_string_nul()
Reads a nul terminated string from the input source.
int32_t read_int32()
Reads a signed 32 bit integer from input source.
Set to a specific value from the start.
Definition: iodevice.h:56
IODevice duplicate()
Create a new IODevice referencing the same resource.
void write_string_nul(const std::string &str)
Writes a nul terminated string to the output source.
Set relative to the current position.
Definition: iodevice.h:59
int write(const void *data, int len, bool send_all=true)
Alias for send(data, len, send_all)
void write_int32(int32_t data)
Writes a signed 32 bit integer to output source.
int16_t read_int16()
Reads a signed 16 bit integer from input source.
void write_int8(int8_t data)
Writes a signed 8 bit integer to output source.
int8_t read_int8()
Reads a signed 8 bit integer from input source.
void write_uint32(uint32_t data)
Writes an unsigned 32 bit integer to output source.
void set_system_mode()
Changes input data endianess to the local systems mode.
void write_float(float data)
Writes a float to output source.
void throw_if_null() const
Throw an exception if this object is invalid.
float read_float()
Reads a float from input source.
I/O device provider interface.
Definition: iodevice_provider.h:39
IODevice()
Constructs a null instance.
void write_int64(int64_t data)
Writes a signed 64 bit integer to output source.
void write_uint16(uint16_t data)
Writes an unsigned 16 bit integer to output source.
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.
std::shared_ptr< IODevice_Impl > impl
Definition: iodevice.h:294
void write_string_text(const std::string &str)
Writes a text string to the output source.
uint64_t read_uint64()
Reads an unsigned 64 bit integer from input source.
SeekMode
Seeking modes.
Definition: iodevice.h:53
int64_t read_int64()
Reads a signed 64 bit integer from input source.
int read(void *data, int len, bool receive_all=true)
Alias for receive(data, len, receive_all)
uint16_t read_uint16()
Reads an unsigned 16 bit integer from input source.
void write_string_a(const std::string &str)
Writes a string to the output source.
void set_little_endian_mode()
Changes input data endianess to little endian mode. This is the default setting.
std::string read_string_a()
Reads a string from the input source.
void write_uint8(uint8_t data)
Writes an unsigned 8 bit integer to output source.
uint32_t read_uint32()
Reads an unsigned 32 bit integer from input source.
int receive(void *data, int len, bool receive_all=true)
Receive data from device.
void write_int16(int16_t data)
Writes a signed 16 bit integer to output source.
void write_uint64(uint64_t data)
Writes an unsigned 64 bit integer to output source.
const IODeviceProvider * get_provider() const
Returns the provider for this object.
bool seek(int position, SeekMode mode=seek_set)
Seek in data stream.