objects.pp 132 KB

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