writer.inc 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. {****************************************************************************}
  11. {* TBinaryObjectWriter *}
  12. {****************************************************************************}
  13. {$ifndef FPUNONE}
  14. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  15. procedure DoubleToExtended(d : double; e : pointer);
  16. var mant : qword;
  17. exp : smallint;
  18. sign : boolean;
  19. begin
  20. mant:=(qword(d) and $000FFFFFFFFFFFFF) shl 12;
  21. exp :=(qword(d) shr 52) and $7FF;
  22. sign:=(qword(d) and $8000000000000000)<>0;
  23. case exp of
  24. 0 : begin
  25. if mant<>0 then //denormalized value: hidden bit is 0. normalize it
  26. begin
  27. exp:=16383-1022;
  28. while (mant and $8000000000000000)=0 do
  29. begin
  30. dec(exp);
  31. mant:=mant shl 1;
  32. end;
  33. dec(exp); //don't shift, most significant bit is not hidden in extended
  34. end;
  35. end;
  36. 2047 : exp:=$7FFF //either infinity or NaN
  37. else
  38. begin
  39. inc(exp,16383-1023);
  40. mant:=(mant shr 1) or $8000000000000000; //unhide hidden bit
  41. end;
  42. end;
  43. if sign then exp:=exp or $8000;
  44. mant:=NtoLE(mant);
  45. exp:=NtoLE(word(exp));
  46. move(mant,pbyte(e)[0],8); //mantissa : bytes 0..7
  47. move(exp,pbyte(e)[8],2); //exponent and sign: bytes 8..9
  48. end;
  49. {$ENDIF}
  50. {$endif}
  51. procedure TBinaryObjectWriter.WriteWord(w : word); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  52. begin
  53. w:=NtoLE(w);
  54. Write(w,2);
  55. end;
  56. procedure TBinaryObjectWriter.WriteDWord(lw : longword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  57. begin
  58. lw:=NtoLE(lw);
  59. Write(lw,4);
  60. end;
  61. procedure TBinaryObjectWriter.WriteQWord(qw : qword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  62. begin
  63. qw:=NtoLE(qw);
  64. Write(qw,8);
  65. end;
  66. {$ifndef FPUNONE}
  67. procedure TBinaryObjectWriter.WriteExtended(e : extended); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  68. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  69. var ext : array[0..9] of byte;
  70. {$ENDIF}
  71. begin
  72. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  73. {$IFDEF FPC_DOUBLE_HILO_SWAPPED}
  74. { SwapDoubleHiLo defined in reader.inc }
  75. SwapDoubleHiLo(e);
  76. {$ENDIF FPC_DOUBLE_HILO_SWAPPED}
  77. DoubleToExtended(e,@(ext[0]));
  78. Write(ext[0],10);
  79. {$ELSE}
  80. Write(e,sizeof(e));
  81. {$ENDIF}
  82. end;
  83. {$endif}
  84. constructor TBinaryObjectWriter.Create(Stream: TStream; BufSize: Integer);
  85. begin
  86. inherited Create;
  87. If (Stream=Nil) then
  88. Raise EWriteError.Create(SEmptyStreamIllegalWriter);
  89. FStream := Stream;
  90. FBufSize := BufSize;
  91. GetMem(FBuffer, BufSize);
  92. end;
  93. destructor TBinaryObjectWriter.Destroy;
  94. begin
  95. // Flush all data which hasn't been written yet
  96. FlushBuffer;
  97. if Assigned(FBuffer) then
  98. FreeMem(FBuffer, FBufSize);
  99. inherited Destroy;
  100. end;
  101. procedure TBinaryObjectWriter.BeginCollection;
  102. begin
  103. WriteValue(vaCollection);
  104. end;
  105. procedure TBinaryObjectWriter.BeginComponent(Component: TComponent;
  106. Flags: TFilerFlags; ChildPos: Integer);
  107. var
  108. Prefix: Byte;
  109. begin
  110. if not FSignatureWritten then
  111. begin
  112. Write(FilerSignature, SizeOf(FilerSignature));
  113. FSignatureWritten := True;
  114. end;
  115. { Only write the flags if they are needed! }
  116. if Flags <> [] then
  117. begin
  118. Prefix := Integer(Flags) or $f0;
  119. Write(Prefix, 1);
  120. if ffChildPos in Flags then
  121. WriteInteger(ChildPos);
  122. end;
  123. WriteStr(Component.ClassName);
  124. WriteStr(Component.Name);
  125. end;
  126. procedure TBinaryObjectWriter.BeginList;
  127. begin
  128. WriteValue(vaList);
  129. end;
  130. procedure TBinaryObjectWriter.EndList;
  131. begin
  132. WriteValue(vaNull);
  133. end;
  134. procedure TBinaryObjectWriter.BeginProperty(const PropName: String);
  135. begin
  136. WriteStr(PropName);
  137. end;
  138. procedure TBinaryObjectWriter.EndProperty;
  139. begin
  140. end;
  141. procedure TBinaryObjectWriter.WriteBinary(const Buffer; Count: LongInt);
  142. begin
  143. WriteValue(vaBinary);
  144. WriteDWord(longword(Count));
  145. Write(Buffer, Count);
  146. end;
  147. procedure TBinaryObjectWriter.WriteBoolean(Value: Boolean);
  148. begin
  149. if Value then
  150. WriteValue(vaTrue)
  151. else
  152. WriteValue(vaFalse);
  153. end;
  154. {$ifndef FPUNONE}
  155. procedure TBinaryObjectWriter.WriteFloat(const Value: Extended);
  156. begin
  157. WriteValue(vaExtended);
  158. WriteExtended(Value);
  159. end;
  160. procedure TBinaryObjectWriter.WriteSingle(const Value: Single);
  161. begin
  162. WriteValue(vaSingle);
  163. WriteDWord(longword(Value));
  164. end;
  165. {$endif}
  166. procedure TBinaryObjectWriter.WriteCurrency(const Value: Currency);
  167. begin
  168. WriteValue(vaCurrency);
  169. WriteQWord(qword(Value));
  170. end;
  171. {$ifndef FPUNONE}
  172. procedure TBinaryObjectWriter.WriteDate(const Value: TDateTime);
  173. begin
  174. WriteValue(vaDate);
  175. WriteQWord(qword(Value));
  176. end;
  177. {$endif}
  178. procedure TBinaryObjectWriter.WriteIdent(const Ident: string);
  179. begin
  180. { Check if Ident is a special identifier before trying to just write
  181. Ident directly }
  182. if UpperCase(Ident) = 'NIL' then
  183. WriteValue(vaNil)
  184. else if UpperCase(Ident) = 'FALSE' then
  185. WriteValue(vaFalse)
  186. else if UpperCase(Ident) = 'TRUE' then
  187. WriteValue(vaTrue)
  188. else if UpperCase(Ident) = 'NULL' then
  189. WriteValue(vaNull) else
  190. begin
  191. WriteValue(vaIdent);
  192. WriteStr(Ident);
  193. end;
  194. end;
  195. procedure TBinaryObjectWriter.WriteInteger(Value: Int64);
  196. var
  197. s: ShortInt;
  198. i: SmallInt;
  199. l: Longint;
  200. begin
  201. { Use the smallest possible integer type for the given value: }
  202. if (Value >= -128) and (Value <= 127) then
  203. begin
  204. WriteValue(vaInt8);
  205. s := Value;
  206. Write(s, 1);
  207. end else if (Value >= -32768) and (Value <= 32767) then
  208. begin
  209. WriteValue(vaInt16);
  210. i := Value;
  211. WriteWord(word(i));
  212. end else if (Value >= -$80000000) and (Value <= $7fffffff) then
  213. begin
  214. WriteValue(vaInt32);
  215. l := Value;
  216. WriteDWord(longword(l));
  217. end else
  218. begin
  219. WriteValue(vaInt64);
  220. WriteQWord(qword(Value));
  221. end;
  222. end;
  223. procedure TBinaryObjectWriter.WriteUInt64(Value: QWord);
  224. var
  225. s: ShortInt;
  226. i: SmallInt;
  227. l: Longint;
  228. begin
  229. { Use the smallest possible integer type for the given value: }
  230. if (Value <= 127) then
  231. begin
  232. WriteValue(vaInt8);
  233. s := Value;
  234. Write(s, 1);
  235. end else if (Value <= 32767) then
  236. begin
  237. WriteValue(vaInt16);
  238. i := Value;
  239. WriteWord(word(i));
  240. end else if (Value <= $7fffffff) then
  241. begin
  242. WriteValue(vaInt32);
  243. l := Value;
  244. WriteDWord(longword(l));
  245. end else
  246. begin
  247. WriteValue(vaQWord);
  248. WriteQWord(Value);
  249. end;
  250. end;
  251. procedure TBinaryObjectWriter.WriteMethodName(const Name: String);
  252. begin
  253. if Length(Name) > 0 then
  254. begin
  255. WriteValue(vaIdent);
  256. WriteStr(Name);
  257. end else
  258. WriteValue(vaNil);
  259. end;
  260. procedure TBinaryObjectWriter.WriteSet(Value: LongInt; SetType: Pointer);
  261. type
  262. tset = set of 0..31;
  263. var
  264. i: Integer;
  265. begin
  266. WriteValue(vaSet);
  267. for i := 0 to 31 do
  268. begin
  269. if (i in tset(Value)) then
  270. WriteStr(GetEnumName(PTypeInfo(SetType), i));
  271. end;
  272. WriteStr('');
  273. end;
  274. procedure TBinaryObjectWriter.WriteString(const Value: String);
  275. var
  276. i: Integer;
  277. b: byte;
  278. begin
  279. i := Length(Value);
  280. if i <= 255 then
  281. begin
  282. WriteValue(vaString);
  283. b := i;
  284. Write(b, 1);
  285. end else
  286. begin
  287. WriteValue(vaLString);
  288. WriteDWord(longword(i));
  289. end;
  290. if i > 0 then
  291. Write(Value[1], i);
  292. end;
  293. procedure TBinaryObjectWriter.WriteWideString(const Value: WideString);
  294. var len : longword;
  295. {$IFDEF ENDIAN_BIG}
  296. i : integer;
  297. ws : widestring;
  298. {$ENDIF}
  299. begin
  300. WriteValue(vaWString);
  301. len:=Length(Value);
  302. WriteDWord(len);
  303. if len > 0 then
  304. begin
  305. {$IFDEF ENDIAN_BIG}
  306. setlength(ws,len);
  307. for i:=1 to len do
  308. ws[i]:=widechar(SwapEndian(word(Value[i])));
  309. Write(ws[1], len*sizeof(widechar));
  310. {$ELSE}
  311. Write(Value[1], len*sizeof(widechar));
  312. {$ENDIF}
  313. end;
  314. end;
  315. procedure TBinaryObjectWriter.WriteUnicodeString(const Value: UnicodeString);
  316. var len : longword;
  317. {$IFDEF ENDIAN_BIG}
  318. i : integer;
  319. us : UnicodeString;
  320. {$ENDIF}
  321. begin
  322. WriteValue(vaUString);
  323. len:=Length(Value);
  324. WriteDWord(len);
  325. if len > 0 then
  326. begin
  327. {$IFDEF ENDIAN_BIG}
  328. setlength(us,len);
  329. for i:=1 to len do
  330. us[i]:=widechar(SwapEndian(word(Value[i])));
  331. Write(us[1], len*sizeof(UnicodeChar));
  332. {$ELSE}
  333. Write(Value[1], len*sizeof(UnicodeChar));
  334. {$ENDIF}
  335. end;
  336. end;
  337. procedure TBinaryObjectWriter.WriteVariant(const VarValue: variant);
  338. begin
  339. { The variant manager will handle varbyref and vararray transparently for us
  340. }
  341. case (tvardata(VarValue).vtype and varTypeMask) of
  342. varEmpty:
  343. begin
  344. WriteValue(vaNil);
  345. end;
  346. varNull:
  347. begin
  348. WriteValue(vaNull);
  349. end;
  350. { all integer sizes must be split for big endian systems }
  351. varShortInt,varSmallInt,varInteger,varInt64:
  352. begin
  353. WriteInteger(VarValue);
  354. end;
  355. varQWord:
  356. begin
  357. WriteUInt64(VarValue);
  358. end;
  359. varBoolean:
  360. begin
  361. WriteBoolean(VarValue);
  362. end;
  363. varCurrency:
  364. begin
  365. WriteCurrency(VarValue);
  366. end;
  367. {$ifndef fpunone}
  368. varSingle:
  369. begin
  370. WriteSingle(VarValue);
  371. end;
  372. varDouble:
  373. begin
  374. WriteFloat(VarValue);
  375. end;
  376. varDate:
  377. begin
  378. WriteDate(VarValue);
  379. end;
  380. {$endif fpunone}
  381. varOleStr,varString:
  382. begin
  383. WriteWideString(VarValue);
  384. end;
  385. else
  386. raise EWriteError.CreateFmt(SUnsupportedPropertyVariantType, [Ord(tvardata(VarValue).vtype)]);
  387. end;
  388. end;
  389. procedure TBinaryObjectWriter.FlushBuffer;
  390. begin
  391. FStream.WriteBuffer(FBuffer^, FBufPos);
  392. FBufPos := 0;
  393. end;
  394. procedure TBinaryObjectWriter.Write(const Buffer; Count: LongInt);
  395. var
  396. CopyNow: LongInt;
  397. SourceBuf: PChar;
  398. begin
  399. SourceBuf:=@Buffer;
  400. while Count > 0 do
  401. begin
  402. CopyNow := Count;
  403. if CopyNow > FBufSize - FBufPos then
  404. CopyNow := FBufSize - FBufPos;
  405. Move(SourceBuf^, PChar(FBuffer)[FBufPos], CopyNow);
  406. Dec(Count, CopyNow);
  407. Inc(FBufPos, CopyNow);
  408. inc(SourceBuf, CopyNow);
  409. if FBufPos = FBufSize then
  410. FlushBuffer;
  411. end;
  412. end;
  413. procedure TBinaryObjectWriter.WriteValue(Value: TValueType);
  414. var
  415. b: byte;
  416. begin
  417. b := byte(Value);
  418. Write(b, 1);
  419. end;
  420. procedure TBinaryObjectWriter.WriteStr(const Value: String);
  421. var
  422. i: integer;
  423. b: byte;
  424. begin
  425. i := Length(Value);
  426. if i > 255 then
  427. i := 255;
  428. b := i;
  429. Write(b, 1);
  430. if i > 0 then
  431. Write(Value[1], i);
  432. end;
  433. {****************************************************************************}
  434. {* TWriter *}
  435. {****************************************************************************}
  436. constructor TWriter.Create(ADriver: TAbstractObjectWriter);
  437. begin
  438. inherited Create;
  439. FDriver := ADriver;
  440. end;
  441. constructor TWriter.Create(Stream: TStream; BufSize: Integer);
  442. begin
  443. inherited Create;
  444. If (Stream=Nil) then
  445. Raise EWriteError.Create(SEmptyStreamIllegalWriter);
  446. FDriver := CreateDriver(Stream, BufSize);
  447. FDestroyDriver := True;
  448. end;
  449. destructor TWriter.Destroy;
  450. begin
  451. if FDestroyDriver then
  452. FDriver.Free;
  453. inherited Destroy;
  454. end;
  455. function TWriter.CreateDriver(Stream: TStream; BufSize: Integer): TAbstractObjectWriter;
  456. begin
  457. Result := TBinaryObjectWriter.Create(Stream, BufSize);
  458. end;
  459. Type
  460. TPosComponent = Class(TObject)
  461. FPos : Integer;
  462. FComponent : TComponent;
  463. Constructor Create(APos : Integer; AComponent : TComponent);
  464. end;
  465. Constructor TPosComponent.Create(APos : Integer; AComponent : TComponent);
  466. begin
  467. FPos:=APos;
  468. FComponent:=AComponent;
  469. end;
  470. // Used as argument for calls to TComponent.GetChildren:
  471. procedure TWriter.AddToAncestorList(Component: TComponent);
  472. begin
  473. FAncestors.AddObject(Component.Name,TPosComponent.Create(FAncestors.Count,Component));
  474. end;
  475. procedure TWriter.DefineProperty(const Name: String;
  476. ReadData: TReaderProc; AWriteData: TWriterProc; HasData: Boolean);
  477. begin
  478. if HasData and Assigned(AWriteData) then
  479. begin
  480. // Write the property name and then the data itself
  481. Driver.BeginProperty(FPropPath + Name);
  482. AWriteData(Self);
  483. Driver.EndProperty;
  484. end;
  485. end;
  486. procedure TWriter.DefineBinaryProperty(const Name: String;
  487. ReadData, AWriteData: TStreamProc; HasData: Boolean);
  488. begin
  489. if HasData and Assigned(AWriteData) then
  490. begin
  491. // Write the property name and then the data itself
  492. Driver.BeginProperty(FPropPath + Name);
  493. WriteBinary(AWriteData);
  494. Driver.EndProperty;
  495. end;
  496. end;
  497. procedure TWriter.Write(const Buffer; Count: Longint);
  498. begin
  499. //This should give an exception if write is not implemented (i.e. TTextObjectWriter)
  500. //but should work with TBinaryObjectWriter.
  501. Driver.Write(Buffer, Count);
  502. end;
  503. procedure TWriter.SetRoot(ARoot: TComponent);
  504. begin
  505. inherited SetRoot(ARoot);
  506. // Use the new root as lookup root too
  507. FLookupRoot := ARoot;
  508. end;
  509. procedure TWriter.WriteBinary(AWriteData: TStreamProc);
  510. var
  511. MemBuffer: TMemoryStream;
  512. BufferSize: Longint;
  513. begin
  514. { First write the binary data into a memory stream, then copy this buffered
  515. stream into the writing destination. This is necessary as we have to know
  516. the size of the binary data in advance (we're assuming that seeking within
  517. the writer stream is not possible) }
  518. MemBuffer := TMemoryStream.Create;
  519. try
  520. AWriteData(MemBuffer);
  521. BufferSize := MemBuffer.Size;
  522. Driver.WriteBinary(MemBuffer.Memory^, BufferSize);
  523. finally
  524. MemBuffer.Free;
  525. end;
  526. end;
  527. procedure TWriter.WriteBoolean(Value: Boolean);
  528. begin
  529. Driver.WriteBoolean(Value);
  530. end;
  531. procedure TWriter.WriteChar(Value: Char);
  532. begin
  533. WriteString(Value);
  534. end;
  535. procedure TWriter.WriteWideChar(Value: WideChar);
  536. begin
  537. WriteWideString(Value);
  538. end;
  539. procedure TWriter.WriteCollection(Value: TCollection);
  540. var
  541. i: Integer;
  542. begin
  543. Driver.BeginCollection;
  544. if Assigned(Value) then
  545. for i := 0 to Value.Count - 1 do
  546. begin
  547. { Each collection item needs its own ListBegin/ListEnd tag, or else the
  548. reader wouldn't be able to know where an item ends and where the next
  549. one starts }
  550. WriteListBegin;
  551. WriteProperties(Value.Items[i]);
  552. WriteListEnd;
  553. end;
  554. WriteListEnd;
  555. end;
  556. procedure TWriter.DetermineAncestor(Component : TComponent);
  557. Var
  558. I : Integer;
  559. begin
  560. // Should be set only when we write an inherited with children.
  561. if Not Assigned(FAncestors) then
  562. exit;
  563. I:=FAncestors.IndexOf(Component.Name);
  564. If (I=-1) then
  565. begin
  566. FAncestor:=Nil;
  567. FAncestorPos:=-1;
  568. end
  569. else
  570. With TPosComponent(FAncestors.Objects[i]) do
  571. begin
  572. FAncestor:=FComponent;
  573. FAncestorPos:=FPos;
  574. end;
  575. end;
  576. procedure TWriter.DoFindAncestor(Component : TComponent);
  577. Var
  578. C : TComponent;
  579. begin
  580. if Assigned(FOnFindAncestor) then
  581. if (Ancestor=Nil) or (Ancestor is TComponent) then
  582. begin
  583. C:=TComponent(Ancestor);
  584. FOnFindAncestor(Self,Component,Component.Name,C,FRootAncestor);
  585. Ancestor:=C;
  586. end;
  587. end;
  588. procedure TWriter.WriteComponent(Component: TComponent);
  589. var
  590. SA : TPersistent;
  591. SR : TComponent;
  592. begin
  593. SR:=FRoot;
  594. SA:=FAncestor;
  595. Try
  596. Component.FComponentState:=Component.FComponentState+[csWriting];
  597. Try
  598. // Possibly set ancestor.
  599. DetermineAncestor(Component);
  600. DoFindAncestor(Component); // Mainly for IDE when a parent form had an ancestor renamed...
  601. // Will call WriteComponentData.
  602. Component.WriteState(Self);
  603. FDriver.EndList;
  604. Finally
  605. Component.FComponentState:=Component.FComponentState-[csWriting];
  606. end;
  607. Finally
  608. FAncestor:=SA;
  609. FRoot:=SR;
  610. end;
  611. end;
  612. procedure TWriter.WriteChildren(Component : TComponent);
  613. Var
  614. SRoot, SRootA : TComponent;
  615. SList : TStringList;
  616. SPos : Integer;
  617. I : Integer;
  618. begin
  619. // Write children list.
  620. // While writing children, the ancestor environment must be saved
  621. // This is recursive...
  622. SRoot:=FRoot;
  623. SRootA:=FRootAncestor;
  624. SList:=FAncestors;
  625. SPos:=FCurrentPos;
  626. try
  627. FAncestors:=Nil;
  628. FCurrentPos:=0;
  629. FAncestorPos:=-1;
  630. if csInline in Component.ComponentState then
  631. FRoot:=Component;
  632. if (FAncestor is TComponent) then
  633. begin
  634. FAncestors:=TStringList.Create;
  635. if csInline in TComponent(FAncestor).ComponentState then
  636. FRootAncestor := TComponent(FAncestor);
  637. TComponent(FAncestor).GetChildren(@AddToAncestorList,FRootAncestor);
  638. FAncestors.Sorted:=True;
  639. end;
  640. try
  641. Component.GetChildren(@WriteComponent, FRoot);
  642. Finally
  643. If Assigned(Fancestors) then
  644. For I:=0 to FAncestors.Count-1 do
  645. FAncestors.Objects[i].Free;
  646. FreeAndNil(FAncestors);
  647. end;
  648. finally
  649. FAncestors:=Slist;
  650. FRoot:=SRoot;
  651. FRootAncestor:=SRootA;
  652. FCurrentPos:=SPos;
  653. FAncestorPos:=Spos;
  654. end;
  655. end;
  656. procedure TWriter.WriteComponentData(Instance: TComponent);
  657. var
  658. Flags: TFilerFlags;
  659. begin
  660. Flags := [];
  661. If (Assigned(FAncestor)) and //has ancestor
  662. (not (csInline in Instance.ComponentState) or // no inline component
  663. // .. or the inline component is inherited
  664. (csAncestor in Instance.Componentstate) and (FAncestors <> nil)) then
  665. Flags:=[ffInherited]
  666. else If csInline in Instance.ComponentState then
  667. Flags:=[ffInline];
  668. If (FAncestors<>Nil) and ((FCurrentPos<>FAncestorPos) or (FAncestor=Nil)) then
  669. Include(Flags,ffChildPos);
  670. FDriver.BeginComponent(Instance,Flags,FCurrentPos);
  671. If (FAncestors<>Nil) then
  672. Inc(FCurrentPos);
  673. WriteProperties(Instance);
  674. WriteListEnd;
  675. // Needs special handling of ancestor.
  676. If not IgnoreChildren then
  677. WriteChildren(Instance);
  678. end;
  679. procedure TWriter.WriteDescendent(ARoot: TComponent; AAncestor: TComponent);
  680. begin
  681. FRoot := ARoot;
  682. FAncestor := AAncestor;
  683. FRootAncestor := AAncestor;
  684. FLookupRoot := ARoot;
  685. WriteComponent(ARoot);
  686. end;
  687. {$ifndef FPUNONE}
  688. procedure TWriter.WriteFloat(const Value: Extended);
  689. begin
  690. Driver.WriteFloat(Value);
  691. end;
  692. procedure TWriter.WriteSingle(const Value: Single);
  693. begin
  694. Driver.WriteSingle(Value);
  695. end;
  696. {$endif}
  697. procedure TWriter.WriteCurrency(const Value: Currency);
  698. begin
  699. Driver.WriteCurrency(Value);
  700. end;
  701. {$ifndef FPUNONE}
  702. procedure TWriter.WriteDate(const Value: TDateTime);
  703. begin
  704. Driver.WriteDate(Value);
  705. end;
  706. {$endif}
  707. procedure TWriter.WriteIdent(const Ident: string);
  708. begin
  709. Driver.WriteIdent(Ident);
  710. end;
  711. procedure TWriter.WriteInteger(Value: LongInt);
  712. begin
  713. Driver.WriteInteger(Value);
  714. end;
  715. procedure TWriter.WriteInteger(Value: Int64);
  716. begin
  717. Driver.WriteInteger(Value);
  718. end;
  719. procedure TWriter.WriteVariant(const VarValue: Variant);
  720. begin
  721. Driver.WriteVariant(VarValue);
  722. end;
  723. procedure TWriter.WriteListBegin;
  724. begin
  725. Driver.BeginList;
  726. end;
  727. procedure TWriter.WriteListEnd;
  728. begin
  729. Driver.EndList;
  730. end;
  731. procedure TWriter.WriteProperties(Instance: TPersistent);
  732. var PropCount,i : integer;
  733. PropList : PPropList;
  734. begin
  735. PropCount:=GetPropList(Instance,PropList);
  736. if PropCount>0 then
  737. try
  738. for i := 0 to PropCount-1 do
  739. if IsStoredProp(Instance,PropList^[i]) then
  740. WriteProperty(Instance,PropList^[i]);
  741. Finally
  742. Freemem(PropList);
  743. end;
  744. Instance.DefineProperties(Self);
  745. end;
  746. procedure TWriter.WriteProperty(Instance: TPersistent; PropInfo: Pointer);
  747. var
  748. HasAncestor: Boolean;
  749. PropType: PTypeInfo;
  750. Value, DefValue: LongInt;
  751. Ident: String;
  752. IntToIdentFn: TIntToIdent;
  753. {$ifndef FPUNONE}
  754. FloatValue, DefFloatValue: Extended;
  755. {$endif}
  756. MethodValue: TMethod;
  757. DefMethodValue: TMethod;
  758. WStrValue, WDefStrValue: WideString;
  759. StrValue, DefStrValue: String;
  760. UStrValue, UDefStrValue: UnicodeString;
  761. AncestorObj: TObject;
  762. Component: TComponent;
  763. ObjValue: TObject;
  764. SavedAncestor: TPersistent;
  765. SavedPropPath, Name: String;
  766. Int64Value, DefInt64Value: Int64;
  767. VarValue, DefVarValue : tvardata;
  768. BoolValue, DefBoolValue: boolean;
  769. Handled: Boolean;
  770. begin
  771. // do not stream properties without getter
  772. if not Assigned(PPropInfo(PropInfo)^.GetProc) then
  773. exit;
  774. // properties without setter are only allowed, if they are subcomponents
  775. PropType := PPropInfo(PropInfo)^.PropType;
  776. if not Assigned(PPropInfo(PropInfo)^.SetProc) then begin
  777. if PropType^.Kind<>tkClass then
  778. exit;
  779. ObjValue := TObject(GetObjectProp(Instance, PropInfo));
  780. if not ObjValue.InheritsFrom(TComponent) or
  781. not (csSubComponent in TComponent(ObjValue).ComponentStyle) then
  782. exit;
  783. end;
  784. { Check if the ancestor can be used }
  785. HasAncestor := Assigned(Ancestor) and ((Instance = Root) or
  786. (Instance.ClassType = Ancestor.ClassType));
  787. //writeln('TWriter.WriteProperty Name=',PropType^.Name,' Kind=',GetEnumName(TypeInfo(TTypeKind),ord(PropType^.Kind)),' HasAncestor=',HasAncestor);
  788. case PropType^.Kind of
  789. tkInteger, tkChar, tkEnumeration, tkSet, tkWChar:
  790. begin
  791. Value := GetOrdProp(Instance, PropInfo);
  792. if HasAncestor then
  793. DefValue := GetOrdProp(Ancestor, PropInfo)
  794. else
  795. DefValue := PPropInfo(PropInfo)^.Default;
  796. // writeln(PPropInfo(PropInfo)^.Name, ', HasAncestor=', ord(HasAncestor), ', Value=', Value, ', Default=', DefValue);
  797. if (Value <> DefValue) or (DefValue=longint($80000000)) then
  798. begin
  799. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  800. case PropType^.Kind of
  801. tkInteger:
  802. begin
  803. // Check if this integer has a string identifier
  804. IntToIdentFn := FindIntToIdent(PPropInfo(PropInfo)^.PropType);
  805. if Assigned(IntToIdentFn) and IntToIdentFn(Value, Ident) then
  806. // Integer can be written a human-readable identifier
  807. WriteIdent(Ident)
  808. else
  809. // Integer has to be written just as number
  810. WriteInteger(Value);
  811. end;
  812. tkChar:
  813. WriteChar(Chr(Value));
  814. tkWChar:
  815. WriteWideChar(WideChar(Value));
  816. tkSet:
  817. Driver.WriteSet(Value, GetTypeData(PropType)^.CompType);
  818. tkEnumeration:
  819. WriteIdent(GetEnumName(PropType, Value));
  820. end;
  821. Driver.EndProperty;
  822. end;
  823. end;
  824. {$ifndef FPUNONE}
  825. tkFloat:
  826. begin
  827. FloatValue := GetFloatProp(Instance, PropInfo);
  828. if HasAncestor then
  829. DefFloatValue := GetFloatProp(Ancestor, PropInfo)
  830. else
  831. begin
  832. DefValue :=PPropInfo(PropInfo)^.Default;
  833. DefFloatValue:=PSingle(@PPropInfo(PropInfo)^.Default)^;
  834. end;
  835. if (FloatValue<>DefFloatValue) or (DefValue=longint($80000000)) then
  836. begin
  837. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  838. WriteFloat(FloatValue);
  839. Driver.EndProperty;
  840. end;
  841. end;
  842. {$endif}
  843. tkMethod:
  844. begin
  845. MethodValue := GetMethodProp(Instance, PropInfo);
  846. if HasAncestor then
  847. DefMethodValue := GetMethodProp(Ancestor, PropInfo)
  848. else begin
  849. DefMethodValue.Data := nil;
  850. DefMethodValue.Code := nil;
  851. end;
  852. Handled:=false;
  853. if Assigned(OnWriteMethodProperty) then
  854. OnWriteMethodProperty(Self,Instance,PPropInfo(PropInfo),MethodValue,
  855. DefMethodValue,Handled);
  856. if (not Handled) and
  857. (MethodValue.Code <> DefMethodValue.Code) and
  858. ((not Assigned(MethodValue.Code)) or
  859. ((Length(FLookupRoot.MethodName(MethodValue.Code)) >= 0))) then
  860. begin
  861. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  862. if Assigned(MethodValue.Code) then
  863. Driver.WriteMethodName(FLookupRoot.MethodName(MethodValue.Code))
  864. else
  865. Driver.WriteMethodName('');
  866. Driver.EndProperty;
  867. end;
  868. end;
  869. tkSString, tkLString, tkAString:
  870. begin
  871. StrValue := GetStrProp(Instance, PropInfo);
  872. if HasAncestor then
  873. DefStrValue := GetStrProp(Ancestor, PropInfo)
  874. else
  875. SetLength(DefStrValue, 0);
  876. if StrValue <> DefStrValue then
  877. begin
  878. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  879. if Assigned(FOnWriteStringProperty) then
  880. FOnWriteStringProperty(Self,Instance,PropInfo,StrValue);
  881. WriteString(StrValue);
  882. Driver.EndProperty;
  883. end;
  884. end;
  885. tkWString:
  886. begin
  887. WStrValue := GetWideStrProp(Instance, PropInfo);
  888. if HasAncestor then
  889. WDefStrValue := GetWideStrProp(Ancestor, PropInfo)
  890. else
  891. SetLength(WDefStrValue, 0);
  892. if WStrValue <> WDefStrValue then
  893. begin
  894. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  895. WriteWideString(WStrValue);
  896. Driver.EndProperty;
  897. end;
  898. end;
  899. tkUString:
  900. begin
  901. UStrValue := GetUnicodeStrProp(Instance, PropInfo);
  902. if HasAncestor then
  903. UDefStrValue := GetUnicodeStrProp(Ancestor, PropInfo)
  904. else
  905. SetLength(UDefStrValue, 0);
  906. if UStrValue <> UDefStrValue then
  907. begin
  908. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  909. WriteUnicodeString(UStrValue);
  910. Driver.EndProperty;
  911. end;
  912. end;
  913. tkVariant:
  914. begin
  915. { Ensure that a Variant manager is installed }
  916. if not assigned(VarClearProc) then
  917. raise EWriteError.Create(SErrNoVariantSupport);
  918. VarValue := tvardata(GetVariantProp(Instance, PropInfo));
  919. if HasAncestor then
  920. DefVarValue := tvardata(GetVariantProp(Ancestor, PropInfo))
  921. else
  922. FillChar(DefVarValue,sizeof(DefVarValue),0);
  923. if (CompareByte(VarValue,DefVarValue,sizeof(VarValue)) <> 0) then
  924. begin
  925. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  926. { can't use variant() typecast, pulls in variants unit }
  927. WriteVariant(pvariant(@VarValue)^);
  928. Driver.EndProperty;
  929. end;
  930. end;
  931. tkClass:
  932. begin
  933. ObjValue := TObject(GetObjectProp(Instance, PropInfo));
  934. if HasAncestor then
  935. begin
  936. AncestorObj := TObject(GetObjectProp(Ancestor, PropInfo));
  937. if (AncestorObj is TComponent) and
  938. (ObjValue is TComponent) then
  939. begin
  940. //writeln('TWriter.WriteProperty AncestorObj=',TComponent(AncestorObj).Name,' OwnerFit=',TComponent(AncestorObj).Owner = FRootAncestor,' ',TComponent(ObjValue).Name,' OwnerFit=',TComponent(ObjValue).Owner = Root);
  941. if (TComponent(AncestorObj).Owner <> FRootAncestor) or
  942. (TComponent(ObjValue).Owner <> Root) or
  943. (UpperCase(TComponent(AncestorObj).Name) <> UpperCase(TComponent(ObjValue).Name)) then
  944. begin
  945. AncestorObj := nil;
  946. end;
  947. end;
  948. end else
  949. AncestorObj := nil;
  950. if not Assigned(ObjValue) then
  951. begin
  952. if ObjValue <> AncestorObj then
  953. begin
  954. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  955. Driver.WriteIdent('NIL');
  956. Driver.EndProperty;
  957. end
  958. end
  959. else if ObjValue.InheritsFrom(TPersistent) then
  960. begin
  961. { Subcomponents are streamed the same way as persistents }
  962. if ObjValue.InheritsFrom(TComponent)
  963. and ((not (csSubComponent in TComponent(ObjValue).ComponentStyle))
  964. or ((TComponent(ObjValue).Owner<>Instance) and (TComponent(ObjValue).Owner<>Nil))) then
  965. begin
  966. Component := TComponent(ObjValue);
  967. if (ObjValue <> AncestorObj)
  968. and not (csTransient in Component.ComponentStyle) then
  969. begin
  970. { Determine the correct name of the component this property contains }
  971. if Component.Owner = LookupRoot then
  972. Name := Component.Name
  973. else if Component = LookupRoot then
  974. Name := 'Owner'
  975. else if Assigned(Component.Owner) and (Length(Component.Owner.Name) > 0)
  976. and (Length(Component.Name) > 0) then
  977. Name := Component.Owner.Name + '.' + Component.Name
  978. else if Length(Component.Name) > 0 then
  979. Name := Component.Name + '.Owner'
  980. else
  981. SetLength(Name, 0);
  982. if Length(Name) > 0 then
  983. begin
  984. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  985. WriteIdent(Name);
  986. Driver.EndProperty;
  987. end; // length Name>0
  988. end; //(ObjValue <> AncestorObj)
  989. end // ObjValue.InheritsFrom(TComponent)
  990. else if ObjValue.InheritsFrom(TCollection) then
  991. begin
  992. if (not HasAncestor) or (not CollectionsEqual(TCollection(ObjValue),
  993. TCollection(GetObjectProp(Ancestor, PropInfo)),root,rootancestor)) then
  994. begin
  995. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  996. SavedPropPath := FPropPath;
  997. try
  998. SetLength(FPropPath, 0);
  999. WriteCollection(TCollection(ObjValue));
  1000. finally
  1001. FPropPath := SavedPropPath;
  1002. Driver.EndProperty;
  1003. end;
  1004. end;
  1005. end // Tcollection
  1006. else
  1007. begin
  1008. SavedAncestor := Ancestor;
  1009. SavedPropPath := FPropPath;
  1010. try
  1011. FPropPath := FPropPath + PPropInfo(PropInfo)^.Name + '.';
  1012. if HasAncestor then
  1013. Ancestor := TPersistent(GetObjectProp(Ancestor, PropInfo));
  1014. WriteProperties(TPersistent(ObjValue));
  1015. finally
  1016. Ancestor := SavedAncestor;
  1017. FPropPath := SavedPropPath;
  1018. end;
  1019. end;
  1020. end; // Inheritsfrom(TPersistent)
  1021. end;
  1022. tkInt64, tkQWord:
  1023. begin
  1024. Int64Value := GetInt64Prop(Instance, PropInfo);
  1025. if HasAncestor then
  1026. DefInt64Value := GetInt64Prop(Ancestor, PropInfo)
  1027. else
  1028. DefInt64Value := 0;
  1029. if Int64Value <> DefInt64Value then
  1030. begin
  1031. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  1032. WriteInteger(Int64Value);
  1033. Driver.EndProperty;
  1034. end;
  1035. end;
  1036. tkBool:
  1037. begin
  1038. BoolValue := GetOrdProp(Instance, PropInfo)<>0;
  1039. if HasAncestor then
  1040. DefBoolValue := GetOrdProp(Ancestor, PropInfo)<>0
  1041. else
  1042. begin
  1043. DefBoolValue := PPropInfo(PropInfo)^.Default<>0;
  1044. DefValue:=PPropInfo(PropInfo)^.Default;
  1045. end;
  1046. // writeln(PPropInfo(PropInfo)^.Name, ', HasAncestor=', ord(HasAncestor), ', Value=', Value, ', Default=', DefBoolValue);
  1047. if (BoolValue<>DefBoolValue) or (DefValue=longint($80000000)) then
  1048. begin
  1049. Driver.BeginProperty(FPropPath + PPropInfo(PropInfo)^.Name);
  1050. WriteBoolean(BoolValue);
  1051. Driver.EndProperty;
  1052. end;
  1053. end;
  1054. end;
  1055. end;
  1056. procedure TWriter.WriteRootComponent(ARoot: TComponent);
  1057. begin
  1058. WriteDescendent(ARoot, nil);
  1059. end;
  1060. procedure TWriter.WriteString(const Value: String);
  1061. begin
  1062. Driver.WriteString(Value);
  1063. end;
  1064. procedure TWriter.WriteWideString(const Value: WideString);
  1065. begin
  1066. Driver.WriteWideString(Value);
  1067. end;
  1068. procedure TWriter.WriteUnicodeString(const Value: UnicodeString);
  1069. begin
  1070. Driver.WriteUnicodeString(Value);
  1071. end;