cstreams.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. This module provides stream classes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cstreams;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils;
  22. {****************************************************************************
  23. TCStream
  24. ****************************************************************************}
  25. {
  26. TCStream is copied directly from classesh.inc from the FCL so
  27. it's compatible with the normal Classes.TStream.
  28. TCFileStream is a merge of THandleStream and TFileStream and updated
  29. to have a 'file' type instead of Handle.
  30. TCCustomMemoryStream and TCMemoryStream are direct copies.
  31. }
  32. const
  33. { TCStream seek origins }
  34. soFromBeginning = 0;
  35. soFromCurrent = 1;
  36. soFromEnd = 2;
  37. { TCFileStream create mode }
  38. fmCreate = $FFFF;
  39. fmOpenRead = 0;
  40. fmOpenWrite = 1;
  41. fmOpenReadWrite = 2;
  42. var
  43. { Used for Error reporting instead of exceptions }
  44. CStreamError : longint;
  45. type
  46. { Fake TComponent class, it isn't used any futher }
  47. TCComponent = class(TObject)
  48. end;
  49. { TCStream abstract class }
  50. TCStream = class(TObject)
  51. private
  52. function GetPosition: Longint;
  53. procedure SetPosition(Pos: Longint);
  54. function GetSize: Longint;
  55. protected
  56. procedure SetSize(NewSize: Longint); virtual;
  57. public
  58. function Read(var Buffer; Count: Longint): Longint; virtual; abstract;
  59. function Write(const Buffer; Count: Longint): Longint; virtual; abstract;
  60. function Seek(Offset: Longint; Origin: Word): Longint; virtual; abstract;
  61. procedure ReadBuffer(var Buffer; Count: Longint);
  62. procedure WriteBuffer(const Buffer; Count: Longint);
  63. function CopyFrom(Source: TCStream; Count: Longint): Longint;
  64. function ReadComponent(Instance: TCComponent): TCComponent;
  65. function ReadComponentRes(Instance: TCComponent): TCComponent;
  66. procedure WriteComponent(Instance: TCComponent);
  67. procedure WriteComponentRes(const ResName: string; Instance: TCComponent);
  68. procedure WriteDescendent(Instance, Ancestor: TCComponent);
  69. procedure WriteDescendentRes(const ResName: string; Instance, Ancestor: TCComponent);
  70. procedure WriteResourceHeader(const ResName: string; {!!!:out} var FixupInfo: Integer);
  71. procedure FixupResourceHeader(FixupInfo: Integer);
  72. procedure ReadResHeader;
  73. function ReadByte : Byte;
  74. function ReadWord : Word;
  75. function ReadDWord : Cardinal;
  76. function ReadAnsiString : AnsiString;
  77. procedure WriteByte(b : Byte);
  78. procedure WriteWord(w : Word);
  79. procedure WriteDWord(d : Cardinal);
  80. Procedure WriteAnsiString (S : AnsiString);
  81. property Position: Longint read GetPosition write SetPosition;
  82. property Size: Longint read GetSize write SetSize;
  83. end;
  84. { TCCustomFileStream class }
  85. TCCustomFileStream = class(TCStream)
  86. protected
  87. FFileName : String;
  88. public
  89. constructor Create(const AFileName: string;{shortstring!} Mode: Word); virtual; abstract;
  90. function EOF: boolean; virtual; abstract;
  91. property FileName : String Read FFilename;
  92. end;
  93. { TFileStream class }
  94. TCFileStream = class(TCCustomFileStream)
  95. Private
  96. FHandle: File;
  97. protected
  98. procedure SetSize(NewSize: Longint); override;
  99. public
  100. constructor Create(const AFileName: string; Mode: Word); override;
  101. destructor Destroy; override;
  102. function Read(var Buffer; Count: Longint): Longint; override;
  103. function Write(const Buffer; Count: Longint): Longint; override;
  104. function Seek(Offset: Longint; Origin: Word): Longint; override;
  105. function EOF: boolean; override;
  106. end;
  107. TCFileStreamClass = class of TCCustomFileStream;
  108. var
  109. CFileStreamClass: TCFileStreamClass = TCFileStream;
  110. type
  111. { TCustomMemoryStream abstract class }
  112. TCCustomMemoryStream = class(TCStream)
  113. private
  114. FMemory: Pointer;
  115. FSize, FPosition: Longint;
  116. protected
  117. procedure SetPointer(Ptr: Pointer; ASize: Longint);
  118. public
  119. function Read(var Buffer; Count: Longint): Longint; override;
  120. function Seek(Offset: Longint; Origin: Word): Longint; override;
  121. procedure SaveToStream(Stream: TCStream);
  122. procedure SaveToFile(const FileName: string);
  123. property Memory: Pointer read FMemory;
  124. end;
  125. { TCMemoryStream }
  126. TCMemoryStream = class(TCCustomMemoryStream)
  127. private
  128. FCapacity: Longint;
  129. procedure SetCapacity(NewCapacity: Longint);
  130. protected
  131. function Realloc(var NewCapacity: Longint): Pointer; virtual;
  132. property Capacity: Longint read FCapacity write SetCapacity;
  133. public
  134. destructor Destroy; override;
  135. procedure Clear;
  136. procedure LoadFromStream(Stream: TCStream);
  137. procedure LoadFromFile(const FileName: string);
  138. procedure SetSize(NewSize: Longint); override;
  139. function Write(const Buffer; Count: Longint): Longint; override;
  140. end;
  141. implementation
  142. Type
  143. PByte = ^Byte;
  144. {*****************************************************************************
  145. TCStream
  146. *****************************************************************************}
  147. function TCStream.GetPosition: Longint;
  148. begin
  149. Result:=Seek(0,soFromCurrent);
  150. end;
  151. procedure TCStream.SetPosition(Pos: Longint);
  152. begin
  153. Seek(pos,soFromBeginning);
  154. end;
  155. function TCStream.GetSize: Longint;
  156. var
  157. p : longint;
  158. begin
  159. p:=GetPosition;
  160. GetSize:=Seek(0,soFromEnd);
  161. Seek(p,soFromBeginning);
  162. end;
  163. procedure TCStream.SetSize(NewSize: Longint);
  164. begin
  165. // We do nothing. Pipe streams don't support this
  166. // As wel as possible read-ony streams !!
  167. end;
  168. procedure TCStream.ReadBuffer(var Buffer; Count: Longint);
  169. begin
  170. CStreamError:=0;
  171. if Read(Buffer,Count)<Count then
  172. CStreamError:=102;
  173. end;
  174. procedure TCStream.WriteBuffer(const Buffer; Count: Longint);
  175. begin
  176. CStreamError:=0;
  177. if Write(Buffer,Count)<Count then
  178. CStreamError:=103;
  179. end;
  180. function TCStream.CopyFrom(Source: TCStream; Count: Longint): Longint;
  181. var
  182. i : longint;
  183. buffer : array[0..1023] of byte;
  184. begin
  185. CStreamError:=0;
  186. Result:=0;
  187. while Count>0 do
  188. begin
  189. if (Count>sizeof(buffer)) then
  190. i:=sizeof(Buffer)
  191. else
  192. i:=Count;
  193. i:=Source.Read(buffer,i);
  194. i:=Write(buffer,i);
  195. dec(count,i);
  196. inc(Result,i);
  197. if i=0 then
  198. exit;
  199. end;
  200. end;
  201. function TCStream.ReadComponent(Instance: TCComponent): TCComponent;
  202. begin
  203. Result:=nil;
  204. end;
  205. function TCStream.ReadComponentRes(Instance: TCComponent): TCComponent;
  206. begin
  207. Result:=nil;
  208. end;
  209. procedure TCStream.WriteComponent(Instance: TCComponent);
  210. begin
  211. end;
  212. procedure TCStream.WriteComponentRes(const ResName: string; Instance: TCComponent);
  213. begin
  214. end;
  215. procedure TCStream.WriteDescendent(Instance, Ancestor: TCComponent);
  216. begin
  217. end;
  218. procedure TCStream.WriteDescendentRes(const ResName: string; Instance, Ancestor: TCComponent);
  219. begin
  220. end;
  221. procedure TCStream.WriteResourceHeader(const ResName: string; {!!!: out} var FixupInfo: Integer);
  222. begin
  223. end;
  224. procedure TCStream.FixupResourceHeader(FixupInfo: Integer);
  225. begin
  226. end;
  227. procedure TCStream.ReadResHeader;
  228. begin
  229. end;
  230. function TCStream.ReadByte : Byte;
  231. var
  232. b : Byte;
  233. begin
  234. ReadBuffer(b,1);
  235. ReadByte:=b;
  236. end;
  237. function TCStream.ReadWord : Word;
  238. var
  239. w : Word;
  240. begin
  241. ReadBuffer(w,2);
  242. ReadWord:=w;
  243. end;
  244. function TCStream.ReadDWord : Cardinal;
  245. var
  246. d : Cardinal;
  247. begin
  248. ReadBuffer(d,4);
  249. ReadDWord:=d;
  250. end;
  251. Function TCStream.ReadAnsiString : AnsiString;
  252. Var
  253. TheSize : Longint;
  254. P : PByte ;
  255. begin
  256. ReadBuffer (TheSize,SizeOf(TheSize));
  257. SetLength(Result,TheSize);
  258. // Illegal typecast if no AnsiStrings defined.
  259. if TheSize>0 then
  260. begin
  261. ReadBuffer (Pointer(Result)^,TheSize);
  262. P:=PByte(PtrInt(Result)+TheSize);
  263. p^:=0;
  264. end;
  265. end;
  266. Procedure TCStream.WriteAnsiString (S : AnsiString);
  267. Var L : Longint;
  268. begin
  269. L:=Length(S);
  270. WriteBuffer (L,SizeOf(L));
  271. WriteBuffer (Pointer(S)^,L);
  272. end;
  273. procedure TCStream.WriteByte(b : Byte);
  274. begin
  275. WriteBuffer(b,1);
  276. end;
  277. procedure TCStream.WriteWord(w : Word);
  278. begin
  279. WriteBuffer(w,2);
  280. end;
  281. procedure TCStream.WriteDWord(d : Cardinal);
  282. begin
  283. WriteBuffer(d,4);
  284. end;
  285. {****************************************************************************}
  286. {* TCFileStream *}
  287. {****************************************************************************}
  288. constructor TCFileStream.Create(const AFileName: string; Mode: Word);
  289. var
  290. oldfilemode : byte;
  291. begin
  292. FFileName:=AFileName;
  293. If Mode=fmcreate then
  294. begin
  295. system.assign(FHandle,AFileName);
  296. {$push} {$I-}
  297. system.rewrite(FHandle,1);
  298. {$pop}
  299. CStreamError:=IOResult;
  300. end
  301. else
  302. begin
  303. oldfilemode:=filemode;
  304. filemode:=$40 or Mode;
  305. system.assign(FHandle,AFileName);
  306. {$push} {$I-}
  307. system.reset(FHandle,1);
  308. {$pop}
  309. CStreamError:=IOResult;
  310. filemode:=oldfilemode;
  311. end;
  312. end;
  313. destructor TCFileStream.Destroy;
  314. begin
  315. {$push} {$I-}
  316. System.Close(FHandle);
  317. {$pop}
  318. CStreamError:=IOResult;
  319. end;
  320. function TCFileStream.Read(var Buffer; Count: Longint): Longint;
  321. begin
  322. CStreamError:=0;
  323. BlockRead(FHandle,Buffer,Count,Result);
  324. If Result=-1 then Result:=0;
  325. end;
  326. function TCFileStream.Write(const Buffer; Count: Longint): Longint;
  327. begin
  328. CStreamError:=0;
  329. BlockWrite (FHandle,(@Buffer)^,Count,Result);
  330. If Result=-1 then Result:=0;
  331. end;
  332. Procedure TCFileStream.SetSize(NewSize: Longint);
  333. begin
  334. {$push} {$I-}
  335. System.Seek(FHandle,NewSize);
  336. System.Truncate(FHandle);
  337. {$pop}
  338. CStreamError:=IOResult;
  339. end;
  340. function TCFileStream.Seek(Offset: Longint; Origin: Word): Longint;
  341. var
  342. l : longint;
  343. begin
  344. {$push} {$I-}
  345. case Origin of
  346. soFromBeginning :
  347. begin
  348. System.Seek(FHandle,Offset);
  349. l:=Offset;
  350. end;
  351. soFromCurrent :
  352. begin
  353. l:=System.FilePos(FHandle);
  354. inc(l,Offset);
  355. System.Seek(FHandle,l);
  356. end;
  357. soFromEnd :
  358. begin
  359. l:=System.FileSize(FHandle);
  360. dec(l,Offset);
  361. if l<0 then
  362. l:=0;
  363. System.Seek(FHandle,l);
  364. end;
  365. end;
  366. {$pop}
  367. CStreamError:=IOResult;
  368. Result:=l;
  369. end;
  370. function TCFileStream.EOF: boolean;
  371. begin
  372. EOF:=system.eof(FHandle);
  373. end;
  374. {****************************************************************************}
  375. {* TCustomMemoryStream *}
  376. {****************************************************************************}
  377. procedure TCCustomMemoryStream.SetPointer(Ptr: Pointer; ASize: Longint);
  378. begin
  379. FMemory:=Ptr;
  380. FSize:=ASize;
  381. end;
  382. function TCCustomMemoryStream.Read(var Buffer; Count: Longint): Longint;
  383. begin
  384. Result:=0;
  385. If (FSize>0) and (FPosition<Fsize) then
  386. begin
  387. Result:=FSize-FPosition;
  388. If Result>Count then Result:=Count;
  389. Move (Pointer(PtrUInt(FMemory)+PtrUInt(FPosition))^,Buffer,Result);
  390. FPosition:=Fposition+Result;
  391. end;
  392. end;
  393. function TCCustomMemoryStream.Seek(Offset: Longint; Origin: Word): Longint;
  394. begin
  395. Case Origin of
  396. soFromBeginning : FPosition:=Offset;
  397. soFromEnd : FPosition:=FSize+Offset;
  398. soFromCurrent : FpoSition:=FPosition+Offset;
  399. end;
  400. Result:=FPosition;
  401. end;
  402. procedure TCCustomMemoryStream.SaveToStream(Stream: TCStream);
  403. begin
  404. if FSize>0 then Stream.WriteBuffer (FMemory^,FSize);
  405. end;
  406. procedure TCCustomMemoryStream.SaveToFile(const FileName: string);
  407. Var S : TCCustomFileStream;
  408. begin
  409. Try
  410. S:=CFileStreamClass.Create (FileName,fmCreate);
  411. SaveToStream(S);
  412. finally
  413. S.free;
  414. end;
  415. end;
  416. {****************************************************************************}
  417. {* TCMemoryStream *}
  418. {****************************************************************************}
  419. Const TMSGrow = 4096; { Use 4k blocks. }
  420. procedure TCMemoryStream.SetCapacity(NewCapacity: Longint);
  421. begin
  422. SetPointer (Realloc(NewCapacity),Fsize);
  423. FCapacity:=NewCapacity;
  424. end;
  425. function TCMemoryStream.Realloc(var NewCapacity: Longint): Pointer;
  426. Var MoveSize : Longint;
  427. begin
  428. CStreamError:=0;
  429. If NewCapacity>0 Then // round off to block size.
  430. NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1);
  431. // Only now check !
  432. If NewCapacity=FCapacity then
  433. Result:=FMemory
  434. else
  435. If NewCapacity=0 then
  436. FreeMem (FMemory,Fcapacity)
  437. else
  438. begin
  439. GetMem (Result,NewCapacity);
  440. If Result=Nil then
  441. CStreamError:=204;
  442. If FCapacity>0 then
  443. begin
  444. MoveSize:=FSize;
  445. If MoveSize>NewCapacity then MoveSize:=NewCapacity;
  446. Move (Fmemory^,Result^,MoveSize);
  447. FreeMem (FMemory,FCapacity);
  448. end;
  449. end;
  450. end;
  451. destructor TCMemoryStream.Destroy;
  452. begin
  453. Clear;
  454. Inherited Destroy;
  455. end;
  456. procedure TCMemoryStream.Clear;
  457. begin
  458. FSize:=0;
  459. FPosition:=0;
  460. SetCapacity (0);
  461. end;
  462. procedure TCMemoryStream.LoadFromStream(Stream: TCStream);
  463. begin
  464. Stream.Position:=0;
  465. SetSize(Stream.Size);
  466. If FSize>0 then Stream.ReadBuffer(FMemory^,FSize);
  467. end;
  468. procedure TCMemoryStream.LoadFromFile(const FileName: string);
  469. Var S : TCCustomFileStream;
  470. begin
  471. Try
  472. S:=CFileStreamClass.Create (FileName,fmOpenRead);
  473. LoadFromStream(S);
  474. finally
  475. S.free;
  476. end;
  477. end;
  478. procedure TCMemoryStream.SetSize(NewSize: Longint);
  479. begin
  480. SetCapacity (NewSize);
  481. FSize:=NewSize;
  482. IF FPosition>FSize then
  483. FPosition:=FSize;
  484. end;
  485. function TCMemoryStream.Write(const Buffer; Count: Longint): Longint;
  486. Var NewPos : Longint;
  487. begin
  488. If Count=0 then
  489. begin
  490. Result:=0;
  491. exit;
  492. end;
  493. NewPos:=FPosition+Count;
  494. If NewPos>Fsize then
  495. begin
  496. IF NewPos>FCapacity then
  497. SetCapacity (NewPos);
  498. FSize:=Newpos;
  499. end;
  500. System.Move (Buffer,Pointer(Ptruint(FMemory)+PtrUInt(FPosition))^,Count);
  501. FPosition:=NewPos;
  502. Result:=Count;
  503. end;
  504. end.