objects.pp 130 KB

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