objects.pp 132 KB

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