GLPersistentClasses.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. (*
  5. Base persistence classes.
  6. These classes are used in GLScene, but are designed for generic purpose.
  7. They implement a slightly different persistence mechanism then that of the VCL,
  8. allowing for object-level versioning (100% backward compatibility) and full
  9. polymorphic persistence.
  10. *)
  11. unit GLPersistentClasses;
  12. interface
  13. {$I GLScene.inc}
  14. uses
  15. System.Classes,
  16. System.SysUtils,
  17. GLStrings;
  18. type
  19. PObject = ^TObject;
  20. //Virtual layer similar to VCL's TReader (but reusable)
  21. TVirtualReader = class
  22. private
  23. FStream: TStream;
  24. public
  25. constructor Create(Stream: TStream); virtual;
  26. property Stream: TStream read FStream;
  27. procedure ReadTypeError;
  28. procedure Read(var Buf; Count: Longint); virtual; abstract;
  29. function NextValue: TValueType; virtual; abstract;
  30. function ReadInteger: Integer; virtual; abstract;
  31. function ReadBoolean: Boolean; virtual; abstract;
  32. function ReadString: string; virtual; abstract;
  33. function ReadFloat: Extended; virtual; abstract;
  34. procedure ReadListBegin; virtual; abstract;
  35. procedure ReadListEnd; virtual; abstract;
  36. function EndOfList: Boolean; virtual; abstract;
  37. procedure ReadTStrings(aStrings: TStrings);
  38. end;
  39. //Virtual layer similar to VCL's TWriter (but reusable)
  40. TVirtualWriter = class
  41. private
  42. FStream: TStream;
  43. public
  44. constructor Create(Stream: TStream); virtual;
  45. property Stream: TStream read FStream;
  46. procedure Write(const Buf; Count: Longint); virtual; abstract;
  47. procedure WriteInteger(anInteger: Integer); virtual; abstract;
  48. procedure WriteBoolean(aBoolean: Boolean); virtual; abstract;
  49. procedure WriteString(const aString: string); virtual; abstract;
  50. procedure WriteFloat(const aFloat: Extended); virtual; abstract;
  51. procedure WriteListBegin; virtual; abstract;
  52. procedure WriteListEnd; virtual; abstract;
  53. procedure WriteTStrings(const aStrings: TStrings; storeObjects: Boolean = True);
  54. end;
  55. TVirtualReaderClass = class of TVirtualReader;
  56. TVirtualWriterClass = class of TVirtualWriter;
  57. (*Interface for persistent objects.
  58. This interface does not really allow polymorphic persistence,
  59. but is rather intended as a way to unify persistence calls
  60. for iterators *)
  61. IPersistentObject = interface(IInterface)
  62. ['{A9A0198A-F11B-4325-A92C-2F24DB41652B}']
  63. procedure WriteToFiler(writer: TVirtualWriter);
  64. procedure ReadFromFiler(reader: TVirtualReader);
  65. end;
  66. (* Base class for persistent objects.
  67. The base requirement is implementation of ReadFromFiler & WriteToFiler
  68. in sub-classes, the immediate benefits are support of streaming (to stream,
  69. file or string), assignment and cloning.
  70. The other requirement being the use of a virtual constructor, which allows
  71. polymorphic construction (don't forget to register your subclasses).
  72. Note that TPersistentObject implements IUnknown, but does *not* implement
  73. reference counting *)
  74. TPersistentObject = class(TPersistent, IPersistentObject)
  75. protected
  76. procedure RaiseFilerException(const archiveVersion: Integer);
  77. function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  78. function _AddRef: Integer; stdcall;
  79. function _Release: Integer; stdcall;
  80. public
  81. constructor Create; virtual;
  82. constructor CreateFromFiler(reader: TVirtualReader);
  83. destructor Destroy; override;
  84. procedure Assign(source: TPersistent); override;
  85. function CreateClone: TPersistentObject; dynamic;
  86. class function FileSignature: string; virtual;
  87. class function FileVirtualWriter: TVirtualWriterClass; virtual;
  88. class function FileVirtualReader: TVirtualReaderClass; virtual;
  89. procedure WriteToFiler(writer: TVirtualWriter); dynamic;
  90. procedure ReadFromFiler(reader: TVirtualReader); dynamic;
  91. procedure SaveToStream(stream: TStream; writerClass: TVirtualWriterClass = nil); dynamic;
  92. procedure LoadFromStream(stream: TStream; readerClass: TVirtualReaderClass = nil); dynamic;
  93. procedure SaveToFile(const fileName: string; writerClass: TVirtualWriterClass = nil); dynamic;
  94. procedure LoadFromFile(const fileName: string; readerClass: TVirtualReaderClass = nil); dynamic;
  95. function SaveToString(writerClass: TVirtualWriterClass = nil): string; dynamic;
  96. procedure LoadFromString(const data: string; readerClass: TVirtualReaderClass = nil); dynamic;
  97. end;
  98. TPersistentObjectClass = class of TPersistentObject;
  99. TPointerObjectList = array[0..MaxInt div (2*SizeOf(Pointer))] of TObject;
  100. PPointerObjectList = ^TPointerObjectList;
  101. TObjectListSortCompare = function(item1, item2: TObject): Integer;
  102. (*A persistent Object list.
  103. Similar to TList but works on TObject items and has facilities for
  104. persistence of contained data. Unlike the VCL's TObjectList, this one
  105. does NOT free its objects upon destruction or Clear, use Clean and CleanFree
  106. for that, and as such can be used for object referral lists too.
  107. But only TPersistentObject items will be streamed appropriately.
  108. The list can be used in a stack-like fashion with Push & Pop, and can
  109. perform basic boolean set operations.
  110. Note: the IndexOf implementation is up to 3 times faster than that of TList *)
  111. TPersistentObjectList = class(TPersistentObject)
  112. private
  113. FList: PPointerObjectList;
  114. FCount: Integer;
  115. FCapacity: Integer;
  116. FGrowthDelta: integer;
  117. protected
  118. procedure Error; virtual;
  119. function Get(Index: Integer): TObject; inline;
  120. procedure Put(Index: Integer; Item: TObject);
  121. procedure SetCapacity(newCapacity: Integer); inline;
  122. procedure SetCount(NewCount: Integer); inline;
  123. function GetFirst: TObject; inline;
  124. procedure SetFirst(item: TObject);
  125. function GetLast: TObject;
  126. procedure SetLast(item: TObject);
  127. // Default event for ReadFromFiler
  128. procedure AfterObjectCreatedByReader(Sender: TObject); virtual;
  129. procedure DoClean;
  130. public
  131. constructor Create; override;
  132. destructor Destroy; override;
  133. procedure WriteToFiler(writer: TVirtualWriter); override;
  134. procedure ReadFromFiler(reader: TVirtualReader); override;
  135. procedure ReadFromFilerWithEvent(reader: TVirtualReader;
  136. afterSenderObjectCreated: TNotifyEvent);
  137. function Add(const item: TObject): Integer; inline;
  138. procedure AddNils(nbVals: Cardinal);
  139. procedure Delete(index: Integer);
  140. procedure DeleteItems(index: Integer; nbVals: Cardinal);
  141. procedure Exchange(Index1, Index2: Integer);
  142. procedure Insert(Index: Integer; Item: TObject);
  143. procedure InsertNils(index: Integer; nbVals: Cardinal);
  144. procedure Move(CurIndex, NewIndex: Integer);
  145. function Remove(Item: TObject): Integer;
  146. procedure DeleteAndFree(index: Integer);
  147. procedure DeleteAndFreeItems(index: Integer; nbVals: Cardinal);
  148. function RemoveAndFree(item: TObject): Integer;
  149. property GrowthDelta: integer read FGrowthDelta write FGrowthDelta;
  150. function Expand: TPersistentObjectList;
  151. property Items[Index: Integer]: TObject read Get write Put; default;
  152. property Count: Integer read FCount write SetCount;
  153. property List: PPointerObjectList read FList;
  154. property Capacity: Integer read FCapacity write SetCapacity;
  155. //Makes sure capacity is at least aCapacity.
  156. procedure RequiredCapacity(aCapacity: Integer);
  157. (*Removes all "nil" from the list.
  158. Note: Capacity is unchanged, no memory us freed, the list is just
  159. made shorter. This functions is orders of magnitude faster than
  160. its TList eponymous. *)
  161. procedure Pack;
  162. //Empty the list without freeing the objects.
  163. procedure Clear; virtual;
  164. //Empty the list and free the objects.
  165. procedure Clean; virtual;
  166. //Empty the list, free the objects and Free self.
  167. procedure CleanFree;
  168. function IndexOf(Item: TObject): Integer;
  169. property First: TObject read GetFirst write SetFirst;
  170. property Last: TObject read GetLast write SetLast;
  171. procedure Push(item: TObject);
  172. function Pop: TObject;
  173. procedure PopAndFree;
  174. function AddObjects(const objectList: TPersistentObjectList): Integer;
  175. procedure RemoveObjects(const objectList: TPersistentObjectList);
  176. procedure Sort(compareFunc: TObjectListSortCompare);
  177. end;
  178. //Wraps a TReader-compatible reader.
  179. TGLBinaryReader = class(TVirtualReader)
  180. protected
  181. function ReadValue: TValueType;
  182. function ReadWideString(vType: TValueType): WideString;
  183. public
  184. procedure Read(var Buf; Count: Longint); override;
  185. function NextValue: TValueType; override;
  186. function ReadInteger: Integer; override;
  187. function ReadBoolean: Boolean; override;
  188. function ReadString: string; override;
  189. function ReadFloat: Extended; override;
  190. procedure ReadListBegin; override;
  191. procedure ReadListEnd; override;
  192. function EndOfList: Boolean; override;
  193. end;
  194. //Wraps a TWriter-compatible writer.
  195. TGLBinaryWriter = class(TVirtualWriter)
  196. protected
  197. procedure WriteAnsiString(const aString: AnsiString); virtual;
  198. procedure WriteWideString(const aString: WideString); virtual;
  199. public
  200. procedure Write(const Buf; Count: Longint); override;
  201. procedure WriteInteger(anInteger: Integer); override;
  202. procedure WriteBoolean(aBoolean: Boolean); override;
  203. procedure WriteString(const aString: string); override;
  204. procedure WriteFloat(const aFloat: Extended); override;
  205. procedure WriteListBegin; override;
  206. procedure WriteListEnd; override;
  207. end;
  208. //Reads object persistence in Text format.
  209. TGLTextReader = class(TVirtualReader)
  210. private
  211. FValueType: string;
  212. FData: string;
  213. protected
  214. procedure ReadLine(const requestedType: string = '');
  215. public
  216. procedure Read(var Buf; Count: Longint); override;
  217. function NextValue: TValueType; override;
  218. function ReadInteger: Integer; override;
  219. function ReadBoolean: Boolean; override;
  220. function ReadString: string; override;
  221. function ReadFloat: Extended; override;
  222. procedure ReadListBegin; override;
  223. procedure ReadListEnd; override;
  224. function EndOfList: Boolean; override;
  225. end;
  226. //Writes object persistence in Text format.
  227. TGLTextWriter = class(TVirtualWriter)
  228. private
  229. FIndentLevel: Integer;
  230. protected
  231. procedure WriteLine(const valueType, data: string);
  232. public
  233. constructor Create(aStream: TStream); override;
  234. destructor Destroy; override;
  235. procedure Write(const Buf; Count: Longint); override;
  236. procedure WriteInteger(anInteger: Integer); override;
  237. procedure WriteBoolean(aBoolean: Boolean); override;
  238. procedure WriteString(const aString: string); override;
  239. procedure WriteFloat(const aFloat: Extended); override;
  240. procedure WriteListBegin; override;
  241. procedure WriteListEnd; override;
  242. end;
  243. //TPersistent which has knowledge of its owner.
  244. TGLOwnedPersistent = class(TPersistent)
  245. private
  246. FOwner: TPersistent;
  247. protected
  248. function GetOwner: TPersistent; override;
  249. public
  250. constructor Create(AOwner: TPersistent); virtual;
  251. end;
  252. //TPersistent that inplements IInterface.
  253. TGLInterfacedPersistent = class(TPersistent, IInterface)
  254. protected
  255. // Implementing IInterface.
  256. function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  257. function _AddRef: Integer; stdcall;
  258. function _Release: Integer; stdcall;
  259. end;
  260. //TCollectionItem thet inplements IInterface.
  261. TGLInterfacedCollectionItem = class(TCollectionItem, IInterface)
  262. protected
  263. // Implementing IInterface.
  264. function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  265. function _AddRef: Integer; stdcall;
  266. function _Release: Integer; stdcall;
  267. end;
  268. //Triggered when file signature does not match.
  269. EInvalidFileSignature = class(Exception)
  270. end;
  271. //Usually triggered when a filing error is detected.
  272. EFilerException = class(Exception)
  273. end;
  274. procedure RaiseFilerException(aClass: TClass; archiveVersion: Integer);
  275. function UTF8ToWideString(const s: AnsiString): WideString;
  276. // ------------------------------------------------------------------
  277. implementation
  278. // ------------------------------------------------------------------
  279. uses
  280. GLApplicationFileIO;
  281. const
  282. cDefaultListGrowthDelta = 64;
  283. cVTInteger = 'Int';
  284. cVTFloat = 'Float';
  285. cVTString = 'Str';
  286. cVTBoolean = 'Bool';
  287. cVTRaw = 'Raw';
  288. cVTListBegin = '{';
  289. cVTListEnd = '}';
  290. cTrue = 'True';
  291. cFalse = 'False';
  292. procedure RaiseFilerException(aClass: TClass; archiveVersion: Integer);
  293. begin
  294. raise EFilerException.Create(aClass.ClassName +
  295. strUnknownArchiveVersion + IntToStr(archiveVersion));
  296. end;
  297. function UTF8ToWideString(const s: AnsiString): WideString;
  298. // Based on Mike Lischke's function (Unicode.pas unit, http://www.delphi-gems.com)
  299. const
  300. bytesFromUTF8: packed array[0..255] of Byte = (
  301. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  302. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  303. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  304. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  305. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  306. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  307. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  308. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5);
  309. offsetsFromUTF8: array[0..5] of Cardinal = (
  310. $00000000, $00003080, $000E2080, $03C82080, $FA082080, $82082080);
  311. MaximumUCS2: Cardinal = $0000FFFF;
  312. MaximumUCS4: Cardinal = $7FFFFFFF;
  313. ReplacementCharacter: Cardinal = $0000FFFD;
  314. halfShift: Integer = 10;
  315. halfBase: Cardinal = $0010000;
  316. halfMask: Cardinal = $3FF;
  317. SurrogateHighStart: Cardinal = $D800;
  318. SurrogateLowStart: Cardinal = $DC00;
  319. var
  320. sLength, L, J, T: Cardinal;
  321. ch: Cardinal;
  322. extraBytesToWrite: Word;
  323. begin
  324. sLength := Length(s);
  325. if sLength = 0 then
  326. begin
  327. Result := '';
  328. Exit;
  329. end;
  330. SetLength(Result, sLength); // creates enough room
  331. L := 1;
  332. T := 1;
  333. while L <= Cardinal(sLength) do
  334. begin
  335. ch := 0;
  336. extraBytesToWrite := bytesFromUTF8[Ord(S[L])];
  337. for J := extraBytesToWrite downto 1 do
  338. begin
  339. ch := ch + Ord(S[L]);
  340. Inc(L);
  341. ch := ch shl 6;
  342. end;
  343. ch := ch + Ord(S[L]);
  344. Inc(L);
  345. ch := ch - offsetsFromUTF8[extraBytesToWrite];
  346. if ch <= MaximumUCS2 then
  347. begin
  348. Result[T] := WideChar(ch);
  349. Inc(T);
  350. end
  351. else if ch > MaximumUCS4 then
  352. begin
  353. Result[T] := WideChar(ReplacementCharacter);
  354. Inc(T);
  355. end
  356. else
  357. begin
  358. ch := ch - halfBase;
  359. Result[T] := WideChar((ch shr halfShift) + SurrogateHighStart);
  360. Inc(T);
  361. Result[T] := WideChar((ch and halfMask) + SurrogateLowStart);
  362. Inc(T);
  363. end;
  364. end;
  365. SetLength(Result, T - 1); // now fix up length
  366. end;
  367. // ------------------
  368. // ------------------ TVirtualReader ------------------
  369. // ------------------
  370. constructor TVirtualReader.Create(Stream: TStream);
  371. begin
  372. FStream := Stream;
  373. end;
  374. procedure TVirtualReader.ReadTypeError;
  375. begin
  376. raise EReadError.CreateFmt('%s, read type error', [ClassName]);
  377. end;
  378. procedure TVirtualReader.ReadTStrings(aStrings: TStrings);
  379. var
  380. i: Integer;
  381. objectsStored: Boolean;
  382. begin
  383. aStrings.BeginUpdate;
  384. aStrings.Clear;
  385. objectsStored := ReadBoolean;
  386. i := ReadInteger;
  387. if objectsStored then
  388. while i > 0 do
  389. begin
  390. aStrings.AddObject(ReadString, TObject(ReadInteger));
  391. Dec(i);
  392. end
  393. else
  394. while i > 0 do
  395. begin
  396. aStrings.Add(ReadString);
  397. Dec(i);
  398. end;
  399. aStrings.EndUpdate;
  400. end;
  401. // ------------------
  402. // ------------------ TVirtualWriter ------------------
  403. // ------------------
  404. constructor TVirtualWriter.Create(Stream: TStream);
  405. begin
  406. FStream := Stream;
  407. end;
  408. procedure TVirtualWriter.WriteTStrings(const aStrings: TStrings;
  409. storeObjects: Boolean = True);
  410. var
  411. i: Integer;
  412. begin
  413. writeBoolean(storeObjects);
  414. if Assigned(aStrings) then
  415. begin
  416. WriteInteger(aStrings.Count);
  417. if storeObjects then
  418. for i := 0 to aStrings.Count - 1 do
  419. begin
  420. WriteString(aStrings[i]);
  421. WriteInteger(Integer(aStrings.Objects[i]));
  422. end
  423. else
  424. for i := 0 to aStrings.Count - 1 do
  425. WriteString(aStrings[i]);
  426. end
  427. else
  428. WriteInteger(0);
  429. end;
  430. // ------------------
  431. // ------------------ TPersistentObject ------------------
  432. // ------------------
  433. constructor TPersistentObject.Create;
  434. begin
  435. inherited Create;
  436. end;
  437. constructor TPersistentObject.CreateFromFiler(reader: TVirtualReader);
  438. begin
  439. Create;
  440. ReadFromFiler(reader);
  441. end;
  442. destructor TPersistentObject.Destroy;
  443. begin
  444. inherited Destroy;
  445. end;
  446. procedure TPersistentObject.Assign(source: TPersistent);
  447. var
  448. ms: TStringStream; // faster than a TMemoryStream...
  449. begin
  450. if source.ClassType = Self.ClassType then
  451. begin
  452. ms := TStringStream.Create('');
  453. try
  454. TPersistentObject(source).SaveToStream(ms);
  455. ms.Position := 0;
  456. LoadFromStream(ms);
  457. finally
  458. ms.Free;
  459. end;
  460. end
  461. else
  462. inherited;
  463. end;
  464. function TPersistentObject.CreateClone: TPersistentObject;
  465. begin
  466. Result := TPersistentObjectClass(Self.ClassType).Create;
  467. Result.Assign(Self);
  468. end;
  469. class function TPersistentObject.FileSignature: string;
  470. begin
  471. Result := '';
  472. end;
  473. class function TPersistentObject.FileVirtualWriter: TVirtualWriterClass;
  474. begin
  475. Result := TGLBinaryWriter;
  476. end;
  477. class function TPersistentObject.FileVirtualReader: TVirtualReaderClass;
  478. begin
  479. Result := TGLBinaryReader;
  480. end;
  481. procedure TPersistentObject.WriteToFiler(writer: TVirtualWriter);
  482. begin
  483. // nothing
  484. Assert(Assigned(writer));
  485. end;
  486. procedure TPersistentObject.ReadFromFiler(reader: TVirtualReader);
  487. begin
  488. // nothing
  489. Assert(Assigned(reader));
  490. end;
  491. procedure TPersistentObject.RaiseFilerException(const archiveVersion: Integer);
  492. begin
  493. raise EFilerException.Create(ClassName + strUnknownArchiveVersion + IntToStr(archiveVersion)); //IGNORE
  494. end;
  495. function TPersistentObject.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  496. begin
  497. if GetInterface(IID, Obj) then
  498. Result := S_OK
  499. else
  500. Result := E_NOINTERFACE;
  501. end;
  502. function TPersistentObject._AddRef: Integer; stdcall;
  503. begin
  504. // ignore
  505. Result := 1;
  506. end;
  507. function TPersistentObject._Release: Integer; stdcall;
  508. begin
  509. // ignore
  510. Result := 0;
  511. end;
  512. procedure TPersistentObject.SaveToStream(stream: TStream; writerClass: TVirtualWriterClass = nil);
  513. var
  514. wr: TVirtualWriter;
  515. fileSig: AnsiString;
  516. begin
  517. if writerClass = nil then
  518. writerClass := TGLBinaryWriter;
  519. wr := writerClass.Create(stream);
  520. try
  521. if FileSignature <> '' then
  522. begin
  523. fileSig := AnsiString(FileSignature);
  524. wr.Write(fileSig[1], Length(fileSig));
  525. end;
  526. WriteToFiler(wr);
  527. finally
  528. wr.Free;
  529. end;
  530. end;
  531. procedure TPersistentObject.LoadFromStream(stream: TStream; readerClass: TVirtualReaderClass = nil);
  532. var
  533. rd: TVirtualReader;
  534. sig: AnsiString;
  535. begin
  536. if readerClass = nil then
  537. readerClass := TGLBinaryReader;
  538. rd := readerClass.Create(stream);
  539. try
  540. if FileSignature <> '' then
  541. begin
  542. SetLength(sig, Length(FileSignature));
  543. rd.Read(sig[1], Length(FileSignature));
  544. if sig <> AnsiString(FileSignature) then
  545. raise EInvalidFileSignature.Create(strInvalidFileSignature);
  546. end;
  547. ReadFromFiler(rd);
  548. finally
  549. rd.Free;
  550. end;
  551. end;
  552. procedure TPersistentObject.SaveToFile(const fileName: string; writerClass: TVirtualWriterClass = nil);
  553. var
  554. fs: TStream;
  555. begin
  556. if writerClass = nil then
  557. writerClass := FileVirtualWriter;
  558. fs := CreateFileStream(fileName, fmCreate);
  559. try
  560. SaveToStream(fs, writerClass);
  561. finally
  562. fs.Free;
  563. end;
  564. end;
  565. procedure TPersistentObject.LoadFromFile(const fileName: string; readerClass: TVirtualReaderClass = nil);
  566. var
  567. fs: TStream;
  568. begin
  569. if readerClass = nil then
  570. readerClass := FileVirtualReader;
  571. fs := CreateFileStream(fileName, fmOpenRead + fmShareDenyWrite);
  572. try
  573. LoadFromStream(fs, readerClass);
  574. finally
  575. fs.Free;
  576. end;
  577. end;
  578. function TPersistentObject.SaveToString(writerClass: TVirtualWriterClass = nil): string;
  579. var
  580. ss: TStringStream;
  581. begin
  582. ss := TStringStream.Create('');
  583. try
  584. SaveToStream(ss, writerClass);
  585. Result := ss.DataString;
  586. finally
  587. ss.Free;
  588. end;
  589. end;
  590. procedure TPersistentObject.LoadFromString(const data: string; readerClass: TVirtualReaderClass = nil);
  591. var
  592. ss: TStringStream;
  593. begin
  594. ss := TStringStream.Create(data);
  595. try
  596. LoadFromStream(ss, readerClass);
  597. finally
  598. ss.Free;
  599. end;
  600. end;
  601. // ------------------
  602. // ------------------ TPersistentObjectList ------------------
  603. // ------------------
  604. constructor TPersistentObjectList.Create;
  605. begin
  606. inherited Create;
  607. FGrowthDelta := cDefaultListGrowthDelta;
  608. end;
  609. destructor TPersistentObjectList.Destroy;
  610. begin
  611. Clear;
  612. inherited Destroy;
  613. end;
  614. function TPersistentObjectList.Add(const item: TObject): Integer;
  615. begin
  616. Result := FCount;
  617. if Result = FCapacity then
  618. SetCapacity(FCapacity + FGrowthDelta);
  619. FList^[Result] := Item;
  620. Inc(FCount);
  621. end;
  622. procedure TPersistentObjectList.AddNils(nbVals: Cardinal);
  623. begin
  624. if Integer(nbVals) + Count > Capacity then
  625. SetCapacity(Integer(nbVals) + Count);
  626. FillChar(FList[FCount], Integer(nbVals) * SizeOf(TObject), 0);
  627. FCount := FCount + Integer(nbVals);
  628. end;
  629. function TPersistentObjectList.AddObjects(const objectList: TPersistentObjectList): Integer;
  630. begin
  631. if Assigned(objectList) then
  632. begin
  633. Result := FCount;
  634. SetCount(Result + objectList.Count);
  635. System.Move(objectList.FList^[0], FList^[Result],
  636. objectList.FCount * SizeOf(TObject));
  637. end
  638. else
  639. Result := 0;
  640. end;
  641. procedure TPersistentObjectList.RemoveObjects(const objectList: TPersistentObjectList);
  642. var
  643. i: Integer;
  644. begin
  645. for i := 0 to objectList.Count - 1 do
  646. Remove(objectList[i]);
  647. end;
  648. procedure TPersistentObjectList.Clear;
  649. begin
  650. if Assigned(Self) and Assigned(FList) then
  651. begin
  652. SetCount(0);
  653. SetCapacity(0);
  654. end;
  655. end;
  656. procedure TPersistentObjectList.Delete(index: Integer);
  657. begin
  658. {$IFOPT R+}
  659. if Cardinal(Index) >= Cardinal(FCount) then
  660. Error;
  661. {$ENDIF}
  662. Dec(FCount);
  663. if index < FCount then
  664. System.Move(FList[index + 1], FList[index], (FCount - index) * SizeOf(TObject));
  665. end;
  666. procedure TPersistentObjectList.DeleteItems(index: Integer; nbVals: Cardinal);
  667. begin
  668. {$IFOPT R+}
  669. Assert(Cardinal(index) < Cardinal(FCount));
  670. {$ENDIF}
  671. if nbVals > 0 then
  672. begin
  673. if index + Integer(nbVals) < FCount then
  674. begin
  675. System.Move(FList[index + Integer(nbVals)],
  676. FList[index],
  677. (FCount - index - Integer(nbVals)) * SizeOf(TObject));
  678. end;
  679. Dec(FCount, nbVals);
  680. end;
  681. end;
  682. procedure TPersistentObjectList.Exchange(index1, index2: Integer);
  683. var
  684. item: TObject;
  685. locList: PPointerObjectList;
  686. begin
  687. {$IFOPT R+}
  688. if (Cardinal(Index1) >= Cardinal(FCount)) or
  689. (Cardinal(Index2) >= Cardinal(FCount)) then
  690. Error;
  691. {$ENDIF}
  692. locList := FList;
  693. item := locList^[index1];
  694. locList^[index1] := locList^[index2];
  695. locList^[index2] := item;
  696. end;
  697. function TPersistentObjectList.Expand: TPersistentObjectList;
  698. begin
  699. if FCount = FCapacity then
  700. SetCapacity(FCapacity + FGrowthDelta);
  701. Result := Self;
  702. end;
  703. function TPersistentObjectList.GetFirst: TObject;
  704. begin
  705. {$IFOPT R+}
  706. if Cardinal(FCount) = 0 then
  707. Error;
  708. {$ENDIF}
  709. Result := FList^[0];
  710. end;
  711. procedure TPersistentObjectList.SetFirst(item: TObject);
  712. begin
  713. {$IFOPT R+}
  714. if Cardinal(FCount) = 0 then
  715. Error;
  716. {$ENDIF}
  717. FList^[0] := item;
  718. end;
  719. procedure TPersistentObjectList.Error;
  720. begin
  721. raise EListError.Create(strListIndexError);
  722. end;
  723. function TPersistentObjectList.Get(Index: Integer): TObject;
  724. begin
  725. {$IFOPT R+}
  726. if Cardinal(Index) >= Cardinal(FCount) then
  727. Error;
  728. {$ENDIF}
  729. Result := FList^[Index];
  730. end;
  731. function TPersistentObjectList.IndexOf(Item: TObject): Integer;
  732. var
  733. I: Integer;
  734. begin
  735. if FCount <= 0 then
  736. Result := -1
  737. else
  738. begin
  739. Result := -1;
  740. for I := 0 to FCount - 1 do
  741. if FList^[I] = Item then
  742. begin
  743. Result := I;
  744. Exit;
  745. end;
  746. end;
  747. end;
  748. procedure TPersistentObjectList.Insert(index: Integer; item: TObject);
  749. begin
  750. {$IFOPT R+}
  751. if Cardinal(index) > Cardinal(FCount) then
  752. Error;
  753. {$ENDIF}
  754. if FCount = FCapacity then
  755. SetCapacity(FCapacity + FGrowthDelta);
  756. if Index < FCount then
  757. System.Move(FList[index], FList[index + 1],
  758. (FCount - index) * SizeOf(TObject));
  759. FList^[index] := item;
  760. Inc(FCount);
  761. end;
  762. procedure TPersistentObjectList.InsertNils(index: Integer; nbVals: Cardinal);
  763. var
  764. nc: Integer;
  765. begin
  766. {$IFOPT R+}
  767. Assert(Cardinal(Index) <= Cardinal(FCount));
  768. {$ENDIF}
  769. if nbVals > 0 then
  770. begin
  771. nc := FCount + Integer(nbVals);
  772. if nc > FCapacity then
  773. SetCapacity(nc);
  774. if Index < FCount then
  775. System.Move(FList[Index], FList[Index + Integer(nbVals)],
  776. (FCount - Index) * SizeOf(TObject));
  777. FillChar(FList[Index], Integer(nbVals) * SizeOf(TObject), 0);
  778. FCount := nc;
  779. end;
  780. end;
  781. function TPersistentObjectList.GetLast: TObject;
  782. begin
  783. {$IFOPT R+}
  784. if Cardinal(FCount) = 0 then
  785. Error;
  786. {$ENDIF}
  787. Result := FList^[FCount - 1];
  788. end;
  789. procedure TPersistentObjectList.SetLast(item: TObject);
  790. begin
  791. {$IFOPT R+}
  792. if Cardinal(FCount) = 0 then
  793. Error;
  794. {$ENDIF}
  795. FList^[FCount - 1] := item;
  796. end;
  797. procedure TPersistentObjectList.Move(CurIndex, NewIndex: Integer);
  798. var
  799. item: Pointer;
  800. begin
  801. if curIndex <> newIndex then
  802. begin
  803. {$IFOPT R+}
  804. if Cardinal(newIndex) >= Cardinal(Count) then
  805. Error;
  806. if Cardinal(curIndex) >= Cardinal(Count) then
  807. Error;
  808. {$ENDIF}
  809. item := FList^[curIndex];
  810. if curIndex < newIndex then
  811. begin
  812. // curIndex+1 necessarily exists since curIndex<newIndex and newIndex<Count
  813. System.Move(List[curIndex + 1], List[curIndex], (NewIndex - CurIndex) * SizeOf(TObject));
  814. end
  815. else
  816. begin
  817. // newIndex+1 necessarily exists since newIndex<curIndex and curIndex<Count
  818. System.Move(List[newIndex], List[newIndex + 1], (CurIndex - NewIndex) * SizeOf(TObject));
  819. end;
  820. FList^[newIndex] := TObject(item);
  821. end;
  822. end;
  823. procedure TPersistentObjectList.Put(Index: Integer; Item: TObject);
  824. begin
  825. {$IFOPT R+}
  826. if Cardinal(Index) >= Cardinal(FCount) then
  827. Error;
  828. {$ENDIF}
  829. FList^[Index] := Item;
  830. end;
  831. function TPersistentObjectList.Remove(item: TObject): Integer;
  832. begin
  833. Result := IndexOf(item);
  834. if Result >= 0 then
  835. Delete(Result);
  836. end;
  837. procedure TPersistentObjectList.Pack;
  838. var
  839. i, j, n: Integer;
  840. p: PPointerObjectList;
  841. pk: PObject;
  842. begin
  843. p := List;
  844. n := Count - 1;
  845. while (n >= 0) and (p^[n] = nil) do
  846. Dec(n);
  847. for i := 0 to n do
  848. begin
  849. if p^[i] = nil then
  850. begin
  851. pk := @(p^[i]);
  852. for j := i + 1 to n do
  853. begin
  854. if p^[j] <> nil then
  855. begin
  856. pk^ := p^[j];
  857. Inc(pk);
  858. end;
  859. end;
  860. SetCount((Cardinal(pk) - Cardinal(p)) div SizeOf(TObject));
  861. Exit;
  862. end;
  863. end;
  864. SetCount(n + 1);
  865. end;
  866. procedure TPersistentObjectList.SetCapacity(newCapacity: Integer);
  867. begin
  868. if newCapacity <> FCapacity then
  869. begin
  870. if newCapacity < FCount then
  871. FCount := newCapacity;
  872. ReallocMem(FList, newCapacity * SizeOf(TObject));
  873. FCapacity := newCapacity;
  874. end;
  875. end;
  876. procedure TPersistentObjectList.RequiredCapacity(aCapacity: Integer);
  877. begin
  878. if FCapacity < aCapacity then
  879. SetCapacity(aCapacity);
  880. end;
  881. procedure TPersistentObjectList.SetCount(newCount: Integer);
  882. begin
  883. if newCount > FCapacity then
  884. SetCapacity(newCount);
  885. if newCount > FCount then
  886. FillChar(FList[FCount], (newCount - FCount) * SizeOf(TObject), 0);
  887. FCount := NewCount;
  888. end;
  889. procedure TPersistentObjectList.DeleteAndFree(index: Integer);
  890. var
  891. obj: TObject;
  892. begin
  893. obj := Get(index);
  894. Delete(index);
  895. obj.Free;
  896. end;
  897. procedure TPersistentObjectList.DeleteAndFreeItems(index: Integer; nbVals: Cardinal);
  898. var
  899. i, n: Integer;
  900. begin
  901. {$IFOPT R+}
  902. Assert(Cardinal(index) < Cardinal(FCount));
  903. {$ENDIF}
  904. n := index + Integer(nbVals);
  905. if n >= FCount then
  906. n := FCount - 1;
  907. for i := index to n do
  908. FList^[i].Free;
  909. DeleteItems(index, nbVals);
  910. end;
  911. function TPersistentObjectList.RemoveAndFree(item: TObject): Integer;
  912. begin
  913. Result := IndexOf(item);
  914. if Result >= 0 then
  915. begin
  916. Delete(Result);
  917. item.Free;
  918. end;
  919. end;
  920. procedure TPersistentObjectList.DoClean;
  921. var
  922. i: Integer;
  923. begin
  924. // a 'for' loop could crash if freeing an item removes other items form the list
  925. i := FCount - 1;
  926. while i >= 0 do
  927. begin
  928. if i < FCount then
  929. FList^[i].Free;
  930. Dec(i);
  931. end;
  932. end;
  933. procedure TPersistentObjectList.Clean;
  934. begin
  935. DoClean;
  936. Clear;
  937. end;
  938. procedure TPersistentObjectList.CleanFree;
  939. begin
  940. if Self <> nil then
  941. begin
  942. Clean;
  943. Destroy;
  944. end;
  945. end;
  946. procedure TPersistentObjectList.WriteToFiler(writer: TVirtualWriter);
  947. (*
  948. Object List Filer Format :
  949. Integer (Version)
  950. ListBegin
  951. ...[Object]...[Object]...
  952. ListEnd
  953. with [Object] being either (read vertically)
  954. Boolean (unused) String (ClassName) Integer (reference)
  955. Integer Object Data Object Data
  956. *)
  957. var
  958. i, objId: integer;
  959. objTypes: TList;
  960. aType: TClass;
  961. begin
  962. objTypes := TList.Create;
  963. try
  964. with writer do
  965. begin
  966. WriteInteger(0); // Archive Version 0 (uh... not exactly... but...)
  967. WriteListBegin;
  968. for i := 0 to FCount - 1 do
  969. begin
  970. if FList^[i] = nil then
  971. begin
  972. // store nil as... nil
  973. WriteBoolean(False);
  974. WriteInteger(0);
  975. end
  976. else if (FList^[i] is TPersistentObject) then
  977. begin
  978. // yeah, a TPersistentObject
  979. aType := FList^[i].ClassType;
  980. objId := objTypes.IndexOf(aType);
  981. if objId < 0 then
  982. begin
  983. // class is unknown
  984. objTypes.Add(aType);
  985. WriteString(aType.ClassName);
  986. end
  987. else
  988. begin
  989. // class already registered
  990. WriteInteger(objId);
  991. end;
  992. TPersistentObject(FList^[i]).WriteToFiler(writer);
  993. end
  994. else
  995. begin
  996. // Dunno that stuff here, store as is
  997. WriteBoolean(False);
  998. WriteInteger(Integer(FList^[i]));
  999. end;
  1000. end;
  1001. WriteListEnd;
  1002. end;
  1003. finally
  1004. objTypes.Free;
  1005. end;
  1006. end;
  1007. procedure TPersistentObjectList.ReadFromFilerWithEvent(reader: TVirtualReader; afterSenderObjectCreated: TNotifyEvent);
  1008. var
  1009. obj: TPersistentObject;
  1010. m: TPersistentObjectClass;
  1011. version: integer;
  1012. objTypes: TList;
  1013. begin
  1014. objTypes := TList.Create;
  1015. try
  1016. Clean;
  1017. with reader do
  1018. begin
  1019. version := ReadInteger;
  1020. if version = 0 then
  1021. begin
  1022. ReadListBegin;
  1023. while not EndOfList do
  1024. case Cardinal(NextValue) of
  1025. Cardinal(vaFalse), Cardinal(vaTrue):
  1026. begin
  1027. // stored 'as was' value
  1028. ReadBoolean; // ignored
  1029. Add(TObject(Cardinal(ReadInteger)));
  1030. end;
  1031. Cardinal(vaString), Cardinal(vaLString), Cardinal(vaWString),
  1032. Cardinal(vaInt64) + 1 { vaUTF8String }:
  1033. begin
  1034. // Unknown class, to be registered
  1035. m := TPersistentObjectClass(FindClass(ReadString));
  1036. objTypes.Add(m);
  1037. obj := m.Create;
  1038. if Assigned(afterSenderObjectCreated) then
  1039. afterSenderObjectCreated(obj);
  1040. obj.ReadFromFiler(reader);
  1041. Add(obj);
  1042. end;
  1043. Cardinal(vaInt8), Cardinal(vaInt16), Cardinal(vaInt32):
  1044. begin
  1045. // known class, direct retrieve
  1046. m := TPersistentObjectClass(objTypes[ReadInteger]);
  1047. obj := m.Create;
  1048. if Assigned(afterSenderObjectCreated) then
  1049. afterSenderObjectCreated(obj);
  1050. obj.ReadFromFiler(reader);
  1051. Add(obj);
  1052. end;
  1053. else
  1054. raise Exception.Create(strBrokenObjectListArchive);
  1055. end;
  1056. ReadListEnd;
  1057. end
  1058. else
  1059. RaiseFilerException(version);
  1060. end;
  1061. finally
  1062. objTypes.Free;
  1063. end;
  1064. end;
  1065. procedure TPersistentObjectList.ReadFromFiler(reader: TVirtualReader);
  1066. begin
  1067. ReadFromFilerWithEvent(reader, AfterObjectCreatedByReader);
  1068. end;
  1069. procedure TPersistentObjectList.AfterObjectCreatedByReader(Sender: TObject);
  1070. begin
  1071. // nothing
  1072. end;
  1073. procedure TPersistentObjectList.Push(item: TObject);
  1074. begin
  1075. Add(item);
  1076. end;
  1077. function TPersistentObjectList.Pop: TObject;
  1078. begin
  1079. if FCount > 0 then
  1080. begin
  1081. Result := FList^[FCount - 1];
  1082. Dec(FCount);
  1083. end
  1084. else
  1085. Result := nil;
  1086. end;
  1087. procedure TPersistentObjectList.PopAndFree;
  1088. begin
  1089. Pop.Free;
  1090. end;
  1091. procedure POListQuickSort(SortList: PPointerObjectList; L, R: Integer;
  1092. compareFunc: TObjectListSortCompare);
  1093. var
  1094. I, J: Integer;
  1095. P, T: TObject;
  1096. begin
  1097. repeat
  1098. I := L;
  1099. J := R;
  1100. P := SortList^[(L + R) shr 1];
  1101. repeat
  1102. while compareFunc(SortList^[I], P) < 0 do
  1103. Inc(I);
  1104. while compareFunc(SortList^[J], P) > 0 do
  1105. Dec(J);
  1106. if I <= J then
  1107. begin
  1108. T := SortList^[I];
  1109. SortList^[I] := SortList^[J];
  1110. SortList^[J] := T;
  1111. Inc(I);
  1112. Dec(J);
  1113. end;
  1114. until I > J;
  1115. if L < J then
  1116. POListQuickSort(SortList, L, J, compareFunc);
  1117. L := I;
  1118. until I >= R;
  1119. end;
  1120. procedure TPersistentObjectList.Sort(compareFunc: TObjectListSortCompare);
  1121. begin
  1122. if Count > 1 then
  1123. POListQuickSort(FList, 0, Count - 1, compareFunc);
  1124. end;
  1125. // ------------------
  1126. // ------------------ TGLBinaryReader ------------------
  1127. // ------------------
  1128. procedure TGLBinaryReader.Read(var Buf; Count: Longint);
  1129. begin
  1130. FStream.Read(Buf, Count);
  1131. end;
  1132. function TGLBinaryReader.ReadValue: TValueType;
  1133. var
  1134. b: byte;
  1135. begin
  1136. Read(b, 1);
  1137. Result := TValueType(b);
  1138. end;
  1139. function TGLBinaryReader.NextValue: TValueType;
  1140. var
  1141. pos: Int64;
  1142. begin
  1143. pos := FStream.Position;
  1144. Result := ReadValue;
  1145. FStream.Position := pos;
  1146. end;
  1147. function TGLBinaryReader.ReadInteger: Integer;
  1148. var
  1149. tempShort: ShortInt;
  1150. tempSmallInt: SmallInt;
  1151. begin
  1152. case ReadValue of
  1153. vaInt8:
  1154. begin
  1155. Read(tempShort, 1);
  1156. Result := tempShort;
  1157. end;
  1158. vaInt16:
  1159. begin
  1160. Read(tempSmallInt, 2);
  1161. Result := tempSmallInt;
  1162. end;
  1163. vaInt32: Read(Result, 4);
  1164. else
  1165. Result := 0;
  1166. ReadTypeError;
  1167. end;
  1168. end;
  1169. function TGLBinaryReader.ReadBoolean: Boolean;
  1170. begin
  1171. case ReadValue of
  1172. vaTrue: Result := True;
  1173. vaFalse: Result := False;
  1174. else
  1175. ReadTypeError;
  1176. Result := False;
  1177. end;
  1178. end;
  1179. function TGLBinaryReader.ReadString: string;
  1180. var
  1181. n: Cardinal;
  1182. vType: TValueType;
  1183. tempString: AnsiString;
  1184. begin
  1185. n := 0;
  1186. vType := ReadValue;
  1187. case Cardinal(vType) of
  1188. Cardinal(vaWString),
  1189. Cardinal(vaInt64) + 1:
  1190. begin // vaUTF8String
  1191. Result := ReadWideString(vType);
  1192. Exit;
  1193. end;
  1194. Cardinal(vaString): Read(n, 1);
  1195. Cardinal(vaLString): Read(n, 4);
  1196. else
  1197. ReadTypeError;
  1198. end;
  1199. SetLength(tempString, n);
  1200. if n > 0 then
  1201. Read(tempString[1], n);
  1202. Result := string(tempString);
  1203. end;
  1204. function TGLBinaryReader.ReadWideString(vType: TValueType): WideString;
  1205. var
  1206. n: Cardinal;
  1207. utf8buf: AnsiString;
  1208. begin
  1209. Read(n, 4);
  1210. case Cardinal(vType) of
  1211. Cardinal(vaWString):
  1212. begin
  1213. SetLength(Result, n);
  1214. if n > 0 then
  1215. Read(Result[1], n * 2);
  1216. end;
  1217. Cardinal(vaInt64) + 1:
  1218. begin // vaUTF8String
  1219. SetLength(utf8buf, n);
  1220. if n > 0 then
  1221. begin
  1222. Read(utf8buf[1], n);
  1223. Result := UTF8ToWideString(utf8buf);
  1224. end;
  1225. end;
  1226. else
  1227. ReadTypeError;
  1228. end;
  1229. end;
  1230. function TGLBinaryReader.ReadFloat: Extended;
  1231. {$IFDEF WIN64}
  1232. var
  1233. C :TExtended80Rec; // Temporary variable to store 10 bytes floating point number in a Win64 application
  1234. {$ENDIF}
  1235. begin
  1236. Result := 0.0;
  1237. {$IFDEF WIN64}
  1238. if ReadValue = vaExtended then
  1239. begin
  1240. Read(C, SizeOf(C)); // Load value into the temp variable
  1241. Result := Extended(C); // Typecast into an Extended: in a win64 application is a Double
  1242. end
  1243. else
  1244. ReadTypeError;
  1245. {$ELSE}
  1246. if ReadValue = vaExtended then
  1247. Read(Result, SizeOf(Result))
  1248. else
  1249. ReadTypeError;
  1250. {$ENDIF}
  1251. end;
  1252. procedure TGLBinaryReader.ReadListBegin;
  1253. begin
  1254. if ReadValue <> vaList then
  1255. ReadTypeError;
  1256. end;
  1257. procedure TGLBinaryReader.ReadListEnd;
  1258. begin
  1259. if ReadValue <> vaNull then
  1260. ReadTypeError;
  1261. end;
  1262. function TGLBinaryReader.EndOfList: Boolean;
  1263. begin
  1264. Result := (NextValue = vaNull);
  1265. end;
  1266. // ------------------
  1267. // ------------------ TGLBinaryWriter ------------------
  1268. // ------------------
  1269. procedure TGLBinaryWriter.Write(const Buf; Count: Longint);
  1270. begin
  1271. FStream.Write(Buf, Count);
  1272. end;
  1273. procedure TGLBinaryWriter.WriteInteger(anInteger: Integer);
  1274. type
  1275. TIntStruct = packed record
  1276. typ: byte;
  1277. val: Integer;
  1278. end;
  1279. var
  1280. ins: TIntStruct;
  1281. begin
  1282. ins.val := anInteger;
  1283. if (anInteger >= Low(ShortInt)) and (anInteger <= High(ShortInt)) then
  1284. begin
  1285. ins.typ := byte(vaInt8);
  1286. Write(ins, 2);
  1287. end
  1288. else if (anInteger >= Low(SmallInt)) and (anInteger <= High(SmallInt)) then
  1289. begin
  1290. ins.typ := byte(vaInt16);
  1291. Write(ins, 3);
  1292. end
  1293. else
  1294. begin
  1295. ins.typ := byte(vaInt32);
  1296. Write(ins, 5);
  1297. end;
  1298. end;
  1299. procedure TGLBinaryWriter.WriteBoolean(aBoolean: Boolean);
  1300. const
  1301. cBoolToType: array[False..True] of byte = (byte(vaFalse), byte(vaTrue));
  1302. begin
  1303. Write(cBoolToType[aBoolean], 1);
  1304. end;
  1305. procedure TGLBinaryWriter.WriteAnsiString(const aString: AnsiString);
  1306. type
  1307. TStringHeader = packed record
  1308. typ: Byte;
  1309. length: Integer;
  1310. end;
  1311. var
  1312. sh: TStringHeader;
  1313. begin
  1314. sh.Length := Length(aString);
  1315. if sh.Length <= 255 then
  1316. begin
  1317. sh.typ := byte(vaString);
  1318. Write(sh, 2);
  1319. if sh.Length > 0 then
  1320. Write(aString[1], sh.Length);
  1321. end
  1322. else
  1323. begin
  1324. sh.typ := byte(vaLString);
  1325. Write(sh, 5);
  1326. Write(aString[1], sh.Length);
  1327. end;
  1328. end;
  1329. procedure TGLBinaryWriter.WriteWideString(const aString: WideString);
  1330. type
  1331. TStringHeader = packed record
  1332. typ: Byte;
  1333. length: Integer;
  1334. end;
  1335. var
  1336. sh: TStringHeader;
  1337. begin
  1338. sh.Length := Length(aString);
  1339. sh.typ := byte(vaWString);
  1340. Write(sh, 5);
  1341. if sh.Length > 0 then
  1342. Write(aString[1], sh.length * SizeOf(WideChar));
  1343. end;
  1344. procedure TGLBinaryWriter.WriteString(const aString: string);
  1345. begin
  1346. {$IFDEF UNICODE}
  1347. // TODO: should really check if the string can be simplified to: vaString / vaLString / vaUTF8String
  1348. WriteWideString(aString);
  1349. {$ELSE}
  1350. WriteAnsiString(aString);
  1351. {$ENDIF}
  1352. end;
  1353. procedure TGLBinaryWriter.WriteFloat(const aFloat: Extended);
  1354. type
  1355. TExtendedStruct = packed record
  1356. typ: Byte;
  1357. {$IFDEF WIN64}
  1358. val :TExtended80Rec; // Structure to handle a 10 bytes floating point value
  1359. {$ELSE}
  1360. val :Extended;
  1361. {$ENDIF}
  1362. end;
  1363. var
  1364. str: TExtendedStruct;
  1365. begin
  1366. {$IFDEF WIN64}
  1367. str.typ := byte(vaExtended);
  1368. str.val := TExtended80Rec(aFloat); // Typecast the float value (in a Win64 app the type is a Double) into the 10 bytes struct
  1369. Write(str, SizeOf(str));
  1370. {$ELSE}
  1371. str.typ := byte(vaExtended);
  1372. str.val := aFloat;
  1373. Write(str, SizeOf(str));
  1374. {$ENDIF}
  1375. end;
  1376. procedure TGLBinaryWriter.WriteListBegin;
  1377. const
  1378. buf: byte = byte(vaList);
  1379. begin
  1380. Write(buf, 1);
  1381. end;
  1382. procedure TGLBinaryWriter.WriteListEnd;
  1383. const
  1384. buf: byte = byte(vaNull);
  1385. begin
  1386. Write(buf, 1);
  1387. end;
  1388. // ------------------
  1389. // ------------------ TGLTextReader ------------------
  1390. // ------------------
  1391. procedure TGLTextReader.ReadLine(const requestedType: string = '');
  1392. var
  1393. line: string;
  1394. c: Byte;
  1395. p: Integer;
  1396. begin
  1397. // will need speed upgrade, someday...
  1398. line := '';
  1399. repeat
  1400. Stream.Read(c, 1);
  1401. if c >= 32 then
  1402. line := line + chr(c);
  1403. until c = 10;
  1404. line := Trim(line);
  1405. p := Pos(' ', line);
  1406. if p > 0 then
  1407. begin
  1408. FValueType := Copy(line, 1, p - 1);
  1409. FData := Trim(Copy(line, p + 1, MaxInt));
  1410. end
  1411. else
  1412. begin
  1413. FValueType := line;
  1414. FData := '';
  1415. end;
  1416. if requestedType <> '' then
  1417. if requestedType <> FValueType then
  1418. raise EFilerException.Create('Invalid type, expected "'
  1419. + requestedType + '", found "FValueType".');
  1420. end;
  1421. procedure TGLTextReader.Read(var Buf; Count: Longint);
  1422. function HexCharToInt(const c: Char): Integer;
  1423. begin
  1424. if c <= '9' then
  1425. Result := Integer(c) - Integer('0')
  1426. else if c < 'a' then
  1427. Result := Integer(c) - Integer('A') + 10
  1428. else
  1429. Result := Integer(c) - Integer('a') + 10;
  1430. end;
  1431. var
  1432. i, j: Integer;
  1433. begin
  1434. ReadLine(cVTRaw);
  1435. j := 1;
  1436. for i := 0 to Count - 1 do
  1437. begin
  1438. PAnsiChar(@Buf)[i] := AnsiChar((HexCharToInt(FData[j]) shl 4)
  1439. + HexCharToInt(FData[j + 1]));
  1440. Inc(j, 2);
  1441. end;
  1442. end;
  1443. function TGLTextReader.NextValue: TValueType;
  1444. var
  1445. p: Int64;
  1446. begin
  1447. p := Stream.Position;
  1448. ReadLine;
  1449. if FValueType = cVTInteger then
  1450. Result := vaInt32
  1451. else if FValueType = cVTFloat then
  1452. Result := vaExtended
  1453. else if FValueType = cVTString then
  1454. Result := vaString
  1455. else if FValueType = cVTBoolean then
  1456. if FData = cTrue then
  1457. Result := vaTrue
  1458. else
  1459. Result := vaFalse
  1460. else if FValueType = cVTRaw then
  1461. Result := vaBinary
  1462. else if FValueType = cVTListBegin then
  1463. Result := vaList
  1464. else
  1465. Result := vaNULL;
  1466. Stream.Position := p;
  1467. end;
  1468. function TGLTextReader.ReadInteger: Integer;
  1469. begin
  1470. ReadLine(cVTInteger);
  1471. Result := StrToInt(FData);
  1472. end;
  1473. function TGLTextReader.ReadBoolean: Boolean;
  1474. begin
  1475. ReadLine(cVTBoolean);
  1476. Result := (FData = cTrue);
  1477. end;
  1478. function TGLTextReader.ReadString: string;
  1479. var
  1480. i: Integer;
  1481. begin
  1482. ReadLine(cVTString);
  1483. Result := '';
  1484. i := 1;
  1485. while i < Length(FData) do
  1486. begin
  1487. if FData[i] = '#' then
  1488. begin
  1489. Result := Result + Char(StrToInt(Copy(FData, i + 1, 3)));
  1490. Inc(i, 3);
  1491. end
  1492. else
  1493. Result := Result + FData[i];
  1494. Inc(i);
  1495. end;
  1496. Assert(FData[i] = '.', 'Invalid stored string.');
  1497. end;
  1498. function TGLTextReader.ReadFloat: Extended;
  1499. var
  1500. oldDc: Char;
  1501. begin
  1502. ReadLine(cVTInteger);
  1503. oldDc := FormatSettings.DecimalSeparator;
  1504. FormatSettings.DecimalSeparator := '.';
  1505. Result := StrToFloat(FData);
  1506. FormatSettings.DecimalSeparator := oldDc;
  1507. end;
  1508. procedure TGLTextReader.ReadListBegin;
  1509. begin
  1510. ReadLine(cVTListBegin);
  1511. end;
  1512. procedure TGLTextReader.ReadListEnd;
  1513. begin
  1514. ReadLine(cVTListEnd);
  1515. end;
  1516. function TGLTextReader.EndOfList: Boolean;
  1517. var
  1518. p: Int64;
  1519. begin
  1520. p := Stream.Position;
  1521. ReadLine;
  1522. Result := (FValueType = cVTListEnd);
  1523. Stream.Position := p;
  1524. end;
  1525. // ------------------
  1526. // ------------------ TGLTextWriter ------------------
  1527. // ------------------
  1528. constructor TGLTextWriter.Create(aStream: TStream);
  1529. begin
  1530. inherited;
  1531. end;
  1532. destructor TGLTextWriter.Destroy;
  1533. begin
  1534. inherited;
  1535. end;
  1536. procedure TGLTextWriter.WriteLine(const valueType, data: string);
  1537. var
  1538. buf: AnsiString;
  1539. begin
  1540. buf := StringOfChar(AnsiChar(#32), FIndentLevel);
  1541. buf := buf + AnsiString(valueType + ' ' + data) + #13#10;
  1542. Stream.Write(buf[1], Length(buf));
  1543. end;
  1544. procedure TGLTextWriter.Write(const Buf; Count: Longint);
  1545. const
  1546. cNibbleToHex: PChar = '0123456789ABCDEF';
  1547. var
  1548. i, j, b: Integer;
  1549. data: string;
  1550. begin
  1551. SetLength(data, Count * 2);
  1552. j := 1;
  1553. for i := 0 to Count - 1 do
  1554. begin
  1555. b := Integer(PAnsiChar(@buf)[i]);
  1556. data[j] := cNibbleToHex[b shr 4];
  1557. data[j + 1] := cNibbleToHex[b and 15];
  1558. Inc(j, 2);
  1559. end;
  1560. WriteLine(cVTRaw, data);
  1561. end;
  1562. procedure TGLTextWriter.WriteInteger(anInteger: Integer);
  1563. begin
  1564. WriteLine(cVTInteger, IntToStr(anInteger));
  1565. end;
  1566. procedure TGLTextWriter.WriteBoolean(aBoolean: Boolean);
  1567. begin
  1568. if aBoolean then
  1569. WriteLine(cVTBoolean, cTrue)
  1570. else
  1571. WriteLine(cVTBoolean, cFalse);
  1572. end;
  1573. procedure TGLTextWriter.WriteString(const aString: string);
  1574. var
  1575. i: Integer;
  1576. s: string;
  1577. begin
  1578. s := '';
  1579. for i := 1 to Length(aString) do
  1580. if aString[i] >= #32 then
  1581. s := s + aString[i]
  1582. else
  1583. s := s + Format('#%.3d', [Integer(aString[i])]);
  1584. WriteLine(cVTString, s + '.');
  1585. end;
  1586. procedure TGLTextWriter.WriteFloat(const aFloat: Extended);
  1587. begin
  1588. WriteLine(cVTInteger, FloatToStr(aFloat));
  1589. end;
  1590. procedure TGLTextWriter.WriteListBegin;
  1591. begin
  1592. WriteLine(cVTListBegin, '');
  1593. Inc(FIndentLevel, 3);
  1594. end;
  1595. procedure TGLTextWriter.WriteListEnd;
  1596. begin
  1597. Dec(FIndentLevel, 3);
  1598. WriteLine(cVTListEnd, '');
  1599. end;
  1600. // ------------------
  1601. // ------------------ TGLOwnedPersistent ------------------
  1602. // ------------------
  1603. constructor TGLOwnedPersistent.Create(AOwner: TPersistent);
  1604. begin
  1605. FOwner := AOwner;
  1606. end;
  1607. function TGLOwnedPersistent.GetOwner: TPersistent;
  1608. begin
  1609. Result := FOwner;
  1610. end;
  1611. // ------------------
  1612. // ------------------ TGLInterfacedPersistent ------------------
  1613. // ------------------
  1614. function TGLInterfacedPersistent._AddRef: Integer; stdcall;
  1615. begin
  1616. Result := -1; //ignore
  1617. end;
  1618. function TGLInterfacedPersistent._Release: Integer; stdcall;
  1619. begin
  1620. Result := -1; //ignore
  1621. end;
  1622. function TGLInterfacedPersistent.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  1623. begin
  1624. if GetInterface(IID, Obj) then
  1625. Result := S_OK
  1626. else
  1627. Result := E_NOINTERFACE;
  1628. end;
  1629. // ------------------
  1630. // ------------------ TGLInterfacedCollectionItem ------------------
  1631. // ------------------
  1632. function TGLInterfacedCollectionItem._AddRef: Integer; stdcall;
  1633. begin
  1634. Result := -1; //ignore
  1635. end;
  1636. function TGLInterfacedCollectionItem._Release: Integer; stdcall;
  1637. begin
  1638. Result := -1; //ignore
  1639. end;
  1640. function TGLInterfacedCollectionItem.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  1641. begin
  1642. if GetInterface(IID, Obj) then
  1643. Result := S_OK
  1644. else
  1645. Result := E_NOINTERFACE;
  1646. end;
  1647. // ------------------------------------------------------------------
  1648. initialization
  1649. // ------------------------------------------------------------------
  1650. RegisterClass(TPersistentObjectList);
  1651. end.