cstreams.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. TCRangeStream = class(TCStream)
  112. private
  113. FBase: TCStream;
  114. FOffset: LongInt;
  115. FMaxOffset: LongInt;
  116. FSize: LongInt;
  117. FPosition: LongInt;
  118. public
  119. constructor Create(ABase: TCStream; AOffset, ASize: LongInt);
  120. function Read(var Buffer; Count: LongInt): LongInt; override;
  121. function Write(const Buffer; Count: LongInt): LongInt; override;
  122. function Seek(Offset: LongInt; Origin: Word): LongInt; override;
  123. end;
  124. { TCustomMemoryStream abstract class }
  125. TCCustomMemoryStream = class(TCStream)
  126. private
  127. FMemory: Pointer;
  128. FSize, FPosition: Longint;
  129. protected
  130. procedure SetPointer(Ptr: Pointer; ASize: Longint);
  131. public
  132. function Read(var Buffer; Count: Longint): Longint; override;
  133. function Seek(Offset: Longint; Origin: Word): Longint; override;
  134. procedure SaveToStream(Stream: TCStream);
  135. procedure SaveToFile(const FileName: string);
  136. property Memory: Pointer read FMemory;
  137. end;
  138. { TCMemoryStream }
  139. TCMemoryStream = class(TCCustomMemoryStream)
  140. private
  141. FCapacity: Longint;
  142. procedure SetCapacity(NewCapacity: Longint);
  143. protected
  144. function Realloc(var NewCapacity: Longint): Pointer; virtual;
  145. property Capacity: Longint read FCapacity write SetCapacity;
  146. public
  147. destructor Destroy; override;
  148. procedure Clear;
  149. procedure LoadFromStream(Stream: TCStream);
  150. procedure LoadFromFile(const FileName: string);
  151. procedure SetSize(NewSize: Longint); override;
  152. function Write(const Buffer; Count: Longint): Longint; override;
  153. end;
  154. implementation
  155. Type
  156. PByte = ^Byte;
  157. {*****************************************************************************
  158. TCStream
  159. *****************************************************************************}
  160. function TCStream.GetPosition: Longint;
  161. begin
  162. Result:=Seek(0,soFromCurrent);
  163. end;
  164. procedure TCStream.SetPosition(Pos: Longint);
  165. begin
  166. Seek(pos,soFromBeginning);
  167. end;
  168. function TCStream.GetSize: Longint;
  169. var
  170. p : longint;
  171. begin
  172. p:=GetPosition;
  173. GetSize:=Seek(0,soFromEnd);
  174. Seek(p,soFromBeginning);
  175. end;
  176. procedure TCStream.SetSize(NewSize: Longint);
  177. begin
  178. // We do nothing. Pipe streams don't support this
  179. // As well as possible read-ony streams !!
  180. end;
  181. procedure TCStream.ReadBuffer(var Buffer; Count: Longint);
  182. begin
  183. CStreamError:=0;
  184. if Read(Buffer,Count)<Count then
  185. CStreamError:=102;
  186. end;
  187. procedure TCStream.WriteBuffer(const Buffer; Count: Longint);
  188. begin
  189. CStreamError:=0;
  190. if Write(Buffer,Count)<Count then
  191. CStreamError:=103;
  192. end;
  193. function TCStream.CopyFrom(Source: TCStream; Count: Longint): Longint;
  194. var
  195. i : longint;
  196. buffer : array[0..1023] of byte;
  197. begin
  198. CStreamError:=0;
  199. Result:=0;
  200. while Count>0 do
  201. begin
  202. if (Count>sizeof(buffer)) then
  203. i:=sizeof(Buffer)
  204. else
  205. i:=Count;
  206. i:=Source.Read(buffer,i);
  207. i:=Write(buffer,i);
  208. dec(count,i);
  209. inc(Result,i);
  210. if i=0 then
  211. exit;
  212. end;
  213. end;
  214. function TCStream.ReadComponent(Instance: TCComponent): TCComponent;
  215. begin
  216. Result:=nil;
  217. end;
  218. function TCStream.ReadComponentRes(Instance: TCComponent): TCComponent;
  219. begin
  220. Result:=nil;
  221. end;
  222. procedure TCStream.WriteComponent(Instance: TCComponent);
  223. begin
  224. end;
  225. procedure TCStream.WriteComponentRes(const ResName: string; Instance: TCComponent);
  226. begin
  227. end;
  228. procedure TCStream.WriteDescendent(Instance, Ancestor: TCComponent);
  229. begin
  230. end;
  231. procedure TCStream.WriteDescendentRes(const ResName: string; Instance, Ancestor: TCComponent);
  232. begin
  233. end;
  234. procedure TCStream.WriteResourceHeader(const ResName: string; {!!!: out} var FixupInfo: Integer);
  235. begin
  236. end;
  237. procedure TCStream.FixupResourceHeader(FixupInfo: Integer);
  238. begin
  239. end;
  240. procedure TCStream.ReadResHeader;
  241. begin
  242. end;
  243. function TCStream.ReadByte : Byte;
  244. var
  245. b : Byte;
  246. begin
  247. ReadBuffer(b,1);
  248. ReadByte:=b;
  249. end;
  250. function TCStream.ReadWord : Word;
  251. var
  252. w : Word;
  253. begin
  254. ReadBuffer(w,2);
  255. ReadWord:=w;
  256. end;
  257. function TCStream.ReadDWord : Cardinal;
  258. var
  259. d : Cardinal;
  260. begin
  261. ReadBuffer(d,4);
  262. ReadDWord:=d;
  263. end;
  264. Function TCStream.ReadAnsiString : AnsiString;
  265. Var
  266. TheSize : Longint;
  267. P : PByte ;
  268. begin
  269. ReadBuffer (TheSize,SizeOf(TheSize));
  270. SetLength(Result,TheSize);
  271. // Illegal typecast if no AnsiStrings defined.
  272. if TheSize>0 then
  273. begin
  274. ReadBuffer (Pointer(Result)^,TheSize);
  275. P:=PByte(PtrInt(Result)+TheSize);
  276. p^:=0;
  277. end;
  278. end;
  279. Procedure TCStream.WriteAnsiString (S : AnsiString);
  280. Var L : Longint;
  281. begin
  282. L:=Length(S);
  283. WriteBuffer (L,SizeOf(L));
  284. WriteBuffer (Pointer(S)^,L);
  285. end;
  286. procedure TCStream.WriteByte(b : Byte);
  287. begin
  288. WriteBuffer(b,1);
  289. end;
  290. procedure TCStream.WriteWord(w : Word);
  291. begin
  292. WriteBuffer(w,2);
  293. end;
  294. procedure TCStream.WriteDWord(d : Cardinal);
  295. begin
  296. WriteBuffer(d,4);
  297. end;
  298. {****************************************************************************}
  299. {* TCFileStream *}
  300. {****************************************************************************}
  301. constructor TCFileStream.Create(const AFileName: string; Mode: Word);
  302. var
  303. oldfilemode : byte;
  304. begin
  305. FFileName:=AFileName;
  306. If Mode=fmcreate then
  307. begin
  308. system.assign(FHandle,AFileName);
  309. {$push} {$I-}
  310. system.rewrite(FHandle,1);
  311. {$pop}
  312. CStreamError:=IOResult;
  313. end
  314. else
  315. begin
  316. oldfilemode:=filemode;
  317. filemode:=$40 or Mode;
  318. system.assign(FHandle,AFileName);
  319. {$push} {$I-}
  320. system.reset(FHandle,1);
  321. {$pop}
  322. CStreamError:=IOResult;
  323. filemode:=oldfilemode;
  324. end;
  325. end;
  326. destructor TCFileStream.Destroy;
  327. begin
  328. {$push} {$I-}
  329. System.Close(FHandle);
  330. {$pop}
  331. CStreamError:=IOResult;
  332. end;
  333. function TCFileStream.Read(var Buffer; Count: Longint): Longint;
  334. begin
  335. CStreamError:=0;
  336. BlockRead(FHandle,Buffer,Count,Result);
  337. If Result=-1 then Result:=0;
  338. end;
  339. function TCFileStream.Write(const Buffer; Count: Longint): Longint;
  340. begin
  341. CStreamError:=0;
  342. BlockWrite (FHandle,(@Buffer)^,Count,Result);
  343. If Result=-1 then Result:=0;
  344. end;
  345. Procedure TCFileStream.SetSize(NewSize: Longint);
  346. begin
  347. {$push} {$I-}
  348. System.Seek(FHandle,NewSize);
  349. System.Truncate(FHandle);
  350. {$pop}
  351. CStreamError:=IOResult;
  352. end;
  353. function TCFileStream.Seek(Offset: Longint; Origin: Word): Longint;
  354. var
  355. l : longint;
  356. begin
  357. {$push} {$I-}
  358. case Origin of
  359. soFromBeginning :
  360. begin
  361. System.Seek(FHandle,Offset);
  362. l:=Offset;
  363. end;
  364. soFromCurrent :
  365. begin
  366. l:=System.FilePos(FHandle);
  367. inc(l,Offset);
  368. System.Seek(FHandle,l);
  369. end;
  370. soFromEnd :
  371. begin
  372. l:=System.FileSize(FHandle);
  373. dec(l,Offset);
  374. if l<0 then
  375. l:=0;
  376. System.Seek(FHandle,l);
  377. end;
  378. else
  379. begin
  380. CStreamError:=103;
  381. l:=Offset;
  382. end;
  383. end;
  384. {$pop}
  385. CStreamError:=IOResult;
  386. Result:=l;
  387. end;
  388. function TCFileStream.EOF: boolean;
  389. begin
  390. EOF:=system.eof(FHandle);
  391. end;
  392. {****************************************************************************}
  393. {* TCRangeStream *}
  394. {****************************************************************************}
  395. constructor TCRangeStream.Create(ABase: TCStream; AOffset, ASize: LongInt);
  396. begin
  397. if not assigned(ABase) then
  398. CStreamError:=155
  399. else
  400. { we allow to be positioned directly at the end for appending }
  401. if (AOffset<0) or (AOffset>ABase.Size) then
  402. CStreamError:=156
  403. else
  404. begin
  405. FBase:=ABase;
  406. FOffset:=AOffset;
  407. if ASize<0 then
  408. FSize:=maxLongint-FOffset
  409. else
  410. FSize:=ASize;
  411. FMaxOffset:=FOffset+FSize-1;
  412. end;
  413. end;
  414. function TCRangeStream.Read(var Buffer; Count: LongInt): LongInt;
  415. begin
  416. Count:=Min(Count,FMaxOffset-FPosition+1);
  417. if Count>0 then
  418. begin
  419. FBase.Seek(FOffset+FPosition,soFromBeginning);
  420. result:=FBase.Read(Buffer,Count);
  421. end
  422. else
  423. result:=0;
  424. FPosition:=FPosition+result;
  425. end;
  426. function TCRangeStream.Write(const Buffer; Count: LongInt): LongInt;
  427. begin
  428. Count:=Min(Count,FMaxOffset-FPosition+1);
  429. if Count>0 then
  430. begin
  431. FBase.Seek(FOffset+FPosition,soFromBeginning);
  432. result:=FBase.Write(Buffer,Count);
  433. end
  434. else
  435. result:=0;
  436. FPosition:=FPosition+result;
  437. end;
  438. function TCRangeStream.Seek(Offset: LongInt; Origin: Word): LongInt;
  439. begin
  440. case Origin of
  441. soFromBeginning:
  442. begin
  443. if Offset>FMaxOffset then
  444. CStreamError:=156
  445. else
  446. FPosition:=FBase.Seek(FOffset+Offset,soFromBeginning)-FOffset;
  447. end;
  448. soFromCurrent:
  449. begin
  450. if Offset>FMaxOffset then
  451. CStreamError:=156
  452. else
  453. FPosition:=FBase.Seek(FOffset+FPosition+Offset,soFromBeginning)-FOffset;
  454. end;
  455. soFromEnd:
  456. begin
  457. if Offset>FSize-1 then
  458. CStreamError:=156
  459. else
  460. FPosition:=FBase.Seek(FMaxOffset-Offset,soFromBeginning)-FOffset;
  461. end;
  462. else
  463. begin
  464. CStreamError:=156;
  465. end;
  466. end;
  467. Result:=FPosition;
  468. end;
  469. {****************************************************************************}
  470. {* TCustomMemoryStream *}
  471. {****************************************************************************}
  472. procedure TCCustomMemoryStream.SetPointer(Ptr: Pointer; ASize: Longint);
  473. begin
  474. FMemory:=Ptr;
  475. FSize:=ASize;
  476. end;
  477. function TCCustomMemoryStream.Read(var Buffer; Count: Longint): Longint;
  478. begin
  479. Result:=0;
  480. If (FSize>0) and (FPosition<Fsize) then
  481. begin
  482. Result:=FSize-FPosition;
  483. If Result>Count then Result:=Count;
  484. Move (Pointer(PtrUInt(FMemory)+PtrUInt(FPosition))^,Buffer,Result);
  485. FPosition:=Fposition+Result;
  486. end;
  487. end;
  488. function TCCustomMemoryStream.Seek(Offset: Longint; Origin: Word): Longint;
  489. begin
  490. Case Origin of
  491. soFromBeginning : FPosition:=Offset;
  492. soFromEnd : FPosition:=FSize+Offset;
  493. soFromCurrent : FpoSition:=FPosition+Offset;
  494. end;
  495. Result:=FPosition;
  496. end;
  497. procedure TCCustomMemoryStream.SaveToStream(Stream: TCStream);
  498. begin
  499. if FSize>0 then Stream.WriteBuffer (FMemory^,FSize);
  500. end;
  501. procedure TCCustomMemoryStream.SaveToFile(const FileName: string);
  502. Var S : TCCustomFileStream;
  503. begin
  504. Try
  505. S:=CFileStreamClass.Create (FileName,fmCreate);
  506. SaveToStream(S);
  507. finally
  508. S.free;
  509. end;
  510. end;
  511. {****************************************************************************}
  512. {* TCMemoryStream *}
  513. {****************************************************************************}
  514. Const TMSGrow = 4096; { Use 4k blocks. }
  515. procedure TCMemoryStream.SetCapacity(NewCapacity: Longint);
  516. begin
  517. SetPointer (Realloc(NewCapacity),Fsize);
  518. FCapacity:=NewCapacity;
  519. end;
  520. function TCMemoryStream.Realloc(var NewCapacity: Longint): Pointer;
  521. Var MoveSize : Longint;
  522. begin
  523. CStreamError:=0;
  524. If NewCapacity>0 Then // round off to block size.
  525. NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1);
  526. // Only now check !
  527. If NewCapacity=FCapacity then
  528. Result:=FMemory
  529. else
  530. If NewCapacity=0 then
  531. begin
  532. FreeMem (FMemory,Fcapacity);
  533. Result:=nil;
  534. end
  535. else
  536. begin
  537. GetMem (Result,NewCapacity);
  538. If Result=Nil then
  539. CStreamError:=204;
  540. If FCapacity>0 then
  541. begin
  542. MoveSize:=FSize;
  543. If MoveSize>NewCapacity then MoveSize:=NewCapacity;
  544. Move (Fmemory^,Result^,MoveSize);
  545. FreeMem (FMemory,FCapacity);
  546. end;
  547. end;
  548. end;
  549. destructor TCMemoryStream.Destroy;
  550. begin
  551. Clear;
  552. Inherited Destroy;
  553. end;
  554. procedure TCMemoryStream.Clear;
  555. begin
  556. FSize:=0;
  557. FPosition:=0;
  558. SetCapacity (0);
  559. end;
  560. procedure TCMemoryStream.LoadFromStream(Stream: TCStream);
  561. begin
  562. Stream.Position:=0;
  563. SetSize(Stream.Size);
  564. If FSize>0 then Stream.ReadBuffer(FMemory^,FSize);
  565. end;
  566. procedure TCMemoryStream.LoadFromFile(const FileName: string);
  567. Var S : TCCustomFileStream;
  568. begin
  569. Try
  570. S:=CFileStreamClass.Create (FileName,fmOpenRead);
  571. LoadFromStream(S);
  572. finally
  573. S.free;
  574. end;
  575. end;
  576. procedure TCMemoryStream.SetSize(NewSize: Longint);
  577. begin
  578. SetCapacity (NewSize);
  579. FSize:=NewSize;
  580. IF FPosition>FSize then
  581. FPosition:=FSize;
  582. end;
  583. function TCMemoryStream.Write(const Buffer; Count: Longint): Longint;
  584. Var NewPos : Longint;
  585. begin
  586. If Count=0 then
  587. begin
  588. Result:=0;
  589. exit;
  590. end;
  591. NewPos:=FPosition+Count;
  592. If NewPos>Fsize then
  593. begin
  594. IF NewPos>FCapacity then
  595. SetCapacity (NewPos);
  596. FSize:=Newpos;
  597. end;
  598. System.Move (Buffer,Pointer(Ptruint(FMemory)+PtrUInt(FPosition))^,Count);
  599. FPosition:=NewPos;
  600. Result:=Count;
  601. end;
  602. end.