streamcoll.pp 8.3 KB

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