writer.inc 35 KB

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