classes.inc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by Michael Van Canneyt and Florian Klaempfl
  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. * Class implementations are in separate files. *
  12. **********************************************************************}
  13. type
  14. {$ifdef CPU16}
  15. TFilerFlagsInt = Byte;
  16. {$else CPU16}
  17. TFilerFlagsInt = LongInt;
  18. {$endif CPU16}
  19. var
  20. ClassList : TThreadlist;
  21. ClassAliasList : TStringList;
  22. {
  23. Include all message strings
  24. Add a language with IFDEF LANG_NAME
  25. just befor the final ELSE. This way English will always be the default.
  26. }
  27. {$IFDEF LANG_GERMAN}
  28. {$i constsg.inc}
  29. {$ELSE}
  30. {$IFDEF LANG_SPANISH}
  31. {$i constss.inc}
  32. {$ENDIF}
  33. {$ENDIF}
  34. { Utility routines }
  35. {$i util.inc}
  36. { TBits implementation }
  37. {$i bits.inc}
  38. { All streams implementations: }
  39. { Tstreams THandleStream TFileStream TResourcseStreams TStringStream }
  40. { TCustomMemoryStream TMemoryStream }
  41. {$i streams.inc}
  42. { TParser implementation}
  43. {$i parser.inc}
  44. { TCollection and TCollectionItem implementations }
  45. {$i collect.inc}
  46. { TList and TThreadList implementations }
  47. {$i lists.inc}
  48. { TStrings and TStringList implementations }
  49. {$i stringl.inc}
  50. { TThread implementation }
  51. { system independend threading code }
  52. var
  53. { event executed by SychronizeInternal to wake main thread if it sleeps in
  54. CheckSynchronize }
  55. SynchronizeTimeoutEvent: PRtlEvent;
  56. { the head of the queue containing the entries to be Synchronized - Nil if the
  57. queue is empty }
  58. ThreadQueueHead: TThread.PThreadQueueEntry;
  59. { the tail of the queue containing the entries to be Synchronized - Nil if the
  60. queue is empty }
  61. ThreadQueueTail: TThread.PThreadQueueEntry;
  62. { used for serialized access to the queue }
  63. ThreadQueueLock: TRtlCriticalSection;
  64. { this list holds all instances of external threads that need to be freed at
  65. the end of the program }
  66. ExternalThreads: TThreadList;
  67. threadvar
  68. { the instance of the current thread; in case of an external thread this is
  69. Nil until TThread.GetCurrentThread was called once (the RTLs need to ensure
  70. that threadvars are initialized with 0!) }
  71. CurrentThreadVar: TThread;
  72. type
  73. { this type is used if a thread is created using
  74. TThread.CreateAnonymousThread }
  75. TAnonymousThread = class(TThread)
  76. private
  77. fProc: TProcedure;
  78. protected
  79. procedure Execute; override;
  80. public
  81. { as in TThread aProc needs to be changed to TProc once closures are
  82. supported }
  83. constructor Create(aProc: TProcedure);
  84. end;
  85. procedure TAnonymousThread.Execute;
  86. begin
  87. fProc();
  88. end;
  89. constructor TAnonymousThread.Create(aProc: TProcedure);
  90. begin
  91. { an anonymous thread is created suspended and with FreeOnTerminate set }
  92. inherited Create(True);
  93. FreeOnTerminate := True;
  94. fProc := aProc;
  95. end;
  96. type
  97. { this type is used by TThread.GetCurrentThread if the thread does not yet
  98. have a value in CurrentThreadVar (Note: the main thread is also created as
  99. a TExternalThread) }
  100. TExternalThread = class(TThread)
  101. protected
  102. { dummy method to remove the warning }
  103. procedure Execute; override;
  104. public
  105. constructor Create;
  106. end;
  107. procedure TExternalThread.Execute;
  108. begin
  109. { empty }
  110. end;
  111. constructor TExternalThread.Create;
  112. begin
  113. FExternalThread := True;
  114. { the parameter is unimportant if FExternalThread is True }
  115. inherited Create(False);
  116. end;
  117. function ThreadProc(ThreadObjPtr: Pointer): PtrInt;
  118. var
  119. FreeThread: Boolean;
  120. Thread: TThread absolute ThreadObjPtr;
  121. begin
  122. { if Suspend checks FSuspended before doing anything, make sure it }
  123. { knows we're currently not suspended (this flag may have been set }
  124. { to true if CreateSuspended was true) }
  125. // Thread.FSuspended:=false;
  126. // wait until AfterConstruction has been called, so we cannot
  127. // free ourselves before TThread.Create has finished
  128. // (since that one may check our VTM in case of $R+, and
  129. // will call the AfterConstruction method in all cases)
  130. // Thread.Suspend;
  131. try
  132. { The thread may be already terminated at this point, e.g. if it was intially
  133. suspended, or if it wasn't ever scheduled for execution for whatever reason.
  134. So bypass user code if terminated. }
  135. if not Thread.Terminated then begin
  136. CurrentThreadVar := Thread;
  137. Thread.Execute;
  138. end;
  139. except
  140. Thread.FFatalException := TObject(AcquireExceptionObject);
  141. end;
  142. FreeThread := Thread.FFreeOnTerminate;
  143. Result := Thread.FReturnValue;
  144. Thread.FFinished := True;
  145. Thread.DoTerminate;
  146. if FreeThread then
  147. Thread.Free;
  148. EndThread(Result);
  149. end;
  150. { system-dependent code }
  151. {$i tthread.inc}
  152. constructor TThread.Create(CreateSuspended: Boolean;
  153. const StackSize: SizeUInt);
  154. begin
  155. inherited Create;
  156. if FExternalThread then
  157. FThreadID := GetCurrentThreadID
  158. else
  159. SysCreate(CreateSuspended, StackSize);
  160. end;
  161. destructor TThread.Destroy;
  162. begin
  163. if not FExternalThread then begin
  164. SysDestroy;
  165. if FHandle <> TThreadID(0) then
  166. CloseThread(FHandle);
  167. end;
  168. RemoveQueuedEvents(Self);
  169. DoneSynchronizeEvent;
  170. { set CurrentThreadVar to Nil? }
  171. inherited Destroy;
  172. end;
  173. procedure TThread.Start;
  174. begin
  175. { suspend/resume are now deprecated in Delphi (they also don't work
  176. on most platforms in FPC), so a different method was required
  177. to start a thread if it's create with fSuspended=true -> that's
  178. what this method is for. }
  179. Resume;
  180. end;
  181. function TThread.GetSuspended: Boolean;
  182. begin
  183. GetSuspended:=FSuspended;
  184. end;
  185. procedure TThread.AfterConstruction;
  186. begin
  187. inherited AfterConstruction;
  188. // enable for all platforms once http://bugs.freepascal.org/view.php?id=16884
  189. // is fixed for all platforms (in case the fix for non-unix platforms also
  190. // requires this field at least)
  191. {$if defined(unix) or defined(windows)}
  192. if not FExternalThread and not FInitialSuspended then
  193. Resume;
  194. {$endif}
  195. end;
  196. procedure ExecuteThreadQueueEntry(aEntry: TThread.PThreadQueueEntry);
  197. begin
  198. if Assigned(aEntry^.Method) then
  199. aEntry^.Method()
  200. // enable once closures are supported
  201. {else
  202. aEntry^.ThreadProc();}
  203. end;
  204. procedure ThreadQueueAppend(aEntry: TThread.PThreadQueueEntry);
  205. begin
  206. { do we really need a synchronized call? }
  207. if GetCurrentThreadID = MainThreadID then begin
  208. ExecuteThreadQueueEntry(aEntry);
  209. if not Assigned(aEntry^.SyncEvent) then
  210. Dispose(aEntry);
  211. end else begin
  212. System.EnterCriticalSection(ThreadQueueLock);
  213. try
  214. { add the entry to the thread queue }
  215. if Assigned(ThreadQueueTail) then begin
  216. ThreadQueueTail^.Next := aEntry;
  217. end else
  218. ThreadQueueHead := aEntry;
  219. ThreadQueueTail := aEntry;
  220. finally
  221. System.LeaveCriticalSection(ThreadQueueLock);
  222. end;
  223. { ensure that the main thread knows that something awaits }
  224. RtlEventSetEvent(SynchronizeTimeoutEvent);
  225. if assigned(WakeMainThread) then
  226. WakeMainThread(aEntry^.Thread);
  227. { is this a Synchronize or Queue entry? }
  228. if Assigned(aEntry^.SyncEvent) then begin
  229. RtlEventWaitFor(aEntry^.SyncEvent);
  230. if Assigned(aEntry^.Exception) then
  231. raise aEntry^.Exception;
  232. end;
  233. end;
  234. end;
  235. procedure TThread.InitSynchronizeEvent;
  236. begin
  237. if Assigned(FSynchronizeEntry) then
  238. Exit;
  239. New(FSynchronizeEntry);
  240. FillChar(FSynchronizeEntry^, SizeOf(TThreadQueueEntry), 0);
  241. FSynchronizeEntry^.Thread := Self;
  242. FSynchronizeEntry^.SyncEvent := RtlEventCreate;
  243. end;
  244. procedure TThread.DoneSynchronizeEvent;
  245. begin
  246. if not Assigned(FSynchronizeEntry) then
  247. Exit;
  248. RtlEventDestroy(FSynchronizeEntry^.SyncEvent);
  249. Dispose(FSynchronizeEntry);
  250. FSynchronizeEntry := Nil;
  251. end;
  252. class procedure TThread.Synchronize(AThread: TThread; AMethod: TThreadMethod);
  253. begin
  254. { ensure that we have a TThread instance }
  255. if not Assigned(AThread) then
  256. AThread := CurrentThread;
  257. { the Synchronize event is instantiated on demand }
  258. AThread.InitSynchronizeEvent;
  259. AThread.FSynchronizeEntry^.Exception := Nil;
  260. AThread.FSynchronizeEntry^.Method := AMethod;
  261. ThreadQueueAppend(AThread.FSynchronizeEntry);
  262. AThread.FSynchronizeEntry^.Method := Nil;
  263. AThread.FSynchronizeEntry^.Next := Nil;
  264. end;
  265. procedure TThread.Synchronize(AMethod: TThreadMethod);
  266. begin
  267. TThread.Synchronize(self,AMethod);
  268. end;
  269. function CheckSynchronize(timeout : longint=0) : boolean;
  270. { assumes being called from GUI thread }
  271. var
  272. exceptobj: Exception;
  273. tmpentry: TThread.PThreadQueueEntry;
  274. begin
  275. result:=false;
  276. { first sanity check }
  277. if Not IsMultiThread then
  278. Exit
  279. { second sanity check }
  280. else if GetCurrentThreadID<>MainThreadID then
  281. raise EThread.CreateFmt(SCheckSynchronizeError,[GetCurrentThreadID])
  282. else
  283. begin
  284. if timeout>0 then
  285. begin
  286. RtlEventWaitFor(SynchronizeTimeoutEvent,timeout);
  287. end
  288. else
  289. RtlEventResetEvent(SynchronizeTimeoutEvent);
  290. System.EnterCriticalSection(ThreadQueueLock);
  291. try
  292. { Note: we don't need to pay attention to recursive calls to
  293. Synchronize as those calls will be executed in the context of
  294. the GUI thread and thus will be executed immediatly instead of
  295. queuing them }
  296. while Assigned(ThreadQueueHead) do begin
  297. { step 1: update the list }
  298. tmpentry := ThreadQueueHead;
  299. ThreadQueueHead := ThreadQueueHead^.Next;
  300. if not Assigned(ThreadQueueHead) then
  301. ThreadQueueTail := Nil;
  302. { step 2: execute the method }
  303. exceptobj := Nil;
  304. try
  305. ExecuteThreadQueueEntry(tmpentry);
  306. except
  307. exceptobj := Exception(AcquireExceptionObject);
  308. end;
  309. { step 3: error handling and cleanup }
  310. if Assigned(tmpentry^.SyncEvent) then begin
  311. { for Synchronize entries we pass back the Exception and trigger
  312. the event that Synchronize waits in }
  313. tmpentry^.Exception := exceptobj;
  314. RtlEventSetEvent(tmpentry^.SyncEvent)
  315. end else begin
  316. { for Queue entries we dispose the entry and raise the exception }
  317. Dispose(tmpentry);
  318. if Assigned(exceptobj) then
  319. raise exceptobj;
  320. end;
  321. end;
  322. finally
  323. System.LeaveCriticalSection(ThreadQueueLock);
  324. end;
  325. end;
  326. end;
  327. class function TThread.GetCurrentThread: TThread;
  328. begin
  329. { if this is the first time GetCurrentThread is called for an external thread
  330. we need to create a corresponding TExternalThread instance }
  331. Result := CurrentThreadVar;
  332. if not Assigned(Result) then begin
  333. Result := TExternalThread.Create;
  334. CurrentThreadVar := Result;
  335. end;
  336. end;
  337. class function TThread.GetIsSingleProcessor: Boolean;
  338. begin
  339. Result := FProcessorCount <= 1;
  340. end;
  341. procedure TThread.Queue(aMethod: TThreadMethod);
  342. begin
  343. Queue(Self, aMethod);
  344. end;
  345. class procedure TThread.Queue(aThread: TThread; aMethod: TThreadMethod); static;
  346. var
  347. queueentry: PThreadQueueEntry;
  348. begin
  349. { ensure that we have a valid TThread instance }
  350. if not Assigned(aThread) then
  351. aThread := CurrentThread;
  352. New(queueentry);
  353. FillChar(queueentry^, SizeOf(TThreadQueueEntry), 0);
  354. queueentry^.Thread := aThread;
  355. queueentry^.Method := aMethod;
  356. { the queueentry is freed by CheckSynchronize (or by RemoveQueuedEvents) }
  357. ThreadQueueAppend(queueentry);
  358. end;
  359. class procedure TThread.RemoveQueuedEvents(aThread: TThread; aMethod: TThreadMethod);
  360. var
  361. entry, tmpentry, lastentry: PThreadQueueEntry;
  362. begin
  363. { anything to do at all? }
  364. if not Assigned(aThread) or not Assigned(aMethod) then
  365. Exit;
  366. System.EnterCriticalSection(ThreadQueueLock);
  367. try
  368. lastentry := Nil;
  369. entry := ThreadQueueHead;
  370. while Assigned(entry) do begin
  371. { first check for the thread }
  372. if Assigned(aThread) and (entry^.Thread <> aThread) then begin
  373. lastentry := entry;
  374. entry := entry^.Next;
  375. Continue;
  376. end;
  377. { then check for the method }
  378. if entry^.Method <> aMethod then begin
  379. lastentry := entry;
  380. entry := entry^.Next;
  381. Continue;
  382. end;
  383. { skip entries added by Synchronize }
  384. if Assigned(entry^.SyncEvent) then begin
  385. lastentry := entry;
  386. entry := entry^.Next;
  387. Continue;
  388. end;
  389. { ok, we need to remove this entry }
  390. tmpentry := entry;
  391. if Assigned(lastentry) then
  392. lastentry^.Next := entry^.Next;
  393. entry := entry^.Next;
  394. if ThreadQueueHead = tmpentry then
  395. ThreadQueueHead := entry;
  396. if ThreadQueueTail = tmpentry then
  397. ThreadQueueTail := lastentry;
  398. { only dispose events added by Queue }
  399. if not Assigned(tmpentry^.SyncEvent) then
  400. Dispose(tmpentry);
  401. end;
  402. finally
  403. System.LeaveCriticalSection(ThreadQueueLock);
  404. end;
  405. end;
  406. class procedure TThread.RemoveQueuedEvents(aMethod: TThreadMethod);
  407. begin
  408. RemoveQueuedEvents(Nil, aMethod);
  409. end;
  410. class procedure TThread.RemoveQueuedEvents(aThread: TThread);
  411. begin
  412. RemoveQueuedEvents(aThread, Nil);
  413. end;
  414. class function TThread.CheckTerminated: Boolean;
  415. begin
  416. { this method only works with threads created by TThread, so we can make a
  417. shortcut here }
  418. if not Assigned(CurrentThreadVar) then
  419. raise EThreadExternalException.Create(SThreadExternal);
  420. Result := CurrentThreadVar.FTerminated;
  421. end;
  422. class procedure TThread.SetReturnValue(aValue: Integer);
  423. begin
  424. { this method only works with threads created by TThread, so we can make a
  425. shortcut here }
  426. if not Assigned(CurrentThreadVar) then
  427. raise EThreadExternalException.Create(SThreadExternal);
  428. CurrentThreadVar.FReturnValue := aValue;
  429. end;
  430. class function TThread.CreateAnonymousThread(aProc: TProcedure): TThread;
  431. begin
  432. if not Assigned(aProc) then
  433. raise Exception.Create(SNoProcGiven);
  434. Result := TAnonymousThread.Create(aProc);
  435. end;
  436. {$ifdef THREADNAME_IS_ANSISTRING}
  437. { the platform implements the AnsiString variant and the UnicodeString variant
  438. simply calls the AnsiString variant }
  439. class procedure TThread.NameThreadForDebugging(aThreadName: UnicodeString; aThreadID: TThreadID);
  440. begin
  441. NameThreadForDebugging(AnsiString(aThreadName), aThreadID);
  442. end;
  443. {$ifndef HAS_TTHREAD_NAMETHREADFORDEBUGGING}
  444. class procedure TThread.NameThreadForDebugging(aThreadName: AnsiString; aThreadID: TThreadID);
  445. begin
  446. { empty }
  447. end;
  448. {$endif}
  449. {$else}
  450. {$ifndef HAS_TTHREAD_NAMETHREADFORDEBUGGING}
  451. { the platform implements the UnicodeString variant and the AnsiString variant
  452. simply calls the UnicodeString variant }
  453. class procedure TThread.NameThreadForDebugging(aThreadName: UnicodeString; aThreadID: TThreadID);
  454. begin
  455. { empty }
  456. end;
  457. {$endif}
  458. class procedure TThread.NameThreadForDebugging(aThreadName: AnsiString; aThreadID: TThreadID);
  459. begin
  460. NameThreadForDebugging(UnicodeString(aThreadName), aThreadID);
  461. end;
  462. {$endif}
  463. class procedure TThread.Yield;
  464. begin
  465. ThreadSwitch;
  466. end;
  467. class procedure TThread.Sleep(aMilliseconds: Cardinal);
  468. begin
  469. SysUtils.Sleep(aMilliseconds);
  470. end;
  471. class procedure TThread.SpinWait(aIterations: LongWord);
  472. begin
  473. { yes, it's just a simple busy wait to burn some cpu cycles... and as the job
  474. of this loop is to burn CPU cycles we switch off any optimizations that
  475. could interfere with this (e.g. loop unrolling) }
  476. {$PUSH}
  477. {$OPTIMIZATION OFF}
  478. while aIterations > 0 do
  479. Dec(aIterations);
  480. {$POP}
  481. end;
  482. {$ifndef HAS_TTHREAD_GETSYSTEMTIMES}
  483. class procedure TThread.GetSystemTimes(out aSystemTimes: TSystemTimes);
  484. begin
  485. { by default we just return a zeroed out record }
  486. FillChar(aSystemTimes, SizeOf(aSystemTimes), 0);
  487. end;
  488. {$endif}
  489. class function TThread.GetTickCount: LongWord;
  490. begin
  491. Result := SysUtils.GetTickCount;
  492. end;
  493. class function TThread.GetTickCount64: QWord;
  494. begin
  495. Result := SysUtils.GetTickCount64;
  496. end;
  497. { TPersistent implementation }
  498. {$i persist.inc }
  499. {$i sllist.inc}
  500. {$i resref.inc}
  501. { TComponent implementation }
  502. {$i compon.inc}
  503. { TBasicAction implementation }
  504. {$i action.inc}
  505. { TDataModule implementation }
  506. {$i dm.inc}
  507. { Class and component registration routines }
  508. {$I cregist.inc}
  509. { Interface related stuff }
  510. {$I intf.inc}
  511. {**********************************************************************
  512. * Miscellaneous procedures and functions *
  513. **********************************************************************}
  514. function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; Strings: TStrings; AddEmptyStrings : Boolean = False): Integer;
  515. var
  516. b, c : pchar;
  517. procedure SkipWhitespace;
  518. begin
  519. while (c^ in Whitespace) do
  520. inc (c);
  521. end;
  522. procedure AddString;
  523. var
  524. l : integer;
  525. s : string;
  526. begin
  527. l := c-b;
  528. if (l > 0) or AddEmptyStrings then
  529. begin
  530. if assigned(Strings) then
  531. begin
  532. setlength(s, l);
  533. if l>0 then
  534. move (b^, s[1],l*SizeOf(char));
  535. Strings.Add (s);
  536. end;
  537. inc (result);
  538. end;
  539. end;
  540. var
  541. quoted : char;
  542. begin
  543. result := 0;
  544. c := Content;
  545. Quoted := #0;
  546. Separators := Separators + [#13, #10] - ['''','"'];
  547. SkipWhitespace;
  548. b := c;
  549. while (c^ <> #0) do
  550. begin
  551. if (c^ = Quoted) then
  552. begin
  553. if ((c+1)^ = Quoted) then
  554. inc (c)
  555. else
  556. Quoted := #0
  557. end
  558. else if (Quoted = #0) and (c^ in ['''','"']) then
  559. Quoted := c^;
  560. if (Quoted = #0) and (c^ in Separators) then
  561. begin
  562. AddString;
  563. inc (c);
  564. SkipWhitespace;
  565. b := c;
  566. end
  567. else
  568. inc (c);
  569. end;
  570. if (c <> b) then
  571. AddString;
  572. end;
  573. { Point and rectangle constructors }
  574. function Point(AX, AY: Integer): TPoint;
  575. begin
  576. with Result do
  577. begin
  578. X := AX;
  579. Y := AY;
  580. end;
  581. end;
  582. function SmallPoint(AX, AY: SmallInt): TSmallPoint;
  583. begin
  584. with Result do
  585. begin
  586. X := AX;
  587. Y := AY;
  588. end;
  589. end;
  590. function Rect(ALeft, ATop, ARight, ABottom: Integer): TRect;
  591. begin
  592. with Result do
  593. begin
  594. Left := ALeft;
  595. Top := ATop;
  596. Right := ARight;
  597. Bottom := ABottom;
  598. end;
  599. end;
  600. function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
  601. begin
  602. with Result do
  603. begin
  604. Left := ALeft;
  605. Top := ATop;
  606. Right := ALeft + AWidth;
  607. Bottom := ATop + AHeight;
  608. end;
  609. end;
  610. function PointsEqual(const P1, P2: TPoint): Boolean; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  611. begin
  612. { lazy, but should work }
  613. result:=QWord(P1)=QWord(P2);
  614. end;
  615. function PointsEqual(const P1, P2: TSmallPoint): Boolean; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  616. begin
  617. { lazy, but should work }
  618. result:=DWord(P1)=DWord(P2);
  619. end;
  620. function InvalidPoint(X, Y: Integer): Boolean;
  621. begin
  622. result:=(X=-1) and (Y=-1);
  623. end;
  624. function InvalidPoint(const At: TPoint): Boolean;
  625. begin
  626. result:=(At.x=-1) and (At.y=-1);
  627. end;
  628. function InvalidPoint(const At: TSmallPoint): Boolean;
  629. begin
  630. result:=(At.x=-1) and (At.y=-1);
  631. end;
  632. { Object filing routines }
  633. var
  634. IntConstList: TThreadList;
  635. type
  636. TIntConst = class
  637. IntegerType: PTypeInfo; // The integer type RTTI pointer
  638. IdentToIntFn: TIdentToInt; // Identifier to Integer conversion
  639. IntToIdentFn: TIntToIdent; // Integer to Identifier conversion
  640. constructor Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  641. AIntToIdent: TIntToIdent);
  642. end;
  643. constructor TIntConst.Create(AIntegerType: PTypeInfo; AIdentToInt: TIdentToInt;
  644. AIntToIdent: TIntToIdent);
  645. begin
  646. IntegerType := AIntegerType;
  647. IdentToIntFn := AIdentToInt;
  648. IntToIdentFn := AIntToIdent;
  649. end;
  650. procedure RegisterIntegerConsts(IntegerType: Pointer; IdentToIntFn: TIdentToInt;
  651. IntToIdentFn: TIntToIdent);
  652. begin
  653. IntConstList.Add(TIntConst.Create(IntegerType, IdentToIntFn, IntToIdentFn));
  654. end;
  655. function FindIntToIdent(AIntegerType: Pointer): TIntToIdent;
  656. var
  657. i: Integer;
  658. begin
  659. with IntConstList.LockList do
  660. try
  661. for i := 0 to Count - 1 do
  662. if TIntConst(Items[i]).IntegerType = AIntegerType then
  663. exit(TIntConst(Items[i]).IntToIdentFn);
  664. Result := nil;
  665. finally
  666. IntConstList.UnlockList;
  667. end;
  668. end;
  669. function FindIdentToInt(AIntegerType: Pointer): TIdentToInt;
  670. var
  671. i: Integer;
  672. begin
  673. with IntConstList.LockList do
  674. try
  675. for i := 0 to Count - 1 do
  676. with TIntConst(Items[I]) do
  677. if TIntConst(Items[I]).IntegerType = AIntegerType then
  678. exit(IdentToIntFn);
  679. Result := nil;
  680. finally
  681. IntConstList.UnlockList;
  682. end;
  683. end;
  684. function IdentToInt(const Ident: String; var Int: LongInt;
  685. const Map: array of TIdentMapEntry): Boolean;
  686. var
  687. i: Integer;
  688. begin
  689. for i := Low(Map) to High(Map) do
  690. if CompareText(Map[i].Name, Ident) = 0 then
  691. begin
  692. Int := Map[i].Value;
  693. exit(True);
  694. end;
  695. Result := False;
  696. end;
  697. function IntToIdent(Int: LongInt; var Ident: String;
  698. const Map: array of TIdentMapEntry): Boolean;
  699. var
  700. i: Integer;
  701. begin
  702. for i := Low(Map) to High(Map) do
  703. if Map[i].Value = Int then
  704. begin
  705. Ident := Map[i].Name;
  706. exit(True);
  707. end;
  708. Result := False;
  709. end;
  710. function GlobalIdentToInt(const Ident: String; var Int: LongInt):boolean;
  711. var
  712. i : Integer;
  713. begin
  714. with IntConstList.LockList do
  715. try
  716. for i := 0 to Count - 1 do
  717. if TIntConst(Items[I]).IdentToIntFn(Ident, Int) then
  718. Exit(True);
  719. Result := false;
  720. finally
  721. IntConstList.UnlockList;
  722. end;
  723. end;
  724. { TPropFixup }
  725. // Tainted. TPropFixup is being removed.
  726. Type
  727. TInitHandler = Class(TObject)
  728. AHandler : TInitComponentHandler;
  729. AClass : TComponentClass;
  730. end;
  731. {$ifndef i8086}
  732. type
  733. TCodePtrList = TList;
  734. {$endif i8086}
  735. Var
  736. InitHandlerList : TList;
  737. FindGlobalComponentList : TCodePtrList;
  738. procedure RegisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  739. begin
  740. if not(assigned(FindGlobalComponentList)) then
  741. FindGlobalComponentList:=TCodePtrList.Create;
  742. if FindGlobalComponentList.IndexOf(CodePointer(AFindGlobalComponent))<0 then
  743. FindGlobalComponentList.Add(CodePointer(AFindGlobalComponent));
  744. end;
  745. procedure UnregisterFindGlobalComponentProc(AFindGlobalComponent: TFindGlobalComponent);
  746. begin
  747. if assigned(FindGlobalComponentList) then
  748. FindGlobalComponentList.Remove(CodePointer(AFindGlobalComponent));
  749. end;
  750. function FindGlobalComponent(const Name: string): TComponent;
  751. var
  752. i : sizeint;
  753. begin
  754. FindGlobalComponent:=nil;
  755. if assigned(FindGlobalComponentList) then
  756. begin
  757. for i:=FindGlobalComponentList.Count-1 downto 0 do
  758. begin
  759. FindGlobalComponent:=TFindGlobalComponent(FindGlobalComponentList[i])(name);
  760. if assigned(FindGlobalComponent) then
  761. break;
  762. end;
  763. end;
  764. end;
  765. procedure RegisterInitComponentHandler(ComponentClass: TComponentClass; Handler: TInitComponentHandler);
  766. Var
  767. I : Integer;
  768. H: TInitHandler;
  769. begin
  770. If (InitHandlerList=Nil) then
  771. InitHandlerList:=TList.Create;
  772. H:=TInitHandler.Create;
  773. H.Aclass:=ComponentClass;
  774. H.AHandler:=Handler;
  775. try
  776. With InitHandlerList do
  777. begin
  778. I:=0;
  779. While (I<Count) and not H.AClass.InheritsFrom(TInitHandler(Items[I]).AClass) do
  780. Inc(I);
  781. { override? }
  782. if (I<Count) and (TInitHandler(Items[I]).AClass=H.AClass) then
  783. begin
  784. TInitHandler(Items[I]).AHandler:=Handler;
  785. H.Free;
  786. end
  787. else
  788. InitHandlerList.Insert(I,H);
  789. end;
  790. except
  791. H.Free;
  792. raise;
  793. end;
  794. end;
  795. { all targets should at least include the sysres.inc dummy in the system unit to compile this }
  796. function CreateComponentfromRes(const res : string;Inst : THandle;var Component : TComponent) : Boolean;
  797. var
  798. ResStream : TResourceStream;
  799. begin
  800. result:=true;
  801. if Inst=0 then
  802. Inst:=HInstance;
  803. try
  804. ResStream:=TResourceStream.Create(Inst,res,RT_RCDATA);
  805. try
  806. Component:=ResStream.ReadComponent(Component);
  807. finally
  808. ResStream.Free;
  809. end;
  810. except
  811. on EResNotFound do
  812. result:=false;
  813. end;
  814. end;
  815. function DefaultInitHandler(Instance: TComponent; RootAncestor: TClass): Boolean;
  816. function doinit(_class : TClass) : boolean;
  817. begin
  818. result:=false;
  819. if (_class.ClassType=TComponent) or (_class.ClassType=RootAncestor) then
  820. exit;
  821. result:=doinit(_class.ClassParent);
  822. result:=CreateComponentfromRes(_class.ClassName,0,Instance) or result;
  823. end;
  824. begin
  825. GlobalNameSpace.BeginWrite;
  826. try
  827. result:=doinit(Instance.ClassType);
  828. finally
  829. GlobalNameSpace.EndWrite;
  830. end;
  831. end;
  832. function InitInheritedComponent(Instance: TComponent; RootAncestor: TClass): Boolean;
  833. Var
  834. I : Integer;
  835. begin
  836. I:=0;
  837. if not Assigned(InitHandlerList) then begin
  838. Result := True;
  839. Exit;
  840. end;
  841. Result:=False;
  842. With InitHandlerList do
  843. begin
  844. I:=0;
  845. // Instance is the normally the lowest one, so that one should be used when searching.
  846. While Not result and (I<Count) do
  847. begin
  848. If (Instance.InheritsFrom(TInitHandler(Items[i]).AClass)) then
  849. Result:=TInitHandler(Items[i]).AHandler(Instance,RootAncestor);
  850. Inc(I);
  851. end;
  852. end;
  853. end;
  854. function InitComponentRes(const ResName: String; Instance: TComponent): Boolean;
  855. begin
  856. Result:=ReadComponentRes(ResName,Instance)=Instance;
  857. end;
  858. function SysReadComponentRes(HInstance : THandle; const ResName: String; Instance: TComponent): TComponent;
  859. Var
  860. H : TFPResourceHandle;
  861. begin
  862. H:=FindResource(HInstance,ResName,RT_RCDATA);
  863. if (PtrInt(H)=0) then
  864. Result:=Nil
  865. else
  866. With TResourceStream.Create(HInstance,ResName,RT_RCDATA) do
  867. try
  868. Result:=ReadComponent(Instance);
  869. Finally
  870. Free;
  871. end;
  872. end;
  873. function ReadComponentRes(const ResName: String; Instance: TComponent): TComponent;
  874. begin
  875. Result:=SysReadComponentRes(Hinstance,Resname,Instance);
  876. end;
  877. function ReadComponentResEx(HInstance: THandle; const ResName: String): TComponent;
  878. begin
  879. Result:=SysReadComponentRes(Hinstance,ResName,Nil);
  880. end;
  881. function ReadComponentResFile(const FileName: String; Instance: TComponent): TComponent;
  882. var
  883. FileStream: TStream;
  884. begin
  885. FileStream := TFileStream.Create(FileName, fmOpenRead {!!!:or fmShareDenyWrite});
  886. try
  887. Result := FileStream.ReadComponentRes(Instance);
  888. finally
  889. FileStream.Free;
  890. end;
  891. end;
  892. procedure WriteComponentResFile(const FileName: String; Instance: TComponent);
  893. var
  894. FileStream: TStream;
  895. begin
  896. FileStream := TFileStream.Create(FileName, fmCreate);
  897. try
  898. FileStream.WriteComponentRes(Instance.ClassName, Instance);
  899. finally
  900. FileStream.Free;
  901. end;
  902. end;
  903. Function FindNestedComponent(Root : TComponent; APath : String; CStyle : Boolean = True) : TComponent;
  904. Function GetNextName : String; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
  905. Var
  906. P : Integer;
  907. CM : Boolean;
  908. begin
  909. P:=Pos('.',APath);
  910. CM:=False;
  911. If (P=0) then
  912. begin
  913. If CStyle then
  914. begin
  915. P:=Pos('->',APath);
  916. CM:=P<>0;
  917. end;
  918. If (P=0) Then
  919. P:=Length(APath)+1;
  920. end;
  921. Result:=Copy(APath,1,P-1);
  922. Delete(APath,1,P+Ord(CM));
  923. end;
  924. Var
  925. C : TComponent;
  926. S : String;
  927. begin
  928. If (APath='') then
  929. Result:=Nil
  930. else
  931. begin
  932. Result:=Root;
  933. While (APath<>'') And (Result<>Nil) do
  934. begin
  935. C:=Result;
  936. S:=Uppercase(GetNextName);
  937. Result:=C.FindComponent(S);
  938. If (Result=Nil) And (S='OWNER') then
  939. Result:=C;
  940. end;
  941. end;
  942. end;
  943. threadvar
  944. GlobalLoaded, GlobalLists: TFpList;
  945. procedure BeginGlobalLoading;
  946. begin
  947. if not Assigned(GlobalLists) then
  948. GlobalLists := TFpList.Create;
  949. GlobalLists.Add(GlobalLoaded);
  950. GlobalLoaded := TFpList.Create;
  951. end;
  952. { Notify all global components that they have been loaded completely }
  953. procedure NotifyGlobalLoading;
  954. var
  955. i: Integer;
  956. begin
  957. for i := 0 to GlobalLoaded.Count - 1 do
  958. TComponent(GlobalLoaded[i]).Loaded;
  959. end;
  960. procedure EndGlobalLoading;
  961. begin
  962. { Free the memory occupied by BeginGlobalLoading }
  963. GlobalLoaded.Free;
  964. GlobalLoaded := TFpList(GlobalLists.Last);
  965. GlobalLists.Delete(GlobalLists.Count - 1);
  966. if GlobalLists.Count = 0 then
  967. begin
  968. GlobalLists.Free;
  969. GlobalLists := nil;
  970. end;
  971. end;
  972. function CollectionsEqual(C1, C2: TCollection): Boolean;
  973. begin
  974. // !!!: Implement this
  975. CollectionsEqual:=false;
  976. end;
  977. function CollectionsEqual(C1, C2: TCollection; Owner1, Owner2: TComponent): Boolean;
  978. procedure stream_collection(s : tstream;c : tcollection;o : tcomponent);
  979. var
  980. w : twriter;
  981. begin
  982. w:=twriter.create(s,4096);
  983. try
  984. w.root:=o;
  985. w.flookuproot:=o;
  986. w.writecollection(c);
  987. finally
  988. w.free;
  989. end;
  990. end;
  991. var
  992. s1,s2 : tmemorystream;
  993. begin
  994. result:=false;
  995. if (c1.classtype<>c2.classtype) or
  996. (c1.count<>c2.count) then
  997. exit;
  998. if c1.count = 0 then
  999. begin
  1000. result:= true;
  1001. exit;
  1002. end;
  1003. s1:=tmemorystream.create;
  1004. try
  1005. s2:=tmemorystream.create;
  1006. try
  1007. stream_collection(s1,c1,owner1);
  1008. stream_collection(s2,c2,owner2);
  1009. result:=(s1.size=s2.size) and (CompareChar(s1.memory^,s2.memory^,s1.size)=0);
  1010. finally
  1011. s2.free;
  1012. end;
  1013. finally
  1014. s1.free;
  1015. end;
  1016. end;
  1017. { Object conversion routines }
  1018. type
  1019. CharToOrdFuncty = Function(var charpo: Pointer): Cardinal;
  1020. function CharToOrd(var P: Pointer): Cardinal;
  1021. begin
  1022. result:= ord(pchar(P)^);
  1023. inc(pchar(P));
  1024. end;
  1025. function WideCharToOrd(var P: Pointer): Cardinal;
  1026. begin
  1027. result:= ord(pwidechar(P)^);
  1028. inc(pwidechar(P));
  1029. end;
  1030. function Utf8ToOrd(var P:Pointer): Cardinal;
  1031. begin
  1032. // Should also check for illegal utf8 combinations
  1033. Result := Ord(PChar(P)^);
  1034. Inc(P);
  1035. if (Result and $80) <> 0 then
  1036. if (Ord(Result) and %11100000) = %11000000 then begin
  1037. Result := ((Result and %00011111) shl 6)
  1038. or (ord(PChar(P)^) and %00111111);
  1039. Inc(P);
  1040. end else if (Ord(Result) and %11110000) = %11100000 then begin
  1041. Result := ((Result and %00011111) shl 12)
  1042. or ((ord(PChar(P)^) and %00111111) shl 6)
  1043. or (ord((PChar(P)+1)^) and %00111111);
  1044. Inc(P,2);
  1045. end else begin
  1046. Result := ((ord(Result) and %00011111) shl 18)
  1047. or ((ord(PChar(P)^) and %00111111) shl 12)
  1048. or ((ord((PChar(P)+1)^) and %00111111) shl 6)
  1049. or (ord((PChar(P)+2)^) and %00111111);
  1050. Inc(P,3);
  1051. end;
  1052. end;
  1053. procedure ObjectBinaryToText(Input, Output: TStream; Encoding: TObjectTextEncoding);
  1054. procedure OutStr(s: String);
  1055. begin
  1056. if Length(s) > 0 then
  1057. Output.Write(s[1], Length(s));
  1058. end;
  1059. procedure OutLn(s: String);
  1060. begin
  1061. OutStr(s + LineEnding);
  1062. end;
  1063. procedure Outchars(P, LastP : Pointer; CharToOrdFunc: CharToOrdFuncty;
  1064. UseBytes: boolean = false);
  1065. var
  1066. res, NewStr: String;
  1067. w: Cardinal;
  1068. InString, NewInString: Boolean;
  1069. begin
  1070. if p = nil then begin
  1071. res:= '''''';
  1072. end
  1073. else
  1074. begin
  1075. res := '';
  1076. InString := False;
  1077. while P < LastP do
  1078. begin
  1079. NewInString := InString;
  1080. w := CharToOrdfunc(P);
  1081. if w = ord('''') then
  1082. begin //quote char
  1083. if not InString then
  1084. NewInString := True;
  1085. NewStr := '''''';
  1086. end
  1087. else if (Ord(w) >= 32) and ((Ord(w) < 127) or (UseBytes and (Ord(w)<256))) then
  1088. begin //printable ascii or bytes
  1089. if not InString then
  1090. NewInString := True;
  1091. NewStr := char(w);
  1092. end
  1093. else
  1094. begin //ascii control chars, non ascii
  1095. if InString then
  1096. NewInString := False;
  1097. NewStr := '#' + IntToStr(w);
  1098. end;
  1099. if NewInString <> InString then
  1100. begin
  1101. NewStr := '''' + NewStr;
  1102. InString := NewInString;
  1103. end;
  1104. res := res + NewStr;
  1105. end;
  1106. if InString then
  1107. res := res + '''';
  1108. end;
  1109. OutStr(res);
  1110. end;
  1111. procedure OutString(s: String);
  1112. begin
  1113. OutChars(Pointer(S),PChar(S)+Length(S),@CharToOrd,Encoding=oteLFM);
  1114. end;
  1115. procedure OutWString(W: WideString);
  1116. begin
  1117. OutChars(Pointer(W),pwidechar(W)+Length(W),@WideCharToOrd);
  1118. end;
  1119. procedure OutUString(W: UnicodeString);
  1120. begin
  1121. OutChars(Pointer(W),pwidechar(W)+Length(W),@WideCharToOrd);
  1122. end;
  1123. procedure OutUtf8Str(s: String);
  1124. begin
  1125. if Encoding=oteLFM then
  1126. OutChars(Pointer(S),PChar(S)+Length(S),@CharToOrd)
  1127. else
  1128. OutChars(Pointer(S),PChar(S)+Length(S),@Utf8ToOrd);
  1129. end;
  1130. function ReadWord : word; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1131. begin
  1132. Result:=Input.ReadWord;
  1133. Result:=LEtoN(Result);
  1134. end;
  1135. function ReadDWord : longword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1136. begin
  1137. Result:=Input.ReadDWord;
  1138. Result:=LEtoN(Result);
  1139. end;
  1140. function ReadQWord : qword; {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1141. begin
  1142. Input.ReadBuffer(Result,sizeof(Result));
  1143. Result:=LEtoN(Result);
  1144. end;
  1145. {$ifndef FPUNONE}
  1146. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1147. function ExtendedToDouble(e : pointer) : double;
  1148. var mant : qword;
  1149. exp : smallint;
  1150. sign : boolean;
  1151. d : qword;
  1152. begin
  1153. move(pbyte(e)[0],mant,8); //mantissa : bytes 0..7
  1154. move(pbyte(e)[8],exp,2); //exponent and sign: bytes 8..9
  1155. mant:=LEtoN(mant);
  1156. exp:=LetoN(word(exp));
  1157. sign:=(exp and $8000)<>0;
  1158. if sign then exp:=exp and $7FFF;
  1159. case exp of
  1160. 0 : mant:=0; //if denormalized, value is too small for double,
  1161. //so it's always zero
  1162. $7FFF : exp:=2047 //either infinity or NaN
  1163. else
  1164. begin
  1165. dec(exp,16383-1023);
  1166. if (exp>=-51) and (exp<=0) then //can be denormalized
  1167. begin
  1168. mant:=mant shr (-exp);
  1169. exp:=0;
  1170. end
  1171. else
  1172. if (exp<-51) or (exp>2046) then //exponent too large.
  1173. begin
  1174. Result:=0;
  1175. exit;
  1176. end
  1177. else //normalized value
  1178. mant:=mant shl 1; //hide most significant bit
  1179. end;
  1180. end;
  1181. d:=word(exp);
  1182. d:=d shl 52;
  1183. mant:=mant shr 12;
  1184. d:=d or mant;
  1185. if sign then d:=d or $8000000000000000;
  1186. Result:=pdouble(@d)^;
  1187. end;
  1188. {$ENDIF}
  1189. {$endif}
  1190. function ReadInt(ValueType: TValueType): Int64;
  1191. begin
  1192. case ValueType of
  1193. vaInt8: Result := ShortInt(Input.ReadByte);
  1194. vaInt16: Result := SmallInt(ReadWord);
  1195. vaInt32: Result := LongInt(ReadDWord);
  1196. vaInt64: Result := Int64(ReadQWord);
  1197. end;
  1198. end;
  1199. function ReadInt: Int64;
  1200. begin
  1201. Result := ReadInt(TValueType(Input.ReadByte));
  1202. end;
  1203. {$ifndef FPUNONE}
  1204. function ReadExtended : extended;
  1205. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1206. var ext : array[0..9] of byte;
  1207. {$ENDIF}
  1208. begin
  1209. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1210. Input.ReadBuffer(ext[0],10);
  1211. Result:=ExtendedToDouble(@(ext[0]));
  1212. {$ELSE}
  1213. Input.ReadBuffer(Result,sizeof(Result));
  1214. {$ENDIF}
  1215. end;
  1216. {$endif}
  1217. function ReadSStr: String;
  1218. var
  1219. len: Byte;
  1220. begin
  1221. len := Input.ReadByte;
  1222. SetLength(Result, len);
  1223. if (len > 0) then
  1224. Input.ReadBuffer(Result[1], len);
  1225. end;
  1226. function ReadLStr: String;
  1227. var
  1228. len: DWord;
  1229. begin
  1230. len := ReadDWord;
  1231. SetLength(Result, len);
  1232. if (len > 0) then
  1233. Input.ReadBuffer(Result[1], len);
  1234. end;
  1235. function ReadWStr: WideString;
  1236. var
  1237. len: DWord;
  1238. {$IFDEF ENDIAN_BIG}
  1239. i : integer;
  1240. {$ENDIF}
  1241. begin
  1242. len := ReadDWord;
  1243. SetLength(Result, len);
  1244. if (len > 0) then
  1245. begin
  1246. Input.ReadBuffer(Pointer(@Result[1])^, len*2);
  1247. {$IFDEF ENDIAN_BIG}
  1248. for i:=1 to len do
  1249. Result[i]:=widechar(SwapEndian(word(Result[i])));
  1250. {$ENDIF}
  1251. end;
  1252. end;
  1253. function ReadUStr: UnicodeString;
  1254. var
  1255. len: DWord;
  1256. {$IFDEF ENDIAN_BIG}
  1257. i : integer;
  1258. {$ENDIF}
  1259. begin
  1260. len := ReadDWord;
  1261. SetLength(Result, len);
  1262. if (len > 0) then
  1263. begin
  1264. Input.ReadBuffer(Pointer(@Result[1])^, len*2);
  1265. {$IFDEF ENDIAN_BIG}
  1266. for i:=1 to len do
  1267. Result[i]:=widechar(SwapEndian(word(Result[i])));
  1268. {$ENDIF}
  1269. end;
  1270. end;
  1271. procedure ReadPropList(indent: String);
  1272. procedure ProcessValue(ValueType: TValueType; Indent: String);
  1273. procedure ProcessBinary;
  1274. var
  1275. ToDo, DoNow, i: LongInt;
  1276. lbuf: array[0..31] of Byte;
  1277. s: String;
  1278. begin
  1279. ToDo := ReadDWord;
  1280. OutLn('{');
  1281. while ToDo > 0 do begin
  1282. DoNow := ToDo;
  1283. if DoNow > 32 then DoNow := 32;
  1284. Dec(ToDo, DoNow);
  1285. s := Indent + ' ';
  1286. Input.ReadBuffer(lbuf, DoNow);
  1287. for i := 0 to DoNow - 1 do
  1288. s := s + IntToHex(lbuf[i], 2);
  1289. OutLn(s);
  1290. end;
  1291. OutLn(indent + '}');
  1292. end;
  1293. var
  1294. s: String;
  1295. { len: LongInt; }
  1296. IsFirst: Boolean;
  1297. {$ifndef FPUNONE}
  1298. ext: Extended;
  1299. {$endif}
  1300. begin
  1301. case ValueType of
  1302. vaList: begin
  1303. OutStr('(');
  1304. IsFirst := True;
  1305. while True do begin
  1306. ValueType := TValueType(Input.ReadByte);
  1307. if ValueType = vaNull then break;
  1308. if IsFirst then begin
  1309. OutLn('');
  1310. IsFirst := False;
  1311. end;
  1312. OutStr(Indent + ' ');
  1313. ProcessValue(ValueType, Indent + ' ');
  1314. end;
  1315. OutLn(Indent + ')');
  1316. end;
  1317. vaInt8: OutLn(IntToStr(ShortInt(Input.ReadByte)));
  1318. vaInt16: OutLn( IntToStr(SmallInt(ReadWord)));
  1319. vaInt32: OutLn(IntToStr(LongInt(ReadDWord)));
  1320. vaInt64: OutLn(IntToStr(Int64(ReadQWord)));
  1321. {$ifndef FPUNONE}
  1322. vaExtended: begin
  1323. ext:=ReadExtended;
  1324. Str(ext,S);// Do not use localized strings.
  1325. OutLn(S);
  1326. end;
  1327. {$endif}
  1328. vaString: begin
  1329. OutString(ReadSStr);
  1330. OutLn('');
  1331. end;
  1332. vaIdent: OutLn(ReadSStr);
  1333. vaFalse: OutLn('False');
  1334. vaTrue: OutLn('True');
  1335. vaBinary: ProcessBinary;
  1336. vaSet: begin
  1337. OutStr('[');
  1338. IsFirst := True;
  1339. while True do begin
  1340. s := ReadSStr;
  1341. if Length(s) = 0 then break;
  1342. if not IsFirst then OutStr(', ');
  1343. IsFirst := False;
  1344. OutStr(s);
  1345. end;
  1346. OutLn(']');
  1347. end;
  1348. vaLString:
  1349. begin
  1350. OutString(ReadLStr);
  1351. OutLn('');
  1352. end;
  1353. vaWString:
  1354. begin
  1355. OutWString(ReadWStr);
  1356. OutLn('');
  1357. end;
  1358. vaUString:
  1359. begin
  1360. OutWString(ReadWStr);
  1361. OutLn('');
  1362. end;
  1363. vaNil:
  1364. OutLn('nil');
  1365. vaCollection: begin
  1366. OutStr('<');
  1367. while Input.ReadByte <> 0 do begin
  1368. OutLn(Indent);
  1369. Input.Seek(-1, soFromCurrent);
  1370. OutStr(indent + ' item');
  1371. ValueType := TValueType(Input.ReadByte);
  1372. if ValueType <> vaList then
  1373. OutStr('[' + IntToStr(ReadInt(ValueType)) + ']');
  1374. OutLn('');
  1375. ReadPropList(indent + ' ');
  1376. OutStr(indent + ' end');
  1377. end;
  1378. OutLn('>');
  1379. end;
  1380. {vaSingle: begin OutLn('!!Single!!'); exit end;
  1381. vaCurrency: begin OutLn('!!Currency!!'); exit end;
  1382. vaDate: begin OutLn('!!Date!!'); exit end;}
  1383. vaUTF8String: begin
  1384. OutUtf8Str(ReadLStr);
  1385. OutLn('');
  1386. end;
  1387. else
  1388. Raise EReadError.CreateFmt(SErrInvalidPropertyType,[Ord(ValueType)]);
  1389. end;
  1390. end;
  1391. begin
  1392. while Input.ReadByte <> 0 do begin
  1393. Input.Seek(-1, soFromCurrent);
  1394. OutStr(indent + ReadSStr + ' = ');
  1395. ProcessValue(TValueType(Input.ReadByte), Indent);
  1396. end;
  1397. end;
  1398. procedure ReadObject(indent: String);
  1399. var
  1400. b: Byte;
  1401. ObjClassName, ObjName: String;
  1402. ChildPos: LongInt;
  1403. begin
  1404. // Check for FilerFlags
  1405. b := Input.ReadByte;
  1406. if (b and $f0) = $f0 then begin
  1407. if (b and 2) <> 0 then ChildPos := ReadInt;
  1408. end else begin
  1409. b := 0;
  1410. Input.Seek(-1, soFromCurrent);
  1411. end;
  1412. ObjClassName := ReadSStr;
  1413. ObjName := ReadSStr;
  1414. OutStr(Indent);
  1415. if (b and 1) <> 0 then OutStr('inherited')
  1416. else
  1417. if (b and 4) <> 0 then OutStr('inline')
  1418. else OutStr('object');
  1419. OutStr(' ');
  1420. if ObjName <> '' then
  1421. OutStr(ObjName + ': ');
  1422. OutStr(ObjClassName);
  1423. if (b and 2) <> 0 then OutStr('[' + IntToStr(ChildPos) + ']');
  1424. OutLn('');
  1425. ReadPropList(indent + ' ');
  1426. while Input.ReadByte <> 0 do begin
  1427. Input.Seek(-1, soFromCurrent);
  1428. ReadObject(indent + ' ');
  1429. end;
  1430. OutLn(indent + 'end');
  1431. end;
  1432. type
  1433. PLongWord = ^LongWord;
  1434. const
  1435. signature: PChar = 'TPF0';
  1436. begin
  1437. if Input.ReadDWord <> PLongWord(Pointer(signature))^ then
  1438. raise EReadError.Create('Illegal stream image' {###SInvalidImage});
  1439. ReadObject('');
  1440. end;
  1441. procedure ObjectBinaryToText(Input, Output: TStream);
  1442. begin
  1443. ObjectBinaryToText(Input,Output,oteDFM);
  1444. end;
  1445. procedure ObjectTextToBinary(Input, Output: TStream);
  1446. var
  1447. parser: TParser;
  1448. procedure WriteWord(w : word); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1449. begin
  1450. w:=NtoLE(w);
  1451. Output.WriteWord(w);
  1452. end;
  1453. procedure WriteDWord(lw : longword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1454. begin
  1455. lw:=NtoLE(lw);
  1456. Output.WriteDWord(lw);
  1457. end;
  1458. procedure WriteQWord(qw : qword); {$ifdef CLASSESINLINE}inline;{$endif CLASSESINLINE}
  1459. begin
  1460. qw:=NtoLE(qw);
  1461. Output.WriteBuffer(qw,sizeof(qword));
  1462. end;
  1463. {$ifndef FPUNONE}
  1464. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1465. procedure DoubleToExtended(d : double; e : pointer);
  1466. var mant : qword;
  1467. exp : smallint;
  1468. sign : boolean;
  1469. begin
  1470. mant:=(qword(d) and $000FFFFFFFFFFFFF) shl 12;
  1471. exp :=(qword(d) shr 52) and $7FF;
  1472. sign:=(qword(d) and $8000000000000000)<>0;
  1473. case exp of
  1474. 0 : begin
  1475. if mant<>0 then //denormalized value: hidden bit is 0. normalize it
  1476. begin
  1477. exp:=16383-1022;
  1478. while (mant and $8000000000000000)=0 do
  1479. begin
  1480. dec(exp);
  1481. mant:=mant shl 1;
  1482. end;
  1483. dec(exp); //don't shift, most significant bit is not hidden in extended
  1484. end;
  1485. end;
  1486. 2047 : exp:=$7FFF //either infinity or NaN
  1487. else
  1488. begin
  1489. inc(exp,16383-1023);
  1490. mant:=(mant shr 1) or $8000000000000000; //unhide hidden bit
  1491. end;
  1492. end;
  1493. if sign then exp:=exp or $8000;
  1494. mant:=NtoLE(mant);
  1495. exp:=NtoLE(word(exp));
  1496. move(mant,pbyte(e)[0],8); //mantissa : bytes 0..7
  1497. move(exp,pbyte(e)[8],2); //exponent and sign: bytes 8..9
  1498. end;
  1499. {$ENDIF}
  1500. procedure WriteExtended(e : extended);
  1501. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1502. var ext : array[0..9] of byte;
  1503. {$ENDIF}
  1504. begin
  1505. {$IFNDEF FPC_HAS_TYPE_EXTENDED}
  1506. DoubleToExtended(e,@(ext[0]));
  1507. Output.WriteBuffer(ext[0],10);
  1508. {$ELSE}
  1509. Output.WriteBuffer(e,sizeof(e));
  1510. {$ENDIF}
  1511. end;
  1512. {$endif}
  1513. procedure WriteString(s: String);
  1514. var size : byte;
  1515. begin
  1516. if length(s)>255 then size:=255
  1517. else size:=length(s);
  1518. Output.WriteByte(size);
  1519. if Length(s) > 0 then
  1520. Output.WriteBuffer(s[1], size);
  1521. end;
  1522. procedure WriteLString(Const s: String);
  1523. begin
  1524. WriteDWord(Length(s));
  1525. if Length(s) > 0 then
  1526. Output.WriteBuffer(s[1], Length(s));
  1527. end;
  1528. procedure WriteWString(Const s: WideString);
  1529. var len : longword;
  1530. {$IFDEF ENDIAN_BIG}
  1531. i : integer;
  1532. ws : widestring;
  1533. {$ENDIF}
  1534. begin
  1535. len:=Length(s);
  1536. WriteDWord(len);
  1537. if len > 0 then
  1538. begin
  1539. {$IFDEF ENDIAN_BIG}
  1540. setlength(ws,len);
  1541. for i:=1 to len do
  1542. ws[i]:=widechar(SwapEndian(word(s[i])));
  1543. Output.WriteBuffer(ws[1], len*sizeof(widechar));
  1544. {$ELSE}
  1545. Output.WriteBuffer(s[1], len*sizeof(widechar));
  1546. {$ENDIF}
  1547. end;
  1548. end;
  1549. procedure WriteInteger(value: Int64);
  1550. begin
  1551. if (value >= -128) and (value <= 127) then begin
  1552. Output.WriteByte(Ord(vaInt8));
  1553. Output.WriteByte(byte(value));
  1554. end else if (value >= -32768) and (value <= 32767) then begin
  1555. Output.WriteByte(Ord(vaInt16));
  1556. WriteWord(word(value));
  1557. end else if (value >= -2147483648) and (value <= 2147483647) then begin
  1558. Output.WriteByte(Ord(vaInt32));
  1559. WriteDWord(longword(value));
  1560. end else begin
  1561. Output.WriteByte(ord(vaInt64));
  1562. WriteQWord(qword(value));
  1563. end;
  1564. end;
  1565. procedure ProcessWideString(const left : widestring);
  1566. var ws : widestring;
  1567. begin
  1568. ws:=left+parser.TokenWideString;
  1569. while parser.NextToken = '+' do
  1570. begin
  1571. parser.NextToken; // Get next string fragment
  1572. if not (parser.Token in [toString,toWString]) then
  1573. parser.CheckToken(toWString);
  1574. ws:=ws+parser.TokenWideString;
  1575. end;
  1576. Output.WriteByte(Ord(vaWstring));
  1577. WriteWString(ws);
  1578. end;
  1579. procedure ProcessProperty; forward;
  1580. procedure ProcessValue;
  1581. var
  1582. {$ifndef FPUNONE}
  1583. flt: Extended;
  1584. {$endif}
  1585. s: String;
  1586. stream: TMemoryStream;
  1587. begin
  1588. case parser.Token of
  1589. toInteger:
  1590. begin
  1591. WriteInteger(parser.TokenInt);
  1592. parser.NextToken;
  1593. end;
  1594. {$ifndef FPUNONE}
  1595. toFloat:
  1596. begin
  1597. Output.WriteByte(Ord(vaExtended));
  1598. flt := Parser.TokenFloat;
  1599. WriteExtended(flt);
  1600. parser.NextToken;
  1601. end;
  1602. {$endif}
  1603. toString:
  1604. begin
  1605. s := parser.TokenString;
  1606. while parser.NextToken = '+' do
  1607. begin
  1608. parser.NextToken; // Get next string fragment
  1609. case parser.Token of
  1610. toString : s:=s+parser.TokenString;
  1611. toWString : begin
  1612. ProcessWideString(s);
  1613. exit;
  1614. end
  1615. else parser.CheckToken(toString);
  1616. end;
  1617. end;
  1618. if (length(S)>255) then
  1619. begin
  1620. Output.WriteByte(Ord(vaLString));
  1621. WriteLString(S);
  1622. end
  1623. else
  1624. begin
  1625. Output.WriteByte(Ord(vaString));
  1626. WriteString(s);
  1627. end;
  1628. end;
  1629. toWString:
  1630. ProcessWideString('');
  1631. toSymbol:
  1632. begin
  1633. if CompareText(parser.TokenString, 'True') = 0 then
  1634. Output.WriteByte(Ord(vaTrue))
  1635. else if CompareText(parser.TokenString, 'False') = 0 then
  1636. Output.WriteByte(Ord(vaFalse))
  1637. else if CompareText(parser.TokenString, 'nil') = 0 then
  1638. Output.WriteByte(Ord(vaNil))
  1639. else
  1640. begin
  1641. Output.WriteByte(Ord(vaIdent));
  1642. WriteString(parser.TokenComponentIdent);
  1643. end;
  1644. Parser.NextToken;
  1645. end;
  1646. // Set
  1647. '[':
  1648. begin
  1649. parser.NextToken;
  1650. Output.WriteByte(Ord(vaSet));
  1651. if parser.Token <> ']' then
  1652. while True do
  1653. begin
  1654. parser.CheckToken(toSymbol);
  1655. WriteString(parser.TokenString);
  1656. parser.NextToken;
  1657. if parser.Token = ']' then
  1658. break;
  1659. parser.CheckToken(',');
  1660. parser.NextToken;
  1661. end;
  1662. Output.WriteByte(0);
  1663. parser.NextToken;
  1664. end;
  1665. // List
  1666. '(':
  1667. begin
  1668. parser.NextToken;
  1669. Output.WriteByte(Ord(vaList));
  1670. while parser.Token <> ')' do
  1671. ProcessValue;
  1672. Output.WriteByte(0);
  1673. parser.NextToken;
  1674. end;
  1675. // Collection
  1676. '<':
  1677. begin
  1678. parser.NextToken;
  1679. Output.WriteByte(Ord(vaCollection));
  1680. while parser.Token <> '>' do
  1681. begin
  1682. parser.CheckTokenSymbol('item');
  1683. parser.NextToken;
  1684. // ConvertOrder
  1685. Output.WriteByte(Ord(vaList));
  1686. while not parser.TokenSymbolIs('end') do
  1687. ProcessProperty;
  1688. parser.NextToken; // Skip 'end'
  1689. Output.WriteByte(0);
  1690. end;
  1691. Output.WriteByte(0);
  1692. parser.NextToken;
  1693. end;
  1694. // Binary data
  1695. '{':
  1696. begin
  1697. Output.WriteByte(Ord(vaBinary));
  1698. stream := TMemoryStream.Create;
  1699. try
  1700. parser.HexToBinary(stream);
  1701. WriteDWord(stream.Size);
  1702. Output.WriteBuffer(Stream.Memory^, stream.Size);
  1703. finally
  1704. stream.Free;
  1705. end;
  1706. parser.NextToken;
  1707. end;
  1708. else
  1709. parser.Error(SInvalidProperty);
  1710. end;
  1711. end;
  1712. procedure ProcessProperty;
  1713. var
  1714. name: String;
  1715. begin
  1716. // Get name of property
  1717. parser.CheckToken(toSymbol);
  1718. name := parser.TokenString;
  1719. while True do begin
  1720. parser.NextToken;
  1721. if parser.Token <> '.' then break;
  1722. parser.NextToken;
  1723. parser.CheckToken(toSymbol);
  1724. name := name + '.' + parser.TokenString;
  1725. end;
  1726. WriteString(name);
  1727. parser.CheckToken('=');
  1728. parser.NextToken;
  1729. ProcessValue;
  1730. end;
  1731. procedure ProcessObject;
  1732. var
  1733. Flags: Byte;
  1734. ObjectName, ObjectType: String;
  1735. ChildPos: Integer;
  1736. begin
  1737. if parser.TokenSymbolIs('OBJECT') then
  1738. Flags :=0 { IsInherited := False }
  1739. else begin
  1740. if parser.TokenSymbolIs('INHERITED') then
  1741. Flags := 1 { IsInherited := True; }
  1742. else begin
  1743. parser.CheckTokenSymbol('INLINE');
  1744. Flags := 4;
  1745. end;
  1746. end;
  1747. parser.NextToken;
  1748. parser.CheckToken(toSymbol);
  1749. ObjectName := '';
  1750. ObjectType := parser.TokenString;
  1751. parser.NextToken;
  1752. if parser.Token = ':' then begin
  1753. parser.NextToken;
  1754. parser.CheckToken(toSymbol);
  1755. ObjectName := ObjectType;
  1756. ObjectType := parser.TokenString;
  1757. parser.NextToken;
  1758. if parser.Token = '[' then begin
  1759. parser.NextToken;
  1760. ChildPos := parser.TokenInt;
  1761. parser.NextToken;
  1762. parser.CheckToken(']');
  1763. parser.NextToken;
  1764. Flags := Flags or 2;
  1765. end;
  1766. end;
  1767. if Flags <> 0 then begin
  1768. Output.WriteByte($f0 or Flags);
  1769. if (Flags and 2) <> 0 then
  1770. WriteInteger(ChildPos);
  1771. end;
  1772. WriteString(ObjectType);
  1773. WriteString(ObjectName);
  1774. // Convert property list
  1775. while not (parser.TokenSymbolIs('END') or
  1776. parser.TokenSymbolIs('OBJECT') or
  1777. parser.TokenSymbolIs('INHERITED') or
  1778. parser.TokenSymbolIs('INLINE')) do
  1779. ProcessProperty;
  1780. Output.WriteByte(0); // Terminate property list
  1781. // Convert child objects
  1782. while not parser.TokenSymbolIs('END') do ProcessObject;
  1783. parser.NextToken; // Skip end token
  1784. Output.WriteByte(0); // Terminate property list
  1785. end;
  1786. const
  1787. signature: PChar = 'TPF0';
  1788. begin
  1789. parser := TParser.Create(Input);
  1790. try
  1791. Output.WriteBuffer(signature[0], 4);
  1792. ProcessObject;
  1793. finally
  1794. parser.Free;
  1795. end;
  1796. end;
  1797. procedure ObjectResourceToText(Input, Output: TStream);
  1798. begin
  1799. Input.ReadResHeader;
  1800. ObjectBinaryToText(Input, Output);
  1801. end;
  1802. procedure ObjectTextToResource(Input, Output: TStream);
  1803. var
  1804. StartPos, FixupInfo: LongInt;
  1805. parser: TParser;
  1806. name: String;
  1807. begin
  1808. // Get form type name
  1809. StartPos := Input.Position;
  1810. parser := TParser.Create(Input);
  1811. try
  1812. if not parser.TokenSymbolIs('OBJECT') then parser.CheckTokenSymbol('INHERITED');
  1813. parser.NextToken;
  1814. parser.CheckToken(toSymbol);
  1815. parser.NextToken;
  1816. parser.CheckToken(':');
  1817. parser.NextToken;
  1818. parser.CheckToken(toSymbol);
  1819. name := parser.TokenString;
  1820. finally
  1821. parser.Free;
  1822. Input.Position := StartPos;
  1823. end;
  1824. name := UpperCase(name);
  1825. Output.WriteResourceHeader(name,FixupInfo); // Write resource header
  1826. ObjectTextToBinary(Input, Output); // Convert the stuff!
  1827. Output.FixupResourceHeader(FixupInfo); // Insert real resource data size
  1828. end;
  1829. { Utility routines }
  1830. function LineStart(Buffer, BufPos: PChar): PChar;
  1831. begin
  1832. Result := BufPos;
  1833. while Result > Buffer do begin
  1834. Dec(Result);
  1835. if Result[0] = #10 then break;
  1836. end;
  1837. end;
  1838. procedure CommonInit;
  1839. begin
  1840. SynchronizeTimeoutEvent:=RtlEventCreate;
  1841. InitCriticalSection(ThreadQueueLock);
  1842. MainThreadID:=GetCurrentThreadID;
  1843. ExternalThreads := TThreadList.Create;
  1844. TThread.FProcessorCount := CPUCount;
  1845. InitCriticalsection(ResolveSection);
  1846. InitHandlerList:=Nil;
  1847. FindGlobalComponentList:=nil;
  1848. IntConstList := TThreadList.Create;
  1849. ClassList := TThreadList.Create;
  1850. ClassAliasList := TStringList.Create;
  1851. { on unix this maps to a simple rw synchornizer }
  1852. GlobalNameSpace := TMultiReadExclusiveWriteSynchronizer.Create;
  1853. RegisterInitComponentHandler(TComponent,@DefaultInitHandler);
  1854. end;
  1855. procedure CommonCleanup;
  1856. var
  1857. i: Integer;
  1858. tmpentry: TThread.PThreadQueueEntry;
  1859. begin
  1860. GlobalNameSpace.BeginWrite;
  1861. with IntConstList.LockList do
  1862. try
  1863. for i := 0 to Count - 1 do
  1864. TIntConst(Items[I]).Free;
  1865. finally
  1866. IntConstList.UnlockList;
  1867. end;
  1868. IntConstList.Free;
  1869. ClassList.Free;
  1870. ClassAliasList.Free;
  1871. RemoveFixupReferences(nil, '');
  1872. DoneCriticalsection(ResolveSection);
  1873. GlobalLists.Free;
  1874. ComponentPages.Free;
  1875. FreeAndNil(NeedResolving);
  1876. { GlobalNameSpace is an interface so this is enough }
  1877. GlobalNameSpace:=nil;
  1878. if (InitHandlerList<>Nil) then
  1879. for i := 0 to InitHandlerList.Count - 1 do
  1880. TInitHandler(InitHandlerList.Items[I]).Free;
  1881. InitHandlerList.Free;
  1882. InitHandlerList:=Nil;
  1883. FindGlobalComponentList.Free;
  1884. FindGlobalComponentList:=nil;
  1885. with ExternalThreads.LockList do
  1886. try
  1887. for i := 0 to Count - 1 do
  1888. TThread(Items[i]).Free;
  1889. finally
  1890. ExternalThreads.UnlockList;
  1891. end;
  1892. FreeAndNil(ExternalThreads);
  1893. RtlEventDestroy(SynchronizeTimeoutEvent);
  1894. { clean up the queue, but keep in mind that the entries used for Synchronize
  1895. are owned by the corresponding TThread }
  1896. while Assigned(ThreadQueueHead) do begin
  1897. tmpentry := ThreadQueueHead;
  1898. ThreadQueueHead := tmpentry^.Next;
  1899. if not Assigned(tmpentry^.SyncEvent) then
  1900. Dispose(tmpentry);
  1901. end;
  1902. DoneCriticalSection(ThreadQueueLock);
  1903. end;
  1904. { TFiler implementation }
  1905. {$i filer.inc}
  1906. { TReader implementation }
  1907. {$i reader.inc}
  1908. { TWriter implementations }
  1909. {$i writer.inc}
  1910. {$i twriter.inc}