StreamEx.pas
上传用户:ctlcnc
上传日期:2021-12-10
资源大小:4933k
文件大小:8k
源码类别:

2D图形编程

开发平台:

Delphi

  1. unit StreamEx;
  2. //---------------------------------------------------------------------------
  3. interface
  4. //---------------------------------------------------------------------------
  5. uses
  6.  Types, Classes;
  7. //---------------------------------------------------------------------------
  8. function stReadLongint(Stream: TStream): Longint;
  9. procedure stWriteLongint(Stream: TStream; const Value: Longint);
  10. function stReadLongword(Stream: TStream): Longword;
  11. procedure stWriteLongword(Stream: TStream; const Value: Longword);
  12. function stReadInt64(Stream: TStream): Int64;
  13. procedure stWriteInt64(Stream: TStream; const Value: Int64);
  14. function stReadByte(Stream: TStream): Byte;
  15. procedure stWriteByte(Stream: TStream; const Value: Byte);
  16. function stReadShortInt(Stream: TStream): ShortInt;
  17. procedure stWriteShortInt(Stream: TStream; const Value: ShortInt);
  18. function stReadWord(Stream: TStream): Word;
  19. procedure stWriteWord(Stream: TStream; const Value: Word);
  20. function stReadSmallInt(Stream: TStream): SmallInt;
  21. procedure stWriteSmallInt(Stream: TStream; const Value: SmallInt);
  22. function stReadChar(Stream: TStream): Char;
  23. function stReadSingle(Stream: TStream): Single;
  24. procedure stWriteSingle(Stream: TStream; const Value: Single);
  25. function stReadDouble(Stream: TStream): Double;
  26. procedure stWriteDouble(Stream: TStream; const Value: Double);
  27. procedure stWriteChar(Stream: TStream; const Value: Char);
  28. function stReadBool(Stream: TStream): Boolean;
  29. procedure stWriteBool(Stream: TStream; const Value: Boolean);
  30. function stReadPoint(Stream: TStream): TPoint;
  31. procedure stWritePoint(Stream: TStream; const Value: TPoint);
  32. function stReadString(Stream: TStream): string;
  33. procedure stWriteString(Stream: TStream; const Value: string);
  34. // Floating-point 3:4 (-8.0 to 7.9375)
  35. procedure stWriteFloat34(Stream: TStream; Value: Single);
  36. function stReadFloat34(Stream: TStream): Single;
  37. //----------------------------------------------------------------------------
  38. implementation
  39. //---------------------------------------------------------------------------
  40. function stReadLongint(Stream: TStream): Longint;
  41. begin
  42.  // read a single integer
  43.  Stream.Read(Result, SizeOf(Longint));
  44. end;
  45. //---------------------------------------------------------------------------
  46. procedure stWriteLongint(Stream: TStream; const Value: Longint);
  47. begin
  48.  // write a single integer
  49.  Stream.Write(Value, SizeOf(Longint));
  50. end;
  51. //---------------------------------------------------------------------------
  52. function stReadLongword(Stream: TStream): Longword;
  53. begin
  54.  // read a single Longword
  55.  Stream.Read(Result, SizeOf(Longword));
  56. end;
  57. //---------------------------------------------------------------------------
  58. procedure stWriteLongword(Stream: TStream; const Value: Longword);
  59. begin
  60.  // write a single Longword
  61.  Stream.Write(Value, SizeOf(Longword));
  62. end;
  63. //---------------------------------------------------------------------------
  64. function stReadInt64(Stream: TStream): Int64;
  65. begin
  66.  // read a single 64-bit integer
  67.  Stream.Read(Result, SizeOf(Int64));
  68. end;
  69. //---------------------------------------------------------------------------
  70. procedure stWriteInt64(Stream: TStream; const Value: Int64);
  71. begin
  72.  // write a single 64-bit integer
  73.  Stream.Write(Value, SizeOf(Int64));
  74. end;
  75. //---------------------------------------------------------------------------
  76. function stReadChar(Stream: TStream): Char;
  77. begin
  78.  // read a single character
  79.  Stream.Read(Result, SizeOf(Char));
  80. end;
  81. //---------------------------------------------------------------------------
  82. procedure stWriteChar(Stream: TStream; const Value: Char);
  83. begin
  84.  // write a single character
  85.  Stream.Write(Value, SizeOf(Char));
  86. end;
  87. //---------------------------------------------------------------------------
  88. function stReadByte(Stream: TStream): Byte;
  89. begin
  90.  // read a single character
  91.  Stream.Read(Result, SizeOf(Byte));
  92. end;
  93. //---------------------------------------------------------------------------
  94. procedure stWriteByte(Stream: TStream; const Value: Byte);
  95. begin
  96.  // write a single character
  97.  Stream.Write(Value, SizeOf(Byte));
  98. end;
  99. //---------------------------------------------------------------------------
  100. function stReadShortInt(Stream: TStream): ShortInt;
  101. begin
  102.  // read a single character
  103.  Stream.Read(Result, SizeOf(ShortInt));
  104. end;
  105. //---------------------------------------------------------------------------
  106. procedure stWriteShortInt(Stream: TStream; const Value: ShortInt);
  107. begin
  108.  // write a single character
  109.  Stream.Write(Value, SizeOf(ShortInt));
  110. end;
  111. //---------------------------------------------------------------------------
  112. function stReadWord(Stream: TStream): Word;
  113. begin
  114.  // read a single character
  115.  Stream.Read(Result, SizeOf(Word));
  116. end;
  117. //---------------------------------------------------------------------------
  118. procedure stWriteWord(Stream: TStream; const Value: Word);
  119. begin
  120.  // write a single character
  121.  Stream.Write(Value, SizeOf(Word));
  122. end;
  123. //---------------------------------------------------------------------------
  124. function stReadSmallInt(Stream: TStream): SmallInt;
  125. begin
  126.  Stream.Read(Result, SizeOf(Word));
  127. end;
  128. //---------------------------------------------------------------------------
  129. procedure stWriteSmallInt(Stream: TStream; const Value: SmallInt);
  130. begin
  131.  Stream.Write(Value, SizeOf(SmallInt));
  132. end;
  133. //---------------------------------------------------------------------------
  134. function stReadDouble(Stream: TStream): Double;
  135. begin
  136.  // read a single character
  137.  Stream.Read(Result, SizeOf(Double));
  138. end;
  139. //---------------------------------------------------------------------------
  140. procedure stWriteDouble(Stream: TStream; const Value: Double);
  141. begin
  142.  // write a single character
  143.  Stream.Write(Value, SizeOf(Double));
  144. end;
  145. //---------------------------------------------------------------------------
  146. function stReadSingle(Stream: TStream): Single;
  147. begin
  148.  // read a single character
  149.  Stream.Read(Result, SizeOf(Single));
  150. end;
  151. //---------------------------------------------------------------------------
  152. procedure stWriteSingle(Stream: TStream; const Value: Single);
  153. begin
  154.  // write a single character
  155.  Stream.Write(Value, SizeOf(Single));
  156. end;
  157. //---------------------------------------------------------------------------
  158. function stReadBool(Stream: TStream): Boolean;
  159. begin
  160.  Stream.Read(Result, SizeOf(Boolean));
  161. end;
  162. //---------------------------------------------------------------------------
  163. procedure stWriteBool(Stream: TStream; const Value: Boolean);
  164. begin
  165.  Stream.Write(Value, SizeOf(Boolean));
  166. end;
  167. //---------------------------------------------------------------------------
  168. function stReadPoint(Stream: TStream): TPoint;
  169. begin
  170.  Stream.Read(Result, SizeOf(TPoint));
  171. end;
  172. //---------------------------------------------------------------------------
  173. procedure stWritePoint(Stream: TStream; const Value: TPoint);
  174. begin
  175.  Stream.Write(Value, SizeOf(TPoint));
  176. end;
  177. //---------------------------------------------------------------------------
  178. function stReadString(Stream: TStream): string;
  179. var
  180.  Count, i: Integer;
  181. begin
  182.  // read string length
  183.  Count:= stReadLongint(Stream);
  184.  // define result length
  185.  SetLength(Result, Count);
  186.  // read char by char
  187.  for i:= 0 to Count - 1 do
  188.   Result[i + 1]:= stReadChar(Stream);
  189. end;
  190. //---------------------------------------------------------------------------
  191. procedure stWriteString(Stream: TStream; const Value: string);
  192. var
  193.  i: Integer;
  194. begin
  195.  // write string length
  196.  stWriteLongint(Stream, Length(Value));
  197.  // write char by char
  198.  for i:= 0 to Length(Value) - 1 do
  199.   stWriteChar(Stream, Value[i + 1]);
  200. end;
  201. //----------------------------------------------------------------------------
  202. procedure stWriteFloat34(Stream: TStream; Value: Single);
  203. var
  204.  Aux: Integer;
  205. begin
  206.  Aux:= Round(Value * 16.0);
  207.  if (Aux > 127) then Aux:= 127;
  208.  if (Aux < -128) then Aux:= -128;
  209.  stWriteShortInt(Stream, Aux);
  210. end;
  211. //----------------------------------------------------------------------------
  212. function stReadFloat34(Stream: TStream): Single;
  213. begin
  214.  Result:= stReadShortInt(Stream) / 16.0;
  215. end;
  216. //----------------------------------------------------------------------------
  217. end.