Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.9KB

  1. //------------------------------------------------------------------------
  2. // Project : SDK Base
  3. // Version : 1.0
  4. //
  5. // Category : Helpers
  6. // Filename : base/source/fstreamer.h
  7. // Created by : Steinberg, 12/2005
  8. // Description : Extract of typed stream i/o methods from FStream
  9. //
  10. //-----------------------------------------------------------------------------
  11. // LICENSE
  12. // (c) 2017, Steinberg Media Technologies GmbH, All Rights Reserved
  13. //-----------------------------------------------------------------------------
  14. // Redistribution and use in source and binary forms, with or without modification,
  15. // are permitted provided that the following conditions are met:
  16. //
  17. // * Redistributions of source code must retain the above copyright notice,
  18. // this list of conditions and the following disclaimer.
  19. // * Redistributions in binary form must reproduce the above copyright notice,
  20. // this list of conditions and the following disclaimer in the documentation
  21. // and/or other materials provided with the distribution.
  22. // * Neither the name of the Steinberg Media Technologies nor the names of its
  23. // contributors may be used to endorse or promote products derived from this
  24. // software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  27. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  29. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  30. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  34. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. // OF THE POSSIBILITY OF SUCH DAMAGE.
  36. //-----------------------------------------------------------------------------
  37. #pragma once
  38. #include "pluginterfaces/base/funknown.h"
  39. namespace Steinberg {
  40. //------------------------------------------------------------------------
  41. enum FSeekMode
  42. {
  43. kSeekSet,
  44. kSeekCurrent,
  45. kSeekEnd
  46. };
  47. //------------------------------------------------------------------------
  48. // FStreamer
  49. //------------------------------------------------------------------------
  50. /** Byteorder-aware base class for typed stream i/o. */
  51. //------------------------------------------------------------------------
  52. class FStreamer
  53. {
  54. public:
  55. //------------------------------------------------------------------------
  56. FStreamer (int16 byteOrder = BYTEORDER);
  57. virtual ~FStreamer () {}
  58. /** @name Implementing class must override. */
  59. ///@{
  60. virtual TSize readRaw (void*, TSize) = 0; ///< Read one buffer of size.
  61. virtual TSize writeRaw (const void*, TSize) = 0; ///< Write one buffer of size.
  62. virtual int64 seek (int64, FSeekMode) = 0; ///< Set file position for stream.
  63. virtual int64 tell () = 0; ///< Return current file position.
  64. ///@}
  65. /** @name Streams are byteOrder aware. */
  66. ///@{
  67. inline void setByteOrder (int32 e) { byteOrder = (int16)e; }
  68. inline int32 getByteOrder () { return byteOrder; }
  69. ///@}
  70. /** @name read and write int8 and char. */
  71. ///@{
  72. bool writeChar8 (char8);
  73. bool readChar8 (char8&);
  74. bool writeUChar8 (unsigned char);
  75. bool readUChar8 (unsigned char&);
  76. bool writeChar16 (char16 c);
  77. bool readChar16 (char16& c);
  78. bool writeInt8 (int8 c){return writeChar8 (c);}
  79. bool readInt8 (int8& c){return readChar8 (c);}
  80. bool writeInt8u (uint8 c){return writeUChar8 (c);}
  81. bool readInt8u (uint8& c){return readUChar8 (c);}
  82. ///@}
  83. /** @name read and write int16. */
  84. ///@{
  85. bool writeInt16 (int16);
  86. bool readInt16 (int16&);
  87. bool writeInt16Array (const int16* array, int32 count);
  88. bool readInt16Array (int16* array, int32 count);
  89. bool writeInt16u (uint16);
  90. bool readInt16u (uint16&);
  91. bool writeInt16uArray (const uint16* array, int32 count);
  92. bool readInt16uArray (uint16* array, int32 count);
  93. ///@}
  94. /** @name read and write int32. */
  95. ///@{
  96. bool writeInt32 (int32);
  97. bool readInt32 (int32&);
  98. bool writeInt32Array (const int32* array, int32 count);
  99. bool readInt32Array (int32* array, int32 count);
  100. bool writeInt32u (uint32);
  101. bool readInt32u (uint32&);
  102. bool writeInt32uArray (const uint32* array, int32 count);
  103. bool readInt32uArray (uint32* array, int32 count);
  104. ///@}
  105. /** @name read and write int64. */
  106. ///@{
  107. bool writeInt64 (int64);
  108. bool readInt64 (int64&);
  109. bool writeInt64Array (const int64* array, int32 count);
  110. bool readInt64Array (int64* array, int32 count);
  111. bool writeInt64u (uint64);
  112. bool readInt64u (uint64&);
  113. bool writeInt64uArray (const uint64* array, int32 count);
  114. bool readInt64uArray (uint64* array, int32 count);
  115. ///@}
  116. /** @name read and write float and float array. */
  117. ///@{
  118. bool writeFloat (float);
  119. bool readFloat (float&);
  120. bool writeFloatArray (const float* array, int32 count);
  121. bool readFloatArray (float* array, int32 count);
  122. ///@}
  123. /** @name read and write double and double array. */
  124. ///@{
  125. bool writeDouble (double);
  126. bool readDouble (double&);
  127. bool writeDoubleArray (const double* array, int32 count);
  128. bool readDoubleArray (double* array, int32 count);
  129. ///@}
  130. /** @name read and write Boolean. */
  131. ///@{
  132. bool writeBool (bool); ///< Write one boolean
  133. bool readBool (bool&); ///< Read one bool.
  134. ///@}
  135. /** @name read and write Strings. */
  136. ///@{
  137. TSize writeString8 (const char8* ptr, bool terminate = false); ///< a direct output function writing only one string (ascii 8bit)
  138. TSize readString8 (char8* ptr, TSize size); ///< a direct input function reading only one string (ascii) (ended by a \n or \0 or eof)
  139. bool writeStr8 (const char8* ptr); ///< write a string length (strlen) and string itself
  140. char8* readStr8 (); ///< read a string length and string text (The return string must be deleted when use is finished)
  141. static int32 getStr8Size (const char8* ptr); ///< returns the size of a saved string
  142. bool writeStringUtf8 (const tchar* ptr); ///< always terminated, converts to utf8 if non ascii characters are in string
  143. int32 readStringUtf8 (tchar* ptr, int32 maxSize); ///< read a UTF8 string
  144. ///@}
  145. bool skip (uint32 bytes);
  146. bool pad (uint32 bytes);
  147. //------------------------------------------------------------------------
  148. protected:
  149. int16 byteOrder;
  150. };
  151. //------------------------------------------------------------------------
  152. /** FStreamSizeHolder Declaration
  153. remembers size of stream chunk for backward compatibility.
  154. <b>Example:</b>
  155. @code
  156. externalize (a)
  157. {
  158. FStreamSizeHolder sizeHolder;
  159. sizeHolder.beginWrite (); // sets start mark, writes dummy size
  160. a << ....
  161. sizeHolder.endWrite (); // jumps to start mark, updates size, jumps back here
  162. }
  163. internalize (a)
  164. {
  165. FStreamSizeHolder sizeHolder;
  166. sizeHolder.beginRead (); // reads size, mark
  167. a >> ....
  168. sizeHolder.endRead (); // jumps forward if new version has larger size
  169. }
  170. @endcode
  171. */
  172. //------------------------------------------------------------------------
  173. class FStreamSizeHolder
  174. {
  175. public:
  176. FStreamSizeHolder (FStreamer &s);
  177. void beginWrite (); ///< remembers position and writes 0
  178. int32 endWrite (); ///< writes and returns size (since the start marker)
  179. int32 beginRead (); ///< returns size
  180. void endRead (); ///< jump to end of chunk
  181. protected:
  182. FStreamer &stream;
  183. int64 sizePos;
  184. };
  185. class IBStream;
  186. //------------------------------------------------------------------------
  187. // IBStreamer
  188. //------------------------------------------------------------------------
  189. /** Wrapper class for typed reading/writing from or to IBStream.
  190. Can be used framework-independent in Plug-ins. */
  191. //------------------------------------------------------------------------
  192. class IBStreamer: public FStreamer
  193. {
  194. public:
  195. //------------------------------------------------------------------------
  196. /** Constructor for a given IBSTream and a byteOrder. */
  197. IBStreamer (IBStream* stream, int16 byteOrder = BYTEORDER);
  198. IBStream* getStream () { return stream; } ///< Returns the associated IBStream.
  199. // FStreamer overrides:
  200. TSize readRaw (void*, TSize) SMTG_OVERRIDE; ///< Read one buffer of size.
  201. TSize writeRaw (const void*, TSize) SMTG_OVERRIDE; ///< Write one buffer of size.
  202. int64 seek (int64, FSeekMode) SMTG_OVERRIDE; ///< Set file position for stream.
  203. int64 tell () SMTG_OVERRIDE; ///< Return current file position.
  204. //------------------------------------------------------------------------
  205. protected:
  206. IBStream* stream;
  207. };
  208. //------------------------------------------------------------------------
  209. } // namespace Steinberg