streamcoll.pp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {$ifndef ver2_0}
  11. {$fatal This unit is only for compiling with 2.0.x, use the streamcoll from the FCL}
  12. {$endif}
  13. {$mode objfpc}
  14. {$h+}
  15. unit streamcoll;
  16. interface
  17. uses
  18. Classes,SysUtils;
  19. type
  20. TStreamCollectionItem = Class(TCollectionItem)
  21. Protected
  22. Procedure WriteInteger(S : TStream; AValue : Integer);
  23. Procedure WriteBoolean(S : TStream; AValue : Boolean);
  24. Procedure WriteString(S : TStream; AValue : String);
  25. Procedure WriteCurrency(S : TStream; AValue : Currency);
  26. Procedure WriteDateTime(S : TStream; AValue : TDateTime);
  27. Procedure WriteFloat(S : TStream; AValue : Double);
  28. Function ReadInteger(S : TStream) : Integer;
  29. Function ReadBoolean(S : TStream) : Boolean;
  30. Function ReadString(S : TStream) : String;
  31. Function ReadCurrency(S : TStream) : Currency;
  32. Function ReadDateTime(S : TStream) : TDateTime;
  33. Function ReadFloat(S : TStream) : Double;
  34. Procedure LoadFromStream(S : TStream; Streamversion : Integer); virtual; abstract;
  35. Procedure SaveToStream(S : TStream); virtual; abstract;
  36. end;
  37. TStreamCollection = Class(TCollection)
  38. Private
  39. FStreaming : Boolean;
  40. Protected
  41. Procedure WriteInteger(S : TStream; AValue : Integer);
  42. Procedure WriteBoolean(S : TStream; AValue : Boolean);
  43. Procedure WriteString(S : TStream; AValue : String);
  44. Procedure WriteCurrency(S : TStream; AValue : Currency);
  45. Procedure WriteDateTime(S : TStream; AValue : TDateTime);
  46. Procedure WriteFloat(S : TStream; AValue : Double);
  47. Function ReadInteger(S : TStream) : Integer;
  48. Function ReadBoolean(S : TStream) : Boolean;
  49. Function ReadString(S : TStream) : String;
  50. Function ReadCurrency(S : TStream) : Currency;
  51. Function ReadDateTime(S : TStream) : TDateTime;
  52. Function ReadFloat(S : TStream) : Double;
  53. Procedure DoSaveToStream(S : TStream); virtual;
  54. Function CurrentStreamVersion : Integer; Virtual;
  55. Procedure DoLoadFromStream(S : TStream; Streamversion : Integer); virtual;
  56. Public
  57. Procedure LoadFromStream(S : TStream);
  58. Procedure SaveToStream(S : TStream);
  59. Property Streaming : Boolean Read FStreaming;
  60. end;
  61. EStreamColl = Class(Exception);
  62. Procedure ColWriteInteger(S : TStream; AValue : Integer);
  63. Procedure ColWriteBoolean(S : TStream; AValue : Boolean);
  64. Procedure ColWriteString(S : TStream; AValue : String);
  65. Procedure ColWriteCurrency(S : TStream; AValue : Currency);
  66. Procedure ColWriteDateTime(S : TStream; AValue : TDateTime);
  67. Procedure ColWriteFloat(S : TStream; AValue : Double);
  68. Function ColReadInteger(S : TStream) : Integer;
  69. Function ColReadBoolean(S : TStream) : Boolean;
  70. Function ColReadString(S : TStream) : String;
  71. Function ColReadCurrency(S : TStream) : Currency;
  72. Function ColReadDateTime(S : TStream) : TDateTime;
  73. Function ColReadFloat(S : TStream) : Double;
  74. implementation
  75. Resourcestring
  76. SErrIllegalStreamVersion = 'Illegal stream version: %d > %d.';
  77. Procedure ColWriteInteger(S : TStream; AValue : Integer);
  78. begin
  79. S.WriteBuffer(AValue,SizeOf(Integer));
  80. end;
  81. Procedure ColWriteBoolean(S : TStream; AValue : Boolean);
  82. begin
  83. ColWriteInteger(S,Ord(AValue));
  84. end;
  85. Procedure ColWriteString(S : TStream; AValue : String);
  86. Var
  87. L : Integer;
  88. begin
  89. L:=Length(AValue);
  90. ColWriteInteger(S,L);
  91. If (L>0) then
  92. S.WriteBuffer(AValue[1],L);
  93. end;
  94. Procedure ColWriteCurrency(S : TStream; AValue : Currency);
  95. begin
  96. S.WriteBuffer(AValue,SizeOf(Currency));
  97. end;
  98. Procedure ColWriteDateTime(S : TStream; AValue : TDateTime);
  99. begin
  100. S.WriteBuffer(AValue,SizeOf(TDateTime));
  101. end;
  102. Procedure ColWriteFloat(S : TStream; AValue : Double);
  103. begin
  104. S.WriteBuffer(AValue,SizeOf(Double));
  105. end;
  106. Function ColReadInteger(S : TStream) : Integer;
  107. begin
  108. S.ReadBuffer(Result,SizeOf(Integer));
  109. end;
  110. Function ColReadBoolean(S : TStream) : Boolean;
  111. Var
  112. I : Integer;
  113. begin
  114. S.ReadBuffer(I,SizeOf(Integer));
  115. Result:=(I<>0);
  116. end;
  117. Function ColReadString(S : TStream) : String;
  118. Var
  119. L : Integer;
  120. begin
  121. L:=ColReadInteger(S);
  122. SetLength(Result,L);
  123. If (L>0) then
  124. S.ReadBuffer(Result[1],L);
  125. end;
  126. Function ColReadCurrency(S : TStream) : Currency;
  127. begin
  128. S.ReadBuffer(Result,SizeOf(Currency));
  129. end;
  130. Function ColReadDateTime(S : TStream) : TDateTime;
  131. begin
  132. S.ReadBuffer(Result,SizeOf(TDateTime));
  133. end;
  134. Function ColReadFloat(S : TStream) : Double;
  135. begin
  136. S.ReadBuffer(Result,SizeOf(Double));
  137. end;
  138. { TStreamCollectionItem }
  139. function TStreamCollectionItem.ReadBoolean(S: TStream): Boolean;
  140. begin
  141. Result:=ColReadBoolean(S);
  142. end;
  143. function TStreamCollectionItem.ReadCurrency(S: TStream): Currency;
  144. begin
  145. Result:=ColReadCurrency(S);
  146. end;
  147. function TStreamCollectionItem.ReadDateTime(S: TStream): TDateTime;
  148. begin
  149. Result:=ColReadDateTime(S);
  150. end;
  151. function TStreamCollectionItem.ReadFloat(S: TStream): Double;
  152. begin
  153. Result:=ColReadFloat(S);
  154. end;
  155. function TStreamCollectionItem.ReadInteger(S: TStream): Integer;
  156. begin
  157. Result:=ColReadinteger(S);
  158. end;
  159. function TStreamCollectionItem.ReadString(S: TStream): String;
  160. begin
  161. Result:=ColReadString(S);
  162. end;
  163. procedure TStreamCollectionItem.WriteBoolean(S: TStream; AValue: Boolean);
  164. begin
  165. ColWriteBoolean(S,AValue);
  166. end;
  167. procedure TStreamCollectionItem.WriteCurrency(S: TStream;
  168. AValue: Currency);
  169. begin
  170. ColWriteCurrency(S,AValue);
  171. end;
  172. procedure TStreamCollectionItem.WriteDateTime(S: TStream;
  173. AValue: TDateTime);
  174. begin
  175. ColWriteDateTime(S,AValue);
  176. end;
  177. procedure TStreamCollectionItem.WriteFloat(S: TStream; AValue: Double);
  178. begin
  179. ColWriteFloat(S,AValue);
  180. end;
  181. procedure TStreamCollectionItem.WriteInteger(S: TStream; AValue: Integer);
  182. begin
  183. ColWriteInteger(S,AValue);
  184. end;
  185. procedure TStreamCollectionItem.WriteString(S: TStream; AValue: String);
  186. begin
  187. ColWriteString(S,AValue);
  188. end;
  189. { TStreamCollection }
  190. function TStreamCollection.CurrentStreamVersion: Integer;
  191. begin
  192. Result:=0;
  193. end;
  194. procedure TStreamCollection.DoLoadFromStream(S: TStream;
  195. Streamversion: Integer);
  196. begin
  197. If (Streamversion>CurrentStreamVersion) then
  198. Raise EStreamColl.CreateFmt(SErrIllegalStreamVersion,[Streamversion,CurrentStreamVersion]);
  199. end;
  200. procedure TStreamCollection.DoSaveToStream(S: TStream);
  201. begin
  202. // Do nothing.
  203. end;
  204. procedure TStreamCollection.LoadFromStream(S: TStream);
  205. Var
  206. I,V,C : Integer;
  207. begin
  208. FStreaming:=True;
  209. Try
  210. V:=ReadInteger(S);
  211. DoLoadFromStream(S,V);
  212. Clear;
  213. C:=ReadInteger(S);
  214. For I:=1 to C do
  215. With Add as TStreamCollectionItem do
  216. LoadFromStream(S,V);
  217. Finally
  218. FStreaming:=False;
  219. end;
  220. end;
  221. function TStreamCollection.ReadBoolean(S: TStream): Boolean;
  222. begin
  223. Result:=ColReadBoolean(S);
  224. end;
  225. function TStreamCollection.ReadCurrency(S: TStream): Currency;
  226. begin
  227. Result:=ColReadCurrency(S);
  228. end;
  229. function TStreamCollection.ReadDateTime(S: TStream): TDateTime;
  230. begin
  231. Result:=ColReadDateTime(S);
  232. end;
  233. function TStreamCollection.ReadFloat(S: TStream): Double;
  234. begin
  235. Result:=ColReadFloat(S);
  236. end;
  237. function TStreamCollection.ReadInteger(S: TStream): Integer;
  238. begin
  239. Result:=ColReadInteger(S);
  240. end;
  241. function TStreamCollection.ReadString(S: TStream): String;
  242. begin
  243. Result:=ColReadString(S);
  244. end;
  245. procedure TStreamCollection.SaveToStream(S: TStream);
  246. Var
  247. I : Integer;
  248. begin
  249. FStreaming:=True;
  250. Try
  251. WriteInteger(S,CurrentStreamVersion);
  252. DoSaveToStream(S);
  253. WriteInteger(S,Count);
  254. For I:=0 to Count-1 do
  255. With TStreamCollectionItem(Items[i]) do
  256. SaveToStream(S);
  257. Finally
  258. FStreaming:=False;
  259. end;
  260. end;
  261. procedure TStreamCollection.WriteBoolean(S: TStream; AValue: Boolean);
  262. begin
  263. ColWriteBoolean(S,AValue);
  264. end;
  265. procedure TStreamCollection.WriteCurrency(S: TStream; AValue: Currency);
  266. begin
  267. ColWriteCurrency(S,AValue);
  268. end;
  269. procedure TStreamCollection.WriteDateTime(S: TStream; AValue: TDateTime);
  270. begin
  271. ColWriteDateTime(S,AValue);
  272. end;
  273. procedure TStreamCollection.WriteFloat(S: TStream; AValue: Double);
  274. begin
  275. ColWriteFloat(S,AValue);
  276. end;
  277. procedure TStreamCollection.WriteInteger(S: TStream; AValue: Integer);
  278. begin
  279. ColWriteInteger(S,AValue);
  280. end;
  281. procedure TStreamCollection.WriteString(S: TStream; AValue: String);
  282. begin
  283. ColWriteString(S,AValue);
  284. end;
  285. end.