ASPiK SDK
cstream.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __cstream__
6 #define __cstream__
7 
8 #include "../lib/vstguifwd.h"
9 #include <algorithm>
10 #include <string>
11 #include <limits>
12 
13 namespace VSTGUI {
14 
15 static const uint32_t kStreamIOError = std::numeric_limits<uint32_t>::max ();
16 static const int64_t kStreamSeekError = -1;
17 
22 {
23 public:
24  explicit OutputStream (ByteOrder byteOrder = kNativeByteOrder) : byteOrder (byteOrder) {}
25  virtual ~OutputStream () noexcept = default;
26 
27  ByteOrder getByteOrder () const { return byteOrder; }
28  void setByteOrder (ByteOrder newByteOrder) { byteOrder = newByteOrder; }
29 
30  bool operator<< (const int8_t& input);
31  bool operator<< (const uint8_t& input);
32  bool operator<< (const int16_t& input);
33  bool operator<< (const uint16_t& input);
34  bool operator<< (const int32_t& input);
35  bool operator<< (const uint32_t& input);
36  bool operator<< (const int64_t& input);
37  bool operator<< (const uint64_t& input);
38  bool operator<< (const double& input);
39 
40  virtual bool operator<< (const std::string& str) = 0;
41 
42  virtual uint32_t writeRaw (const void* buffer, uint32_t size) = 0;
43 private:
44  ByteOrder byteOrder;
45 };
46 
51 {
52 public:
53  explicit InputStream (ByteOrder byteOrder = kNativeByteOrder) : byteOrder (byteOrder) {}
54  virtual ~InputStream () noexcept = default;
55 
56  ByteOrder getByteOrder () const { return byteOrder; }
57  void setByteOrder (ByteOrder newByteOrder) { byteOrder = newByteOrder; }
58 
59  bool operator>> (int8_t& output);
60  bool operator>> (uint8_t& output);
61  bool operator>> (int16_t& output);
62  bool operator>> (uint16_t& output);
63  bool operator>> (int32_t& output);
64  bool operator>> (uint32_t& output);
65  bool operator>> (int64_t& output);
66  bool operator>> (uint64_t& output);
67  bool operator>> (double& output);
68 
69  virtual bool operator>> (std::string& string) = 0;
70 
71  virtual uint32_t readRaw (void* buffer, uint32_t size) = 0;
72 private:
73  ByteOrder byteOrder;
74 };
75 
80 {
81 public:
82  virtual ~SeekableStream () noexcept = default;
83  enum SeekMode {
84  kSeekSet,
85  kSeekCurrent,
86  kSeekEnd
87  };
88 
89  virtual int64_t seek (int64_t pos, SeekMode mode) = 0;
90  virtual int64_t tell () const = 0;
91  virtual void rewind () = 0;
92 };
93 
97 class CMemoryStream : virtual public OutputStream, virtual public InputStream, public SeekableStream, public AtomicReferenceCounted
98 {
99 public:
100  CMemoryStream (uint32_t initialSize = 1024, uint32_t delta = 1024, bool binaryMode = true, ByteOrder byteOrder = kNativeByteOrder);
101  CMemoryStream (const int8_t* buffer, uint32_t bufferSize, bool binaryMode = true, ByteOrder byteOrder = kNativeByteOrder);
102  ~CMemoryStream () noexcept override;
103 
104  uint32_t writeRaw (const void* buffer, uint32_t size) override;
105  uint32_t readRaw (void* buffer, uint32_t size) override;
106 
107  int64_t seek (int64_t pos, SeekMode mode) override;
108  int64_t tell () const override { return static_cast<int64_t> (pos); }
109  void rewind () override { pos = 0; }
110 
111  const int8_t* getBuffer () const { return buffer; }
112 
113  bool operator<< (const std::string& str) override;
114  bool operator>> (std::string& string) override;
115 
116  bool end (); // write a zero byte if binaryMode is false
117 protected:
118  bool resize (uint32_t newSize);
119 
120  int8_t* buffer;
121  uint32_t bufferSize;
122  uint32_t size;
123  uint32_t pos;
124  uint32_t delta;
125  bool binaryMode;
126  bool ownsBuffer;
127 };
128 
133 {
134 public:
135  CFileStream ();
136  ~CFileStream () noexcept override;
137 
138  enum {
139  kReadMode = 1 << 0,
140  kWriteMode = 1 << 1,
141  kTruncateMode = 1 << 2,
142  kBinaryMode = 1 << 3
143  };
144 
145  bool open (UTF8StringPtr path, int32_t mode, ByteOrder byteOrder = kNativeByteOrder);
146 
147  uint32_t writeRaw (const void* buffer, uint32_t size) override;
148  uint32_t readRaw (void* buffer, uint32_t size) override;
149 
150  int64_t seek (int64_t pos, SeekMode mode) override;
151  int64_t tell () const override;
152  void rewind () override;
153 
154  bool operator<< (const std::string& str) override;
155  bool operator>> (std::string& string) override;
156 protected:
157  FILE* stream;
158  int32_t openMode;
159 };
160 
161 static const int8_t unixPathSeparator = '/';
162 static const int8_t windowsPathSeparator = '\\';
166 inline void unixfyPath (std::string& path)
167 {
168  std::replace (path.begin (), path.end (), windowsPathSeparator, unixPathSeparator);
169 }
170 
171 //------------------------------------------------------------------------
172 inline bool removeLastPathComponent (std::string& path)
173 {
174  size_t sepPos = path.find_last_of (unixPathSeparator);
175  if (sepPos != std::string::npos)
176  {
177  path.erase (sepPos);
178  return true;
179  }
180  return false;
181 }
182 
183 //------------------------------------------------------------------------
184 inline bool pathIsAbsolute (const std::string& path)
185 {
186 #if MAC || LINUX
187  return !path.empty () && path[0] == unixPathSeparator;
188 #elif WINDOWS
189  return path.length () >= 2 && path[1] == ':';
190 #else
191  return false;
192 #endif
193 }
194 
199 {
200 public:
201  explicit CResourceInputStream (ByteOrder byteOrder = kNativeByteOrder);
202  ~CResourceInputStream () noexcept override;
203 
204  bool open (const CResourceDescription& res);
205 
206  bool operator>> (std::string& string) override { return false; }
207  uint32_t readRaw (void* buffer, uint32_t size) override;
208  int64_t seek (int64_t pos, SeekMode mode) override;
209  int64_t tell () const override;
210  void rewind () override;
211 protected:
212  void* platformHandle;
213 };
214 
215 
216 } // namespace
217 
218 #endif
Describes a resource by name or by ID.
Definition: cresourcedescription.h:16
Definition: cstream.h:50
int64_t seek(int64_t pos, SeekMode mode) override
returns -1 if seek fails otherwise new position
Definition: cstream.cpp:109
Definition: cstream.h:79
Definition: cstream.h:21
Definition: cstream.h:132
Definition: customcontrols.cpp:8
int64_t seek(int64_t pos, SeekMode mode) override
returns -1 if seek fails otherwise new position
Definition: cstream.cpp:426
Definition: vstguibase.h:247
Definition: cstream.h:198
Definition: cstream.h:97
int64_t seek(int64_t pos, SeekMode mode) override
returns -1 if seek fails otherwise new position
Definition: cstream.cpp:261
virtual int64_t seek(int64_t pos, SeekMode mode)=0
returns -1 if seek fails otherwise new position