1 | // tStream.h  |
2 | //  |
3 | // A stream is an abstract base class that specifies a consistent and simple API for input and output. For example, if  |
4 | // a tool needs to write files to disk, or memory, or possibly a custom file system, it makes sense to use the tStream  |
5 | // API since it abstracts away the underlying device. Essentially a stream is any object that can have bytes written  |
6 | // to, or read from. All streams support the idea of a current read/write index, along with read and write functions.  |
7 | //  |
8 | // Copyright (c) 2006, 2017 Tristan Grimmer.  |
9 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
10 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
11 | //  |
12 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
13 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
14 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
15 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
16 | // PERFORMANCE OF THIS SOFTWARE.  |
17 |   |
18 | #pragma once  |
19 | namespace tSystem  |
20 | {  |
21 |   |
22 |   |
23 | class tStream  |
24 | {  |
25 | public:  |
26 | enum tMode  |
27 | {  |
28 | tMode_Invalid = 1 << 0,  |
29 | tMode_Null = tMode_Invalid,  |
30 | tMode_Read = 1 << 1,  |
31 | tMode_Write = 1 << 2,  |
32 | };  |
33 | typedef uint32 tModes;  |
34 |   |
35 | tStream(tModes);  |
36 | virtual ~tStream();  |
37 |   |
38 | void SetMode(uint32 modeBits);  |
39 | tModes GetMode() const { return Modes; }  |
40 |   |
41 | int GetPos() const { return Position; }  |
42 | int Tell() const { return GetPos(); }  |
43 |   |
44 | // Returns the number of bytes read from the stream and written to the dest buffer. If an error occurs, -1 is  |
45 | // returned. This can happen if, but not iff: a) The stream is not in read mode. b) 'dest' is null. c) 'numBytes'  |
46 | // is not >= 0, or d) There was a read error.  |
47 | virtual int Read(uint8* dest, int numBytes) = 0;  |
48 |   |
49 | // Returns the number of bytes written to the stream and read from the src buffer. If an error occurs, -1 is  |
50 | // returned. This can happen if, but not iff: a) The stream is not in write mode. b) 'src' is null. c) 'numBytes'  |
51 | // is not >= 0, or d) There was a write error.  |
52 | virtual int Write(const uint8* src, int numBytes) = 0;  |
53 |   |
54 | protected:  |
55 | tModes Modes;  |
56 | int Position;  |
57 | };  |
58 |   |
59 |   |
60 | }  |
61 | |