streams.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1998 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************}
  12. {* TStream *}
  13. {****************************************************************************}
  14. function TStream.GetPosition: Longint;
  15. begin
  16. Result:=Seek(0,soFromCurrent);
  17. end;
  18. procedure TStream.SetPosition(Pos: Longint);
  19. begin
  20. Seek(pos,soFromBeginning);
  21. end;
  22. function TStream.GetSize: Longint;
  23. var
  24. p : longint;
  25. begin
  26. p:=GetPosition;
  27. GetSize:=Seek(0,soFromEnd);
  28. Seek(p,soFromBeginning);
  29. end;
  30. procedure TStream.SetSize(NewSize: Longint);
  31. begin
  32. // We do nothing. Pipe streams don't support this
  33. // As wel as possible read-ony streams !!
  34. end;
  35. procedure TStream.ReadBuffer(var Buffer; Count: Longint);
  36. begin
  37. if Read(Buffer,Count)<Count then
  38. {$ifdef NoExceptions}
  39. ;
  40. {$else}
  41. Raise(EReadError);
  42. {$endif}
  43. end;
  44. procedure TStream.WriteBuffer(const Buffer; Count: Longint);
  45. begin
  46. if Write(Buffer,Count)<Count then
  47. {$ifdef NoExceptions}
  48. ;
  49. {$else}
  50. Raise(EWriteError);
  51. {$endif}
  52. end;
  53. function TStream.CopyFrom(Source: TStream; Count: Longint): Longint;
  54. var
  55. i : longint;
  56. buffer : array[0..1023] of byte;
  57. begin
  58. CopyFrom:=0;
  59. while Count>0 do
  60. begin
  61. if (Count>sizeof(buffer)) then
  62. i:=sizeof(Buffer)
  63. else
  64. i:=Count;
  65. i:=Source.Read(buffer,i);
  66. i:=Write(buffer,i);
  67. dec(count,i);
  68. CopyFrom:=CopyFrom+i;
  69. if i=0 then
  70. exit;
  71. end;
  72. end;
  73. function TStream.ReadComponent(Instance: TComponent): TComponent;
  74. var
  75. Reader : TReader;
  76. begin
  77. Reader.Create(Self,1024);
  78. if assigned(Instance) then
  79. ReadComponent:=Reader.ReadRootComponent(Instance)
  80. else
  81. begin
  82. {!!!!!}
  83. end;
  84. Reader.Destroy;
  85. end;
  86. function TStream.ReadComponentRes(Instance: TComponent): TComponent;
  87. begin
  88. {!!!!!}
  89. end;
  90. procedure TStream.WriteComponent(Instance: TComponent);
  91. var
  92. Writer : TWriter;
  93. begin
  94. Writer.Create(Self,1024);
  95. Writer.WriteRootComponent(Instance);
  96. Writer.Destroy;
  97. end;
  98. procedure TStream.WriteComponentRes(const ResName: string; Instance: TComponent);
  99. var
  100. startpos,s : longint;
  101. begin
  102. {$ifdef Win16Res}
  103. { Numeric resource type }
  104. WriteByte($ff);
  105. { Application defined data }
  106. WriteWord($0a);
  107. { write the name as asciiz }
  108. WriteBuffer(ResName[1],length(ResName));
  109. WriteByte(0);
  110. { Movable, Pure and Discardable }
  111. WriteWord($1030);
  112. { size isn't known yet }
  113. WriteDWord(0);
  114. startpos:=GetPosition;
  115. WriteComponent(Instance);
  116. { calculate size }
  117. s:=GetPosition-startpos;
  118. { back patch size }
  119. SetPosition(startpos-4);
  120. WriteDWord(s);
  121. {$endif Win16Res}
  122. end;
  123. procedure TStream.WriteDescendent(Instance, Ancestor: TComponent);
  124. begin
  125. {!!!!!}
  126. end;
  127. procedure TStream.WriteDescendentRes(const ResName: string; Instance, Ancestor: TComponent);
  128. begin
  129. {!!!!!}
  130. end;
  131. procedure TStream.ReadResHeader;
  132. begin
  133. {$ifdef Win16Res}
  134. try
  135. { application specific resource ? }
  136. if ReadByte<>$ff then
  137. raise EInvalidImage;
  138. if ReadWord<>$000a then
  139. raise EInvalidImage;
  140. { read name }
  141. while ReadByte<>0 do
  142. ;
  143. { check the access specifier }
  144. if ReadWord<>$1030 then
  145. raise EInvalidImage;
  146. { ignore the size }
  147. ReadDWord;
  148. except
  149. {/////
  150. on EInvalidImage do
  151. raise;
  152. else
  153. raise(EInvalidImage);
  154. }
  155. end;
  156. {$endif Win16Res}
  157. end;
  158. function TStream.ReadByte : Byte;
  159. var
  160. b : Byte;
  161. begin
  162. ReadBuffer(b,1);
  163. ReadByte:=b;
  164. end;
  165. function TStream.ReadWord : Word;
  166. var
  167. w : Word;
  168. begin
  169. ReadBuffer(w,2);
  170. ReadWord:=w;
  171. end;
  172. function TStream.ReadDWord : Cardinal;
  173. var
  174. d : Cardinal;
  175. begin
  176. ReadBuffer(d,4);
  177. ReadDWord:=d;
  178. end;
  179. Function TStream.ReadAnsiString : String;
  180. Type PByte = ^Byte;
  181. Var TheSize : Longint;
  182. P : PByte ;
  183. begin
  184. ReadBuffer (TheSize,SizeOf(TheSize));
  185. //!! SetLength(Result,Size);
  186. //!! Illegal typecast if no AnsiStrings defined.
  187. //!! ReadBuffer (Pointer (Result^),Size);
  188. //!! P:=Pointer(Result^)+Size;
  189. //!! p^:=0;
  190. end;
  191. Procedure TStream.WriteAnsiString (S : String);
  192. Var L : Longint;
  193. begin
  194. L:=Length(S);
  195. WriteBuffer (L,SizeOf(L));
  196. //!! WriteBuffer (Pointer(S)^,L);
  197. end;
  198. procedure TStream.WriteByte(b : Byte);
  199. begin
  200. WriteBuffer(b,1);
  201. end;
  202. procedure TStream.WriteWord(w : Word);
  203. begin
  204. WriteBuffer(w,2);
  205. end;
  206. procedure TStream.WriteDWord(d : Cardinal);
  207. begin
  208. WriteBuffer(d,4);
  209. end;
  210. {****************************************************************************}
  211. {* THandleStream *}
  212. {****************************************************************************}
  213. Constructor THandleStream.Create(AHandle: Integer);
  214. begin
  215. FHandle:=AHandle;
  216. end;
  217. function THandleStream.Read(var Buffer; Count: Longint): Longint;
  218. begin
  219. Result:=OSReadHandle(FHandle,Buffer,Count);
  220. If Result=-1 then Result:=0;
  221. end;
  222. function THandleStream.Write(const Buffer; Count: Longint): Longint;
  223. begin
  224. Result:=OSWriteHandle(FHandle,Buffer,Count);
  225. If Result=-1 then Result:=0;
  226. end;
  227. {****************************************************************************}
  228. {* TFileStream *}
  229. {****************************************************************************}
  230. constructor TFileStream.Create(const FileName: string; Mode: Word);
  231. begin
  232. FHandle:=OSCreateFile (Filename,Mode);
  233. If FHandle<0 then
  234. {$ifdef NoExceptions}
  235. RunError(255);
  236. {$else}
  237. raise EFCreateError;
  238. {$endif}
  239. end;
  240. destructor TFileStream.Destroy;
  241. begin
  242. OSCloseHandle(FHandle);
  243. end;
  244. Procedure TFileStream.SetSize(NewSize: Longint);
  245. begin
  246. OSSetHandleSize (FHandle,NewSize);
  247. end;
  248. function TFileStream.Seek(Offset: Longint; Origin: Word): Longint;
  249. begin
  250. Result:=OSSeekHandle (FHandle,OffSet,Origin);
  251. end;
  252. {****************************************************************************}
  253. {* TCustomMemoryStream *}
  254. {****************************************************************************}
  255. procedure TCustomMemoryStream.SetPointer(Ptr: Pointer; Size: Longint);
  256. begin
  257. FMemory:=Ptr;
  258. FSize:=Size;
  259. end;
  260. function TCustomMemoryStream.Read(var Buffer; Count: Longint): Longint;
  261. begin
  262. Result:=0;
  263. If FSize>0 and FPosition<Fsize then
  264. begin
  265. Result:=FSize-FPosition;
  266. If Result>Count then Result:=Count;
  267. Move ((FMemory+FPosition)^,Buffer,Result);
  268. FPosition:=Fposition+Result;
  269. end;
  270. end;
  271. function TCustomMemoryStream.Seek(Offset: Longint; Origin: Word): Longint;
  272. begin
  273. Case Origin of
  274. soFromBeginning : FPosition:=Offset;
  275. soFromEnd : FPosition:=FSize+Offset;
  276. soFromCurrent : FpoSition:=FPosition+Offset;
  277. end;
  278. Result:=FPosition;
  279. end;
  280. procedure TCustomMemoryStream.SaveToStream(Stream: TStream);
  281. begin
  282. if FSize>0 then Stream.WriteBuffer (FMemory^,FSize);
  283. end;
  284. procedure TCustomMemoryStream.SaveToFile(const FileName: string);
  285. Var S : TFileStream;
  286. begin
  287. S:=TFileStream.Create (FileName,fmCreate);
  288. SaveToStream(S);
  289. S.free;
  290. end;
  291. {****************************************************************************}
  292. {* TMemoryStream *}
  293. {****************************************************************************}
  294. Const TMSGrow = 4096; { Use 4k blocks. }
  295. procedure TMemoryStream.SetCapacity(NewCapacity: Longint);
  296. begin
  297. SetPointer (Realloc(NewCapacity),Fsize);
  298. FCapacity:=NewCapacity;
  299. end;
  300. function TMemoryStream.Realloc(var NewCapacity: Longint): Pointer;
  301. Var MoveSize : Longint;
  302. begin
  303. If NewCapacity>0 Then // round off to block size.
  304. NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1);
  305. // Only now check !
  306. If NewCapacity<>FCapacity then
  307. If NewCapacity=0 then
  308. FreeMem (FMemory,Fcapacity)
  309. else
  310. begin
  311. GetMem (Result,NewCapacity);
  312. If FCapacity>0 then
  313. begin
  314. MoveSize:=FSize;
  315. If MoveSize>NewCapacity then MoveSize:=NewCapacity;
  316. Move (Fmemory^,Result^,MoveSize);
  317. FreeMem (FMemory,FCapacity);
  318. end;
  319. end;
  320. end;
  321. destructor TMemoryStream.Destroy;
  322. begin
  323. Clear;
  324. Inherited Destroy;
  325. end;
  326. procedure TMemoryStream.Clear;
  327. begin
  328. FSize:=0;
  329. FPosition:=0;
  330. SetCapacity (0);
  331. end;
  332. procedure TMemoryStream.LoadFromStream(Stream: TStream);
  333. begin
  334. Stream.Position:=0;
  335. SetSize(Stream.Size);
  336. If FSize>0 then Stream.ReadBuffer(FMemory^,FSize);
  337. end;
  338. procedure TMemoryStream.LoadFromFile(const FileName: string);
  339. Var S : TFileStream;
  340. begin
  341. S:=TFileStream.Create (FileName,fmOpenRead);
  342. LoadFromStream(S);
  343. S.free;
  344. end;
  345. procedure TMemoryStream.SetSize(NewSize: Longint);
  346. begin
  347. SetCapacity (NewSize);
  348. If FSize>NewSize then FSize:=NewSize;
  349. IF FPosition>FSize then FPosition:=FSize;
  350. end;
  351. function TMemoryStream.Write(const Buffer; Count: Longint): Longint;
  352. Var NewPos : Longint;
  353. begin
  354. If Count=0 then
  355. exit(0);
  356. NewPos:=FPosition+Count;
  357. If NewPos>Fsize then
  358. begin
  359. IF NewPos>FCapacity then
  360. SetCapacity (NewPos);
  361. FSize:=Newpos;
  362. end;
  363. System.Move (Buffer,(FMemory+FPosition)^,Count);
  364. FPosition:=NewPos;
  365. Result:=Count;
  366. end;
  367. {****************************************************************************}
  368. {* TStringStream *}
  369. {****************************************************************************}
  370. procedure TStringStream.SetSize(NewSize: Longint);
  371. begin
  372. //!! Setlength(FDataString,NewSize);
  373. If FPosition>NewSize then FPosition:=NewSize;
  374. end;
  375. constructor TStringStream.Create(const AString: string);
  376. begin
  377. Inherited create;
  378. FDataString:=AString;
  379. end;
  380. function TStringStream.Read(var Buffer; Count: Longint): Longint;
  381. begin
  382. Result:=Length(FDataString)-FPosition;
  383. If Result>Count then Result:=Count;
  384. // This supposes FDataString to be of type AnsiString !
  385. //!! Move (Pchar(FDataString)[FPosition],Buffer,Count);
  386. FPosition:=FPosition+Count;
  387. end;
  388. function TStringStream.ReadString(Count: Longint): string;
  389. Var NewLen : Longint;
  390. begin
  391. NewLen:=Length(FDataString)-FPosition;
  392. If NewLen>Count then NewLen:=Count;
  393. //!! SetLength(Result,NewLen);
  394. //!! Read (Pointer(Result)^,NewLen);
  395. end;
  396. function TStringStream.Seek(Offset: Longint; Origin: Word): Longint;
  397. begin
  398. Case Origin of
  399. soFromBeginning : FPosition:=Offset;
  400. soFromEnd : FPosition:=Length(FDataString)+Offset;
  401. soFromCurrent : FpoSition:=FPosition+Offset;
  402. end;
  403. If FPosition>Length(FDataString) then FPosition:=Length(FDataString);
  404. If FPosition<0 then FPosition:=0;
  405. Result:=FPosition;
  406. end;
  407. function TStringStream.Write(const Buffer; Count: Longint): Longint;
  408. begin
  409. Result:=Count;
  410. SetSize(FPosition+Count);
  411. // This supposes that FDataString is of type AnsiString)
  412. //!! Move (Buffer,PCHar(FDataString)[Fposition],Count);
  413. FPosition:=FPosition+Count;
  414. end;
  415. procedure TStringStream.WriteString(const AString: string);
  416. begin
  417. //!! Write (PChar(Astring)[0],Length(AString));
  418. end;
  419. {****************************************************************************}
  420. {* TResourceStream *}
  421. {****************************************************************************}
  422. procedure TResourceStream.Initialize(Instance: THandle; Name, ResType: PChar);
  423. begin
  424. end;
  425. constructor TResourceStream.Create(Instance: THandle; const ResName: string; ResType: PChar);
  426. begin
  427. end;
  428. constructor TResourceStream.CreateFromID(Instance: THandle; ResID: Integer; ResType: PChar);
  429. begin
  430. end;
  431. destructor TResourceStream.Destroy;
  432. begin
  433. end;
  434. function TResourceStream.Write(const Buffer; Count: Longint): Longint;
  435. begin
  436. end;
  437. {
  438. $Log$
  439. Revision 1.6 1998-06-11 21:15:28 michael
  440. + Implemented (Custom)Memory and StringStream
  441. Revision 1.5 1998/06/11 13:46:33 michael
  442. + Fixed some functions. TFileStream OK.
  443. Revision 1.4 1998/06/10 21:53:07 michael
  444. + Implemented Handle/FileStreams
  445. Revision 1.3 1998/05/06 12:58:35 michael
  446. + Added WriteAnsiString method to TStream
  447. Revision 1.2 1998/05/05 15:25:04 michael
  448. + Fix to be able to compile from florian
  449. Revision 1.1 1998/05/04 14:30:12 michael
  450. * Split file according to Class; implemented dummys for all methods, so unit compiles.
  451. }