iodevice.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 "../api_core.h"
34 #include "../System/cl_platform.h"
35 #include <memory>
36 #include <string>
37 
38 namespace clan
39 {
42 
43 class IODeviceProvider;
44 class IODevice_Impl;
45 
51 class CL_API_CORE IODevice
52 {
55 
56 public:
58  enum SeekMode
59  {
62 
65 
67  seek_end
68  };
69 
73 
74 public:
76  IODevice();
77 
81  IODevice(IODeviceProvider *provider);
82 
83  ~IODevice();
84 
88 
89 public:
91  bool is_null() const { return !impl; }
92 
94  void throw_if_null() const;
95 
97 
99  int get_size() const;
100 
102 
104  int get_position() const;
105 
107 
108  bool is_little_endian() const;
109 
111  const IODeviceProvider *get_provider() const;
112 
114  IODeviceProvider *get_provider();
115 
119 
120 public:
122 
127  int send(const void *data, int len, bool send_all = true);
128 
136  int receive(void *data, int len, bool receive_all = true);
137 
143  int peek(void *data, int len);
144 
150  bool seek(int position, SeekMode mode = seek_set);
151 
159  int read(void *data, int len, bool receive_all = true);
160 
168  int write(const void *data, int len, bool send_all = true);
169 
171  void set_system_mode();
172 
174  void set_big_endian_mode();
175 
177  void set_little_endian_mode();
178 
182  void write_int64(byte64 data);
183 
187  void write_uint64(ubyte64 data);
188 
192  void write_int32(byte32 data);
193 
197  void write_uint32(ubyte32 data);
198 
202  void write_int16(byte16 data);
203 
207  void write_uint16(ubyte16 data);
208 
212  void write_int8(byte8 data);
213 
217  void write_uint8(ubyte8 data);
218 
224  void write_float(float data);
225 
232  void write_string_a(const std::string &str);
233 
239  void write_string_nul(const std::string &str);
240 
248  void write_string_text(const std::string &str);
249 
251 
252  byte64 read_int64();
253 
255 
256  ubyte64 read_uint64();
257 
259 
260  byte32 read_int32();
261 
263 
264  ubyte32 read_uint32();
265 
267 
268  byte16 read_int16();
269 
271 
272  ubyte16 read_uint16();
273 
275 
276  byte8 read_int8();
277 
279 
280  ubyte8 read_uint8();
281 
283 
285  float read_float();
286 
288 
291  std::string read_string_a();
292 
294 
297  std::string read_string_nul();
298 
308  std::string read_string_text(const char *skip_initial_chars, const char *read_until_chars, bool allow_eof = true);
309 
311  IODevice duplicate();
312 
316 
317 protected:
318  std::shared_ptr<IODevice_Impl> impl;
320 };
321 
322 }
323 
bool is_null() const
Returns true if this object is invalid.
Definition: iodevice.h:91
I/O Device interface.
Definition: iodevice.h:51
char byte8
Definition: cl_platform.h:59
Set to a specific value from the start.
Definition: iodevice.h:61
Set relative to the current position.
Definition: iodevice.h:64
unsigned long long ubyte64
Definition: cl_platform.h:66
I/O device provider interface.
Definition: iodevice_provider.h:41
unsigned short ubyte16
Definition: cl_platform.h:62
int byte32
Definition: cl_platform.h:63
long long byte64
Definition: cl_platform.h:65
SeekMode
Seeking modes.
Definition: iodevice.h:58
std::shared_ptr< IODevice_Impl > impl
Definition: iodevice.h:318
unsigned char ubyte8
Definition: cl_platform.h:60
unsigned int ubyte32
Definition: cl_platform.h:64
short byte16
Definition: cl_platform.h:61