RavEngine
Loading...
Searching...
No Matches
stream.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_BASE_IO_STREAM_H_
29#define OZZ_OZZ_BASE_IO_STREAM_H_
30
31// Provides Stream interface used to read/write a memory buffer or a file with
32// Crt fread/fwrite/fseek/ftell like functions.
33
34#include <cstddef>
35
36#include "ozz/base/platform.h"
37
38namespace ozz {
39namespace io {
40
41// Declares a stream access interface that conforms with CRT FILE API.
42// This interface should be used to remap io operations.
43class OZZ_BASE_DLL Stream {
44 public:
45 // Tests whether a file is opened.
46 virtual bool opened() const = 0;
47
48 // Reads _size bytes of data to _buffer from the stream. _buffer must be big
49 // enough to store _size bytes. The position indicator of the stream is
50 // advanced by the total amount of bytes read.
51 // Returns the number of bytes actually read, which may be less than _size.
52 virtual size_t Read(void* _buffer, size_t _size) = 0;
53
54 // Writes _size bytes of data from _buffer to the stream. The position
55 // indicator of the stream is advanced by the total number of bytes written.
56 // Returns the number of bytes actually written, which may be less than _size.
57 virtual size_t Write(const void* _buffer, size_t _size) = 0;
58
59 // Declares seeking origin enumeration.
60 enum Origin {
61 kCurrent, // Current position of the stream pointer.
62 kEnd, // End of stream.
63 kSet, // Beginning of stream.
64 };
65 // Sets the position indicator associated with the stream to a new position
66 // defined by adding _offset to a reference position specified by _origin.
67 // Returns a zero value if successful, otherwise returns a non-zero value.
68 virtual int Seek(int _offset, Origin _origin) = 0;
69
70 // Returns the current value of the position indicator of the stream.
71 // Returns -1 if an error occurs.
72 virtual int Tell() const = 0;
73
74 // Returns the current size of the stream.
75 virtual size_t Size() const = 0;
76
77 protected:
78 Stream() {}
79
80 // Required virtual destructor.
81 virtual ~Stream() {}
82
83 private:
84 Stream(const Stream&);
85 void operator=(const Stream&);
86};
87
88// Implements Stream of type File.
89class OZZ_BASE_DLL File : public Stream {
90 public:
91 // Test if a file at path _filename exists.
92 // Note that this function is costly. If you aim to open the file right after,
93 // then open it and use File::opened() to test if it's actually existing.
94 static bool Exist(const char* _filename);
95
96 // Open a file at path _filename with mode * _mode, in conformance with fopen
97 // specifications.
98 // Use opened() function to test opening result.
99 File(const char* _filename, const char* _mode);
100
101 // Gives _file ownership to the FileStream, which will be in charge of closing
102 // it. _file must be nullptr or a valid std::FILE pointer.
103 explicit File(void* _file);
104
105 // Close the file if it is opened.
106 virtual ~File();
107
108 // Close the file if it is opened.
109 void Close();
110
111 // See Stream::opened for details.
112 virtual bool opened() const;
113
114 // See Stream::Read for details.
115 virtual size_t Read(void* _buffer, size_t _size);
116
117 // See Stream::Write for details.
118 virtual size_t Write(const void* _buffer, size_t _size);
119
120 // See Stream::Seek for details.
121 virtual int Seek(int _offset, Origin _origin);
122
123 // See Stream::Tell for details.
124 virtual int Tell() const;
125
126 // See Stream::Tell for details.
127 virtual size_t Size() const;
128
129 private:
130 // The CRT file pointer.
131 void* file_;
132};
133
134// Implements an in-memory Stream. Allows to use a memory buffer as a Stream.
135// The opening mode is equivalent to fopen w+b (binary read/write).
136class OZZ_BASE_DLL MemoryStream : public Stream {
137 public:
138 // Construct an empty memory stream opened in w+b mode.
139 MemoryStream();
140
141 // Closes the stream and deallocates memory buffer.
142 virtual ~MemoryStream();
143
144 // See Stream::opened for details.
145 virtual bool opened() const;
146
147 // See Stream::Read for details.
148 virtual size_t Read(void* _buffer, size_t _size);
149
150 // See Stream::Write for details.
151 virtual size_t Write(const void* _buffer, size_t _size);
152
153 // See Stream::Seek for details.
154 virtual int Seek(int _offset, Origin _origin);
155
156 // See Stream::Tell for details.
157 virtual int Tell() const;
158
159 // See Stream::Tell for details.
160 virtual size_t Size() const;
161
162 private:
163 // Resizes buffers size to _size bytes. If _size is less than the actual
164 // buffer size, then it remains unchanged.
165 // Returns true if the buffer can contains _size bytes.
166 bool Resize(size_t _size);
167
168 // Size of the buffer increment.
169 static const size_t kBufferSizeIncrement;
170
171 // Maximum stream size.
172 static const size_t kMaxSize;
173
174 // Buffer of data.
175 byte* buffer_;
176
177 // The size of the buffer, which is greater or equal to the size of the data
178 // it contains (end_).
179 size_t alloc_size_;
180
181 // The effective size of the data in the buffer.
182 int end_;
183
184 // The cursor position in the buffer of data.
185 int tell_;
186};
187} // namespace io
188} // namespace ozz
189#endif // OZZ_OZZ_BASE_IO_STREAM_H_
Definition stream.h:89
Definition stream.h:136
Definition stream.h:43