objects.pp 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1997-98 by the Free Pascal development team.
  5. Objects.pas clone for Free Pascal
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {************[ SOURCE FILE OF FREE VISION ]****************}
  13. { }
  14. { System independent clone of objects.pas }
  15. { }
  16. { Interface Copyright (c) 1992 Borland International }
  17. { }
  18. { Parts Copyright (c) 1992,96 by Florian Klaempfl }
  19. { [email protected] }
  20. { }
  21. { Parts Copyright (c) 1996 by Frank ZAGO }
  22. { [email protected] }
  23. { }
  24. { Parts Copyright (c) 1995 by MH Spiegel }
  25. { }
  26. { Parts Copyright (c) 1996, 1997 by Leon de Boer }
  27. { [email protected] }
  28. { }
  29. { Free Vision project coordinator Balazs Scheidler }
  30. { [email protected] }
  31. { }
  32. UNIT Objects;
  33. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  34. INTERFACE
  35. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  36. {==== Select assembler ==============================================}
  37. {$IFDEF CPU86}
  38. {$ASMMODE ATT}
  39. {$ENDIF}
  40. {$IFDEF CPU68}
  41. {$ASMMODE MOT}
  42. {$ENDIF}
  43. {==== Compiler directives ===========================================}
  44. {$H-} { No ansistrings }
  45. {$E+} { Emulation is on }
  46. {$X+} { Extended syntax is ok }
  47. {$R-} { Disable range checking }
  48. {$ifndef Linux}
  49. {$S-} { Disable Stack Checking }
  50. {$endif}
  51. {$I-} { Disable IO Checking }
  52. {$Q-} { Disable Overflow Checking }
  53. {$V-} { Turn off strict VAR strings }
  54. {====================================================================}
  55. {***************************************************************************}
  56. { PUBLIC CONSTANTS }
  57. {***************************************************************************}
  58. {---------------------------------------------------------------------------}
  59. { STREAM ERROR STATE MASKS }
  60. {---------------------------------------------------------------------------}
  61. CONST
  62. stOk = 0; { No stream error }
  63. stError = -1; { Access error }
  64. stInitError = -2; { Initialize error }
  65. stReadError = -3; { Stream read error }
  66. stWriteError = -4; { Stream write error }
  67. stGetError = -5; { Get object error }
  68. stPutError = -6; { Put object error }
  69. stSeekError = -7; { Seek error in stream }
  70. stOpenError = -8; { Error opening stream }
  71. {---------------------------------------------------------------------------}
  72. { STREAM ACCESS MODE CONSTANTS }
  73. {---------------------------------------------------------------------------}
  74. CONST
  75. stCreate = $3C00; { Create new file }
  76. stOpenRead = $3D00; { Read access only }
  77. stOpenWrite = $3D01; { Write access only }
  78. stOpen = $3D02; { Read/write access }
  79. {---------------------------------------------------------------------------}
  80. { TCollection ERROR CODES }
  81. {---------------------------------------------------------------------------}
  82. CONST
  83. coIndexError = -1; { Index out of range }
  84. coOverflow = -2; { Overflow }
  85. {---------------------------------------------------------------------------}
  86. { VMT HEADER CONSTANT - HOPEFULLY WE CAN DROP THIS LATER }
  87. {---------------------------------------------------------------------------}
  88. CONST
  89. vmtHeaderSize = 8; { VMT header size }
  90. CONST
  91. {---------------------------------------------------------------------------}
  92. { MAXIUM DATA SIZES }
  93. {---------------------------------------------------------------------------}
  94. {$IFDEF FPC}
  95. MaxBytes = 128*1024*1024; { Maximum data size }
  96. {$ELSE}
  97. MaxBytes = 16384;
  98. {$ENDIF}
  99. MaxWords = MaxBytes DIV SizeOf(Word); { Max word data size }
  100. MaxPtrs = MaxBytes DIV SizeOf(Pointer); { Max ptr data size }
  101. MaxCollectionSize = MaxBytes DIV SizeOf(Pointer); { Max collection size }
  102. {***************************************************************************}
  103. { PUBLIC TYPE DEFINITIONS }
  104. {***************************************************************************}
  105. {---------------------------------------------------------------------------}
  106. { CHARACTER SET }
  107. {---------------------------------------------------------------------------}
  108. TYPE
  109. TCharSet = SET Of Char; { Character set }
  110. PCharSet = ^TCharSet; { Character set ptr }
  111. {---------------------------------------------------------------------------}
  112. { GENERAL ARRAYS }
  113. {---------------------------------------------------------------------------}
  114. TYPE
  115. TByteArray = ARRAY [0..MaxBytes-1] Of Byte; { Byte array }
  116. PByteArray = ^TByteArray; { Byte array pointer }
  117. TWordArray = ARRAY [0..MaxWords-1] Of Word; { Word array }
  118. PWordArray = ^TWordArray; { Word array pointer }
  119. TPointerArray = Array [0..MaxPtrs-1] Of Pointer; { Pointer array }
  120. PPointerArray = ^TPointerArray; { Pointer array ptr }
  121. {---------------------------------------------------------------------------}
  122. { POINTER TO STRING }
  123. {---------------------------------------------------------------------------}
  124. TYPE
  125. PString = ^String; { String pointer }
  126. {---------------------------------------------------------------------------}
  127. { OS dependent File type / consts }
  128. {---------------------------------------------------------------------------}
  129. {$IFDEF GO32V1}
  130. type
  131. FNameStr = String[79];
  132. THandle = Integer;
  133. const
  134. MaxReadBytes = $fffe;
  135. invalidhandle = -1;
  136. {$ENDIF}
  137. {$IFDEF GO32V2}
  138. type
  139. FNameStr = String;
  140. THandle = Integer;
  141. const
  142. MaxReadBytes = $fffe;
  143. invalidhandle = -1;
  144. {$ENDIF}
  145. {$IFDEF Win32}
  146. type
  147. FNameStr = String;
  148. THandle = Longint;
  149. const
  150. MaxReadBytes = $fffe;
  151. invalidhandle = -1;
  152. {$ENDIF}
  153. {$IFDEF OS2}
  154. type
  155. FNameStr = String;
  156. THandle = Word;
  157. const
  158. MaxReadBytes = $7fffffff;
  159. invalidhandle = $ffff;
  160. {$ENDIF}
  161. {$IFDEF LINUX}
  162. type
  163. FNameStr = String;
  164. { values are words, though the OS calls return 32-bit values }
  165. { to check (CEC) }
  166. THandle = Longint;
  167. const
  168. MaxReadBytes = $7fffffff;
  169. invalidhandle = -1;
  170. {$ENDIF}
  171. {$IFDEF AMIGA}
  172. type
  173. FNameStr = String;
  174. THandle = Longint;
  175. const
  176. MaxReadBytes = $fffe;
  177. invalidhandle = -1;
  178. {$ENDIF}
  179. {$IFDEF ATARI}
  180. type
  181. FNameStr = String[79];
  182. THandle = Integer;
  183. const
  184. MaxReadBytes = $fffe;
  185. invalidhandle = -1;
  186. {$ENDIF}
  187. {$IFDEF MAC}
  188. type
  189. FNameStr = String;
  190. THandle = Integer;
  191. const
  192. MaxReadBytes = $fffe;
  193. invalidhandle = -1;
  194. {$ENDIF}
  195. {---------------------------------------------------------------------------}
  196. { DOS ASCIIZ FILENAME }
  197. {---------------------------------------------------------------------------}
  198. TYPE
  199. AsciiZ = Array [0..255] Of Char; { Filename array }
  200. {---------------------------------------------------------------------------}
  201. { BIT SWITCHED TYPE CONSTANTS }
  202. {---------------------------------------------------------------------------}
  203. TYPE
  204. Sw_Word = Cardinal; { Long Word now }
  205. Sw_Integer = LongInt; { Long integer now }
  206. {***************************************************************************}
  207. { PUBLIC RECORD DEFINITIONS }
  208. {***************************************************************************}
  209. {---------------------------------------------------------------------------}
  210. { TYPE CONVERSION RECORDS }
  211. {---------------------------------------------------------------------------}
  212. TYPE
  213. WordRec = packed RECORD
  214. Lo, Hi: Byte; { Word to bytes }
  215. END;
  216. LongRec = packed RECORD
  217. Lo, Hi: Word; { LongInt to words }
  218. END;
  219. PtrRec = packed RECORD
  220. Ofs, Seg: Word; { Pointer to words }
  221. END;
  222. {---------------------------------------------------------------------------}
  223. { TStreamRec RECORD - STREAM OBJECT RECORD }
  224. {---------------------------------------------------------------------------}
  225. TYPE
  226. PStreamRec = ^TStreamRec; { Stream record ptr }
  227. TStreamRec = Packed RECORD
  228. ObjType: Sw_Word; { Object type id }
  229. VmtLink: pointer; { VMT link }
  230. Load : Pointer; { Object load code }
  231. Store: Pointer; { Object store code }
  232. Next : PStreamRec; { Next stream record }
  233. END;
  234. {***************************************************************************}
  235. { PUBLIC OBJECT DEFINITIONS }
  236. {***************************************************************************}
  237. {---------------------------------------------------------------------------}
  238. { TPoint OBJECT - POINT OBJECT }
  239. {---------------------------------------------------------------------------}
  240. TYPE
  241. PPoint = ^TPoint;
  242. TPoint = OBJECT
  243. X, Y: Sw_Integer;
  244. END;
  245. {---------------------------------------------------------------------------}
  246. { TRect OBJECT - RECTANGLE OBJECT }
  247. {---------------------------------------------------------------------------}
  248. TRect = OBJECT
  249. A, B: TPoint; { Corner points }
  250. FUNCTION Empty: Boolean;
  251. FUNCTION Equals (R: TRect): Boolean;
  252. FUNCTION Contains (P: TPoint): Boolean;
  253. PROCEDURE Copy (R: TRect);
  254. PROCEDURE Union (R: TRect);
  255. PROCEDURE Intersect (R: TRect);
  256. PROCEDURE Move (ADX, ADY: Sw_Integer);
  257. PROCEDURE Grow (ADX, ADY: Sw_Integer);
  258. PROCEDURE Assign (XA, YA, XB, YB: Sw_Integer);
  259. END;
  260. {---------------------------------------------------------------------------}
  261. { TObject OBJECT - BASE ANCESTOR OBJECT }
  262. {---------------------------------------------------------------------------}
  263. TYPE
  264. TObject = OBJECT
  265. CONSTRUCTOR Init;
  266. PROCEDURE Free;
  267. DESTRUCTOR Done; Virtual;
  268. END;
  269. PObject = ^TObject;
  270. { ******************************* REMARK ****************************** }
  271. { Two new virtual methods have been added to the object in the form of }
  272. { Close and Open. The main use here is in the Disk Based Descendants }
  273. { the calls open and close the given file so these objects can be }
  274. { used like standard files. Two new fields have also been added to }
  275. { speed up seeks on descendants. All existing code will compile and }
  276. { work completely normally oblivious to these new methods and fields. }
  277. { ****************************** END REMARK *** Leon de Boer, 15May96 * }
  278. {---------------------------------------------------------------------------}
  279. { TStream OBJECT - STREAM ANCESTOR OBJECT }
  280. {---------------------------------------------------------------------------}
  281. TYPE
  282. TStream = OBJECT (TObject)
  283. Status : Integer; { Stream status }
  284. ErrorInfo : Integer; { Stream error info }
  285. StreamSize: LongInt; { Stream current size }
  286. Position : LongInt; { Current position }
  287. FUNCTION Get: PObject;
  288. FUNCTION StrRead: PChar;
  289. FUNCTION GetPos: Longint; Virtual;
  290. FUNCTION GetSize: Longint; Virtual;
  291. FUNCTION ReadStr: PString;
  292. PROCEDURE Open (OpenMode: Word); Virtual;
  293. PROCEDURE Close; Virtual;
  294. PROCEDURE Reset;
  295. PROCEDURE Flush; Virtual;
  296. PROCEDURE Truncate; Virtual;
  297. PROCEDURE Put (P: PObject);
  298. PROCEDURE StrWrite (P: PChar);
  299. PROCEDURE WriteStr (P: PString);
  300. PROCEDURE Seek (Pos: LongInt); Virtual;
  301. PROCEDURE Error (Code, Info: Integer); Virtual;
  302. PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
  303. PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
  304. PROCEDURE CopyFrom (Var S: TStream; Count: Longint);
  305. END;
  306. PStream = ^TStream;
  307. { ******************************* REMARK ****************************** }
  308. { A few minor changes to this object and an extra field added called }
  309. { FName which holds an AsciiZ array of the filename this allows the }
  310. { streams file to be opened and closed like a normal text file. All }
  311. { existing code should work without any changes. }
  312. { ****************************** END REMARK *** Leon de Boer, 19May96 * }
  313. {---------------------------------------------------------------------------}
  314. { TDosStream OBJECT - DOS FILE STREAM OBJECT }
  315. {---------------------------------------------------------------------------}
  316. TYPE
  317. TDosStream = OBJECT (TStream)
  318. Handle: THandle; { DOS file handle }
  319. FName : AsciiZ; { AsciiZ filename }
  320. CONSTRUCTOR Init (FileName: FNameStr; Mode: Word);
  321. DESTRUCTOR Done; Virtual;
  322. PROCEDURE Close; Virtual;
  323. PROCEDURE Truncate; Virtual;
  324. PROCEDURE Seek (Pos: LongInt); Virtual;
  325. PROCEDURE Open (OpenMode: Word); Virtual;
  326. PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
  327. PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
  328. END;
  329. PDosStream = ^TDosStream;
  330. { ******************************* REMARK ****************************** }
  331. { A few minor changes to this object and an extra field added called }
  332. { lastmode which holds the read or write condition last using the }
  333. { speed up buffer which helps speed up the flush, position and size }
  334. { functions. All existing code should work without any changes. }
  335. { ****************************** END REMARK *** Leon de Boer, 19May96 * }
  336. {---------------------------------------------------------------------------}
  337. { TBufStream OBJECT - BUFFERED DOS FILE STREAM }
  338. {---------------------------------------------------------------------------}
  339. TYPE
  340. TBufStream = OBJECT (TDosStream)
  341. LastMode: Byte; { Last buffer mode }
  342. BufSize : Sw_Word; { Buffer size }
  343. BufPtr : Sw_Word; { Buffer start }
  344. BufEnd : Sw_Word; { Buffer end }
  345. Buffer : PByteArray; { Buffer allocated }
  346. CONSTRUCTOR Init (FileName: FNameStr; Mode, Size: Word);
  347. DESTRUCTOR Done; Virtual;
  348. PROCEDURE Close; Virtual;
  349. PROCEDURE Flush; Virtual;
  350. PROCEDURE Truncate; Virtual;
  351. PROCEDURE Seek (Pos: LongInt); Virtual;
  352. PROCEDURE Open (OpenMode: Word); Virtual;
  353. PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
  354. PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
  355. END;
  356. PBufStream = ^TBufStream;
  357. { ******************************* REMARK ****************************** }
  358. { All the changes here should be completely transparent to existing }
  359. { code. Basically the memory blocks do not have to be base segments }
  360. { but this means our list becomes memory blocks rather than segments. }
  361. { The stream will also expand like the other standard streams!! }
  362. { ****************************** END REMARK *** Leon de Boer, 19May96 * }
  363. {---------------------------------------------------------------------------}
  364. { TMemoryStream OBJECT - MEMORY STREAM OBJECT }
  365. {---------------------------------------------------------------------------}
  366. TYPE
  367. TMemoryStream = OBJECT (TStream)
  368. BlkCount: Sw_Word; { Number of segments }
  369. BlkSize : Word; { Memory block size }
  370. MemSize : LongInt; { Memory alloc size }
  371. BlkList : PPointerArray; { Memory block list }
  372. CONSTRUCTOR Init (ALimit: Longint; ABlockSize: Word);
  373. DESTRUCTOR Done; Virtual;
  374. PROCEDURE Truncate; Virtual;
  375. PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
  376. PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
  377. PRIVATE
  378. FUNCTION ChangeListSize (ALimit: Sw_Word): Boolean;
  379. END;
  380. PMemoryStream = ^TMemoryStream;
  381. TYPE
  382. TItemList = Array [0..MaxCollectionSize - 1] Of Pointer;
  383. PItemList = ^TItemList;
  384. { ******************************* REMARK ****************************** }
  385. { The changes here look worse than they are. The Sw_Integer simply }
  386. { switches between Integers and LongInts if switched between 16 and 32 }
  387. { bit code. All existing code will compile without any changes. }
  388. { ****************************** END REMARK *** Leon de Boer, 10May96 * }
  389. {---------------------------------------------------------------------------}
  390. { TCollection OBJECT - COLLECTION ANCESTOR OBJECT }
  391. {---------------------------------------------------------------------------}
  392. TCollection = OBJECT (TObject)
  393. Items: PItemList; { Item list pointer }
  394. Count: Sw_Integer; { Item count }
  395. Limit: Sw_Integer; { Item limit count }
  396. Delta: Sw_Integer; { Inc delta size }
  397. CONSTRUCTOR Init (ALimit, ADelta: Sw_Integer);
  398. CONSTRUCTOR Load (Var S: TStream);
  399. DESTRUCTOR Done; Virtual;
  400. FUNCTION At (Index: Sw_Integer): Pointer;
  401. FUNCTION IndexOf (Item: Pointer): Sw_Integer; Virtual;
  402. FUNCTION GetItem (Var S: TStream): Pointer; Virtual;
  403. FUNCTION LastThat (Test: Pointer): Pointer;
  404. FUNCTION FirstThat (Test: Pointer): Pointer;
  405. PROCEDURE Pack;
  406. PROCEDURE FreeAll;
  407. PROCEDURE DeleteAll;
  408. PROCEDURE Free (Item: Pointer);
  409. PROCEDURE Insert (Item: Pointer); Virtual;
  410. PROCEDURE Delete (Item: Pointer);
  411. PROCEDURE AtFree (Index: Sw_Integer);
  412. PROCEDURE FreeItem (Item: Pointer); Virtual;
  413. PROCEDURE AtDelete (Index: Sw_Integer);
  414. PROCEDURE ForEach (Action: Pointer);
  415. PROCEDURE SetLimit (ALimit: Sw_Integer); Virtual;
  416. PROCEDURE Error (Code, Info: Integer); Virtual;
  417. PROCEDURE AtPut (Index: Sw_Integer; Item: Pointer);
  418. PROCEDURE AtInsert (Index: Sw_Integer; Item: Pointer);
  419. PROCEDURE Store (Var S: TStream);
  420. PROCEDURE PutItem (Var S: TStream; Item: Pointer); Virtual;
  421. END;
  422. PCollection = ^TCollection;
  423. {---------------------------------------------------------------------------}
  424. { TSortedCollection OBJECT - SORTED COLLECTION ANCESTOR }
  425. {---------------------------------------------------------------------------}
  426. TYPE
  427. TSortedCollection = OBJECT (TCollection)
  428. Duplicates: Boolean; { Duplicates flag }
  429. CONSTRUCTOR Init (ALimit, ADelta: Sw_Integer);
  430. CONSTRUCTOR Load (Var S: TStream);
  431. FUNCTION KeyOf (Item: Pointer): Pointer; Virtual;
  432. FUNCTION IndexOf (Item: Pointer): Sw_Integer; Virtual;
  433. FUNCTION Compare (Key1, Key2: Pointer): Sw_Integer; Virtual;
  434. FUNCTION Search (Key: Pointer; Var Index: Sw_Integer): Boolean;Virtual;
  435. PROCEDURE Insert (Item: Pointer); Virtual;
  436. PROCEDURE Store (Var S: TStream);
  437. END;
  438. PSortedCollection = ^TSortedCollection;
  439. {---------------------------------------------------------------------------}
  440. { TStringCollection OBJECT - STRING COLLECTION OBJECT }
  441. {---------------------------------------------------------------------------}
  442. TYPE
  443. TStringCollection = OBJECT (TSortedCollection)
  444. FUNCTION GetItem (Var S: TStream): Pointer; Virtual;
  445. FUNCTION Compare (Key1, Key2: Pointer): Sw_Integer; Virtual;
  446. PROCEDURE FreeItem (Item: Pointer); Virtual;
  447. PROCEDURE PutItem (Var S: TStream; Item: Pointer); Virtual;
  448. END;
  449. PStringCollection = ^TStringCollection;
  450. {---------------------------------------------------------------------------}
  451. { TStrCollection OBJECT - STRING COLLECTION OBJECT }
  452. {---------------------------------------------------------------------------}
  453. TYPE
  454. TStrCollection = OBJECT (TSortedCollection)
  455. FUNCTION Compare (Key1, Key2: Pointer): Sw_Integer; Virtual;
  456. FUNCTION GetItem (Var S: TStream): Pointer; Virtual;
  457. PROCEDURE FreeItem (Item: Pointer); Virtual;
  458. PROCEDURE PutItem (Var S: TStream; Item: Pointer); Virtual;
  459. END;
  460. PStrCollection = ^TStrCollection;
  461. { ******************************* REMARK ****************************** }
  462. { This is a completely >> NEW << object which holds a collection of }
  463. { strings but does not alphabetically sort them. It is a very useful }
  464. { object for insert ordered list boxes! }
  465. { ****************************** END REMARK *** Leon de Boer, 15May96 * }
  466. {---------------------------------------------------------------------------}
  467. { TUnSortedStrCollection - UNSORTED STRING COLLECTION OBJECT }
  468. {---------------------------------------------------------------------------}
  469. TYPE
  470. TUnSortedStrCollection = OBJECT (TStringCollection)
  471. PROCEDURE Insert (Item: Pointer); Virtual;
  472. END;
  473. PUnSortedStrCollection = ^TUnSortedStrCollection;
  474. {---------------------------------------------------------------------------}
  475. { TResourceCollection OBJECT - RESOURCE COLLECTION OBJECT }
  476. {---------------------------------------------------------------------------}
  477. TYPE
  478. TResourceCollection = OBJECT (TStringCollection)
  479. FUNCTION KeyOf (Item: Pointer): Pointer; Virtual;
  480. FUNCTION GetItem (Var S: TStream): Pointer; Virtual;
  481. PROCEDURE FreeItem (Item: Pointer); Virtual;
  482. PROCEDURE PutItem (Var S: TStream; Item: Pointer); Virtual;
  483. END;
  484. PResourceCollection = ^TResourceCollection;
  485. {---------------------------------------------------------------------------}
  486. { TResourceFile OBJECT - RESOURCE FILE OBJECT }
  487. {---------------------------------------------------------------------------}
  488. TYPE
  489. TResourceFile = OBJECT (TObject)
  490. Stream : PStream; { File as a stream }
  491. Modified: Boolean; { Modified flag }
  492. CONSTRUCTOR Init (AStream: PStream);
  493. DESTRUCTOR Done; Virtual;
  494. FUNCTION Count: Sw_Integer;
  495. FUNCTION KeyAt (I: Sw_Integer): String;
  496. FUNCTION Get (Key: String): PObject;
  497. FUNCTION SwitchTo (AStream: PStream; Pack: Boolean): PStream;
  498. PROCEDURE Flush;
  499. PROCEDURE Delete (Key: String);
  500. PROCEDURE Put (Item: PObject; Key: String);
  501. PRIVATE
  502. BasePos: LongInt; { Base position }
  503. IndexPos: LongInt; { Index position }
  504. Index: TResourceCollection; { Index collection }
  505. END;
  506. PResourceFile = ^TResourceFile;
  507. TYPE
  508. TStrIndexRec = Packed RECORD
  509. Key, Count, Offset: Word;
  510. END;
  511. TStrIndex = Array [0..9999] Of TStrIndexRec;
  512. PStrIndex = ^TStrIndex;
  513. {---------------------------------------------------------------------------}
  514. { TStringList OBJECT - STRING LIST OBJECT }
  515. {---------------------------------------------------------------------------}
  516. TStringList = OBJECT (TObject)
  517. CONSTRUCTOR Load (Var S: TStream);
  518. DESTRUCTOR Done; Virtual;
  519. FUNCTION Get (Key: Sw_Word): String;
  520. PRIVATE
  521. Stream : PStream;
  522. BasePos : Longint;
  523. IndexSize: Sw_Word;
  524. Index : PStrIndex;
  525. PROCEDURE ReadStr (Var S: String; Offset, Skip: Sw_Word);
  526. END;
  527. PStringList = ^TStringList;
  528. {---------------------------------------------------------------------------}
  529. { TStrListMaker OBJECT - RESOURCE FILE OBJECT }
  530. {---------------------------------------------------------------------------}
  531. TYPE
  532. TStrListMaker = OBJECT (TObject)
  533. CONSTRUCTOR Init (AStrSize, AIndexSize: Sw_Word);
  534. DESTRUCTOR Done; Virtual;
  535. PROCEDURE Put (Key: Sw_Word; S: String);
  536. PROCEDURE Store (Var S: TStream);
  537. PRIVATE
  538. StrPos : Sw_Word;
  539. StrSize : Sw_Word;
  540. Strings : PByteArray;
  541. IndexPos : Sw_Word;
  542. IndexSize: Sw_Word;
  543. Index : PStrIndex;
  544. Cur : TStrIndexRec;
  545. PROCEDURE CloseCurrent;
  546. END;
  547. PStrListMaker = ^TStrListMaker;
  548. {***************************************************************************}
  549. { INTERFACE ROUTINES }
  550. {***************************************************************************}
  551. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  552. { DYNAMIC STRING INTERFACE ROUTINES }
  553. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  554. {-NewStr-------------------------------------------------------------
  555. Allocates a dynamic string into memory. If S is nil, NewStr returns
  556. a nil pointer, otherwise NewStr allocates Length(S)+1 bytes of memory
  557. containing a copy of S, and returns a pointer to the string.
  558. 12Jun96 LdB
  559. ---------------------------------------------------------------------}
  560. FUNCTION NewStr (Const S: String): PString;
  561. {-DisposeStr---------------------------------------------------------
  562. Disposes of a PString allocated by the function NewStr.
  563. 12Jun96 LdB
  564. ---------------------------------------------------------------------}
  565. PROCEDURE DisposeStr (P: PString);
  566. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  567. { STREAM INTERFACE ROUTINES }
  568. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  569. {-Abstract-----------------------------------------------------------
  570. Terminates program with a run-time error 211. When implementing
  571. an abstract object type, call Abstract in those virtual methods that
  572. must be overridden in descendant types. This ensures that any
  573. attempt to use instances of the abstract object type will fail.
  574. 12Jun96 LdB
  575. ---------------------------------------------------------------------}
  576. PROCEDURE Abstract;
  577. {-RegisterObjects----------------------------------------------------
  578. Registers the three standard objects TCollection, TStringCollection
  579. and TStrCollection.
  580. 02Sep97 LdB
  581. ---------------------------------------------------------------------}
  582. PROCEDURE RegisterObjects;
  583. {-RegisterType-------------------------------------------------------
  584. Registers the given object type with Free Vision's streams, creating
  585. a list of known objects. Streams can only store and return these known
  586. object types. Each registered object needs a unique stream registration
  587. record, of type TStreamRec.
  588. 02Sep97 LdB
  589. ---------------------------------------------------------------------}
  590. PROCEDURE RegisterType (Var S: TStreamRec);
  591. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  592. { GENERAL FUNCTION INTERFACE ROUTINES }
  593. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  594. {-LongMul------------------------------------------------------------
  595. Returns the long integer value of X * Y integer values.
  596. 04Sep97 LdB
  597. ---------------------------------------------------------------------}
  598. FUNCTION LongMul (X, Y: Integer): LongInt;
  599. {-LongDiv------------------------------------------------------------
  600. Returns the integer value of long integer X divided by integer Y.
  601. 04Sep97 LdB
  602. ---------------------------------------------------------------------}
  603. FUNCTION LongDiv (X: Longint; Y: Integer): Integer;
  604. {***************************************************************************}
  605. { PUBLIC INITIALIZED VARIABLES }
  606. {***************************************************************************}
  607. CONST
  608. {---------------------------------------------------------------------------}
  609. { INITIALIZED DOS/DPMI/WIN/OS2 PUBLIC VARIABLES }
  610. {---------------------------------------------------------------------------}
  611. StreamError: Pointer = Nil; { Stream error ptr }
  612. DosStreamError: Word = $0; { Dos stream error }
  613. {---------------------------------------------------------------------------}
  614. { STREAM REGISTRATION RECORDS }
  615. {---------------------------------------------------------------------------}
  616. CONST
  617. RCollection: TStreamRec = (
  618. ObjType: 50;
  619. VmtLink: Ofs(TypeOf(TCollection)^);
  620. Load: @TCollection.Load;
  621. Store: @TCollection.Store);
  622. RStringCollection: TStreamRec = (
  623. ObjType: 51;
  624. VmtLink: Ofs(TypeOf(TStringCollection)^);
  625. Load: @TStringCollection.Load;
  626. Store: @TStringCollection.Store);
  627. RStrCollection: TStreamRec = (
  628. ObjType: 69;
  629. VmtLink: Ofs(TypeOf(TStrCollection)^);
  630. Load: @TStrCollection.Load;
  631. Store: @TStrCollection.Store);
  632. RStringList: TStreamRec = (
  633. ObjType: 52;
  634. VmtLink: Ofs(TypeOf(TStringList)^);
  635. Load: @TStringList.Load;
  636. Store: Nil);
  637. RStrListMaker: TStreamRec = (
  638. ObjType: 52;
  639. VmtLink: Ofs(TypeOf(TStrListMaker)^);
  640. Load: Nil;
  641. Store: @TStrListMaker.Store);
  642. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  643. IMPLEMENTATION
  644. {<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
  645. {***************************************************************************}
  646. { HELPER ROUTINES FOR CALLING }
  647. {***************************************************************************}
  648. type
  649. FramePointer = pointer;
  650. PointerLocal = function(_EBP: FramePointer; Param1: pointer): pointer;
  651. PointerConstructor = function(VMT: pointer; Obj: pointer; Param1: pointer): pointer;
  652. PointerMethod = function(Obj: pointer; Param1: pointer): pointer;
  653. function PreviousFramePointer: FramePointer;assembler;
  654. asm
  655. {$ifdef i386}
  656. movl (%ebp), %eax
  657. {$endif}
  658. {$ifdef m68k}
  659. move.l a6,d0
  660. {$endif}
  661. end ['EAX'];
  662. function CallPointerConstructor(Ctor: pointer; Obj: pointer; VMT: pointer; Param1: pointer): pointer;
  663. begin
  664. asm
  665. {$ifdef i386}
  666. movl Obj, %esi
  667. {$endif}
  668. {$ifdef m68k}
  669. move.l Obj, a5
  670. {$endif}
  671. end;
  672. CallPointerConstructor := PointerConstructor(Ctor)(VMT, Obj, Param1)
  673. end;
  674. function CallPointerMethod(Method: pointer; Obj: pointer; Param1: pointer): pointer;
  675. begin
  676. asm
  677. {$ifdef i386}
  678. movl Obj, %esi
  679. {$endif}
  680. {$ifdef m68k}
  681. move.l Obj, a5
  682. {$endif}
  683. end;
  684. CallPointerMethod := PointerMethod(Method)(Obj, Param1)
  685. end;
  686. function CallPointerLocal(Func: pointer; Frame: FramePointer; Param1: pointer): pointer;
  687. begin
  688. CallPointerLocal := PointerLocal(Func)(Frame, Param1)
  689. end;
  690. {***************************************************************************}
  691. { PRIVATE INITIALIZED VARIABLES }
  692. {***************************************************************************}
  693. {---------------------------------------------------------------------------}
  694. { INITIALIZED DOS/DPMI/WIN/OS2 PRIVATE VARIABLES }
  695. {---------------------------------------------------------------------------}
  696. CONST
  697. StreamTypes: PStreamRec = Nil; { Stream types reg }
  698. {***************************************************************************}
  699. { PRIVATE INTERNAL ROUTINES }
  700. {***************************************************************************}
  701. {$I objinc.inc}
  702. {---------------------------------------------------------------------------}
  703. { RegisterError -> Platforms DOS/DPMI/WINDOWS/OS2 - Checked 12Jun96 LdB }
  704. {---------------------------------------------------------------------------}
  705. PROCEDURE RegisterError;
  706. BEGIN
  707. RunError(212); { Register error }
  708. END;
  709. {***************************************************************************}
  710. { OBJECT METHODS }
  711. {***************************************************************************}
  712. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  713. { TRect OBJECT METHODS }
  714. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  715. PROCEDURE CheckEmpty (Var Rect: TRect);
  716. BEGIN
  717. With Rect Do Begin
  718. If (A.X >= B.X) OR (A.Y >= B.Y) Then Begin { Zero or reversed }
  719. A.X := 0; { Clear a.x }
  720. A.Y := 0; { Clear a.y }
  721. B.X := 0; { Clear b.x }
  722. B.Y := 0; { Clear b.y }
  723. End;
  724. End;
  725. END;
  726. {--TRect--------------------------------------------------------------------}
  727. { Empty -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  728. {---------------------------------------------------------------------------}
  729. FUNCTION TRect.Empty: Boolean;
  730. BEGIN
  731. Empty := (A.X >= B.X) OR (A.Y >= B.Y); { Empty result }
  732. END;
  733. {--TRect--------------------------------------------------------------------}
  734. { Equals -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  735. {---------------------------------------------------------------------------}
  736. FUNCTION TRect.Equals (R: TRect): Boolean;
  737. BEGIN
  738. Equals := (A.X = R.A.X) AND (A.Y = R.A.Y) AND
  739. (B.X = R.B.X) AND (B.Y = R.B.Y); { Equals result }
  740. END;
  741. {--TRect--------------------------------------------------------------------}
  742. { Contains -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  743. {---------------------------------------------------------------------------}
  744. FUNCTION TRect.Contains (P: TPoint): Boolean;
  745. BEGIN
  746. Contains := (P.X >= A.X) AND (P.X < B.X) AND
  747. (P.Y >= A.Y) AND (P.Y < B.Y); { Contains result }
  748. END;
  749. {--TRect--------------------------------------------------------------------}
  750. { Copy -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  751. {---------------------------------------------------------------------------}
  752. PROCEDURE TRect.Copy (R: TRect);
  753. BEGIN
  754. A := R.A; { Copy point a }
  755. B := R.B; { Copy point b }
  756. END;
  757. {--TRect--------------------------------------------------------------------}
  758. { Union -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  759. {---------------------------------------------------------------------------}
  760. PROCEDURE TRect.Union (R: TRect);
  761. BEGIN
  762. If (R.A.X < A.X) Then A.X := R.A.X; { Take if smaller }
  763. If (R.A.Y < A.Y) Then A.Y := R.A.Y; { Take if smaller }
  764. If (R.B.X > B.X) Then B.X := R.B.X; { Take if larger }
  765. If (R.B.Y > B.Y) Then B.Y := R.B.Y; { Take if larger }
  766. END;
  767. {--TRect--------------------------------------------------------------------}
  768. { Intersect -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  769. {---------------------------------------------------------------------------}
  770. PROCEDURE TRect.Intersect (R: TRect);
  771. BEGIN
  772. If (R.A.X > A.X) Then A.X := R.A.X; { Take if larger }
  773. If (R.A.Y > A.Y) Then A.Y := R.A.Y; { Take if larger }
  774. If (R.B.X < B.X) Then B.X := R.B.X; { Take if smaller }
  775. If (R.B.Y < B.Y) Then B.Y := R.B.Y; { Take if smaller }
  776. CheckEmpty(Self); { Check if empty }
  777. END;
  778. {--TRect--------------------------------------------------------------------}
  779. { Move -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  780. {---------------------------------------------------------------------------}
  781. PROCEDURE TRect.Move (ADX, ADY: Sw_Integer);
  782. BEGIN
  783. Inc(A.X, ADX); { Adjust A.X }
  784. Inc(A.Y, ADY); { Adjust A.Y }
  785. Inc(B.X, ADX); { Adjust B.X }
  786. Inc(B.Y, ADY); { Adjust B.Y }
  787. END;
  788. {--TRect--------------------------------------------------------------------}
  789. { Grow -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  790. {---------------------------------------------------------------------------}
  791. PROCEDURE TRect.Grow (ADX, ADY: Sw_Integer);
  792. BEGIN
  793. Dec(A.X, ADX); { Adjust A.X }
  794. Dec(A.Y, ADY); { Adjust A.Y }
  795. Inc(B.X, ADX); { Adjust B.X }
  796. Inc(B.Y, ADY); { Adjust B.Y }
  797. CheckEmpty(Self); { Check if empty }
  798. END;
  799. {--TRect--------------------------------------------------------------------}
  800. { Assign -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  801. {---------------------------------------------------------------------------}
  802. PROCEDURE TRect.Assign (XA, YA, XB, YB: Sw_Integer);
  803. BEGIN
  804. A.X := XA; { Hold A.X value }
  805. A.Y := YA; { Hold A.Y value }
  806. B.X := XB; { Hold B.X value }
  807. B.Y := YB; { Hold B.Y value }
  808. END;
  809. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  810. { TObject OBJECT METHODS }
  811. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  812. TYPE
  813. DummyObject = OBJECT (TObject) { Internal object }
  814. Data: RECORD END; { Helps size VMT link }
  815. END;
  816. { ******************************* REMARK ****************************** }
  817. { I Prefer this code because it self sizes VMT link rather than using a }
  818. { fixed record structure thus it should work on all compilers without a }
  819. { specific record to match each compiler. }
  820. { ****************************** END REMARK *** Leon de Boer, 10May96 * }
  821. {--TObject------------------------------------------------------------------}
  822. { Init -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  823. {---------------------------------------------------------------------------}
  824. CONSTRUCTOR TObject.Init;
  825. VAR LinkSize: LongInt; Dummy: DummyObject;
  826. BEGIN
  827. LinkSize := LongInt(@Dummy.Data)-LongInt(@Dummy); { Calc VMT link size }
  828. FillChar(Pointer(LongInt(@Self)+LinkSize)^,
  829. SizeOf(Self)-LinkSize, #0); { Clear data fields }
  830. END;
  831. {--TObject------------------------------------------------------------------}
  832. { Free -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  833. {---------------------------------------------------------------------------}
  834. PROCEDURE TObject.Free;
  835. BEGIN
  836. Dispose(PObject(@Self), Done); { Dispose of self }
  837. END;
  838. {--TObject------------------------------------------------------------------}
  839. { Done -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  840. {---------------------------------------------------------------------------}
  841. DESTRUCTOR TObject.Done;
  842. BEGIN { Abstract method }
  843. END;
  844. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  845. { TStream OBJECT METHODS }
  846. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  847. {--TStream------------------------------------------------------------------}
  848. { Get -> Platforms DOS/DPMI/WIN/OS2 - Checked 02Sep97 LdB }
  849. {---------------------------------------------------------------------------}
  850. FUNCTION TStream.Get: PObject;
  851. VAR ObjType: Sw_Word; P: PStreamRec;
  852. BEGIN
  853. Read(ObjType, SizeOf(ObjType)); { Read object type }
  854. If (ObjType<>0) Then Begin { Object registered }
  855. P := StreamTypes; { Current reg list }
  856. While (P <> Nil) AND (P^.ObjType <> ObjType) { Find object type OR }
  857. Do P := P^.Next; { Find end of chain }
  858. If (P=Nil) Then Begin { Not registered }
  859. Error(stGetError, ObjType); { Obj not registered }
  860. Get := Nil; { Return nil pointer }
  861. End Else
  862. Get :=PObject(
  863. CallPointerConstructor(P^.Load,Nil,P^.VMTLink, @Self)) { Call constructor }
  864. End Else Get := Nil; { Return nil pointer }
  865. END;
  866. {--TStream------------------------------------------------------------------}
  867. { StrRead -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  868. {---------------------------------------------------------------------------}
  869. FUNCTION TStream.StrRead: PChar;
  870. VAR L: Word; P: PChar;
  871. BEGIN
  872. Read(L, SizeOf(L)); { Read length }
  873. If (L = 0) Then StrRead := Nil Else Begin { Check for empty }
  874. GetMem(P, L + 1); { Allocate memory }
  875. If (P <> Nil) Then Begin { Check allocate okay }
  876. Read(P[0], L); { Read the data }
  877. P[L] := #0; { Terminate with #0 }
  878. End;
  879. StrRead := P; { Return PChar }
  880. End;
  881. END;
  882. {--TStream------------------------------------------------------------------}
  883. { ReadStr -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  884. {---------------------------------------------------------------------------}
  885. FUNCTION TStream.ReadStr: PString;
  886. VAR L: Byte; P: PString;
  887. BEGIN
  888. Read(L, 1); { Read string length }
  889. If (L > 0) Then Begin
  890. GetMem(P, L + 1); { Allocate memory }
  891. If (P <> Nil) Then Begin { Check allocate okay }
  892. P^[0] := Char(L); { Hold length }
  893. Read(P^[1], L); { Read string data }
  894. End;
  895. ReadStr := P; { Return string ptr }
  896. End Else ReadStr := Nil;
  897. END;
  898. {--TStream------------------------------------------------------------------}
  899. { GetPos -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  900. {---------------------------------------------------------------------------}
  901. FUNCTION TStream.GetPos: LongInt;
  902. BEGIN
  903. If (Status=stOk) Then GetPos := Position { Return position }
  904. Else GetPos := -1; { Stream in error }
  905. END;
  906. {--TStream------------------------------------------------------------------}
  907. { GetSize -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  908. {---------------------------------------------------------------------------}
  909. FUNCTION TStream.GetSize: LongInt;
  910. BEGIN
  911. If (Status=stOk) Then GetSize := StreamSize { Return stream size }
  912. Else GetSize := -1; { Stream in error }
  913. END;
  914. {--TStream------------------------------------------------------------------}
  915. { Close -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  916. {---------------------------------------------------------------------------}
  917. PROCEDURE TStream.Close;
  918. BEGIN { Abstract method }
  919. END;
  920. {--TStream------------------------------------------------------------------}
  921. { Reset -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  922. {---------------------------------------------------------------------------}
  923. PROCEDURE TStream.Reset;
  924. BEGIN
  925. Status := 0; { Clear status }
  926. ErrorInfo := 0; { Clear error info }
  927. END;
  928. {--TStream------------------------------------------------------------------}
  929. { Flush -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  930. {---------------------------------------------------------------------------}
  931. PROCEDURE TStream.Flush;
  932. BEGIN { Abstract method }
  933. END;
  934. {--TStream------------------------------------------------------------------}
  935. { Truncate -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  936. {---------------------------------------------------------------------------}
  937. PROCEDURE TStream.Truncate;
  938. BEGIN
  939. Abstract; { Abstract error }
  940. END;
  941. {--TStream------------------------------------------------------------------}
  942. { Get -> Platforms DOS/DPMI/WIN/OS2 - Checked 02Sep97 LdB }
  943. {---------------------------------------------------------------------------}
  944. PROCEDURE TStream.Put (P: PObject);
  945. VAR ObjType: Sw_Word; Link: pointer; Q: PStreamRec; VmtPtr: ^pointer;
  946. BEGIN
  947. VmtPtr := Pointer(P); { Xfer object to ptr }
  948. Link := VmtPtr^; { VMT link }
  949. ObjType := 0; { Set objtype to zero }
  950. If (P<>Nil) AND (Link<>Nil) Then Begin { We have a VMT link }
  951. Q := StreamTypes; { Current reg list }
  952. While (Q <> Nil) AND (Q^.VMTLink <> Link) { Find link match OR }
  953. Do Q := Q^.Next; { Find end of chain }
  954. If (Q=Nil) Then Begin { End of chain found }
  955. Error(stPutError, 0); { Not registered error }
  956. Exit; { Now exit }
  957. End Else ObjType := Q^.ObjType; { Update object type }
  958. End;
  959. Write(ObjType, SizeOf(ObjType)); { Write object type }
  960. If (ObjType<>0) Then { Registered object }
  961. CallPointerMethod(Q^.Store, P, @Self);
  962. END;
  963. {--TStream------------------------------------------------------------------}
  964. { Seek -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  965. {---------------------------------------------------------------------------}
  966. PROCEDURE TStream.Seek (Pos: LongInt);
  967. BEGIN
  968. If (Status = stOk) Then Begin { Check status }
  969. If (Pos < 0) Then Pos := 0; { Remove negatives }
  970. If (Pos <= StreamSize) Then Position := Pos { If valid set pos }
  971. Else Error(stSeekError, Pos); { Position error }
  972. End;
  973. END;
  974. {--TStream------------------------------------------------------------------}
  975. { StrWrite -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  976. {---------------------------------------------------------------------------}
  977. PROCEDURE TStream.StrWrite (P: PChar);
  978. VAR L: Word; Q: PByteArray;
  979. BEGIN
  980. L := 0; { Preset zero size }
  981. Q := PByteArray(P); { Transfer type }
  982. If (Q <> Nil) Then While (Q^[L] <> 0) Do Inc(L); { PChar length }
  983. Write(L, SizeOf(L)); { Store length }
  984. If (P <> Nil) Then Write(P[0], L); { Write data }
  985. END;
  986. {--TStream------------------------------------------------------------------}
  987. { WriteStr -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  988. {---------------------------------------------------------------------------}
  989. PROCEDURE TStream.WriteStr (P: PString);
  990. CONST Empty: String[1] = '';
  991. BEGIN
  992. If (P <> Nil) Then Write(P^, Length(P^) + 1) { Write string }
  993. Else Write(Empty, 1); { Write empty string }
  994. END;
  995. {--TStream------------------------------------------------------------------}
  996. { Open -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  997. {---------------------------------------------------------------------------}
  998. PROCEDURE TStream.Open (OpenMode: Word);
  999. BEGIN { Abstract method }
  1000. END;
  1001. {--TStream------------------------------------------------------------------}
  1002. { Error -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  1003. {---------------------------------------------------------------------------}
  1004. PROCEDURE TStream.Error (Code, Info: Integer);
  1005. TYPE TErrorProc = Procedure(Var S: TStream);
  1006. BEGIN
  1007. Status := Code; { Hold error code }
  1008. ErrorInfo := Info; { Hold error info }
  1009. If (StreamError <> Nil) Then
  1010. TErrorProc(StreamError)(Self); { Call error ptr }
  1011. END;
  1012. {--TStream------------------------------------------------------------------}
  1013. { Read -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  1014. {---------------------------------------------------------------------------}
  1015. PROCEDURE TStream.Read (Var Buf; Count: Sw_Word);
  1016. BEGIN
  1017. Abstract; { Abstract error }
  1018. END;
  1019. {--TStream------------------------------------------------------------------}
  1020. { Write -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  1021. {---------------------------------------------------------------------------}
  1022. PROCEDURE TStream.Write (Var Buf; Count: Sw_Word);
  1023. BEGIN
  1024. Abstract; { Abstract error }
  1025. END;
  1026. {--TStream------------------------------------------------------------------}
  1027. { CopyFrom -> Platforms DOS/DPMI/WIN/OS2 - Checked 10May96 LdB }
  1028. {---------------------------------------------------------------------------}
  1029. PROCEDURE TStream.CopyFrom (Var S: TStream; Count: Longint);
  1030. VAR W: Word; Buffer: Array[0..1023] of Byte;
  1031. BEGIN
  1032. While (Count > 0) Do Begin
  1033. If (Count > SizeOf(Buffer)) Then { To much data }
  1034. W := SizeOf(Buffer) Else W := Count; { Size to transfer }
  1035. S.Read(Buffer, W); { Read from stream }
  1036. Write(Buffer, W); { Write to stream }
  1037. Dec(Count, W); { Dec write count }
  1038. End;
  1039. END;
  1040. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1041. { TDosStream OBJECT METHODS }
  1042. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1043. {--TDosStream---------------------------------------------------------------}
  1044. { Init -> Platforms DOS/DPMI/WIN/OS2 - Checked 16May96 LdB }
  1045. {---------------------------------------------------------------------------}
  1046. CONSTRUCTOR TDosStream.Init (FileName: FNameStr; Mode: Word);
  1047. VAR Success: Integer;
  1048. BEGIN
  1049. Inherited Init; { Call ancestor }
  1050. FileName := FileName+#0; { Make asciiz }
  1051. Move(FileName[1], FName, Length(FileName)); { Create asciiz name }
  1052. Handle := FileOpen(FName, Mode); { Open the file }
  1053. If (Handle <> 0) Then Begin { Handle valid }
  1054. Success := SetFilePos(Handle, 0, 2, StreamSize); { Locate end of file }
  1055. If (Success = 0) Then
  1056. Success := SetFilePos(Handle, 0, 0, Position); { Reset to file start }
  1057. End Else Success := 103; { Open file failed }
  1058. If (Handle = 0) OR (Success <> 0) Then Begin { Open failed }
  1059. Handle := InvalidHandle; { Reset invalid handle }
  1060. Error(stInitError, Success); { Call stream error }
  1061. End;
  1062. END;
  1063. {--TDosStream---------------------------------------------------------------}
  1064. { Done -> Platforms DOS/DPMI/WIN/OS2 - Checked 16May96 LdB }
  1065. {---------------------------------------------------------------------------}
  1066. DESTRUCTOR TDosStream.Done;
  1067. BEGIN
  1068. If (Handle <> InvalidHandle) Then FileClose(Handle); { Close the file }
  1069. Inherited Done; { Call ancestor }
  1070. END;
  1071. {--TDosStream---------------------------------------------------------------}
  1072. { Close -> Platforms DOS/DPMI/WIN/OS2 - Checked 16May96 LdB }
  1073. {---------------------------------------------------------------------------}
  1074. PROCEDURE TDosStream.Close;
  1075. BEGIN
  1076. If (Handle <> InvalidHandle) Then FileClose(Handle); { Close the file }
  1077. Position := 0; { Zero the position }
  1078. Handle := invalidhandle; { Handle now invalid }
  1079. END;
  1080. {--TDosStream---------------------------------------------------------------}
  1081. { Truncate -> Platforms DOS/DPMI/WIN/OS2 - Checked 16May96 LdB }
  1082. {---------------------------------------------------------------------------}
  1083. PROCEDURE TDosStream.Truncate;
  1084. VAR Success: Integer;
  1085. BEGIN
  1086. If (Status=stOk) Then Begin { Check status okay }
  1087. Success := SetFileSize(Handle, Position); { Truncate file }
  1088. If (Success = 0) Then StreamSize := Position { Adjust size }
  1089. Else Error(stError, Success); { Identify error }
  1090. End;
  1091. END;
  1092. {--TDosStream---------------------------------------------------------------}
  1093. { Seek -> Platforms DOS/DPMI/WIN/OS2 - Checked 16May96 LdB }
  1094. {---------------------------------------------------------------------------}
  1095. PROCEDURE TDosStream.Seek (Pos: LongInt);
  1096. VAR Success: Integer; Li: LongInt;
  1097. BEGIN
  1098. If (Status=stOk) Then Begin { Check status okay }
  1099. If (Pos < 0) Then Pos := 0; { Negatives removed }
  1100. If (Handle = InvalidHandle) Then Success := 103 Else { File not open }
  1101. Success := SetFilePos(Handle, Pos, 0, Li); { Set file position }
  1102. If ((Success = -1) OR (Li <> Pos)) Then Begin { We have an error }
  1103. If (Success = -1) Then Error(stSeekError, 0) { General seek error }
  1104. Else Error(stSeekError, Success); { Specific seek error }
  1105. End Else Position := Li; { Adjust position }
  1106. End;
  1107. END;
  1108. {--TDosStream---------------------------------------------------------------}
  1109. { Open -> Platforms DOS/DPMI/WIN/OS2 - Checked 16May96 LdB }
  1110. {---------------------------------------------------------------------------}
  1111. PROCEDURE TDosStream.Open (OpenMode: Word);
  1112. BEGIN
  1113. If (Status=stOk) Then Begin { Check status okay }
  1114. If (Handle = InvalidHandle) Then Begin { File not open }
  1115. Handle := FileOpen(FName, OpenMode); { Open the file }
  1116. Position := 0; { Reset position }
  1117. If (Handle=0) Then Begin { File open failed }
  1118. Handle := InvalidHandle; { Reset handle }
  1119. Error(stOpenError, 103); { Call stream error }
  1120. End;
  1121. End Else Error(stOpenError, 104); { File already open }
  1122. End;
  1123. END;
  1124. {--TDosStream---------------------------------------------------------------}
  1125. { Read -> Platforms DOS/DPMI/WIN/OS2 - Checked 16May96 LdB }
  1126. {---------------------------------------------------------------------------}
  1127. PROCEDURE TDosStream.Read (Var Buf; Count: Sw_Word);
  1128. VAR Success: Integer; W, BytesMoved: Sw_Word; P: PByteArray;
  1129. BEGIN
  1130. If (Position + Count > StreamSize) Then { Insufficient data }
  1131. Error(stReadError, 0); { Read beyond end!!! }
  1132. If (Handle = InvalidHandle) Then Error(stReadError, 103); { File not open }
  1133. P := @Buf; { Transfer address }
  1134. While (Count>0) AND (Status=stOk) Do Begin { Check status & count }
  1135. W := Count; { Transfer read size }
  1136. If (Count>MaxReadBytes) Then
  1137. W := MaxReadBytes; { Cant read >64K bytes }
  1138. Success := FileRead(Handle, P^, W, BytesMoved); { Read from file }
  1139. If ((Success<>0) OR (BytesMoved<>W)) Then Begin { Error was detected }
  1140. BytesMoved := 0; { Clear bytes moved }
  1141. If (Success <> 0) Then
  1142. Error(stReadError, Success) { Specific read error }
  1143. Else Error(stReadError, 0); { Non specific error }
  1144. End;
  1145. Inc(Position, BytesMoved); { Adjust position }
  1146. P := Pointer(LongInt(P) + BytesMoved); { Adjust buffer ptr }
  1147. Dec(Count, BytesMoved); { Adjust count left }
  1148. End;
  1149. If (Count<>0) Then FillChar(P^, Count, #0); { Error clear buffer }
  1150. END;
  1151. {--TDosStream---------------------------------------------------------------}
  1152. { Write -> Platforms DOS/DPMI/WIN/OS2 - Checked 16May96 LdB }
  1153. {---------------------------------------------------------------------------}
  1154. PROCEDURE TDosStream.Write (Var Buf; Count: Sw_Word);
  1155. VAR Success: Integer; W, BytesMoved: Sw_Word; P: PByteArray;
  1156. BEGIN
  1157. If (Handle = InvalidHandle) Then Error(stWriteError, 103); { File not open }
  1158. P := @Buf; { Transfer address }
  1159. While (Count>0) AND (Status=stOk) Do Begin { Check status & count }
  1160. W := Count; { Transfer read size }
  1161. If (Count>MaxReadBytes) Then
  1162. W := MaxReadBytes; { Cant read >64K bytes }
  1163. Success := FileWrite(Handle, P^, W, BytesMoved); { Write to file }
  1164. If ((Success<>0) OR (BytesMoved<>W)) Then Begin { Error was detected }
  1165. BytesMoved := 0; { Clear bytes moved }
  1166. If (Success<>0) Then
  1167. Error(stWriteError, Success) { Specific write error }
  1168. Else Error(stWriteError, 0); { Non specific error }
  1169. End;
  1170. Inc(Position, BytesMoved); { Adjust position }
  1171. P := Pointer(LongInt(P) + BytesMoved); { Transfer address }
  1172. Dec(Count, BytesMoved); { Adjust count left }
  1173. If (Position > StreamSize) Then { File expanded }
  1174. StreamSize := Position; { Adjust stream size }
  1175. End;
  1176. END;
  1177. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1178. { TBufStream OBJECT METHODS }
  1179. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1180. {--TBufStream---------------------------------------------------------------}
  1181. { Init -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1182. {---------------------------------------------------------------------------}
  1183. CONSTRUCTOR TBufStream.Init (FileName: FNameStr; Mode, Size: Word);
  1184. BEGIN
  1185. Inherited Init(FileName, Mode); { Call ancestor }
  1186. BufSize := Size; { Hold buffer size }
  1187. If (Size<>0) Then GetMem(Buffer, Size); { Allocate buffer }
  1188. If (Buffer=Nil) Then Error(stInitError, 0); { Buffer allocate fail }
  1189. END;
  1190. {--TBufStream---------------------------------------------------------------}
  1191. { Done -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1192. {---------------------------------------------------------------------------}
  1193. DESTRUCTOR TBufStream.Done;
  1194. BEGIN
  1195. Flush; { Flush the file }
  1196. Inherited Done; { Call ancestor }
  1197. If (Buffer<>Nil) Then FreeMem(Buffer, BufSize); { Release buffer }
  1198. END;
  1199. {--TBufStream---------------------------------------------------------------}
  1200. { Close -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1201. {---------------------------------------------------------------------------}
  1202. PROCEDURE TBufStream.Close;
  1203. BEGIN
  1204. Flush; { Flush the buffer }
  1205. Inherited Close; { Call ancestor }
  1206. END;
  1207. {--TBufStream---------------------------------------------------------------}
  1208. { Flush -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1209. {---------------------------------------------------------------------------}
  1210. PROCEDURE TBufStream.Flush;
  1211. VAR Success: Integer; W: Sw_Word;
  1212. BEGIN
  1213. If (LastMode=2) AND (BufPtr<>0) Then Begin { Must update file }
  1214. If (Handle = InvalidHandle) Then Success := 103 { File is not open }
  1215. Else Success := FileWrite(Handle, Buffer^,
  1216. BufPtr, W); { Write to file }
  1217. If (Success<>0) OR (W<>BufPtr) Then { We have an error }
  1218. If (Success=0) Then Error(stWriteError, 0) { Unknown write error }
  1219. Else Error(stError, Success); { Specific write error }
  1220. End;
  1221. BufPtr := 0; { Reset buffer ptr }
  1222. BufEnd := 0; { Reset buffer end }
  1223. END;
  1224. {--TBufStream---------------------------------------------------------------}
  1225. { Truncate -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1226. {---------------------------------------------------------------------------}
  1227. PROCEDURE TBufStream.Truncate;
  1228. BEGIN
  1229. Flush; { Flush buffer }
  1230. Inherited Truncate; { Truncate file }
  1231. END;
  1232. {--TBufStream---------------------------------------------------------------}
  1233. { Seek -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1234. {---------------------------------------------------------------------------}
  1235. PROCEDURE TBufStream.Seek (Pos: LongInt);
  1236. BEGIN
  1237. If (Status=stOk) Then Begin { Check status okay }
  1238. If (Position<>Pos) Then Begin { Move required }
  1239. Flush; { Flush the buffer }
  1240. Inherited Seek(Pos); { Call ancestor }
  1241. End;
  1242. End;
  1243. END;
  1244. {--TBufStream---------------------------------------------------------------}
  1245. { Open -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1246. {---------------------------------------------------------------------------}
  1247. PROCEDURE TBufStream.Open (OpenMode: Word);
  1248. BEGIN
  1249. If (Status=stOk) Then Begin { Check status okay }
  1250. BufPtr := 0; { Clear buffer start }
  1251. BufEnd := 0; { Clear buffer end }
  1252. Inherited Open(OpenMode); { Call ancestor }
  1253. End;
  1254. END;
  1255. {--TBufStream---------------------------------------------------------------}
  1256. { Read -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1257. {---------------------------------------------------------------------------}
  1258. PROCEDURE TBufStream.Read (Var Buf; Count: Sw_Word);
  1259. VAR Success: Integer; W, Bw: Sw_Word; P: PByteArray;
  1260. BEGIN
  1261. If (Position + Count > StreamSize) Then { Read pas stream end }
  1262. Error(stReadError, 0); { Call stream error }
  1263. If (Handle = InvalidHandle) Then Error(stReadError, 103); { File not open }
  1264. P := @Buf; { Transfer address }
  1265. If (LastMode=2) Then Flush; { Flush write buffer }
  1266. LastMode := 1; { Now set read mode }
  1267. While (Count>0) AND (Status=stOk) Do Begin { Check status & count }
  1268. If (BufPtr=BufEnd) Then Begin { Buffer is empty }
  1269. If (Position + BufSize > StreamSize) Then
  1270. Bw := StreamSize - Position { Amount of file left }
  1271. Else Bw := BufSize; { Full buffer size }
  1272. Success := FileRead(Handle, Buffer^, Bw, W); { Read from file }
  1273. If ((Success<>0) OR (Bw<>W)) Then Begin { Error was detected }
  1274. If (Success<>0) Then
  1275. Error(stReadError, Success) { Specific read error }
  1276. Else Error(stReadError, 0); { Non specific error }
  1277. End Else Begin
  1278. BufPtr := 0; { Reset BufPtr }
  1279. BufEnd := W; { End of buffer }
  1280. End;
  1281. End;
  1282. If (Status=stOk) Then Begin { Status still okay }
  1283. W := BufEnd - BufPtr; { Space in buffer }
  1284. If (Count < W) Then W := Count; { Set transfer size }
  1285. Move(Buffer^[BufPtr], P^, W); { Data from buffer }
  1286. Dec(Count, W); { Reduce count }
  1287. Inc(BufPtr, W); { Advance buffer ptr }
  1288. P := Pointer(LongInt(P) + W); { Transfer address }
  1289. Inc(Position, W); { Advance position }
  1290. End;
  1291. End;
  1292. If (Status<>stOk) AND (Count>0) Then
  1293. FillChar(P^, Count, #0); { Error clear buffer }
  1294. END;
  1295. {--TBufStream---------------------------------------------------------------}
  1296. { Write -> Platforms DOS/DPMI/WIN/OS2 - Checked 17May96 LdB }
  1297. {---------------------------------------------------------------------------}
  1298. PROCEDURE TBufStream.Write (Var Buf; Count: Sw_Word);
  1299. VAR Success: Integer; W: Sw_Word; P: PByteArray;
  1300. BEGIN
  1301. If (Handle = InvalidHandle) Then Error(stWriteError, 103); { File not open }
  1302. If (LastMode=1) Then Flush; { Flush read buffer }
  1303. LastMode := 2; { Now set write mode }
  1304. P := @Buf; { Transfer address }
  1305. While (Count>0) AND (Status=stOk) Do Begin { Check status & count }
  1306. If (BufPtr=BufSize) Then Begin { Buffer is full }
  1307. Success := FileWrite(Handle, Buffer^, BufSize,
  1308. W); { Write to file }
  1309. If (Success<>0) OR (W<>BufSize) Then { We have an error }
  1310. If (Success=0) Then Error(stWriteError, 0) { Unknown write error }
  1311. Else Error(stError, Success); { Specific write error }
  1312. BufPtr := 0; { Reset BufPtr }
  1313. End;
  1314. If (Status=stOk) Then Begin { Status still okay }
  1315. W := BufSize - BufPtr; { Space in buffer }
  1316. If (Count < W) Then W := Count; { Transfer size }
  1317. Move(P^, Buffer^[BufPtr], W); { Data to buffer }
  1318. Dec(Count, W); { Reduce count }
  1319. Inc(BufPtr, W); { Advance buffer ptr }
  1320. P := Pointer(LongInt(P) + W); { Transfer address }
  1321. Inc(Position, W); { Advance position }
  1322. If (Position > StreamSize) Then { File has expanded }
  1323. StreamSize := Position; { Update new size }
  1324. End;
  1325. End;
  1326. END;
  1327. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1328. { TMemoryStream OBJECT METHODS }
  1329. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1330. {--TMemoryStream------------------------------------------------------------}
  1331. { Init -> Platforms DOS/DPMI/WIN/OS2 - Checked 19May96 LdB }
  1332. {---------------------------------------------------------------------------}
  1333. CONSTRUCTOR TMemoryStream.Init (ALimit: LongInt; ABlockSize: Word);
  1334. VAR W: Word;
  1335. BEGIN
  1336. Inherited Init; { Call ancestor }
  1337. If (ABlockSize=0) Then BlkSize := 8192 Else { Default blocksize }
  1338. BlkSize := ABlockSize; { Set blocksize }
  1339. If (ALimit = 0) Then W := 1 Else { At least 1 block }
  1340. W := (ALimit + BlkSize - 1) DIV BlkSize; { Blocks needed }
  1341. If NOT ChangeListSize(W) Then { Try allocate blocks }
  1342. Error(stInitError, 0); { Initialize error }
  1343. END;
  1344. {--TMemoryStream------------------------------------------------------------}
  1345. { Done -> Platforms DOS/DPMI/WIN/OS2 - Checked 19May96 LdB }
  1346. {---------------------------------------------------------------------------}
  1347. DESTRUCTOR TMemoryStream.Done;
  1348. BEGIN
  1349. ChangeListSize(0); { Release all memory }
  1350. Inherited Done; { Call ancestor }
  1351. END;
  1352. {--TMemoryStream------------------------------------------------------------}
  1353. { Truncate -> Platforms DOS/DPMI/WIN/OS2 - Checked 19May96 LdB }
  1354. {---------------------------------------------------------------------------}
  1355. PROCEDURE TMemoryStream.Truncate;
  1356. VAR W: Word;
  1357. BEGIN
  1358. If (Status=stOk) Then Begin { Check status okay }
  1359. If (Position = 0) Then W := 1 Else { At least one block }
  1360. W := (Position + BlkSize - 1) DIV BlkSize; { Blocks needed }
  1361. If ChangeListSize(W) Then StreamSize := Position { Set stream size }
  1362. Else Error(stError, 0); { Error truncating }
  1363. End;
  1364. END;
  1365. {--TMemoryStream------------------------------------------------------------}
  1366. { Read -> Platforms DOS/DPMI/WIN/OS2 - Checked 19May96 LdB }
  1367. {---------------------------------------------------------------------------}
  1368. PROCEDURE TMemoryStream.Read (Var Buf; Count: Sw_Word);
  1369. VAR W, CurBlock, BlockPos: Word; Li: LongInt; P, Q: PByteArray;
  1370. BEGIN
  1371. If (Position + Count > StreamSize) Then { Insufficient data }
  1372. Error(stReadError, 0); { Read beyond end!!! }
  1373. P := @Buf; { Transfer address }
  1374. While (Count>0) AND (Status=stOk) Do Begin { Check status & count }
  1375. CurBlock := Position DIV BlkSize; { Current block }
  1376. { * REMARK * - Do not shorten this, result can be > 64K }
  1377. Li := CurBlock; { Transfer current block }
  1378. Li := Li * BlkSize; { Current position }
  1379. { * REMARK END * - Leon de Boer }
  1380. BlockPos := Position - Li; { Current position }
  1381. W := BlkSize - BlockPos; { Current block space }
  1382. If (W > Count) Then W := Count; { Adjust read size }
  1383. Q := Pointer(LongInt(BlkList^[CurBlock]) +
  1384. BlockPos); { Calc pointer }
  1385. Move(Q^, P^, W); { Move data to buffer }
  1386. Inc(Position, W); { Adjust position }
  1387. P := Pointer(LongInt(P) + W); { Transfer address }
  1388. Dec(Count, W); { Adjust count left }
  1389. End;
  1390. If (Count<>0) Then FillChar(P^, Count, #0); { Error clear buffer }
  1391. END;
  1392. {--TMemoryStream------------------------------------------------------------}
  1393. { Write -> Platforms DOS/DPMI/WIN/OS2 - Checked 19May96 LdB }
  1394. {---------------------------------------------------------------------------}
  1395. PROCEDURE TMemoryStream.Write (Var Buf; Count: Sw_Word);
  1396. VAR W, CurBlock, BlockPos: Word; Li: LongInt; P, Q: PByteArray;
  1397. BEGIN
  1398. If (Position + Count > MemSize) Then Begin { Expansion needed }
  1399. If (Position + Count = 0) Then W := 1 Else { At least 1 block }
  1400. W := (Position+Count+BlkSize-1) DIV BlkSize; { Blocks needed }
  1401. If NOT ChangeListSize(W) Then
  1402. Error(stWriteError, 0); { Expansion failed!!! }
  1403. End;
  1404. P := @Buf; { Transfer address }
  1405. While (Count>0) AND (Status=stOk) Do Begin { Check status & count }
  1406. CurBlock := Position DIV BlkSize; { Current segment }
  1407. { * REMARK * - Do not shorten this, result can be > 64K }
  1408. Li := CurBlock; { Transfer current block }
  1409. Li := Li * BlkSize; { Current position }
  1410. { * REMARK END * - Leon de Boer }
  1411. BlockPos := Position - Li; { Current position }
  1412. W := BlkSize - BlockPos; { Current block space }
  1413. If (W > Count) Then W := Count; { Adjust write size }
  1414. Q := Pointer(LongInt(BlkList^[CurBlock]) +
  1415. BlockPos); { Calc pointer }
  1416. Move(P^, Q^, W); { Transfer data }
  1417. Inc(Position, W); { Adjust position }
  1418. P := Pointer(LongInt(P) + W); { Transfer address }
  1419. Dec(Count, W); { Adjust count left }
  1420. If (Position > StreamSize) Then { File expanded }
  1421. StreamSize := Position; { Adjust stream size }
  1422. End;
  1423. END;
  1424. {***************************************************************************}
  1425. { TMemoryStream PRIVATE METHODS }
  1426. {***************************************************************************}
  1427. {--TMemoryStream------------------------------------------------------------}
  1428. { ChangeListSize -> Platforms DOS/DPMI/WIN/OS2 - Checked 19May96 LdB }
  1429. {---------------------------------------------------------------------------}
  1430. FUNCTION TMemoryStream.ChangeListSize (ALimit: Sw_Word): Boolean;
  1431. VAR I, W: Word; Li: LongInt; P: PPointerArray;
  1432. BEGIN
  1433. If (ALimit <> BlkCount) Then Begin { Change is needed }
  1434. ChangeListSize := False; { Preset failure }
  1435. If (ALimit > MaxPtrs) Then Exit; { To many blocks req }
  1436. If (ALimit <> 0) Then Begin { Create segment list }
  1437. Li := ALimit * SizeOf(Pointer); { Block array size }
  1438. If (MaxAvail > Li) Then Begin
  1439. GetMem(P, Li); { Allocate memory }
  1440. FillChar(P^, Li, #0); { Clear the memory }
  1441. End Else Exit; { Insufficient memory }
  1442. If (BlkCount <> 0) AND (BlkList <> Nil) Then { Current list valid }
  1443. If (BlkCount <= ALimit) Then Move(BlkList^,
  1444. P^, BlkCount * SizeOf(Pointer)) Else { Move whole old list }
  1445. Move(BlkList^, P^, Li); { Move partial list }
  1446. End Else P := Nil; { No new block list }
  1447. If (ALimit < BlkCount) Then { Shrink stream size }
  1448. For W := BlkCount-1 DownTo ALimit Do
  1449. FreeMem(BlkList^[W], BlkSize); { Release memory block }
  1450. If (P <> Nil) AND (ALimit > BlkCount) Then Begin { Expand stream size }
  1451. For W := BlkCount To ALimit-1 Do Begin
  1452. If (MaxAvail < BlkSize) Then Begin { Check enough memory }
  1453. For I := BlkCount To W-1 Do
  1454. FreeMem(P^[I], BlkSize); { Free mem allocated }
  1455. FreeMem(P, Li); { Release memory }
  1456. Exit; { Now exit }
  1457. End Else GetMem(P^[W], BlkSize); { Allocate memory }
  1458. End;
  1459. End;
  1460. If (BlkCount <> 0) AND (BlkList<>Nil) Then
  1461. FreeMem(BlkList, BlkCount * SizeOf(Pointer)); { Release old list }
  1462. BlkList := P; { Hold new block list }
  1463. BlkCount := ALimit; { Hold new count }
  1464. { * REMARK * - Do not shorten this, result can be > 64K }
  1465. MemSize := BlkCount; { Block count }
  1466. MemSize := MemSize * BlkSize; { Current position }
  1467. { * REMARK END * - Leon de Boer }
  1468. End;
  1469. ChangeListSize := True; { Successful }
  1470. END;
  1471. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1472. { TCollection OBJECT METHODS }
  1473. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1474. {--TCollection--------------------------------------------------------------}
  1475. { Init -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1476. {---------------------------------------------------------------------------}
  1477. CONSTRUCTOR TCollection.Init (ALimit, ADelta: Sw_Integer);
  1478. BEGIN
  1479. Inherited Init; { Call ancestor }
  1480. Delta := ADelta; { Set increment }
  1481. SetLimit(ALimit); { Set limit }
  1482. END;
  1483. {--TCollection--------------------------------------------------------------}
  1484. { Load -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1485. {---------------------------------------------------------------------------}
  1486. CONSTRUCTOR TCollection.Load (Var S: TStream);
  1487. VAR C, I: Sw_Integer;
  1488. BEGIN
  1489. S.Read(Count, Sizeof(Count)); { Read count }
  1490. S.Read(Limit, Sizeof(Limit)); { Read limit }
  1491. S.Read(Delta, Sizeof(Delta)); { Read delta }
  1492. Items := Nil; { Clear item pointer }
  1493. C := Count; { Hold count }
  1494. I := Limit; { Hold limit }
  1495. Count := 0; { Clear count }
  1496. Limit := 0; { Clear limit }
  1497. SetLimit(I); { Set requested limit }
  1498. Count := C; { Set count }
  1499. For I := 0 To C-1 Do AtPut(I, GetItem(S)); { Get each item }
  1500. END;
  1501. {--TCollection--------------------------------------------------------------}
  1502. { Done -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1503. {---------------------------------------------------------------------------}
  1504. DESTRUCTOR TCollection.Done;
  1505. BEGIN
  1506. FreeAll; { Free all items }
  1507. SetLimit(0); { Release all memory }
  1508. END;
  1509. {--TCollection--------------------------------------------------------------}
  1510. { At -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1511. {---------------------------------------------------------------------------}
  1512. FUNCTION TCollection.At (Index: Sw_Integer): Pointer;
  1513. BEGIN
  1514. If (Index < 0) OR (Index >= Count) Then Begin { Invalid index }
  1515. Error(coIndexError, Index); { Call error }
  1516. At := Nil; { Return nil }
  1517. End Else At := Items^[Index]; { Return item }
  1518. END;
  1519. {--TCollection--------------------------------------------------------------}
  1520. { IndexOf -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1521. {---------------------------------------------------------------------------}
  1522. FUNCTION TCollection.IndexOf (Item: Pointer): Sw_Integer;
  1523. VAR I: Sw_Integer;
  1524. BEGIN
  1525. If (Count>0) Then Begin { Count is positive }
  1526. For I := 0 To Count-1 Do { For each item }
  1527. If (Items^[I]=Item) Then Begin { Look for match }
  1528. IndexOf := I; { Return index }
  1529. Exit; { Now exit }
  1530. End;
  1531. End;
  1532. IndexOf := -1; { Return index }
  1533. END;
  1534. {--TCollection--------------------------------------------------------------}
  1535. { GetItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1536. {---------------------------------------------------------------------------}
  1537. FUNCTION TCollection.GetItem (Var S: TStream): Pointer;
  1538. BEGIN
  1539. GetItem := S.Get; { Item off stream }
  1540. END;
  1541. {--TCollection--------------------------------------------------------------}
  1542. { LastThat -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1543. {---------------------------------------------------------------------------}
  1544. FUNCTION TCollection.LastThat (Test: Pointer): Pointer;
  1545. VAR I: LongInt;
  1546. BEGIN
  1547. For I := Count DownTo 1 Do
  1548. Begin { Down from last item }
  1549. IF Boolean(Longint(CallPointerLocal(Test,PreviousFramePointer,Items^[I-1]))) THEN
  1550. Begin { Test each item }
  1551. LastThat := Items^[I-1]; { Return item }
  1552. Exit; { Now exit }
  1553. End;
  1554. End;
  1555. LastThat := Nil; { None passed test }
  1556. END;
  1557. {--TCollection--------------------------------------------------------------}
  1558. { FirstThat -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1559. {---------------------------------------------------------------------------}
  1560. FUNCTION TCollection.FirstThat (Test: Pointer): Pointer;
  1561. VAR I: LongInt;
  1562. BEGIN
  1563. For I := 1 To Count Do Begin { Up from first item }
  1564. IF Boolean(Longint(CallPointerLocal(Test,PreviousFramePointer,Items^[I-1]))) THEN
  1565. Begin { Test each item }
  1566. FirstThat := Items^[I-1]; { Return item }
  1567. Exit; { Now exit }
  1568. End;
  1569. End;
  1570. FirstThat := Nil; { None passed test }
  1571. END;
  1572. {--TCollection--------------------------------------------------------------}
  1573. { Pack -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1574. {---------------------------------------------------------------------------}
  1575. PROCEDURE TCollection.Pack;
  1576. VAR I, J: Sw_Integer;
  1577. BEGIN
  1578. I := 0; { Initialize dest }
  1579. J := 0; { Intialize test }
  1580. While (I<Count) AND (J<Limit) Do Begin { Check fully packed }
  1581. If (Items^[J]<>Nil) Then Begin { Found a valid item }
  1582. If (I<>J) Then Begin
  1583. Items^[I] := Items^[J]; { Transfer item }
  1584. Items^[J] := Nil; { Now clear old item }
  1585. End;
  1586. Inc(I); { One item packed }
  1587. End;
  1588. Inc(J); { Next item to test }
  1589. End;
  1590. If (I<Count) Then Count := I; { New packed count }
  1591. END;
  1592. {--TCollection--------------------------------------------------------------}
  1593. { FreeAll -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1594. {---------------------------------------------------------------------------}
  1595. PROCEDURE TCollection.FreeAll;
  1596. VAR I: Sw_Integer;
  1597. BEGIN
  1598. For I := 0 To Count-1 Do FreeItem(At(I)); { Release each item }
  1599. Count := 0; { Clear item count }
  1600. END;
  1601. {--TCollection--------------------------------------------------------------}
  1602. { DeleteAll -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1603. {---------------------------------------------------------------------------}
  1604. PROCEDURE TCollection.DeleteAll;
  1605. BEGIN
  1606. Count := 0; { Clear item count }
  1607. END;
  1608. {--TCollection--------------------------------------------------------------}
  1609. { Free -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1610. {---------------------------------------------------------------------------}
  1611. PROCEDURE TCollection.Free (Item: Pointer);
  1612. BEGIN
  1613. Delete(Item); { Delete from list }
  1614. FreeItem(Item); { Free the item }
  1615. END;
  1616. {--TCollection--------------------------------------------------------------}
  1617. { Insert -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1618. {---------------------------------------------------------------------------}
  1619. PROCEDURE TCollection.Insert (Item: Pointer);
  1620. BEGIN
  1621. AtInsert(Count, Item); { Insert item }
  1622. END;
  1623. {--TCollection--------------------------------------------------------------}
  1624. { Delete -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1625. {---------------------------------------------------------------------------}
  1626. PROCEDURE TCollection.Delete (Item: Pointer);
  1627. BEGIN
  1628. AtDelete(IndexOf(Item)); { Delete from list }
  1629. END;
  1630. {--TCollection--------------------------------------------------------------}
  1631. { AtFree -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1632. {---------------------------------------------------------------------------}
  1633. PROCEDURE TCollection.AtFree (Index: Sw_Integer);
  1634. VAR Item: Pointer;
  1635. BEGIN
  1636. Item := At(Index); { Retreive item ptr }
  1637. AtDelete(Index); { Delete item }
  1638. FreeItem(Item); { Free the item }
  1639. END;
  1640. {--TCollection--------------------------------------------------------------}
  1641. { FreeItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1642. {---------------------------------------------------------------------------}
  1643. PROCEDURE TCollection.FreeItem (Item: Pointer);
  1644. VAR P: PObject;
  1645. BEGIN
  1646. P := PObject(Item); { Convert pointer }
  1647. If (P<>Nil) Then Dispose(P, Done); { Dispose of object }
  1648. END;
  1649. {--TCollection--------------------------------------------------------------}
  1650. { AtDelete -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1651. {---------------------------------------------------------------------------}
  1652. PROCEDURE TCollection.AtDelete (Index: Sw_Integer);
  1653. BEGIN
  1654. If (Index >= 0) AND (Index < Count) Then Begin { Valid index }
  1655. Dec(Count); { One less item }
  1656. If (Count>Index) Then Move(Items^[Index+1],
  1657. Items^[Index], (Count-Index)*Sizeof(Pointer)); { Shuffle items down }
  1658. End Else Error(coIndexError, Index); { Index error }
  1659. END;
  1660. {--TCollection--------------------------------------------------------------}
  1661. { ForEach -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1662. {---------------------------------------------------------------------------}
  1663. PROCEDURE TCollection.ForEach (Action: Pointer);
  1664. VAR I: LongInt;
  1665. BEGIN
  1666. For I := 1 To Count Do { Up from first item }
  1667. CallPointerLocal(Action,PreviousFramePointer,Items^[I-1]); { Call with each item }
  1668. END;
  1669. {--TCollection--------------------------------------------------------------}
  1670. { SetLimit -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1671. {---------------------------------------------------------------------------}
  1672. PROCEDURE TCollection.SetLimit (ALimit: Sw_Integer);
  1673. VAR
  1674. AItems: PItemList;
  1675. BEGIN
  1676. If (ALimit < Count) Then
  1677. ALimit := Count;
  1678. If (ALimit > MaxCollectionSize) Then
  1679. ALimit := MaxCollectionSize;
  1680. If (ALimit <> Limit) Then
  1681. Begin
  1682. If (ALimit = 0) Then
  1683. AItems := Nil
  1684. Else
  1685. Begin
  1686. GetMem(AItems, ALimit * SizeOf(Pointer));
  1687. If (AItems<>Nil) Then
  1688. FillChar(AItems^,ALimit * SizeOf(Pointer), #0);
  1689. End;
  1690. If (AItems<>Nil) OR (ALimit=0) Then
  1691. Begin
  1692. If (AItems <>Nil) AND (Items <> Nil) Then
  1693. Move(Items^, AItems^, Count*SizeOf(Pointer));
  1694. If (Limit <> 0) AND (Items <> Nil) Then
  1695. FreeMem(Items, Limit * SizeOf(Pointer));
  1696. end;
  1697. Items := AItems;
  1698. Limit := ALimit;
  1699. End;
  1700. END;
  1701. {--TCollection--------------------------------------------------------------}
  1702. { Error -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1703. {---------------------------------------------------------------------------}
  1704. PROCEDURE TCollection.Error (Code, Info: Integer);
  1705. BEGIN
  1706. RunError(212 - Code); { Run error }
  1707. END;
  1708. {--TCollection--------------------------------------------------------------}
  1709. { AtPut -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1710. {---------------------------------------------------------------------------}
  1711. PROCEDURE TCollection.AtPut (Index: Sw_Integer; Item: Pointer);
  1712. BEGIN
  1713. If (Index >= 0) AND (Index < Count) Then { Index valid }
  1714. Items^[Index] := Item { Put item in index }
  1715. Else Error(coIndexError, Index); { Index error }
  1716. END;
  1717. {--TCollection--------------------------------------------------------------}
  1718. { AtInsert -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1719. {---------------------------------------------------------------------------}
  1720. PROCEDURE TCollection.AtInsert (Index: Sw_Integer; Item: Pointer);
  1721. VAR I: Sw_Integer;
  1722. BEGIN
  1723. If (Index >= 0) AND (Index <= Count) Then Begin { Valid index }
  1724. If (Count=Limit) Then SetLimit(Limit+Delta); { Expand size if able }
  1725. If (Limit>Count) Then Begin
  1726. If (Index < Count) Then Begin { Not last item }
  1727. For I := Count-1 DownTo Index Do { Start from back }
  1728. Items^[I+1] := Items^[I]; { Move each item }
  1729. End;
  1730. Items^[Index] := Item; { Put item in list }
  1731. Inc(Count); { Inc count }
  1732. End Else Error(coOverflow, Index); { Expand failed }
  1733. End Else Error(coIndexError, Index); { Index error }
  1734. END;
  1735. {--TCollection--------------------------------------------------------------}
  1736. { Store -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1737. {---------------------------------------------------------------------------}
  1738. PROCEDURE TCollection.Store (Var S: TStream);
  1739. PROCEDURE DoPutItem (P: Pointer);{$IFNDEF FPC}FAR;{$ENDIF}
  1740. BEGIN
  1741. PutItem(S, P); { Put item on stream }
  1742. END;
  1743. BEGIN
  1744. S.Write(Count, Sizeof(Count)); { Write count }
  1745. S.Write(Limit, Sizeof(Limit)); { Write limit }
  1746. S.Write(Delta, Sizeof(Delta)); { Write delta }
  1747. ForEach(@DoPutItem); { Each item to stream }
  1748. END;
  1749. {--TCollection--------------------------------------------------------------}
  1750. { PutItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1751. {---------------------------------------------------------------------------}
  1752. PROCEDURE TCollection.PutItem (Var S: TStream; Item: Pointer);
  1753. BEGIN
  1754. S.Put(Item); { Put item on stream }
  1755. END;
  1756. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1757. { TSortedCollection OBJECT METHODS }
  1758. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1759. {--TSortedCollection--------------------------------------------------------}
  1760. { Init -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1761. {---------------------------------------------------------------------------}
  1762. CONSTRUCTOR TSortedCollection.Init (ALimit, ADelta: Sw_Integer);
  1763. BEGIN
  1764. Inherited Init(ALimit, ADelta); { Call ancestor }
  1765. Duplicates := False; { Clear flag }
  1766. END;
  1767. {--TSortedCollection--------------------------------------------------------}
  1768. { Load -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1769. {---------------------------------------------------------------------------}
  1770. CONSTRUCTOR TSortedCollection.Load (Var S: TStream);
  1771. BEGIN
  1772. Inherited Load(S); { Call ancestor }
  1773. S.Read(Duplicates, SizeOf(Duplicates)); { Read duplicate flag }
  1774. END;
  1775. {--TSortedCollection--------------------------------------------------------}
  1776. { KeyOf -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1777. {---------------------------------------------------------------------------}
  1778. FUNCTION TSortedCollection.KeyOf (Item: Pointer): Pointer;
  1779. BEGIN
  1780. KeyOf := Item; { Return item as key }
  1781. END;
  1782. {--TSortedCollection--------------------------------------------------------}
  1783. { IndexOf -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1784. {---------------------------------------------------------------------------}
  1785. FUNCTION TSortedCollection.IndexOf (Item: Pointer): Sw_Integer;
  1786. VAR I, J: Sw_Integer;
  1787. BEGIN
  1788. J := -1; { Preset result }
  1789. If Search(KeyOf(Item), I) Then Begin { Search for item }
  1790. If Duplicates Then { Duplicates allowed }
  1791. While (I < Count) AND (Item <> Items^[I]) Do
  1792. Inc(I); { Count duplicates }
  1793. If (I < Count) Then J := I; { Index result }
  1794. End;
  1795. IndexOf := J; { Return result }
  1796. END;
  1797. {--TSortedCollection--------------------------------------------------------}
  1798. { Compare -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1799. {---------------------------------------------------------------------------}
  1800. FUNCTION TSortedCollection.Compare (Key1, Key2: Pointer): Sw_Integer;
  1801. BEGIN
  1802. Abstract; { Abstract method }
  1803. Compare:=0;
  1804. END;
  1805. {--TSortedCollection--------------------------------------------------------}
  1806. { Search -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1807. {---------------------------------------------------------------------------}
  1808. FUNCTION TSortedCollection.Search (Key: Pointer; Var Index: Sw_Integer): Boolean;
  1809. VAR L, H, I, C: Sw_Integer;
  1810. BEGIN
  1811. Search := False; { Preset failure }
  1812. L := 0; { Start count }
  1813. H := Count - 1; { End count }
  1814. While (L <= H) Do Begin
  1815. I := (L + H) SHR 1; { Mid point }
  1816. C := Compare(KeyOf(Items^[I]), Key); { Compare with key }
  1817. If (C < 0) Then L := I + 1 Else Begin { Item to left }
  1818. H := I - 1; { Item to right }
  1819. If C = 0 Then Begin { Item match found }
  1820. Search := True; { Result true }
  1821. If NOT Duplicates Then L := I; { Force kick out }
  1822. End;
  1823. End;
  1824. End;
  1825. Index := L; { Return result }
  1826. END;
  1827. {--TSortedCollection--------------------------------------------------------}
  1828. { Insert -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1829. {---------------------------------------------------------------------------}
  1830. PROCEDURE TSortedCollection.Insert (Item: Pointer);
  1831. VAR I: Sw_Integer;
  1832. BEGIN
  1833. If NOT Search(KeyOf(Item), I) OR Duplicates Then { Item valid }
  1834. AtInsert(I, Item); { Insert the item }
  1835. END;
  1836. {--TSortedCollection--------------------------------------------------------}
  1837. { Store -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1838. {---------------------------------------------------------------------------}
  1839. PROCEDURE TSortedCollection.Store (Var S: TStream);
  1840. BEGIN
  1841. TCollection.Store(S); { Call ancestor }
  1842. S.Write(Duplicates, SizeOf(Duplicates)); { Write duplicate flag }
  1843. END;
  1844. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1845. { TStringCollection OBJECT METHODS }
  1846. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1847. {--TStringCollection--------------------------------------------------------}
  1848. { GetItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1849. {---------------------------------------------------------------------------}
  1850. FUNCTION TStringCollection.GetItem (Var S: TStream): Pointer;
  1851. BEGIN
  1852. GetItem := S.ReadStr; { Get new item }
  1853. END;
  1854. {--TStringCollection--------------------------------------------------------}
  1855. { Compare -> Platforms DOS/DPMI/WIN/OS2 - Checked 21Aug97 LdB }
  1856. {---------------------------------------------------------------------------}
  1857. FUNCTION TStringCollection.Compare (Key1, Key2: Pointer): Sw_Integer;
  1858. VAR I, J: Sw_Integer; P1, P2: PString;
  1859. BEGIN
  1860. P1 := PString(Key1); { String 1 pointer }
  1861. P2 := PString(Key2); { String 2 pointer }
  1862. If (Length(P1^)<Length(P2^)) Then J := Length(P1^)
  1863. Else J := Length(P2^); { Shortest length }
  1864. I := 1; { First character }
  1865. While (I<J) AND (P1^[I]=P2^[I]) Do Inc(I); { Scan till fail }
  1866. If (I=J) Then Begin { Possible match }
  1867. { * REMARK * - Bug fix 21 August 1997 }
  1868. If (P1^[I]<P2^[I]) Then Compare := -1 Else { String1 < String2 }
  1869. If (P1^[I]>P2^[I]) Then Compare := 1 Else { String1 > String2 }
  1870. If (Length(P1^)>Length(P2^)) Then Compare := 1 { String1 > String2 }
  1871. Else If (Length(P1^)<Length(P2^)) Then { String1 < String2 }
  1872. Compare := -1 Else Compare := 0; { String1 = String2 }
  1873. { * REMARK END * - Leon de Boer }
  1874. End Else If (P1^[I]<P2^[I]) Then Compare := -1 { String1 < String2 }
  1875. Else Compare := 1; { String1 > String2 }
  1876. END;
  1877. {--TStringCollection--------------------------------------------------------}
  1878. { FreeItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1879. {---------------------------------------------------------------------------}
  1880. PROCEDURE TStringCollection.FreeItem (Item: Pointer);
  1881. BEGIN
  1882. DisposeStr(Item); { Dispose item }
  1883. END;
  1884. {--TStringCollection--------------------------------------------------------}
  1885. { PutItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 22May96 LdB }
  1886. {---------------------------------------------------------------------------}
  1887. PROCEDURE TStringCollection.PutItem (Var S: TStream; Item: Pointer);
  1888. BEGIN
  1889. S.WriteStr(Item); { Write string }
  1890. END;
  1891. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1892. { TStrCollection OBJECT METHODS }
  1893. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1894. {--TStrCollection-----------------------------------------------------------}
  1895. { Compare -> Platforms DOS/DPMI/WIN/OS2 - Checked 23May96 LdB }
  1896. {---------------------------------------------------------------------------}
  1897. FUNCTION TStrCollection.Compare (Key1, Key2: Pointer): Sw_Integer;
  1898. VAR I, J: Sw_Integer; P1, P2: PByteArray;
  1899. BEGIN
  1900. P1 := PByteArray(Key1); { PChar 1 pointer }
  1901. P2 := PByteArray(Key2); { PChar 2 pointer }
  1902. I := 0; { Preset no size }
  1903. If (P1<>Nil) Then While (P1^[I]<>0) Do Inc(I); { PChar 1 length }
  1904. J := 0; { Preset no size }
  1905. If (P2<>Nil) Then While (P2^[J]<>0) Do Inc(J); { PChar 2 length }
  1906. If (I < J) Then J := I; { Shortest length }
  1907. I := 0; { First character }
  1908. While (I<J) AND (P1^[I]=P2^[I]) Do Inc(I); { Scan till fail }
  1909. If (P1^[I]=P2^[I]) Then Compare := 0 Else { Strings matched }
  1910. If (P1^[I]<P2^[I]) Then Compare := -1 Else { String1 < String2 }
  1911. Compare := 1; { String1 > String2 }
  1912. END;
  1913. {--TStrCollection-----------------------------------------------------------}
  1914. { GetItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 23May96 LdB }
  1915. {---------------------------------------------------------------------------}
  1916. FUNCTION TStrCollection.GetItem (Var S: TStream): Pointer;
  1917. BEGIN
  1918. GetItem := S.StrRead; { Get string item }
  1919. END;
  1920. {--TStrCollection-----------------------------------------------------------}
  1921. { FreeItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 23May96 LdB }
  1922. {---------------------------------------------------------------------------}
  1923. PROCEDURE TStrCollection.FreeItem (Item: Pointer);
  1924. VAR I: Sw_Integer; P: PByteArray;
  1925. BEGIN
  1926. If (Item<>Nil) Then Begin { Item is valid }
  1927. P := PByteArray(Item); { Create byte pointer }
  1928. I := 0; { Preset no size }
  1929. While (P^[I]<>0) Do Inc(I); { Find PChar end }
  1930. FreeMem(Item, I+1); { Release memory }
  1931. End;
  1932. END;
  1933. {--TStrCollection-----------------------------------------------------------}
  1934. { PutItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 23May96 LdB }
  1935. {---------------------------------------------------------------------------}
  1936. PROCEDURE TStrCollection.PutItem (Var S: TStream; Item: Pointer);
  1937. BEGIN
  1938. S.StrWrite(Item); { Write the string }
  1939. END;
  1940. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1941. { TUnSortedStrCollection OBJECT METHODS }
  1942. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1943. {--TUnSortedCollection------------------------------------------------------}
  1944. { Insert -> Platforms DOS/DPMI/WIN/OS2 - Checked 23May96 LdB }
  1945. {---------------------------------------------------------------------------}
  1946. PROCEDURE TUnSortedStrCollection.Insert (Item: Pointer);
  1947. BEGIN
  1948. AtInsert(Count, Item); { Insert - NO sorting }
  1949. END;
  1950. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1951. { TResourceItem RECORD }
  1952. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1953. TYPE
  1954. TResourceItem = packed RECORD
  1955. Posn: LongInt; { Resource position }
  1956. Size: LongInt; { Resource size }
  1957. Key : String; { Resource key }
  1958. End;
  1959. PResourceItem = ^TResourceItem;
  1960. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1961. { TResourceCollection OBJECT METHODS }
  1962. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  1963. {--TResourceCollection------------------------------------------------------}
  1964. { KeyOf -> Platforms DOS/DPMI/WIN/OS2 - Checked 24May96 LdB }
  1965. {---------------------------------------------------------------------------}
  1966. FUNCTION TResourceCollection.KeyOf (Item: Pointer): Pointer;
  1967. BEGIN
  1968. KeyOf := @PResourceItem(Item)^.Key; { Pointer to key }
  1969. END;
  1970. {--TResourceCollection------------------------------------------------------}
  1971. { GetItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 24May96 LdB }
  1972. {---------------------------------------------------------------------------}
  1973. FUNCTION TResourceCollection.GetItem (Var S: TStream): Pointer;
  1974. VAR B: Byte; Pos: Longint; Size: Longint; Ts: String; P: PResourceItem;
  1975. BEGIN
  1976. S.Read(Pos, SizeOf(Pos)); { Read position }
  1977. S.Read(Size, SizeOf(Size)); { Read size }
  1978. S.Read(B, 1); { Read key length }
  1979. GetMem(P, B + (SizeOf(TResourceItem) -
  1980. SizeOf(Ts) + 1)); { Allocate min memory }
  1981. If (P<>Nil) Then Begin { If allocate works }
  1982. P^.Posn := Pos; { Xfer position }
  1983. P^.Size := Size; { Xfer size }
  1984. P^.Key[0] := Char(B); { Xfer string length }
  1985. S.Read(P^.Key[1], B); { Xfer string data }
  1986. End;
  1987. GetItem := P; { Return pointer }
  1988. END;
  1989. {--TResourceCollection------------------------------------------------------}
  1990. { FreeItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 24May96 LdB }
  1991. {---------------------------------------------------------------------------}
  1992. PROCEDURE TResourceCollection.FreeItem (Item: Pointer);
  1993. VAR Ts: String;
  1994. BEGIN
  1995. If (Item<>Nil) Then FreeMem(Item,
  1996. SizeOf(TResourceItem) - SizeOf(Ts) +
  1997. Length(PResourceItem(Item)^.Key) + 1); { Release memory }
  1998. END;
  1999. {--TResourceCollection------------------------------------------------------}
  2000. { PutItem -> Platforms DOS/DPMI/WIN/OS2 - Checked 24May96 LdB }
  2001. {---------------------------------------------------------------------------}
  2002. PROCEDURE TResourceCollection.PutItem (Var S: TStream; Item: Pointer);
  2003. VAR Ts: String;
  2004. BEGIN
  2005. If (Item<>Nil) Then S.Write(PResourceItem(Item)^,
  2006. SizeOf(TResourceItem) - SizeOf(Ts) +
  2007. Length(PResourceItem(Item)^.Key) + 1); { Write to stream }
  2008. END;
  2009. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2010. { PRIVATE RESOURCE MANAGER CONSTANTS }
  2011. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2012. CONST
  2013. RStreamMagic: LongInt = $52504246; { 'FBPR' }
  2014. RStreamBackLink: LongInt = $4C424246; { 'FBBL' }
  2015. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2016. { PRIVATE RESOURCE MANAGER TYPES }
  2017. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2018. TYPE
  2019. {$IFDEF NewExeFormat} { New EXE format }
  2020. TExeHeader = packed RECORD
  2021. eHdrSize: Word;
  2022. eMinAbove: Word;
  2023. eMaxAbove: Word;
  2024. eInitSS: Word;
  2025. eInitSP: Word;
  2026. eCheckSum: Word;
  2027. eInitPC: Word;
  2028. eInitCS: Word;
  2029. eRelocOfs: Word;
  2030. eOvlyNum: Word;
  2031. eRelocTab: Word;
  2032. eSpace: Array[1..30] of Byte;
  2033. eNewHeader: Word;
  2034. END;
  2035. {$ENDIF}
  2036. THeader = packed RECORD
  2037. Signature: Word;
  2038. Case Integer Of
  2039. 0: (
  2040. LastCount: Word;
  2041. PageCount: Word;
  2042. ReloCount: Word);
  2043. 1: (
  2044. InfoType: Word;
  2045. InfoSize: Longint);
  2046. End;
  2047. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2048. { TResourceFile OBJECT METHODS }
  2049. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2050. {--TResourceFile------------------------------------------------------------}
  2051. { Init -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2052. {---------------------------------------------------------------------------}
  2053. CONSTRUCTOR TResourceFile.Init(AStream: PStream);
  2054. VAR Found, Stop: Boolean; Header: THeader;
  2055. {$IFDEF NewExeFormat} ExeHeader: TExeHeader; {$ENDIF}
  2056. BEGIN
  2057. TObject.Init; { Initialize object }
  2058. Found := False; { Preset false }
  2059. If (AStream<>Nil) Then Begin
  2060. Stream := AStream; { Hold stream }
  2061. BasePos := Stream^.GetPos; { Get position }
  2062. Repeat
  2063. Stop := True; { Preset stop }
  2064. If (BasePos <= Stream^.GetSize-SizeOf(THeader))
  2065. Then Begin { Valid file header }
  2066. Stream^.Seek(BasePos); { Seek to position }
  2067. Stream^.Read(Header, SizeOf(THeader)); { Read header }
  2068. Case Header.Signature Of
  2069. {$IFDEF NewExeFormat} { New format file }
  2070. $5A4D: Begin
  2071. Stream^.Read(ExeHeader, SizeOf(TExeHeader));
  2072. BasePos := ExeHeader.eNewHeader; { Hold position }
  2073. Stop := False; { Clear stop flag }
  2074. End;
  2075. $454E: Begin
  2076. BasePos := Stream^.GetSize - 8; { Hold position }
  2077. Stop := False; { Clear stop flag }
  2078. End;
  2079. $4246: Begin
  2080. Stop := False; { Clear stop flag }
  2081. Case Header.Infotype Of
  2082. $5250: Begin { Found Resource }
  2083. Found := True; { Found flag is true }
  2084. Stop := True; { Set stop flag }
  2085. End;
  2086. $4C42: Dec(BasePos, Header.InfoSize-8);{ Found BackLink }
  2087. $4648: Dec(BasePos, SizeOf(THeader)*2);{ Found HelpFile }
  2088. Else Stop := True; { Set stop flag }
  2089. End;
  2090. End;
  2091. $424E: If Header.InfoType = $3230 { Found Debug Info }
  2092. Then Begin
  2093. Dec(BasePos, Header.InfoSize); { Adjust position }
  2094. Stop := False; { Clear stop flag }
  2095. End;
  2096. {$ELSE}
  2097. $5A4D: Begin
  2098. Inc(BasePos, LongInt(Header.PageCount)*512
  2099. - (-Header.LastCount AND 511)); { Calc position }
  2100. Stop := False; { Clear stop flag }
  2101. End;
  2102. $4246: If Header.InfoType = $5250 Then { Header was found }
  2103. Found := True Else Begin
  2104. Inc(BasePos, Header.InfoSize + 8); { Adjust position }
  2105. Stop := False; { Clear stop flag }
  2106. End;
  2107. {$ENDIF}
  2108. End;
  2109. End;
  2110. Until Stop; { Until flag is set }
  2111. End;
  2112. If Found Then Begin { Resource was found }
  2113. Stream^.Seek(BasePos + SizeOf(LongInt) * 2); { Seek to position }
  2114. Stream^.Read(IndexPos, SizeOf(LongInt)); { Read index position }
  2115. Stream^.Seek(BasePos + IndexPos); { Seek to resource }
  2116. Index.Load(Stream^); { Load resource }
  2117. End Else Begin
  2118. IndexPos := SizeOf(LongInt) * 3; { Set index position }
  2119. Index.Init(0, 8); { Set index }
  2120. End;
  2121. END;
  2122. {--TResourceFile------------------------------------------------------------}
  2123. { Done -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2124. {---------------------------------------------------------------------------}
  2125. DESTRUCTOR TResourceFile.Done;
  2126. BEGIN
  2127. Flush; { Flush the file }
  2128. Index.Done; { Dispose of index }
  2129. If (Stream<>Nil) Then Dispose(Stream, Done); { Dispose of stream }
  2130. END;
  2131. {--TResourceFile------------------------------------------------------------}
  2132. { Count -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2133. {---------------------------------------------------------------------------}
  2134. FUNCTION TResourceFile.Count: Sw_Integer;
  2135. BEGIN
  2136. Count := Index.Count; { Return index count }
  2137. END;
  2138. {--TResourceFile------------------------------------------------------------}
  2139. { KeyAt -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2140. {---------------------------------------------------------------------------}
  2141. FUNCTION TResourceFile.KeyAt (I: Sw_Integer): String;
  2142. BEGIN
  2143. KeyAt := PResourceItem(Index.At(I))^.Key; { Return key }
  2144. END;
  2145. {--TResourceFile------------------------------------------------------------}
  2146. { Get -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2147. {---------------------------------------------------------------------------}
  2148. FUNCTION TResourceFile.Get (Key: String): PObject;
  2149. VAR I: Sw_Integer;
  2150. BEGIN
  2151. If (Stream = Nil) OR (NOT Index.Search(@Key, I)) { No match on key }
  2152. Then Get := Nil Else Begin
  2153. Stream^.Seek(BasePos +
  2154. PResourceItem(Index.At(I))^.Posn); { Seek to position }
  2155. Get := Stream^.Get; { Get item }
  2156. End;
  2157. END;
  2158. {--TResourceFile------------------------------------------------------------}
  2159. { SwitchTo -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2160. {---------------------------------------------------------------------------}
  2161. FUNCTION TResourceFile.SwitchTo (AStream: PStream; Pack: Boolean): PStream;
  2162. VAR NewBasePos: LongInt;
  2163. PROCEDURE DoCopyResource (Item: PResourceItem);{$IFNDEF FPC}FAR;{$ENDIF}
  2164. BEGIN
  2165. Stream^.Seek(BasePos + Item^.Posn); { Move stream position }
  2166. Item^.Posn := AStream^.GetPos - NewBasePos; { Hold new position }
  2167. AStream^.CopyFrom(Stream^, Item^.Size); { Copy the item }
  2168. END;
  2169. BEGIN
  2170. SwitchTo := Stream; { Preset return }
  2171. If (AStream<>Nil) AND (Stream<>Nil) Then Begin { Both streams valid }
  2172. NewBasePos := AStream^.GetPos; { Get position }
  2173. If Pack Then Begin
  2174. AStream^.Seek(NewBasePos + SizeOf(LongInt)*3); { Seek to position }
  2175. Index.ForEach(@DoCopyResource); { Copy each resource }
  2176. IndexPos := AStream^.GetPos - NewBasePos; { Hold index position }
  2177. End Else Begin
  2178. Stream^.Seek(BasePos); { Seek to position }
  2179. AStream^.CopyFrom(Stream^, IndexPos); { Copy the resource }
  2180. End;
  2181. Stream := AStream; { Hold new stream }
  2182. BasePos := NewBasePos; { New base position }
  2183. Modified := True; { Set modified flag }
  2184. End;
  2185. END;
  2186. {--TResourceFile------------------------------------------------------------}
  2187. { Flush -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2188. {---------------------------------------------------------------------------}
  2189. PROCEDURE TResourceFile.Flush;
  2190. VAR ResSize: LongInt; LinkSize: LongInt;
  2191. BEGIN
  2192. If (Modified) AND (Stream<>Nil) Then Begin { We have modification }
  2193. Stream^.Seek(BasePos + IndexPos); { Seek to position }
  2194. Index.Store(Stream^); { Store the item }
  2195. ResSize := Stream^.GetPos - BasePos; { Hold position }
  2196. LinkSize := ResSize + SizeOf(LongInt) * 2; { Hold link size }
  2197. Stream^.Write(RStreamBackLink, SizeOf(LongInt)); { Write link back }
  2198. Stream^.Write(LinkSize, SizeOf(LongInt)); { Write link size }
  2199. Stream^.Seek(BasePos); { Move stream position }
  2200. Stream^.Write(RStreamMagic, SizeOf(LongInt)); { Write number }
  2201. Stream^.Write(ResSize, SizeOf(LongInt)); { Write record size }
  2202. Stream^.Write(IndexPos, SizeOf(LongInt)); { Write index position }
  2203. Stream^.Flush; { Flush the stream }
  2204. End;
  2205. Modified := False; { Clear modified flag }
  2206. END;
  2207. {--TResourceFile------------------------------------------------------------}
  2208. { Delete -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2209. {---------------------------------------------------------------------------}
  2210. PROCEDURE TResourceFile.Delete (Key: String);
  2211. VAR I: Sw_Integer;
  2212. BEGIN
  2213. If Index.Search(@Key, I) Then Begin { Search for key }
  2214. Index.Free(Index.At(I)); { Delete from index }
  2215. Modified := True; { Set modified flag }
  2216. End;
  2217. END;
  2218. {--TResourceFile------------------------------------------------------------}
  2219. { Put -> Platforms DOS/DPMI/WIN/OS2 - Checked 18Jun96 LdB }
  2220. {---------------------------------------------------------------------------}
  2221. PROCEDURE TResourceFile.Put (Item: PObject; Key: String);
  2222. VAR I: Sw_Integer; Ts: String; P: PResourceItem;
  2223. BEGIN
  2224. If (Stream=Nil) Then Exit; { Stream not valid }
  2225. If Index.Search(@Key, I) Then P := Index.At(I) { Search for item }
  2226. Else Begin
  2227. GetMem(P, Length(Key) + (SizeOf(TResourceItem) -
  2228. SizeOf(Ts) + 1)); { Allocate memory }
  2229. If (P<>Nil) Then Begin
  2230. P^.Key := Key; { Store key }
  2231. Index.AtInsert(I, P); { Insert item }
  2232. End;
  2233. End;
  2234. If (P<>Nil) Then Begin
  2235. P^.Posn := IndexPos; { Set index position }
  2236. Stream^.Seek(BasePos + IndexPos); { Seek file position }
  2237. Stream^.Put(Item); { Put item on stream }
  2238. IndexPos := Stream^.GetPos - BasePos; { Hold index position }
  2239. P^.Size := IndexPos - P^.Posn; { Calc size }
  2240. Modified := True; { Set modified flag }
  2241. End;
  2242. END;
  2243. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2244. { TStringList OBJECT METHODS }
  2245. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2246. {--TStringList--------------------------------------------------------------}
  2247. { Load -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2248. {---------------------------------------------------------------------------}
  2249. CONSTRUCTOR TStringList.Load (Var S: TStream);
  2250. VAR Size: Word;
  2251. BEGIN
  2252. Stream := @S; { Hold stream pointer }
  2253. S.Read(Size, SizeOf(Word)); { Read size }
  2254. BasePos := S.GetPos; { Hold position }
  2255. S.Seek(BasePos + Size); { Seek to position }
  2256. S.Read(IndexSize, SizeOf(Integer)); { Read index size }
  2257. GetMem(Index, IndexSize * SizeOf(TStrIndexRec)); { Allocate memory }
  2258. S.Read(Index^, IndexSize * SizeOf(TStrIndexRec)); { Read indexes }
  2259. END;
  2260. {--TStringList--------------------------------------------------------------}
  2261. { Done -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2262. {---------------------------------------------------------------------------}
  2263. DESTRUCTOR TStringList.Done;
  2264. BEGIN
  2265. FreeMem(Index, IndexSize * SizeOf(TStrIndexRec)); { Release memory }
  2266. END;
  2267. {--TStringList--------------------------------------------------------------}
  2268. { Get -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2269. {---------------------------------------------------------------------------}
  2270. FUNCTION TStringList.Get (Key: Sw_Word): String;
  2271. VAR I: Word; S: String;
  2272. BEGIN
  2273. S := ''; { Preset empty string }
  2274. If (IndexSize>0) Then Begin { We must have strings }
  2275. I := 0; { First entry }
  2276. While (I<IndexSize) AND (S='') Do Begin
  2277. If ((Key - Index^[I].Key)<Index^[I].Count) { Diff less than count }
  2278. Then ReadStr(S, Index^[I].Offset,
  2279. Key-Index^[I].Key); { Read the string }
  2280. Inc(I); { Next entry }
  2281. End;
  2282. End;
  2283. Get := S; { Return empty string }
  2284. END;
  2285. {***************************************************************************}
  2286. { TStringList PRIVATE METHODS }
  2287. {***************************************************************************}
  2288. {--TStringLis---------------------------------------------------------------}
  2289. { ReadStr -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2290. {---------------------------------------------------------------------------}
  2291. PROCEDURE TStringList.ReadStr (Var S: String; Offset, Skip: Sw_Word);
  2292. BEGIN
  2293. Stream^.Seek(BasePos + Offset); { Seek to position }
  2294. Inc(Skip); { Adjust skip }
  2295. Repeat
  2296. Stream^.Read(S[0], 1); { Read string size }
  2297. Stream^.Read(S[1], Ord(S[0])); { Read string data }
  2298. Dec(Skip); { One string read }
  2299. Until (Skip = 0);
  2300. END;
  2301. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2302. { TStrListMaker OBJECT METHODS }
  2303. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2304. {--TStrListMaker------------------------------------------------------------}
  2305. { Init -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2306. {---------------------------------------------------------------------------}
  2307. CONSTRUCTOR TStrListMaker.Init (AStrSize, AIndexSize: Sw_Word);
  2308. BEGIN
  2309. Inherited Init; { Call ancestor }
  2310. StrSize := AStrSize; { Hold size }
  2311. IndexSize := AIndexSize; { Hold index size }
  2312. GetMem(Strings, AStrSize); { Allocate memory }
  2313. GetMem(Index, AIndexSize * SizeOf(TStrIndexRec)); { Allocate memory }
  2314. END;
  2315. {--TStrListMaker------------------------------------------------------------}
  2316. { Done -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2317. {---------------------------------------------------------------------------}
  2318. DESTRUCTOR TStrListMaker.Done;
  2319. BEGIN
  2320. FreeMem(Index, IndexSize * SizeOf(TStrIndexRec)); { Free index memory }
  2321. FreeMem(Strings, StrSize); { Free data memory }
  2322. END;
  2323. {--TStrListMaker------------------------------------------------------------}
  2324. { Put -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2325. {---------------------------------------------------------------------------}
  2326. PROCEDURE TStrListMaker.Put (Key: Sw_Word; S: String);
  2327. BEGIN
  2328. If (Cur.Count = 16) OR (Key <> Cur.Key + Cur.Count)
  2329. Then CloseCurrent; { Close current }
  2330. If (Cur.Count = 0) Then Begin
  2331. Cur.Key := Key; { Set key }
  2332. Cur.Offset := StrPos; { Set offset }
  2333. End;
  2334. Inc(Cur.Count); { Inc count }
  2335. Move(S, Strings^[StrPos], Length(S) + 1); { Move string data }
  2336. Inc(StrPos, Length(S) + 1); { Adjust position }
  2337. END;
  2338. {--TStrListMaker------------------------------------------------------------}
  2339. { Store -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2340. {---------------------------------------------------------------------------}
  2341. PROCEDURE TStrListMaker.Store (Var S: TStream);
  2342. BEGIN
  2343. CloseCurrent; { Close all current }
  2344. S.Write(StrPos, SizeOf(Word)); { Write position }
  2345. S.Write(Strings^, StrPos); { Write string data }
  2346. S.Write(IndexPos, SizeOf(Word)); { Write index position }
  2347. S.Write(Index^, IndexPos * SizeOf(TStrIndexRec)); { Write indexes }
  2348. END;
  2349. {***************************************************************************}
  2350. { TStrListMaker PRIVATE METHODS }
  2351. {***************************************************************************}
  2352. {--TStrListMaker------------------------------------------------------------}
  2353. { CloseCurrent -> Platforms DOS/DPMI/WIN/OS2 - Checked 30Jun97 LdB }
  2354. {---------------------------------------------------------------------------}
  2355. PROCEDURE TStrListMaker.CloseCurrent;
  2356. BEGIN
  2357. If (Cur.Count <> 0) Then Begin
  2358. Index^[IndexPos] := Cur; { Hold index position }
  2359. Inc(IndexPos); { Next index }
  2360. Cur.Count := 0; { Adjust count }
  2361. End;
  2362. END;
  2363. {***************************************************************************}
  2364. { INTERFACE ROUTINES }
  2365. {***************************************************************************}
  2366. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2367. { DYNAMIC STRING INTERFACE ROUTINES }
  2368. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2369. {---------------------------------------------------------------------------}
  2370. { NewStr -> Platforms DOS/DPMI/WINDOWS/OS2 - Checked 12Jun96 LdB }
  2371. {---------------------------------------------------------------------------}
  2372. FUNCTION NewStr (Const S: String): PString;
  2373. VAR P: PString;
  2374. BEGIN
  2375. If (S = '') Then P := Nil Else Begin { Return nil }
  2376. GetMem(P, Length(S) + 1); { Allocate memory }
  2377. If (P<>Nil) Then P^ := S; { Hold string }
  2378. End;
  2379. NewStr := P; { Return result }
  2380. END;
  2381. {---------------------------------------------------------------------------}
  2382. { DisposeStr -> Platforms DOS/DPMI/WINDOWS/OS2 - Checked 12Jun96 LdB }
  2383. {---------------------------------------------------------------------------}
  2384. PROCEDURE DisposeStr (P: PString);
  2385. BEGIN
  2386. If (P <> Nil) Then FreeMem(P, Length(P^) + 1); { Release memory }
  2387. END;
  2388. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2389. { STREAM INTERFACE ROUTINES }
  2390. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2391. {---------------------------------------------------------------------------}
  2392. { Abstract -> Platforms DOS/DPMI/WINDOWS/OS2 - Checked 12Jun96 LdB }
  2393. {---------------------------------------------------------------------------}
  2394. PROCEDURE Abstract;
  2395. BEGIN
  2396. RunError(211); { Abstract error }
  2397. END;
  2398. {---------------------------------------------------------------------------}
  2399. { RegisterObjects -> Platforms DOS/DPMI/WINDOWS/OS2 - Checked 02Sep97 LdB }
  2400. {---------------------------------------------------------------------------}
  2401. PROCEDURE RegisterObjects;
  2402. BEGIN
  2403. RegisterType(RCollection); { Register object }
  2404. RegisterType(RStringCollection); { Register object }
  2405. RegisterType(RStrCollection); { Register object }
  2406. END;
  2407. {---------------------------------------------------------------------------}
  2408. { RegisterType -> Platforms DOS/DPMI/WINDOWS/OS2 - Checked 02Sep97 LdB }
  2409. {---------------------------------------------------------------------------}
  2410. PROCEDURE RegisterType (Var S: TStreamRec);
  2411. VAR P: PStreamRec;
  2412. BEGIN
  2413. P := StreamTypes; { Current reg list }
  2414. While (P <> Nil) AND (P^.ObjType <> S.ObjType)
  2415. Do P := P^.Next; { Find end of chain }
  2416. If (P = Nil) AND (S.ObjType <> 0) Then Begin { Valid end found }
  2417. S.Next := StreamTypes; { Chain the list }
  2418. StreamTypes := @S; { We are now first }
  2419. End Else RegisterError; { Register the error }
  2420. END;
  2421. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2422. { GENERAL FUNCTION INTERFACE ROUTINES }
  2423. {+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
  2424. {---------------------------------------------------------------------------}
  2425. { LongMul -> Platforms DOS/DPMI/WINDOWS/OS2 - Checked 04Sep97 LdB }
  2426. {---------------------------------------------------------------------------}
  2427. FUNCTION LongMul (X, Y: Integer): LongInt;
  2428. BEGIN
  2429. LongMul:=Longint(X*Y);
  2430. END;
  2431. {---------------------------------------------------------------------------}
  2432. { LongDiv -> Platforms DOS/DPMI/WINDOWS/OS2 - Checked 04Sep97 LdB }
  2433. {---------------------------------------------------------------------------}
  2434. FUNCTION LongDiv (X: LongInt; Y: Integer): Integer;
  2435. BEGIN
  2436. LongDiv := Integer(X DIV Y);
  2437. END;
  2438. END.
  2439. {
  2440. $Log$
  2441. Revision 1.24 1999-01-12 14:21:50 peter
  2442. * fixed TColletcion.AtInsert
  2443. Revision 1.23 1999/01/06 10:11:06 daniel
  2444. * Removed on more handle:=-1 statement
  2445. Revision 1.22 1998/12/30 10:26:16 peter
  2446. * reinserted old version, because daniel skipped 3 versions !!
  2447. Revision 1.19 1998/12/18 17:21:28 peter
  2448. * fixed firstthat,lastthat
  2449. Revision 1.18 1998/12/16 21:57:20 peter
  2450. * fixed currentframe,previousframe
  2451. + testcall to test the callspec unit
  2452. Revision 1.17 1998/12/16 00:22:25 peter
  2453. * more temp symbols removed
  2454. Revision 1.16 1998/12/08 10:11:27 peter
  2455. * tpoint contains now sw_integer (needed to support 64k files in the
  2456. editor)
  2457. Revision 1.15 1998/11/26 14:41:22 michael
  2458. + Fixed TREsourcefile.init
  2459. Revision 1.14 1998/11/24 17:11:22 peter
  2460. * made a real fpc only version, no platform.inc
  2461. * applied fixes from the mailinglist
  2462. + included some routines from callspec
  2463. Revision 1.13 1998/11/16 10:21:24 peter
  2464. * fixes for H+
  2465. Revision 1.12 1998/11/12 11:54:50 peter
  2466. * fixed for 0.99.8
  2467. Revision 1.11 1998/11/12 11:45:09 peter
  2468. + released object registration
  2469. Revision 1.10 1998/10/23 16:51:18 pierre
  2470. * vmtlink type changed to pointer
  2471. Revision 1.9 1998/10/22 18:23:55 peter
  2472. + packed record for conversion records
  2473. Revision 1.8 1998/09/09 15:29:02 peter
  2474. * removed some warnings
  2475. Revision 1.7 1998/07/15 12:08:33 carl
  2476. + Atari TOS support
  2477. Revision 1.6 1998/07/08 12:00:25 carl
  2478. * fixed problem with m68k asm syntax
  2479. * i386_att put back in, and only in cpu86 defined
  2480. Revision 1.4 1998/05/30 14:24:42 peter
  2481. * ATT asmparsing always
  2482. Revision 1.3 1998/05/25 09:50:04 peter
  2483. * Platform.inc -> platform.inc
  2484. Revision 1.2 1998/05/21 19:30:58 peter
  2485. * objects compiles for linux
  2486. + assign(pchar), assign(char), rename(pchar), rename(char)
  2487. * fixed read_text_as_array
  2488. + read_text_as_pchar which was not yet in the rtl
  2489. }