objects.pp 134 KB

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