objects.tex 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1998, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \chapter{The Objects unit.}
  22. This chapter documents the \file{objects} unit. The unit was implemented by
  23. many people, and was mainly taken from the FreeVision sources.
  24. The methods and fields that are in a \var{Private} part of an object
  25. declaration have been left out of this documentation.
  26. \section{Constants}
  27. The following constants are error codes, returned by the various stream
  28. objects.
  29. \begin{verbatim}
  30. CONST
  31. stOk = 0; { No stream error }
  32. stError = -1; { Access error }
  33. stInitError = -2; { Initialize error }
  34. stReadError = -3; { Stream read error }
  35. stWriteError = -4; { Stream write error }
  36. stGetError = -5; { Get object error }
  37. stPutError = -6; { Put object error }
  38. stSeekError = -7; { Seek error in stream }
  39. stOpenError = -8; { Error opening stream }
  40. \end{verbatim}
  41. These constants can be passed to constructors of file streams:
  42. \begin{verbatim}
  43. CONST
  44. stCreate = $3C00; { Create new file }
  45. stOpenRead = $3D00; { Read access only }
  46. stOpenWrite = $3D01; { Write access only }
  47. stOpen = $3D02; { Read/write access }
  48. \end{verbatim}
  49. The following constants are error codes, returned by the collection list
  50. objects:
  51. \begin{verbatim}
  52. CONST
  53. coIndexError = -1; { Index out of range }
  54. coOverflow = -2; { Overflow }
  55. \end{verbatim}
  56. Maximum data sizes (used in determining how many data can be used.
  57. \begin{verbatim}
  58. CONST
  59. MaxBytes = 128*1024*1024; { Maximum data size }
  60. MaxWords = MaxBytes DIV SizeOf(Word); { Max word data size }
  61. MaxPtrs = MaxBytes DIV SizeOf(Pointer); { Max ptr data size }
  62. MaxCollectionSize = MaxBytes DIV SizeOf(Pointer); { Max collection size }
  63. \end{verbatim}
  64. \section{Types}
  65. The follwing auxiliary types are defined:
  66. \begin{verbatim}
  67. TYPE
  68. { Character set }
  69. TCharSet = SET Of Char;
  70. PCharSet = ^TCharSet;
  71. { Byte array }
  72. TByteArray = ARRAY [0..MaxBytes-1] Of Byte;
  73. PByteArray = ^TByteArray;
  74. { Word array }
  75. TWordArray = ARRAY [0..MaxWords-1] Of Word;
  76. PWordArray = ^TWordArray;
  77. { Pointer array }
  78. TPointerArray = Array [0..MaxPtrs-1] Of Pointer;
  79. PPointerArray = ^TPointerArray;
  80. { String pointer }
  81. PString = ^String;
  82. { Filename array }
  83. AsciiZ = Array [0..255] Of Char;
  84. Sw_Word = Cardinal;
  85. Sw_Integer = LongInt;
  86. \end{verbatim}
  87. The following records are used internaly for easy type conversion:
  88. \begin{verbatim}
  89. TYPE
  90. { Word to bytes}
  91. WordRec = packed RECORD
  92. Lo, Hi: Byte;
  93. END;
  94. { LongInt to words }
  95. LongRec = packed RECORD
  96. Lo, Hi: Word;
  97. END;
  98. { Pointer to words }
  99. PtrRec = packed RECORD
  100. Ofs, Seg: Word;
  101. END;
  102. \end{verbatim}
  103. The following record is used when streaming objects:
  104. \begin{verbatim}
  105. TYPE
  106. PStreamRec = ^TStreamRec;
  107. TStreamRec = Packed RECORD
  108. ObjType: Sw_Word;
  109. VmtLink: pointer;
  110. Load : Pointer;
  111. Store: Pointer;
  112. Next : PStreamRec;
  113. END;
  114. \end{verbatim}
  115. The \var{TPoint} basic object is used in the \var{TRect} object (see
  116. \sees{TRect}):
  117. \begin{verbatim}
  118. TYPE
  119. PPoint = ^TPoint;
  120. TPoint = OBJECT
  121. X, Y: Sw_Integer;
  122. END;
  123. \end{verbatim}
  124. \section{Procedures and Functions}
  125. \begin{function}{NewStr}
  126. \Declaration
  127. Function NewStr (Const S: String): PString;
  128. \Description
  129. \var{NewStr} makes a copy of the string \var{S} on the heap,
  130. and returns a pointer to this copy.
  131. The allocated memory is not based on the declared size of the string passed
  132. to \var{NewStr}, but is baed on the actual length of the string.
  133. \Errors
  134. If not enough memory is available, an 'out of memory' error will occur.
  135. \SeeAlso
  136. \seep{DisposeStr}
  137. \end{function}
  138. \begin{procedure}{DisposeStr}
  139. \Declaration
  140. Procedure DisposeStr (P: PString);
  141. \Description
  142. \var{DisposeStr} removes a dynamically allocated string from the heap.
  143. \Errors
  144. None.
  145. \SeeAlso
  146. \seef{NewStr}
  147. \end{procedure}
  148. \begin{procedure}{Abstract}
  149. \Declaration
  150. Procedure Abstract;
  151. \Description
  152. When implementing abstract methods, do not declare them as \var{abstract}.
  153. Instead, define them simply as \var{virtual}. In the implementation of such
  154. abstract methods, call the \var{Abstract} procedure. This allows explicit
  155. control of what happens when an abstract method is called.
  156. The current implementation of \var{Abstract} terminates the program with
  157. a run-time error 211.
  158. \Errors
  159. None.
  160. \SeeAlso Most abstract types.
  161. \end{procedure}
  162. \begin{procedure}{RegisterObjects}
  163. \Declaration
  164. Procedure RegisterObjects;
  165. \Description
  166. \var{RegisterObjects} registers the following objects for streaming:
  167. \begin{enumerate}
  168. \item \var{TCollection}, see \sees{TCollection}.
  169. \item \var{TStringCollection}, see \sees{TStringCollection}.
  170. \item \var{TStrCollection}, see \sees{TStrCollection}.
  171. \end{enumerate}
  172. \Errors
  173. None.
  174. \SeeAlso
  175. \seep{RegisterType}
  176. \end{procedure}
  177. \begin{procedure}{RegisterType}
  178. \Declaration
  179. Procedure RegisterType (Var S: TStreamRec);
  180. \Description
  181. \var{RegisterType} registers a new type for streaming. An object cannot
  182. be streamed unless it has been registered first.
  183. The stream record \var{S} needs to have the following fields set:
  184. \begin{description}
  185. \item[ObjType: Sw\_Word] This should be a unique identifier. Each possible
  186. type should have it's own identifier.
  187. \item[VmtLink: pointer] This should contain a pointer to the VMT (Virtual
  188. Method Table) of the object you try to register. You can get it with the
  189. following expression:
  190. \begin{verbatim}
  191. VmtLink: Ofs(TypeOf(MyType)^);
  192. \end{verbatim}
  193. \item[Load : Pointer] is a pointer to a method that initializes an instance
  194. of that object, and reads the initial values from a stream. This method
  195. should accept as it's sole argument a \var{PStream} type variable.
  196. \item[Store: Pointer]is a pointer to a method that stores an instance of the
  197. object to a stream. This method should accept as it's sole argument
  198. a \var{PStream} type variable.
  199. \end{description}
  200. \Errors
  201. In case of error (if a object with the same \var{ObjType}) is already
  202. registered), run-time error 212 occurs.
  203. \end{procedure}
  204. \begin{function}{LongMul}
  205. \Declaration
  206. Function LongMul (X, Y: Integer): LongInt;
  207. \Description
  208. \var{LongMul} multiplies \var{X} with \var{Y}. The result is of
  209. type \var{Longint}. This avoids possible overflow errors you would normally
  210. get when multiplying \var{X} and \var{Y} that are too big.
  211. \Errors
  212. None.
  213. \SeeAlso
  214. \seef{LongDiv}
  215. \end{function}
  216. \begin{function}{LongDiv}
  217. \Declaration
  218. Function LongDiv (X: Longint; Y: Integer): Integer;
  219. \Description
  220. \var{LongDiv} divides \var{X} by \var{Y}. The result is of
  221. type \var{Integer} instead of type \var{Longint}, as you would get
  222. normally.
  223. \Errors
  224. If Y is zero, a run-time error will be generated.
  225. \SeeAlso
  226. \seef{LongMul}
  227. \end{function}
  228. \section{TRect}
  229. \label{se:TRect}
  230. The \var{TRect} object is declared as follows:
  231. \begin{verbatim}
  232. TRect = OBJECT
  233. A, B: TPoint;
  234. FUNCTION Empty: Boolean;
  235. FUNCTION Equals (R: TRect): Boolean;
  236. FUNCTION Contains (P: TPoint): Boolean;
  237. PROCEDURE Copy (R: TRect);
  238. PROCEDURE Union (R: TRect);
  239. PROCEDURE Intersect (R: TRect);
  240. PROCEDURE Move (ADX, ADY: Sw_Integer);
  241. PROCEDURE Grow (ADX, ADY: Sw_Integer);
  242. PROCEDURE Assign (XA, YA, XB, YB: Sw_Integer);
  243. END;
  244. \end{verbatim}
  245. \begin{function}{TRect.Empty}
  246. \Declaration
  247. Function TRect.Empty: Boolean;
  248. \Description
  249. \var{Empty} returns \var{True} if the rectangle defined by the corner points
  250. \var{A}, \var{B} has zero or negative surface.
  251. \Errors
  252. None.
  253. \SeeAlso
  254. \seef{TRect.Equals}, \seef{TRect.Contains}
  255. \end{function}
  256. \latex{\inputlisting{objectex/ex1.pp}}
  257. \html{\input{objectex/ex1.tex}}
  258. \begin{function}{TRect.Equals}
  259. \Declaration
  260. Function TRect.Equals (R: TRect): Boolean;
  261. \Description
  262. \var{Equals} returns \var{True} if the rectangle has the
  263. same corner points \var{A,B} as the rectangle R, and \var{False}
  264. otherwise.
  265. \Errors
  266. None.
  267. \SeeAlso
  268. \seefl{Empty}{TRect.Empty}, \seefl{Contains}{TRect.Contains}
  269. \end{function}
  270. For an example, see \seef{TRect.Empty}
  271. \begin{function}{TRect.Contains}
  272. \Declaration
  273. Function TRect.Contains (P: TPoint): Boolean;
  274. \Description
  275. \var{Contains} returns \var{True} if the point \var{P} is contained
  276. in the rectangle (including borders), \var{False} otherwise.
  277. \Errors
  278. None.
  279. \SeeAlso
  280. \seepl{Intersect}{TRect.Intersect}, \seefl{Equals}{TRect.Equals}
  281. \end{function}
  282. \begin{procedure}{TRect.Copy}
  283. \Declaration
  284. Procedure TRect.Copy (R: TRect);
  285. \Description
  286. Assigns the rectangle R to the object. After the call to \var{Copy}, the
  287. rectangle R has been copied to the object that invoked \var{Copy}.
  288. \Errors
  289. None.
  290. \SeeAlso
  291. \seepl{Assign}{TRect.Assign}
  292. \end{procedure}
  293. \latex{\inputlisting{objectex/ex2.pp}}
  294. \html{\input{objectex/ex2.tex}}
  295. \begin{procedure}{TRect.Union}
  296. \Declaration
  297. Procedure TRect.Union (R: TRect);
  298. \Description
  299. \var{Union} enlarges the current rectangle so that it becomes the union
  300. of the current rectangle with the rectangle \var{R}.
  301. \Errors
  302. None.
  303. \SeeAlso
  304. \seepl{Intersect}{TRect.Intersect}
  305. \end{procedure}
  306. \latex{\inputlisting{objectex/ex3.pp}}
  307. \html{\input{objectex/ex3.tex}}
  308. \begin{procedure}{TRect.Intersect}
  309. \Declaration
  310. Procedure TRect.Intersect (R: TRect);
  311. \Description
  312. \var{Intersect} makes the intersection of the current rectangle with
  313. \var{R}. If the intersection is empty, then the rectangle is set to the empty
  314. rectangle at coordinate (0,0).
  315. \Errors
  316. None.
  317. \SeeAlso
  318. \seepl{Union}{TRect.Union}
  319. \end{procedure}
  320. \latex{\inputlisting{objectex/ex4.pp}}
  321. \html{\input{objectex/ex4.tex}}
  322. \begin{procedure}{TRect.Move}
  323. \Declaration
  324. Procedure TRect.Move (ADX, ADY: Sw\_Integer);
  325. \Description
  326. \var{Move} moves the current rectangle along a vector with components
  327. \var{(ADX,ADY)}. It adds \var{ADX} to the X-coordinate of both corner
  328. points, and \var{ADY} to both end points.
  329. \Errors
  330. None.
  331. \SeeAlso
  332. \seepl{Grow}{TRect.Grow}
  333. \end{procedure}
  334. \latex{\inputlisting{objectex/ex5.pp}}
  335. \html{\input{objectex/ex5.tex}}
  336. \begin{procedure}{TRect.Grow}
  337. \Declaration
  338. Procedure TRect.Grow (ADX, ADY: Sw\_Integer);
  339. \Description
  340. \var{Grow} expands the rectangle with an amount \var{ADX} in the \var{X}
  341. direction (both on the left and right side of the rectangle, thus adding a
  342. length 2*ADX to the width of the rectangle), and an amount \var{ADY} in
  343. the \var{Y} direction (both on the top and the bottom side of the rectangle,
  344. adding a length 2*ADY to the height of the rectangle.
  345. \var{ADX} and \var{ADY} can be negative. If the resulting rectangle is empty, it is set
  346. to the empty rectangle at \var{(0,0)}.
  347. \Errors
  348. None.
  349. \SeeAlso
  350. \seepl{Move}{TRect.Move}.
  351. \end{procedure}
  352. \latex{\inputlisting{objectex/ex6.pp}}
  353. \html{\input{objectex/ex7.tex}}
  354. \begin{procedure}{TRect.Assign}
  355. \Declaration
  356. Procedure Trect.Assign (XA, YA, XB, YB: Sw\_Integer);
  357. \Description
  358. \var{Assign} sets the corner points of the rectangle to \var{(XA,YA)} and
  359. \var{(Xb,Yb)}.
  360. \Errors
  361. None.
  362. \SeeAlso
  363. \seepl{Copy}{TRect.Copy}
  364. \end{procedure}
  365. For an example, see \seep{TRect.Copy}.
  366. \section{TObject}
  367. \label{se:TObject}
  368. The full declaration of the \var{TObject} type is:
  369. \begin{verbatim}
  370. TYPE
  371. TObject = OBJECT
  372. CONSTRUCTOR Init;
  373. PROCEDURE Free;
  374. DESTRUCTOR Done;Virtual;
  375. END;
  376. PObject = ^TObject;
  377. \end{verbatim}
  378. \begin{procedure}{TObject.Init}
  379. \Declaration
  380. Constructor TObject.Init;
  381. \Description
  382. Instantiates a new object of type \var{TObject}. It fills the instance up
  383. with Zero bytes.
  384. \Errors
  385. None.
  386. \SeeAlso
  387. \seepl{Free}{TObject.Free}, \seepl{Done}{TObject.Done}
  388. \end{procedure}
  389. For an example, see \seepl{Free}{TObject.Free}
  390. \begin{procedure}{TObject.Free}
  391. \Declaration
  392. Procedure TObject.Free;
  393. \Description
  394. \var{Free} calls the destructor of the object, and releases the memory
  395. occupied by the instance of the object.
  396. \Errors
  397. No checking is performed to see whether \var{self} is \var{nil} and whether
  398. the object is indeed allocated on the heap.
  399. \SeeAlso
  400. \seepl{Init}{TObject.Init}, \seepl{Done}{TObject.Done}
  401. \end{procedure}
  402. \latex{\inputlisting{objectex/ex7.pp}}
  403. \html{\input{objectex/ex7.tex}}
  404. \begin{procedure}{TObject.Done}
  405. \Declaration
  406. Destructor TObject.Done;Virtual;
  407. \Description
  408. \var{Done}, the destructor of \var{TObject} does nothing. It is mainly
  409. intended to be used in the \seep{TObject.Free} method.
  410. \Errors
  411. None.
  412. \SeeAlso
  413. \seepl{Free}{TObject.Free}, \seepl{Init}{TObject.Init}
  414. \end{procedure}
  415. \section{TStream}
  416. \label{se:TStream}
  417. The \var{TStream} object is the ancestor for all streaming objects, i.e.
  418. objects that have the capability to store and retrieve data.
  419. It defines a number of methods that are common to all objects that implement
  420. streaming, many of them are virtual, and are only implemented in the
  421. descendrnt types.
  422. Programs should not instantiate objects of type TStream directly, but
  423. instead instantiate a descendant type, such as \var{TDosStream},
  424. \var{TMemoryStream}.
  425. This is the full declaration of the \var{TStream} object:
  426. \begin{verbatim}
  427. TYPE
  428. TStream = OBJECT (TObject)
  429. Status : Integer; { Stream status }
  430. ErrorInfo : Integer; { Stream error info }
  431. StreamSize: LongInt; { Stream current size }
  432. Position : LongInt; { Current position }
  433. FUNCTION Get: PObject;
  434. FUNCTION StrRead: PChar;
  435. FUNCTION GetPos: Longint; Virtual;
  436. FUNCTION GetSize: Longint; Virtual;
  437. FUNCTION ReadStr: PString;
  438. PROCEDURE Open (OpenMode: Word); Virtual;
  439. PROCEDURE Close; Virtual;
  440. PROCEDURE Reset;
  441. PROCEDURE Flush; Virtual;
  442. PROCEDURE Truncate; Virtual;
  443. PROCEDURE Put (P: PObject);
  444. PROCEDURE StrWrite (P: PChar);
  445. PROCEDURE WriteStr (P: PString);
  446. PROCEDURE Seek (Pos: LongInt); Virtual;
  447. PROCEDURE Error (Code, Info: Integer); Virtual;
  448. PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
  449. PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
  450. PROCEDURE CopyFrom (Var S: TStream; Count: Longint);
  451. END;
  452. PStream = ^TStream;
  453. \end{verbatim}
  454. \begin{function}{TStream.Get}
  455. \Declaration
  456. Function TStream.Get : PObject;
  457. \Description
  458. \var{Get} reads an object definition from a stream, and returns
  459. a pointer to an instance of this object.
  460. \Errors
  461. On error, \var{TStream.Status} is set, and NIL is returned.
  462. \SeeAlso
  463. \seepl{Put}{TStream.Put}
  464. \end{function}
  465. \begin{function}{TStream.StrRead}
  466. \Declaration
  467. Function TStream.StrRead: PChar;
  468. \Description
  469. \var{StrRead} reads a string from the stream, allocates memory
  470. for it, and returns a pointer to a null-terminated copy of the string
  471. on the heap.
  472. \Errors
  473. On error, \var{Nil} is returned.
  474. \SeeAlso
  475. \seepl{StrWrite}{TStream.StrWrite}, \seefl{ReadStr}{TStream.ReadStr}
  476. \end{function}
  477. \begin{function}{TStream.GetPos}
  478. \Declaration
  479. TSTream.GetPos : Longint; Virtual;
  480. \Description
  481. If the stream's status is \var{stOk}, \var{GetPos} returns the current
  482. position in the stream. Otherwise it returns \var{-1}
  483. \Errors
  484. \var{-1} is returned if the status is an error condition.
  485. \SeeAlso
  486. \seepl{Seek}{TStream.Seek}, \seefl{GetSize}{TStream.GetSize}
  487. \end{function}
  488. \begin{function}{TStream.GetSize}
  489. \Declaration
  490. Function TStream.GetSize: Longint; Virtual;
  491. \Description
  492. If the stream's status is \var{stOk} then \var{GetSize} returns
  493. the size of the stream, otherwise it returns \var{-1}.
  494. \Errors
  495. \var{-1} is returned if the status is an error condition.
  496. \SeeAlso
  497. \seepl{Seek}{TStream.Seek}, \seefl{GetPos}{TStream.GetPos}
  498. \end{function}
  499. \begin{function}{TStream.ReadStr}
  500. \Declaration
  501. Function TStream.ReadStr: PString;
  502. \Description
  503. \var{ReadStr} reads a string from the stream, copies it to the heap
  504. and returns a pointer to this copy. The string is saved as a pascal
  505. string, and hence is NOT null terminated.
  506. \Errors
  507. On error (e.g. not enough memory), \var{Nil} is returned.
  508. \SeeAlso
  509. \seefl{StrRead}{TStream.StrRead}
  510. \end{function}
  511. \begin{procedure}{TStream.Open}
  512. \Declaration
  513. Procedure TStream.Open (OpenMode: Word); Virtual;
  514. \Description
  515. \var{Open} is an abstract method, that should be overridden by descendent
  516. objects. Since opening a stream depends on the stream's type this is not
  517. surprising.
  518. \Errors
  519. None.
  520. \SeeAlso
  521. \seepl{Close}{TStream.Close}, \seepl{Reset}{TStream.Reset}
  522. \end{procedure}
  523. \begin{procedure}{TStream.Close}
  524. \Declaration
  525. Procedure TStream.Close; Virtual;
  526. \Description
  527. \var{Close} is an abstract method, that should be overridden by descendent
  528. objects. Since Closing a stream depends on the stream's type this is not
  529. surprising.
  530. \Errors
  531. None.
  532. \SeeAlso
  533. \seepl{Open}{TStream.Open}, \seepl{Reset}{TStream.Reset}
  534. \end{procedure}
  535. \begin{procedure}{TStream.Reset}
  536. \Declaration
  537. PROCEDURE TStream.Reset;
  538. \Description
  539. \var{Reset} sets the stream's status to \var{0}, as well as the ErrorInfo
  540. \Errors
  541. None.
  542. \SeeAlso
  543. \seepl{Open}{TStream.Open}, \seepl{Close}{TStream.Close}
  544. \end{procedure}
  545. \begin{procedure}{TStream.Flush}
  546. \Declaration
  547. Procedure TStream.Flush; Virtual;
  548. \Description
  549. \var{Flush} is an abstract method that should be overridden by descendent
  550. objects. It serves to enable the programmer to tell streams that implement
  551. a buffer to clear the buffer.
  552. \Errors
  553. None.
  554. \SeeAlso
  555. \seepl{Truncate}{TStream.Truncate}
  556. \end{procedure}
  557. \begin{procedure}{TStream.Truncate}
  558. \Declaration
  559. Procedure TStream.Truncate; Virtual;
  560. \Description
  561. \var{Truncate} is an abstract procedure that should be overridden by
  562. descendent objects. It serves to enable the programmer to truncate the
  563. size of the stream to the current file position.
  564. \Errors
  565. None.
  566. \SeeAlso
  567. \seepl{Seek}{TStream.Seek}
  568. \end{procedure}
  569. \begin{procedure}{TStream.Put}
  570. \Declaration
  571. Procedure TStream.Put (P: PObject);
  572. \Description
  573. \var{Put} writes the object pointed to by \var{P}. \var{P} should be
  574. non-nil. The object type must have been registered with \seep{RegisterType}.
  575. After the object has been written, it can be read again with \seefl{Get}{TStream.Get}.
  576. \Errors
  577. No check is done whether P is \var{Nil} or not. Passing \var{Nil} will cause
  578. a run-time error 216 to be generated. If the object has not been registered,
  579. the status of the stream will be set to \var{stPutError}.
  580. \SeeAlso
  581. \seefl{Get}{TStream.Get}
  582. \end{procedure}
  583. \begin{procedure}{TStream.StrWrite}
  584. \Declaration
  585. Procedure TStream.StrWrite (P: PChar);
  586. \Description
  587. \var{StrWrite} writes the null-terminated string \var{P} to the stream.
  588. \var{P} can only be 65355 bytes long.
  589. \Errors
  590. None.
  591. \SeeAlso
  592. \seepl{WriteStr}{TStream.WriteStr}, \seefl{StrRead}{TStream.StrRead},
  593. \seefl{ReadStr}{TStream.ReadStr}
  594. \end{procedure}
  595. \begin{procedure}{TStream.WriteStr}
  596. \Declaration
  597. Procedure TStream.WriteStr (P: PString);
  598. \Description
  599. \var{StrWrite} writes the pascal string pointed to by \var{P} to the stream.
  600. \Errors
  601. None.
  602. \SeeAlso
  603. \seepl{StrWrite}{TStream.StrWrite}, \seefl{StrRead}{TStream.StrRead},
  604. \seefl{ReadStr}{TStream.ReadStr}
  605. \end{procedure}
  606. \begin{procedure}{TStream.Seek}
  607. \Declaration
  608. PROCEDURE TStream.Seek (Pos: LongInt); Virtual;
  609. \Description
  610. Seek sets the position to \var{Pos}. This position is counted
  611. from the beginning, and is zero based. (i.e. seeek(0) sets the position
  612. pointer on the first byte of the stream)
  613. \Errors
  614. If \var{Pos} is larger than the stream size, \var{Status} is set to
  615. \var{StSeekError}.
  616. \SeeAlso
  617. \seefl{GetPos}{TStream.GetPos}, \seefl{GetSize}{TStream.GetSize}
  618. \end{procedure}
  619. \begin{procedure}{TStream.Error}
  620. \Declaration
  621. Procedure TStream.Error (Code, Info: Integer); Virtual;
  622. \Description
  623. \var{Error} sets the stream's status to \var{Code} and \var{ErrorInfo}
  624. to \var{Info}. If the \var{StreamError} procedural variable is set,
  625. \var{Error} executes it, passing \var{Self} as an argument.
  626. This method should not be called directly from a program. It is intended to
  627. be used in descendent objects.
  628. \Errors
  629. None.
  630. \SeeAlso
  631. \end{procedure}
  632. \begin{procedure}{TStream.Read}
  633. \Declaration
  634. Procedure TStream.Read (Var Buf; Count: Sw\_Word); Virtual;
  635. \Description
  636. \var{Read} is an abstract method that should be overridden by descendent
  637. objects.
  638. \var{Read} reads \var{Count} bytes from the stream into \var{Buf}.
  639. It updates the position pointer, increasing it's value with \var{Count}.
  640. \var{Buf} must be large enough to contain \var{Count} bytes.
  641. \Errors
  642. No checking is done to see if \var{Buf} is large enough to contain
  643. \var{Count} bytes.
  644. \SeeAlso
  645. \seepl{Write}{TStream.Write}, \seefl{ReadStr}{TStream.ReadStr},
  646. \seefl{StrRead}{TStream.StrRead}
  647. \end{procedure}
  648. \begin{procedure}{TStream.Write}
  649. \Declaration
  650. Procedure TStream.Write (Var Buf; Count: Sw\_Word); Virtual;
  651. \Description
  652. \var{Write} is an abstract method that should be overridden by descendent
  653. objects.
  654. \var{Write} writes \var{Count} bytes to the stream from \var{Buf}.
  655. It updates the position pointer, increasing it's value with \var{Count}.
  656. \Errors
  657. No checking is done to see if \var{Buf} actually contains \var{Count} bytes.
  658. \SeeAlso
  659. \seepl{Read}{TStream.Read}, \seepl{WriteStr}{TStream.WriteStr},
  660. \seepl{StrWrite}{TStream.StrWrite}
  661. \end{procedure}
  662. \begin{procedure}{TStream.CopyFrom}
  663. \Declaration
  664. Procedure TStream.CopyFrom (Var S: TStream; Count: Longint);
  665. \Description
  666. \var{CopyFrom} reads Count bytes from stream \var{S} and stores them
  667. in the current stream. It uses the \seepl{Read}{TStream.Read} method
  668. to read the data, and the \seepl{Write}{TStream.Write} method to
  669. write in the current stream.
  670. \Errors
  671. None.
  672. \SeeAlso
  673. \seepl{Read}{TStream.Read}, \seepl{Write}{TStream.Write}
  674. \end{procedure}
  675. \section{TDosStream}
  676. \label{se:TDosStream}
  677. \var{TDosStream} is a steam that stores it's contents in a file.
  678. it overrides a couple of methods of \var{TSteam} for this.
  679. In addition to the fields inherited from \var{TStream} (see \sees{TStream}),
  680. there are some extra fields, that describe the file. (mainly the name and
  681. the OS file handle)
  682. No buffering in memory is done when using \var{TDosStream}.
  683. All data are written directly to the file. For a stream that buffers
  684. in memory, see \sees{TBufStream}.
  685. Here is the full declaration of the \var{TDosStream} object:
  686. \begin{verbatim}
  687. TYPE
  688. TDosStream = OBJECT (TStream)
  689. Handle: THandle; { DOS file handle }
  690. FName : AsciiZ; { AsciiZ filename }
  691. CONSTRUCTOR Init (FileName: FNameStr; Mode: Word);
  692. DESTRUCTOR Done; Virtual;
  693. PROCEDURE Close; Virtual;
  694. PROCEDURE Truncate; Virtual;
  695. PROCEDURE Seek (Pos: LongInt); Virtual;
  696. PROCEDURE Open (OpenMode: Word); Virtual;
  697. PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
  698. PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
  699. END;
  700. PDosStream = ^TDosStream;
  701. \end{verbatim}
  702. \begin{procedure}{TDosStream.Init}
  703. \Declaration
  704. Constructor Init (FileName: FNameStr; Mode: Word);
  705. \Description
  706. \var{Init} instantiates an instance of \var{TDosStream}. The name of the
  707. file that contains (or will contain) the data of the stream is given in
  708. \var{FileName}. The \var{Mode} parameter determines whether a new file
  709. should be created and what access rights you have on the file.
  710. It can be one of the following constants:
  711. \begin{description}
  712. \item[stCreate] Creates a new file.
  713. \item[stOpenRead] Read access only.
  714. \item[stOpenWrite] Write access only.
  715. \item[stOpen] Read and write access.
  716. \end{description}
  717. \Errors
  718. On error, \var{Status} is set to \var{stInitError}, and \var{ErrorInfo}
  719. is set to the \dos error code.
  720. \SeeAlso
  721. \seepl{Done}{TDosStream.Done}
  722. \end{procedure}
  723. \begin{procedure}{TDosStream.Done}
  724. \Declaration
  725. Destructor TDosStream.Done; Virtual;
  726. \Description
  727. \var{Done} closes the file if it was open and cleans up the
  728. instance of \var{TDosStream}.
  729. \Errors
  730. None.
  731. \SeeAlso
  732. \seepl{Init}{TDosStream.Init},
  733. \seepl{Close}{TDosStream.Close}
  734. \end{procedure}
  735. \begin{procedure}{TDosStream.Close}
  736. \Declaration
  737. Pocedure TDosStream.Close; Virtual;
  738. \Description
  739. \var{Close} closes the file if it was open, and sets \var{Handle} to -1.
  740. Contrary to \seepl{Done}{TDosStream.Done} it does not clean up the instance
  741. of \var{TDosStream}
  742. \Errors
  743. None.
  744. \SeeAlso
  745. \seep{TStream.Close}, \seepl{Init}{TDosStream.Init},
  746. \seepl{Done}{TDosStream.Done}
  747. \end{procedure}
  748. \begin{procedure}{TDosStream.Truncate}
  749. \Declaration
  750. Procedure TDosStream.Truncate; Virtual;
  751. \Description
  752. If the status of the stream is \var{stOK}, then \var{Truncate} tries to
  753. truncate the stream size to the current file position.
  754. \Errors
  755. If an error occurs, the stream's status is set to \var{stError} and
  756. \var{ErrorInfo} is set to the OS error code.
  757. \SeeAlso
  758. \seep{TStream.Truncate}, \seefl{GetSize}{TStream.GetSize}
  759. \end{procedure}
  760. \begin{procedure}{TDosStream.Seek}
  761. \Declaration
  762. Procedure TDosStream.Seek (Pos: LongInt); Virtual;
  763. \Description
  764. If the stream's status is \var{stOK}, then \var{Seek} sets the
  765. file position to \var{Pos}. \var{Pos} is a zero-based offset, counted from
  766. the beginning of the file.
  767. \Errors
  768. In case an error occurs, the stream's status is set to \var{stSeekError},
  769. and the OS error code is stored in \var{ErrorInfo}.
  770. \SeeAlso
  771. \seep{TStream.Seek}, \seefl{GetPos}{TStream.GetPos}
  772. \end{procedure}
  773. \begin{procedure}{TDosStream.Open}
  774. \Declaration
  775. Procedure TDosStream.Open (OpenMode: Word); Virtual;
  776. \Description
  777. If the stream's status is \var{stOK}, and the stream is closed then
  778. \var{Open} re-opens the file stream with mode \var{OpenMode}.
  779. This call can be used after a \seepl{Close}{TDosStream.Close} call.
  780. \Errors
  781. If an error occurs when re-opening the file, then \var{Status} is set
  782. to \var{stOpenError}, and the OS error code is stored in \var{ErrorInfo}
  783. \SeeAlso
  784. \seep{TStream.Open}, \seepl{Close}{TDosStream.Close}
  785. \end{procedure}
  786. \begin{procedure}{TDosStream.Read}
  787. \Declaration
  788. Procedure TDosStream.Read (Var Buf; Count: Sw\_Word); Virtual;
  789. \Description
  790. If the Stream is open and the stream status is \var{stOK} then
  791. \var{Read} will read \var{Count} bytes from the stream and place them
  792. in \var{Buf}.
  793. \Errors
  794. In case of an error, \var{Status} is set to \var{StReadError}, and
  795. \var{ErrorInfo} gets the OS specific error, or 0 when an attempt was
  796. made to read beyond the end of the stream.
  797. \SeeAlso
  798. \seep{TStream.Read}, \seepl{Write}{TDosStream.Write}
  799. \end{procedure}
  800. \begin{procedure}{TDosStream.Write}
  801. \Declaration
  802. Procedure TDosStream.Write (Var Buf; Count: Sw\_Word); Virtual;
  803. \Description
  804. If the Stream is open and the stream status is \var{stOK} then
  805. \var{Write} will write \var{Count} bytes from \var{Buf} and place them
  806. in the stream.
  807. \Errors
  808. In case of an error, \var{Status} is set to \var{StWriteError}, and
  809. \var{ErrorInfo} gets the OS specific error.
  810. \SeeAlso
  811. \seep{TStream.Write}, \seepl{Read}{TDosStream.Read}
  812. \end{procedure}
  813. \section{TBufStream}
  814. \label{se:TBufStream}
  815. \var{Bufstream} implements a buffered file stream. That is, all data written
  816. to the stream is written to memory first. Only when the buffer is full, or
  817. on explicit request, the data is written to disk.
  818. Also, when reading from the stream, first the buffer is checked if there is
  819. any unread data in it. If so, this is read first. If not the buffer is
  820. filled again, and then the data is read from the buffer.
  821. The size of the buffer is fixed and is set when constructing the file.
  822. This is useful if you need heavy throughput for your stream, because it
  823. speeds up operations.
  824. \begin{verbatim}
  825. TYPE
  826. TBufStream = OBJECT (TDosStream)
  827. LastMode: Byte; { Last buffer mode }
  828. BufSize : Sw_Word; { Buffer size }
  829. BufPtr : Sw_Word; { Buffer start }
  830. BufEnd : Sw_Word; { Buffer end }
  831. Buffer : PByteArray; { Buffer allocated }
  832. CONSTRUCTOR Init (FileName: FNameStr; Mode, Size: Word);
  833. DESTRUCTOR Done; Virtual;
  834. PROCEDURE Close; Virtual;
  835. PROCEDURE Flush; Virtual;
  836. PROCEDURE Truncate; Virtual;
  837. PROCEDURE Seek (Pos: LongInt); Virtual;
  838. PROCEDURE Open (OpenMode: Word); Virtual;
  839. PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
  840. PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
  841. END;
  842. PBufStream = ^TBufStream;
  843. \end{verbatim}
  844. \begin{procedure}{TBufStream.Init}
  845. \Declaration
  846. Constructor Init (FileName: FNameStr; Mode,Size: Word);
  847. \Description
  848. \var{Init} instantiates an instance of \var{TBufStream}. The name of the
  849. file that contains (or will contain) the data of the stream is given in
  850. \var{FileName}. The \var{Mode} parameter determines whether a new file
  851. should be created and what access rights you have on the file.
  852. It can be one of the following constants:
  853. \begin{description}
  854. \item[stCreate] Creates a new file.
  855. \item[stOpenRead] Read access only.
  856. \item[stOpenWrite] Write access only.
  857. \item[stOpen] Read and write access.
  858. \end{description}
  859. The \var{Size} parameter determines the size of the buffer that will be
  860. created. It should be different from zero.
  861. \Errors
  862. On error, \var{Status} is set to \var{stInitError}, and \var{ErrorInfo}
  863. is set to the \dos error code.
  864. \SeeAlso
  865. \seep{TDosStream.Init}, \seepl{Done}{TBufStream.Done}
  866. \end{procedure}
  867. \begin{procedure}{TBufStream.Done}
  868. \Declaration
  869. Destructor TBufStream.Done; Virtual;
  870. \Description
  871. \var{Done} flushes and closes the file if it was open and cleans up the
  872. instance of \var{TBufStream}.
  873. \Errors
  874. None.
  875. \SeeAlso
  876. \seep{TDosStream.Done}, \seepl{Init}{TBufStream.Init},
  877. \seepl{Close}{TBufStream.Close}
  878. \end{procedure}
  879. \begin{procedure}{TBufStream.Close}
  880. \Declaration
  881. Pocedure TBufStream.Close; Virtual;
  882. \Description
  883. \var{Close} flushes and closes the file if it was open, and sets \var{Handle} to -1.
  884. Contrary to \seepl{Done}{TBufStream.Done} it does not clean up the instance
  885. of \var{TBufStream}
  886. \Errors
  887. None.
  888. \SeeAlso
  889. \seep{TStream.Close}, \seepl{Init}{TBufStream.Init},
  890. \seepl{Done}{TBufStream.Done}
  891. \end{procedure}
  892. \begin{procedure}{TBufStream.Flush}
  893. \Declaration
  894. Pocedure TBufStream.Flush; Virtual;
  895. \Description
  896. When the stream is in write mode, the contents of the buffer are written to
  897. disk, and the buffer position is set to zero.
  898. When the stream is in read mode, the buffer position is set to zero.
  899. \Errors
  900. Write errors may occur if the file was in write mode.
  901. see \seepl{Write}{TBufStream.Write} for more info on the errors.
  902. \SeeAlso
  903. \seep{TStream.Close}, \seepl{Init}{TBufStream.Init},
  904. \seepl{Done}{TBufStream.Done}
  905. \end{procedure}
  906. \begin{procedure}{TBufStream.Truncate}
  907. \Declaration
  908. Procedure TBufStream.Truncate; Virtual;
  909. \Description
  910. If the status of the stream is \var{stOK}, then \var{Truncate} tries to
  911. flush the buffer, and then truncates the stream size to the current
  912. file position.
  913. \Errors
  914. Errors can be those of \seepl{Flush}{TBufStream.Flush} or
  915. \seep{TDosStream.Truncate}.
  916. \SeeAlso
  917. \seep{TStream.Truncate}, \seep{TDosStream.Truncate},
  918. \seefl{GetSize}{TStream.GetSize}
  919. \end{procedure}
  920. \begin{procedure}{TBufStream.Seek}
  921. \Declaration
  922. Procedure TBufStream.Seek (Pos: LongInt); Virtual;
  923. \Description
  924. If the stream's status is \var{stOK}, then \var{Seek} sets the
  925. file position to \var{Pos}. \var{Pos} is a zero-based offset, counted from
  926. the beginning of the file.
  927. \Errors
  928. In case an error occurs, the stream's status is set to \var{stSeekError},
  929. and the OS error code is stored in \var{ErrorInfo}.
  930. \SeeAlso
  931. \seep{TStream.Seek}, \seefl{GetPos}{TStream.GetPos}
  932. \end{procedure}
  933. \begin{procedure}{TBufStream.Open}
  934. \Declaration
  935. Procedure TBufStream.Open (OpenMode: Word); Virtual;
  936. \Description
  937. If the stream's status is \var{stOK}, and the stream is closed then
  938. \var{Open} re-opens the file stream with mode \var{OpenMode}.
  939. This call can be used after a \seepl{Close}{TBufStream.Close} call.
  940. \Errors
  941. If an error occurs when re-opening the file, then \var{Status} is set
  942. to \var{stOpenError}, and the OS error code is stored in \var{ErrorInfo}
  943. \SeeAlso
  944. \seep{TStream.Open}, \seepl{Close}{TBufStream.Close}
  945. \end{procedure}
  946. \begin{procedure}{TBufStream.Read}
  947. \Declaration
  948. Procedure TBufStream.Read (Var Buf; Count: Sw\_Word); Virtual;
  949. \Description
  950. If the Stream is open and the stream status is \var{stOK} then
  951. \var{Read} will read \var{Count} bytes from the stream and place them
  952. in \var{Buf}.
  953. \var{Read} will first try to read the data from the stream's internal
  954. buffer. If insufficient data is available, the buffer will be filled before
  955. contiunuing to read. This process is repeated until all needed data
  956. has been read.
  957. \Errors
  958. In case of an error, \var{Status} is set to \var{StReadError}, and
  959. \var{ErrorInfo} gets the OS specific error, or 0 when an attempt was
  960. made to read beyond the end of the stream.
  961. \SeeAlso
  962. \seep{TStream.Read}, \seepl{Write}{TBufStream.Write}
  963. \end{procedure}
  964. \begin{procedure}{TBufStream.Write}
  965. \Declaration
  966. Procedure TBufStream.Write (Var Buf; Count: Sw\_Word); Virtual;
  967. \Description
  968. If the Stream is open and the stream status is \var{stOK} then
  969. \var{Write} will write \var{Count} bytes from \var{Buf} and place them
  970. in the stream.
  971. \var{Write} will first try to write the data to the stream's internal
  972. buffer. When the internal buffer is full, then the contents will be written
  973. to disk. This process is repeated until all data has been written.
  974. \Errors
  975. In case of an error, \var{Status} is set to \var{StWriteError}, and
  976. \var{ErrorInfo} gets the OS specific error.
  977. \SeeAlso
  978. \seep{TStream.Write}, \seepl{Read}{TBufStream.Read}
  979. \end{procedure}
  980. \section{TMemoryStream}
  981. \label{se:TMemoryStream}
  982. The \var{TMemoryStream} object implements a stream that stores it's data
  983. in memory. The data is stored on the heap, with the possibility to specify
  984. the maximum amout of data, and the the size of the memory blocks being used.
  985. \begin{verbatim}
  986. TYPE
  987. TMemoryStream = OBJECT (TStream)
  988. BlkCount: Sw_Word; { Number of segments }
  989. BlkSize : Word; { Memory block size }
  990. MemSize : LongInt; { Memory alloc size }
  991. BlkList : PPointerArray; { Memory block list }
  992. CONSTRUCTOR Init (ALimit: Longint; ABlockSize: Word);
  993. DESTRUCTOR Done; Virtual;
  994. PROCEDURE Truncate; Virtual;
  995. PROCEDURE Read (Var Buf; Count: Sw_Word); Virtual;
  996. PROCEDURE Write (Var Buf; Count: Sw_Word); Virtual;
  997. END;
  998. PMemoryStream = ^TMemoryStream;
  999. \end{verbatim}
  1000. \begin{procedure}{TMemoryStream.Init}
  1001. \Declaration
  1002. Constructor TMemoryStream.Init (ALimit: Longint; ABlockSize: Word);
  1003. \Description
  1004. \var{Init} instantiates a new \var{TMemoryStream} object. The
  1005. memorystreamobject will initially allocate at least \var{ALimit} bytes memory,
  1006. divided into memory blocks of size \var{ABlockSize}.
  1007. The number of blocks needed to get to \var{ALimit} bytes is rounded up.
  1008. By default, the number of blocks is 1, and the size of a block is 8192. This
  1009. is selected if you specify 0 as the blocksize.
  1010. \Errors
  1011. If the stream cannot allocate the initial memory needed for the memory blocks, then
  1012. the stream's status is set to \var{stInitError}.
  1013. \SeeAlso
  1014. \seepl{Done}{TMemoryStream.Done}
  1015. \end{procedure}
  1016. \begin{procedure}{TMemoryStream.Done}
  1017. \Declaration
  1018. Destructor TMemoryStream.Done; Virtual;
  1019. \Description
  1020. \var{Done} releases the memory blocks used by the stream, and then cleans up
  1021. the memory used by the stream object itself.
  1022. \Errors
  1023. None.
  1024. \SeeAlso
  1025. \seepl{Init}{TMemoryStream.Init}
  1026. \end{procedure}
  1027. \begin{procedure}{TMemoryStream.Truncate}
  1028. \Declaration
  1029. Procedure TMemoryStream.Truncate; Virtual;
  1030. \Description
  1031. \var{Truncate} sets the size of the memory stream equal to the current
  1032. position. It de-allocates any memory-blocks that are no longer needed, so
  1033. that the new size of the stream is the current position in the stream,
  1034. rounded up to the first multiple of the stream blocksize.
  1035. \Errors
  1036. If an error occurs during memory de-allocation, the stream's status is set
  1037. to \var{stError}
  1038. \SeeAlso
  1039. \seep{TStream.Truncate}
  1040. \end{procedure}
  1041. \begin{procedure}{TMemoryStream.Read}
  1042. \Declaration
  1043. Procedure Read (Var Buf; Count: Sw\_Word); Virtual;
  1044. \Description
  1045. \var{Read} reads \var{Count} bytes from the stream to \var{Buf}. It updates
  1046. the position of the stream.
  1047. \Errors
  1048. If there is not enough data available, no data is read, and the stream's
  1049. status is set to \var{stReadError}.
  1050. \SeeAlso
  1051. \var{TStream.Read}, \seepl{Write}{TMemoryStream.Write}
  1052. \end{procedure}
  1053. \begin{procedure}{TMemoryStream.Write}
  1054. \Declaration
  1055. Procedure Write (Var Buf; Count: Sw\_Word); Virtual;
  1056. \Description
  1057. \var{Write} copies \var{Count} bytes from \var{Buf} to the stream. It
  1058. updates the position of the stream.
  1059. If not enough memory is available to hold the extra \var{Count} bytes,
  1060. then the stream will try to expand, by allocating as much blocks with
  1061. size \var{BlkSize} (as specified in the constuctor call
  1062. \seepl{Init}{TMemoryStream.Init}) as needed.
  1063. \Errors
  1064. If the stream cannot allocate more memory, then the status is set to
  1065. \var{stWriteError}
  1066. \SeeAlso
  1067. \seep{TStream.Write}, \seepl{Read}{TMemoryStream.Read}
  1068. \end{procedure}
  1069. \section{TCollection}
  1070. \label{se:TCollection}
  1071. The \var{TCollection} object manages a collection of pointers or objects.
  1072. It also provides a series of methods to manipulate these pointers or
  1073. objects.
  1074. Whether or not objects are used depends on the kind of calls you use.
  1075. ALl kinds come in 2 flavors, one for objects, one for pointers.
  1076. This is the full declaration of the \var{TCollection} object:
  1077. \begin{verbatim}
  1078. TYPE
  1079. TItemList = Array [0..MaxCollectionSize - 1] Of Pointer;
  1080. PItemList = ^TItemList;
  1081. TCollection = OBJECT (TObject)
  1082. Items: PItemList; { Item list pointer }
  1083. Count: Sw_Integer; { Item count }
  1084. Limit: Sw_Integer; { Item limit count }
  1085. Delta: Sw_Integer; { Inc delta size }
  1086. Constructor Init (ALimit, ADelta: Sw_Integer);
  1087. Constructor Load (Var S: TStream);
  1088. Destructor Done; Virtual;
  1089. Function At (Index: Sw_Integer): Pointer;
  1090. Function IndexOf (Item: Pointer): Sw_Integer; Virtual;
  1091. Function GetItem (Var S: TStream): Pointer; Virtual;
  1092. Function LastThat (Test: Pointer): Pointer;
  1093. Function FirstThat (Test: Pointer): Pointer;
  1094. Procedure Pack;
  1095. Procedure FreeAll;
  1096. Procedure DeleteAll;
  1097. Procedure Free (Item: Pointer);
  1098. Procedure Insert (Item: Pointer); Virtual;
  1099. Procedure Delete (Item: Pointer);
  1100. Procedure AtFree (Index: Sw_Integer);
  1101. Procedure FreeItem (Item: Pointer); Virtual;
  1102. Procedure AtDelete (Index: Sw_Integer);
  1103. Procedure ForEach (Action: Pointer);
  1104. Procedure SetLimit (ALimit: Sw_Integer); Virtual;
  1105. Procedure Error (Code, Info: Integer); Virtual;
  1106. Procedure AtPut (Index: Sw_Integer; Item: Pointer);
  1107. Procedure AtInsert (Index: Sw_Integer; Item: Pointer);
  1108. Procedure Store (Var S: TStream);
  1109. Procedure PutItem (Var S: TStream; Item: Pointer); Virtual;
  1110. END;
  1111. PCollection = ^TCollection;
  1112. \end{verbatim}
  1113. \begin{procedure}{TCollection.Init}
  1114. \Declaration
  1115. Constructor TCollection.Init (ALimit, ADelta: Sw\_Integer);
  1116. \Description
  1117. \var{Init} initializes a new instance of a collection. It sets the (initial) maximum number
  1118. of items in the collection to \var{ALimit}. \var{ADelta} is the increase
  1119. size : The number of memory places that will be allocatiod in case \var{ALimit} is reached,
  1120. and another element is added to the collection.
  1121. \Errors
  1122. None.
  1123. \SeeAlso
  1124. \seepl{Load}{TCollection.Load}, \seepl{Done}{TCollection.Done}
  1125. \end{procedure}
  1126. \begin{procedure}{TCollection.Load}
  1127. \Declaration
  1128. Constructor TCollection.Load (Var S: TStream);
  1129. \Description
  1130. \var{Load} initializes a new instance of a collection. It reads from stream
  1131. \var{S} the item count, the item limit count, and the increase size. After
  1132. that, it reads the specified number of items from the stream.
  1133. % Do not call this method if you intend to use only pointers in your collection.
  1134. \Errors
  1135. Errors returned can be those of \seefl{GetItem}{TCollection.GetItem}.
  1136. \SeeAlso
  1137. \seepl{Init}{TCollection.Init}, \seefl{GetItem}{TCollection.GetItem},
  1138. \seepl{Done}{TCollection.Done}.
  1139. \end{procedure}
  1140. \begin{procedure}{TCollection.Done}
  1141. \Declaration
  1142. Destructor TCollection.Done; Virtual;
  1143. \Description
  1144. \var{Done} frees all objects in the collection, and then releases all memory
  1145. occupied by the instance.
  1146. % Do not call this method if you intend to use only pointers in your collection.
  1147. \Errors
  1148. None.
  1149. \SeeAlso
  1150. \seepl{Init}{TCollection.Init}, \seepl{FreeAll}{TCollection.FreeAll}
  1151. \end{procedure}
  1152. \begin{function}{TCollection.At}
  1153. \Declaration
  1154. Function TCollection.At (Index: Sw\_Integer): Pointer;
  1155. \Description
  1156. \var{At} returns the item at position \var{Index}.
  1157. \Errors
  1158. If \var{Index} is less than zero or larger than the number of items
  1159. in the collection, seepl{Error}{TCollection.Error} is called with
  1160. \var{coIndexError} and \var{Index} as arguments, resulting in a run-time
  1161. error.
  1162. \SeeAlso
  1163. \seepl{Insert}{TCollection.Insert}
  1164. \end{function}
  1165. \begin{function}{TCollection.IndexOf}
  1166. \Declaration
  1167. Function TCollection.IndexOf (Item: Pointer): Sw\_Integer; Virtual;
  1168. \Description
  1169. \var{IndexOf} returns the index of \var{Item} in the collection.
  1170. If \var{Item} isn't present in the collection, -1 is returned.
  1171. \Errors
  1172. \SeeAlso
  1173. \end{function}
  1174. \begin{function}{TCollection.GetItem}
  1175. \Declaration
  1176. Function TCollection.GetItem (Var S: TStream): Pointer; Virtual;
  1177. \Description
  1178. \var{GetItem} reads a single item off the stream \var{S}, and
  1179. returns a pointer to this item.
  1180. \Errors
  1181. Possible errors are the ones from \seef{TStream.Get}.
  1182. \SeeAlso
  1183. \seef{TStream.Get}, seepl{Store}{TCollection.Store}
  1184. \end{function}
  1185. \begin{function}{TCollection.LastThat}
  1186. \Declaration
  1187. Function TCollection.LastThat (Test: Pointer): Pointer;
  1188. \Description
  1189. This function returns the last item in the collection for which \var{Test}
  1190. returns a non-nil result. \var{Test} is a function that accepts 1 argument:
  1191. a pointer to an object, and that returns a pointer as a result.
  1192. \Errors
  1193. None.
  1194. \SeeAlso
  1195. \seefl{FirstThat}{TCollection.FirstThat}
  1196. \end{function}
  1197. \begin{function}{TCollection.FirstThat}
  1198. \Declaration
  1199. Function TCollection.FirstThat (Test: Pointer): Pointer;
  1200. \Description
  1201. This function returns the first item in the collection for which \var{Test}
  1202. returns a non-nil result. \var{Test} is a function that accepts 1 argument:
  1203. a pointer to an object, and that returns a pointer as a result.
  1204. \Errors
  1205. None.
  1206. \SeeAlso
  1207. \seefl{LastThat}{TCollection.LastThat}
  1208. \end{function}
  1209. \begin{procedure}{TCollection.Pack}
  1210. \Declaration
  1211. Procedure TCollection.Pack;
  1212. \Description
  1213. \var{Pack} removes all \var{Nil} pointers from the collection, and adjusts
  1214. \var{Count} to reflect this change. No memory is freed as a result of this
  1215. call. In order to free any memory, you can call \var{SetLimit} with an
  1216. argument of \var{Count} after a call to \var{Pack}.
  1217. \Errors
  1218. None.
  1219. \SeeAlso
  1220. \seepl{SetLimit}{TCollection.SetLimit}
  1221. \end{procedure}
  1222. \begin{procedure}{TCollection.FreeAll}
  1223. \Declaration
  1224. Procedure TCollection.FreeAll;
  1225. \Description
  1226. \var{FreeAll} calls the destructor of each object in the collection.
  1227. It doesn't release any memory occumpied by the collection itself, but it
  1228. does set \var{Count} to zero.
  1229. \Errors
  1230. \SeeAlso
  1231. \seepl{DeleteAll}{TCollection.DeleteAll}, \seepl{FreeItem}{TCollection.FreeItem}
  1232. \end{procedure}
  1233. \begin{procedure}{TCollection.DeleteAll}
  1234. \Declaration
  1235. Procedure TCollection.DeleteAll;
  1236. \Description
  1237. \var{DeleteAll} deletes all elements from the collection. It just sets
  1238. the \var{Count} variable to zero. Contrary to
  1239. \seepl{FreeAll}{TCollection.FreeAll}, \var{DeletAll} doesn't call the
  1240. destructor of the objects.
  1241. \Errors
  1242. None.
  1243. \SeeAlso
  1244. \seepl{FreeAll}{TCollection.FreeAll}, \seepl{Delete}{TCollection.Delete}
  1245. \end{procedure}
  1246. \begin{procedure}{TCollection.Free}
  1247. \Declaration
  1248. Procedure TCollection.Free (Item: Pointer);
  1249. \Description
  1250. \var{Free} Deletes \var{Item} from the collection, and calls the destructor
  1251. \var{Done} of the object.
  1252. \Errors
  1253. If the \var{Item} is not in the collection, \var{Error} will be called with
  1254. \var{coIndexError}.
  1255. \SeeAlso
  1256. \seepl{FreeItem}{TCollection.FreeItem},
  1257. \end{procedure}
  1258. \begin{procedure}{TCollection.Insert}
  1259. \Declaration
  1260. Procedure TCollection.Insert (Item: Pointer); Virtual;
  1261. \Description
  1262. \var{Insert} inserts \var{Item} in the collection. \var{TCollection}
  1263. inserts this item at the end, but descendent objects may insert it at
  1264. another place.
  1265. \Errors
  1266. None.
  1267. \SeeAlso
  1268. \seepl{AtInsert}{TCollection.AtInsert}, \seepl{AtPut}{TCollection.AtPut},
  1269. \end{procedure}
  1270. \begin{procedure}{TCollection.Delete}
  1271. \Declaration
  1272. Procedure TCollection.Delete (Item: Pointer);
  1273. \Description
  1274. \var{Delete} deletes \var{Item} from the collection. It doesn't call the
  1275. item's destructor, though. For this the \seepl{Free}{TCollection.Free}
  1276. call is provided.
  1277. \Errors
  1278. If the \var{Item} is not in the collection, \var{Error} will be called with
  1279. \var{coIndexError}.
  1280. \SeeAlso
  1281. \seepl{AtDelete}{TCollection.AtDelete},\seepl{Free}{TCollection.Free}
  1282. \end{procedure}
  1283. \begin{procedure}{TCollection.AtFree}
  1284. \Declaration
  1285. Procedure TCollection.AtFree (Index: Sw\_Integer);
  1286. \Description
  1287. \var{AtFree} deletes the item at position \var{Index} in the collection,
  1288. and calls the item's destructor if it is not \var{Nil}.
  1289. \Errors
  1290. If \var{Index} isn't valid then \seepl{Error}{TCollection.Error} is called
  1291. with \var{CoIndexError}.
  1292. \SeeAlso
  1293. \seepl{Free}{TCollection.Free}, \seepl{AtDelete}{TCollection.AtDelete}
  1294. \end{procedure}
  1295. \begin{procedure}{TCollection.FreeItem}
  1296. \Declaration
  1297. Procedure TCollection.FreeItem (Item: Pointer); Virtual;
  1298. \Description
  1299. \var{FreeItem} calls the destructor of \var{Item} if it is not nil.
  1300. \Errors
  1301. None.
  1302. \SeeAlso
  1303. \seepl{Free}{TCollection.AtFree}, seepl{AtFree}{TCollection.AtFree}
  1304. \end{procedure}
  1305. \begin{procedure}{TCollection.AtDelete}
  1306. \Declaration
  1307. Procedure TCollection.AtDelete (Index: Sw\_Integer);
  1308. \Description
  1309. \var{AtDelete} deletes the pointer at position \var{Index} in the
  1310. collection. It doesn't call the object's destructor.
  1311. \Errors
  1312. If \var{Index} isn't valid then \seepl{Error}{TCollection.Error} is called
  1313. with \var{CoIndexError}.
  1314. \SeeAlso
  1315. \seepl{Delete}{TCollection.Delete}
  1316. \end{procedure}
  1317. \begin{procedure}{TCollection.ForEach}
  1318. \Declaration
  1319. Procedure TCollection.ForEach (Action: Pointer);
  1320. \Description
  1321. \var{ForEach} calls \var{Action} for each element in the collection,
  1322. and passes the element as an argument to \var{Action}.
  1323. \var{Action} is a procedural type variable that accepts a pointer as an
  1324. argument.
  1325. \Errors
  1326. None.
  1327. \SeeAlso
  1328. \seefl{FirstThat}{TCollection.FirstThat}, \seefl{LastThat}{TCollection.LastThat}
  1329. \end{procedure}
  1330. \begin{procedure}{TCollection.SetLimit}
  1331. \Declaration
  1332. Procedure TCollection.SetLimit (ALimit: Sw\_Integer); Virtual;
  1333. \Description
  1334. \var{SetLimit} sets the maximum number of elements in the collection.
  1335. \var{ALimit} must not be less than \var{Count}, and should not be larger
  1336. than \var{MaxCollectionSize}
  1337. \Errors
  1338. None.
  1339. \SeeAlso
  1340. \seepl{Init}{TCollection.Init}
  1341. \end{procedure}
  1342. \begin{procedure}{TCollection.Error}
  1343. \Declaration
  1344. Procedure TCollection.Error (Code, Info: Integer); Virtual;
  1345. \Description
  1346. \var{Error} is called by the various \var{TCollection} methods
  1347. in case of an error condition. The default behaviour is to make
  1348. a call to \var{RunError} with an error of \var{212-Code}.
  1349. \Errors
  1350. \SeeAlso
  1351. \end{procedure}
  1352. \begin{procedure}{TCollection.AtPut}
  1353. \Declaration
  1354. Procedure TCollection.AtPut (Index: Sw\_Integer; Item: Pointer);
  1355. \Description
  1356. \var{AtPut} sets the element at position \var{Index} in the collection
  1357. to \var{Item}. Any previous value is overwritten.
  1358. \Errors
  1359. If \var{Index} isn't valid then \seepl{Error}{TCollection.Error} is called
  1360. with \var{CoIndexError}.
  1361. \SeeAlso
  1362. \end{procedure}
  1363. \begin{procedure}{TCollection.AtInsert}
  1364. \Declaration
  1365. Procedure TCollection.AtInsert (Index: Sw\_Integer; Item: Pointer);
  1366. \Description
  1367. \var{AtInsert} inserts \var{Item} in the collection at position \var{Index},
  1368. shifting all elements by one position. In case the current limit is reached,
  1369. the collection will try to expand with a call to \var{SetLimit}
  1370. \Errors
  1371. If \var{Index} isn't valid then \seepl{Error}{TCollection.Error} is called
  1372. with \var{CoIndexError}. If the collection fails to expand, then
  1373. \var{coOverFlow} is passd to \var{Error}.
  1374. \SeeAlso
  1375. \seepl{Insert}{TCollection.Insert}
  1376. \end{procedure}
  1377. \begin{procedure}{TCollection.Store}
  1378. \Declaration
  1379. Procedure TCollection.Store (Var S: TStream);
  1380. \Description
  1381. \var{Store} writes the collection to the stream \var{S}. It does
  1382. this by writeing the current \var{Count}, \var{Limit} and \var{Delta}
  1383. to the stream, and then writing each item to the stream.
  1384. The contents of the stream are then suitable for instantiating another
  1385. collection with \seepl{Load}{TCollection.Load}.
  1386. \Errors
  1387. Errors returned are those by \seep{TStream.Put}.
  1388. \SeeAlso
  1389. \seepl{Load}{TCollection.Load}, \seepl{PutItem}{TCollection.PutItem}
  1390. \end{procedure}
  1391. \begin{procedure}{TCollection.PutItem}
  1392. \Declaration
  1393. Procedure TCollection.PutItem (Var S: TStream; Item: Pointer); Virtual;
  1394. \Description
  1395. \var{PutItem} writes \var{Item} to stream \var{S}.
  1396. \Errors
  1397. Errors are those returned by \seep{TStream.Put}.
  1398. \SeeAlso
  1399. \seepl{Store}{TCollection.Store}, \seefl{GetItem}{TCollection.GetItem}.
  1400. \end{procedure}
  1401. \section{TSortedCollection}
  1402. \label{se:TSortedCollection}
  1403. \var{TSortedCollection} is an abstract class, implementing a sorted
  1404. collection. You should never use an instance of \var{TSortedCollection}
  1405. directly, instead you should declare a descendent type, and override the
  1406. \seefl{Compare}{TSortedCollection.Compare} method.
  1407. Because the collection is ordered, \var{TSortedCollection} overrides some
  1408. \var{TCollection} methods, to provide faster routines for lookup.
  1409. The \seefl{Compare}{TSortedCollection.Compare} method decides how elements
  1410. in the collection should be ordered. Since \var{TCollection} has no way
  1411. of knowing how to order pointers, you must override the compare method.
  1412. Additionally, \var{TCollection} provides a means to filter out duplicates.
  1413. if you set \var{Duplicates} to \var{False} (the default) then duplicates
  1414. will not be allowed.
  1415. Here is the complete declaration of \var{TSortedCollection}
  1416. \begin{verbatim}
  1417. TYPE
  1418. TSortedCollection = OBJECT (TCollection)
  1419. Duplicates: Boolean; { Duplicates flag }
  1420. Constructor Init (ALimit, ADelta: Sw_Integer);
  1421. Constructor Load (Var S: TStream);
  1422. Function KeyOf (Item: Pointer): Pointer; Virtual;
  1423. Function IndexOf (Item: Pointer): Sw_Integer; Virtual;
  1424. Function Compare (Key1, Key2: Pointer): Sw_Integer; Virtual;
  1425. Function Search (Key: Pointer; Var Index: Sw_Integer): Boolean;Virtual;
  1426. Procedure Insert (Item: Pointer); Virtual;
  1427. Procedure Store (Var S: TStream);
  1428. END;
  1429. PSortedCollection = ^TSortedCollection;
  1430. \end{verbatim}
  1431. \begin{procedure}{TSortedCollection.Init}
  1432. \Declaration
  1433. Constructor TSortedCollection.Init (ALimit, ADelta: Sw\_Integer);
  1434. \Description
  1435. \var{Init} calls the inherited constuctor (see \seep{TCollection.Init}) and
  1436. sets the \var{Duplicates} flag to false.
  1437. You should not call this method directly, since \var{TSortedCollection} is a
  1438. abstract class. Instead, the descendent classes should call it via the
  1439. \var{inherited} keyword.
  1440. \Errors
  1441. None.
  1442. \SeeAlso
  1443. \seepl{Load}{TSortedCollection.Load}, \seepl{Done}{TCollection.Done}
  1444. \end{procedure}
  1445. \begin{procedure}{TSortedCollection.Load}
  1446. \Declaration
  1447. Constructor Load (Var S: TStream);
  1448. \Description
  1449. \var{Load} calls the inherited constuctor (see \seep{TCollection.Load}) and
  1450. reads the \var{Duplicates} flag from the stream..
  1451. You should not call this method directly, since \var{TSortedCollection} is a
  1452. abstract class. Instead, the descendent classes should call it via the
  1453. \var{inherited} keyword.
  1454. \Errors
  1455. None.
  1456. \SeeAlso
  1457. \seepl{Init}{TSortedCollection.Init}, \seepl{Done}{TCollection.Done}
  1458. \end{procedure}
  1459. \begin{function}{TSortedCollection.KeyOf}
  1460. \Declaration
  1461. Function TSortedCollection.KeyOf (Item: Pointer): Pointer; Virtual;
  1462. \Description
  1463. \var{KeyOf} returns the key associated with \var{Item}.
  1464. \var{TSortedCollection} returns the item itself as the key, descendent
  1465. objects can override this method to calculate a (unique) key based on the
  1466. item passed (such as hash values).
  1467. \var{Keys} are used to sort the objects, they are used to search and sort
  1468. the items in the collection. If descendent types override this method then
  1469. it allows possibly for faster search/sort methods based on keys rather than
  1470. on the objects themselves.
  1471. \Errors
  1472. None.
  1473. \SeeAlso
  1474. \seefl{IndexOf}{TSortedCollection.IndexOf},
  1475. \seefl{Compare}{TSortedCollection.Compare}.
  1476. \end{function}
  1477. \begin{function}{TSortedCollection.IndexOf}
  1478. \Declaration
  1479. Function TSortedCollection.IndexOf (Item: Pointer): Sw\_Integer; Virtual;
  1480. \Description
  1481. \var{IndexOf} returns the index of \var{Item} in the collection. It searches
  1482. for the object based on it's key. If duplicates are allowed, then it returns
  1483. the index of last object that matches \var{Item}.
  1484. In case \var{Item} is not found in the collection, -1 is returned.
  1485. \Errors
  1486. None.
  1487. \SeeAlso
  1488. \seefl{Search}{TSortedCollection.Search},
  1489. \seefl{Compare}{TSortedCollection.Compare}.
  1490. \end{function}
  1491. \begin{function}{TSortedCollection.Compare}
  1492. \Declaration
  1493. Function TSortedCollection.Compare (Key1, Key2: Pointer): Sw\_Integer; Virtual;
  1494. \Description
  1495. \var{Compare} is an abstract method that should be overridden by descendent
  1496. objects in order to compare two items in the collection. This method is used
  1497. in the \seefl{Search}{TSortedCollection.Search} method and in the
  1498. \seepl{Insert}{TSortedCollection.Insert} method to determine the ordering of
  1499. the objects.
  1500. The function should compare the two keys of items and return the following
  1501. function results:
  1502. \begin{description}
  1503. \item [Result < 0] If \var{Key1} is logically before \var{Key2}
  1504. (\var{Key1<Key2})
  1505. \item [Result = 0] If \var{Key1} and \var{Key2} are equal. (\var{Key1=Key2})
  1506. \item [Result > 0] If \var{Key1} is logically after \var{Key2}
  1507. (\var{Key1>Key2})
  1508. \end{description}
  1509. \Errors
  1510. An 'abstract run-time error' will be generated if you call
  1511. \var{TSortedCollection.Compare} directly.
  1512. \SeeAlso
  1513. \seefl{IndexOf}{TSortedCollection.IndexOf},
  1514. \seefl{Search}{TSortedCollection.Search}
  1515. \end{function}
  1516. \begin{function}{TSortedCollection.Search}
  1517. \Declaration
  1518. Function TSortedCollection.Search (Key: Pointer; Var Index: Sw\_Integer): Boolean;Virtual;
  1519. \Description
  1520. \var{Search} looks for the item with key \var{Key} and returns the position
  1521. of the item (if present) in the collection in \var{Index}.
  1522. Instead of a linear search as \var{TCollection} does, \var{TSortedCollection}
  1523. uses a binary search based on the keys of the objects. It uses the
  1524. \seefl{Compare}{TSortedCollection.Compare} function to implement this
  1525. search.
  1526. If the item is found, \var{Search} returns \var{True}, otherwise \var{False}
  1527. is returned.
  1528. \Errors
  1529. None.
  1530. \SeeAlso
  1531. \seefl{IndexOf}{TCollection.IndexOf}.
  1532. \end{function}
  1533. \begin{procedure}{TSortedCollection.Insert}
  1534. \Declaration
  1535. Procedure TSortedCollection.Insert (Item: Pointer); Virtual;
  1536. \Description
  1537. \var{Insert} inserts an item in the collection at the correct position, such
  1538. that the collection is ordered at all times. You should never use
  1539. \seepl{Atinsert}{TCollection.AtInsert}, since then the collection ordering
  1540. is not guaranteed.
  1541. If \var{Item} is already present in the collection, and \var{Duplicates} is
  1542. \var{False}, the item will not be inserted.
  1543. \Errors
  1544. None.
  1545. \SeeAlso
  1546. \seepl{AtInsert}{TCollection.AtInsert}
  1547. \end{procedure}
  1548. \begin{procedure}{TSortedCollection.Store}
  1549. \Declaration
  1550. Procedure TSortedCollection.Store (Var S: TStream);
  1551. \Description
  1552. \var{Store} writes the collection to the stream \var{S}. It does this by
  1553. calling the inherited \seep{TCollection.Store}, and then writing the
  1554. \var{Duplicates} flag to the stream.
  1555. After a \var{Store}, the collection can be loaded from the stream with the
  1556. constructor \seepl{Load}{TSortedCollection.Load}
  1557. \Errors
  1558. Errors can be those of \seep{TStream.Put}.
  1559. \SeeAlso
  1560. \seepl{Load}{TSortedCollection.Load}
  1561. \end{procedure}
  1562. \section{TStringCollection}
  1563. \label{se:TStringCollection}
  1564. The \var{TStringCollection} object manages a sorted collection of pascal
  1565. strings.
  1566. To this end, it overrides the \seefl{Compare}{TSortedCollection.Compare}
  1567. method of \var{TSortedCollection}, and it introduces methods to read/write
  1568. strings from a stream.
  1569. Here is the full declaration of the \var{TStringCollection} object:
  1570. \begin{verbatim}
  1571. TYPE
  1572. TStringCollection = OBJECT (TSortedCollection)
  1573. Function GetItem (Var S: TStream): Pointer; Virtual;
  1574. Function Compare (Key1, Key2: Pointer): Sw_Integer; Virtual;
  1575. Procedure FreeItem (Item: Pointer); Virtual;
  1576. Procedure PutItem (Var S: TStream; Item: Pointer); Virtual;
  1577. END;
  1578. PStringCollection = ^TStringCollection;
  1579. \end{verbatim}
  1580. \begin{function}{TStringCollection.GetItem}
  1581. \Declaration
  1582. Function TStringCollection.GetItem (Var S: TStream): Pointer; Virtual;
  1583. \Description
  1584. \var{GetItem} reads a string from the stream \var{S} and returns a pointer
  1585. to it. It doesn't insert the string in the collection.
  1586. This method is primarily introduced to be able to load and store the
  1587. collection from and to a stream.
  1588. \Errors
  1589. The errors returned are those of \seef{TStream.ReadStr}.
  1590. \SeeAlso
  1591. \seepl{PutItem}{TStringCollection.PutItem}
  1592. \end{function}
  1593. \begin{function}{TStringCollection.Compare}
  1594. \Declaration
  1595. Function TStringCollection.Compare (Key1, Key2: Pointer): Sw\_Integer; Virtual;
  1596. \Description
  1597. \var{TStringCollection} overrides the \var{Compare} function so it compares
  1598. the two keys as if they were pointers to strings. The compare is done case
  1599. sensitive. It returns the following results:
  1600. \begin{description}
  1601. \item[-1] if the first string is alphabetically earlier than the second
  1602. string.
  1603. \item[0] if the two strings are equal.
  1604. \item[1] if the first string is alphabetically later than the second string.
  1605. \end{description}
  1606. \Errors
  1607. None.
  1608. \SeeAlso
  1609. \seef{TSortedCollection.Compare}
  1610. \end{function}
  1611. \begin{procedure}{TStringCollection.FreeItem}
  1612. \Declaration
  1613. Procedure TStringCollection.FreeItem (Item: Pointer); Virtual;
  1614. \Description
  1615. \var{TStringCollection} overrides \var{FreeItem} so that the string pointed
  1616. to by \var{Item} is disposed from memory.
  1617. \Errors
  1618. None.
  1619. \SeeAlso
  1620. \seep{TCollection.FreeItem}
  1621. \end{procedure}
  1622. \begin{procedure}{TStringCollection.PutItem}
  1623. \Declaration
  1624. Procedure TStringCollection.PutItem (Var S: TStream; Item: Pointer); Virtual;
  1625. \Description
  1626. \var{PutItem} writes the string pointed to by \var{Item} to the stream
  1627. \var{S}.
  1628. This method is primarily used in the \var{Load} and \var{Store} methods,
  1629. and should not be used directly.
  1630. \Errors
  1631. Errors are those of \seep{TStream.WriteStr}.
  1632. \SeeAlso
  1633. \seefl{GetItem}{TStringCollection.GetItem}
  1634. \end{procedure}
  1635. \section{TStrCollection}
  1636. \label{se:TStrCollection}
  1637. The \var{TStrCollection} object manages a sorted collection
  1638. of null-terminated strings (pchar strings).
  1639. To this end, it overrides the \seefl{Compare}{TSortedCollection.Compare}
  1640. method of \var{TSortedCollection}, and it introduces methods to read/write
  1641. strings from a stream.
  1642. Here is the full declaration of the \var{TStrCollection} object:
  1643. \begin{verbatim}
  1644. TYPE
  1645. TStrCollection = OBJECT (TSortedCollection)
  1646. Function Compare (Key1, Key2: Pointer): Sw_Integer; Virtual;
  1647. Function GetItem (Var S: TStream): Pointer; Virtual;
  1648. Procedure FreeItem (Item: Pointer); Virtual;
  1649. Procedure PutItem (Var S: TStream; Item: Pointer); Virtual;
  1650. END;
  1651. PStrCollection = ^TStrCollection;
  1652. \end{verbatim}
  1653. \begin{function}{TStrCollection.GetItem}
  1654. \Declaration
  1655. Function TStrCollection.GetItem (Var S: TStream): Pointer; Virtual;
  1656. \Description
  1657. \var{GetItem} reads a null-terminated string from the stream \var{S}
  1658. and returns a pointer to it. It doesn't insert the string in the
  1659. collection.
  1660. This method is primarily introduced to be able to load and store the
  1661. collection from and to a stream.
  1662. \Errors
  1663. The errors returned are those of \seef{TStream.StrRead}.
  1664. \SeeAlso
  1665. \seepl{PutItem}{TStrCollection.PutItem}
  1666. \end{function}
  1667. \begin{function}{TStrCollection.Compare}
  1668. \Declaration
  1669. Function TStrCollection.Compare (Key1, Key2: Pointer): Sw\_Integer; Virtual;
  1670. \Description
  1671. \var{TStrCollection} overrides the \var{Compare} function so it compares
  1672. the two keys as if they were pointers to strings. The compare is done case
  1673. sensitive. It returns
  1674. \begin{description}
  1675. \item[-1] if the first string is alphabetically earlier than the second
  1676. string.
  1677. \item[0] if the two strings are equal.
  1678. \item[1] if the first string is alphabetically later than the second string.
  1679. \end{description}
  1680. \Errors
  1681. None.
  1682. \SeeAlso
  1683. \seef{TSortedCollection.Compare}
  1684. \end{function}
  1685. \begin{procedure}{TStrCollection.FreeItem}
  1686. \Declaration
  1687. Procedure TStrCollection.FreeItem (Item: Pointer); Virtual;
  1688. \Description
  1689. \var{TStrCollection} overrides \var{FreeItem} so that the string pointed
  1690. to by \var{Item} is disposed from memory.
  1691. \Errors
  1692. None.
  1693. \SeeAlso
  1694. \seep{TCollection.FreeItem}
  1695. \end{procedure}
  1696. \begin{procedure}{TStrCollection.PutItem}
  1697. \Declaration
  1698. Procedure TStrCollection.PutItem (Var S: TStream; Item: Pointer); Virtual;
  1699. \Description
  1700. \var{PutItem} writes the string pointed to by \var{Item} to the stream
  1701. \var{S}.
  1702. This method is primarily used in the \var{Load} and \var{Store} methods,
  1703. and should not be used directly.
  1704. \Errors
  1705. Errors are those of \seep{TStream.StrWrite}.
  1706. \SeeAlso
  1707. \seefl{GetItem}{TStrCollection.GetItem}
  1708. \end{procedure}
  1709. \section{TUnSortedStrCollection}
  1710. \label{se:TUnSortedStrCollection}
  1711. The \var{TUnSortedStrCollection} object manages an unsorted list of objects.
  1712. To this end, it overrides the \seep{TSortedCollection.Insert} method to add
  1713. strings at the end of the collection, rather than in the alphabetically
  1714. correct position.
  1715. Take care, the \seefl{Search}{TSortedCollection.Search} and
  1716. \seefl{IndexOf}{TCollection.IndexOf} methods will not work on an unsorted
  1717. string collection.
  1718. Here is the full declaration of the {TUnsortedStrCollection} object:
  1719. \begin{verbatim}
  1720. TYPE
  1721. TUnSortedStrCollection = OBJECT (TStringCollection)
  1722. Procedure Insert (Item: Pointer); Virtual;
  1723. END;
  1724. PUnSortedStrCollection = ^TUnSortedStrCollection;
  1725. \end{verbatim}
  1726. \begin{procedure}{TUnSortedStrCollection.Insert}
  1727. \Declaration
  1728. Procedure TUnSortedStrCollection.Insert (Item: Pointer); Virtual;
  1729. \Description
  1730. \var{Insert} inserts a string at the end of the collection, instead
  1731. of on it's alphabetical place, resulting in an unsorted collection of
  1732. strings.
  1733. \Errors
  1734. \SeeAlso
  1735. \end{procedure}
  1736. \section{TResourceCollection}
  1737. \label{se:TResourceCollection}
  1738. A \var{TResourceCollection} manages a collection of resource names.
  1739. It stores the position and the size of a resource, as well as the name of
  1740. the resource. It stores these items in records that look like this:
  1741. \begin{verbatim}
  1742. TYPE
  1743. TResourceItem = packed RECORD
  1744. Posn: LongInt;
  1745. Size: LongInt;
  1746. Key : String;
  1747. End;
  1748. PResourceItem = ^TResourceItem;
  1749. \end{verbatim}
  1750. It overrides some methods of \var{TStringCollection} in order to accomplish
  1751. this.
  1752. Remark that the \var{TResourceCollection} manages the names of the
  1753. resources and their assiciated positions and sizes, it doesn't manage
  1754. the resources themselves.
  1755. Here is the full declaration of the \var{TResourceCollection} object:
  1756. \begin{verbatim}
  1757. TYPE
  1758. TResourceCollection = OBJECT (TStringCollection)
  1759. Function KeyOf (Item: Pointer): Pointer; Virtual;
  1760. Function GetItem (Var S: TStream): Pointer; Virtual;
  1761. Procedure FreeItem (Item: Pointer); Virtual;
  1762. Procedure PutItem (Var S: TStream; Item: Pointer); Virtual;
  1763. END;
  1764. PResourceCollection = ^TResourceCollection;
  1765. \end{verbatim}
  1766. \begin{function}{TResourceCollection.KeyOf}
  1767. \Declaration
  1768. Function TResourceCollection.KeyOf (Item: Pointer): Pointer; Virtual;
  1769. \Description
  1770. \var{KeyOf} returns the key of an item in the collection. For resources, the
  1771. key is a pointer to the string with the resource name.
  1772. \Errors
  1773. None.
  1774. \SeeAlso
  1775. \seef{TStringCollection.Compare}
  1776. \end{function}
  1777. \begin{function}{TResourceCollection.GetItem}
  1778. \Declaration
  1779. Function TResourceCollection.GetItem (Var S: TStream): Pointer; Virtual;
  1780. \Description
  1781. \var{GetItem} reads a resource item from the stream \var{S}. It reads the
  1782. position, size and name from the stream, in that order. It DOES NOT read the
  1783. resource itself from the stream.
  1784. The resulting item is not inserted in the collection. This call is manly for
  1785. internal use by the \seep{TCollection.Load} method.
  1786. \Errors
  1787. Errors returned are those by \seep{TStream.Read}
  1788. \SeeAlso
  1789. \seep{TCollection.Load}, \seep{TStream.Read}
  1790. \end{function}
  1791. \begin{procedure}{TResourceCollection.FreeItem}
  1792. \Declaration
  1793. Procedure TResourceCollection.FreeItem (Item: Pointer); Virtual;
  1794. \Description
  1795. \var{FreeItem} releases the memory occupied by \var{Item}. It de-allocates
  1796. the name, and then the resourceitem record.
  1797. It does NOT remove the item from the collection.
  1798. \Errors
  1799. None.
  1800. \SeeAlso
  1801. \seep{TCollection.FreeItem}
  1802. \end{procedure}
  1803. \begin{procedure}{TResourceCollection.PutItem}
  1804. \Declaration
  1805. Procedure TResourceCollection.PutItem (Var S: TStream; Item: Pointer); Virtual;
  1806. \Description
  1807. \var{PutItem} writes \var{Item} to the stream \var{S}. It does this by
  1808. writing the position and size and name of the resource item to the stream.
  1809. This method is used primarily by the \seepl{Store}{TCollection.Store}
  1810. method.
  1811. \Errors
  1812. Errors returned are those by \seep{TStream.Write}.
  1813. \SeeAlso
  1814. \seepl{Store}{TCollection.Store}
  1815. \end{procedure}
  1816. \section{TResourceFile}
  1817. \label{se:TResourceFile}
  1818. \begin{verbatim}
  1819. TYPE
  1820. TResourceFile = OBJECT (TObject)
  1821. Stream : PStream; { File as a stream }
  1822. Modified: Boolean; { Modified flag }
  1823. Constructor Init (AStream: PStream);
  1824. Destructor Done; Virtual;
  1825. Function Count: Sw_Integer;
  1826. Function KeyAt (I: Sw_Integer): String;
  1827. Function Get (Key: String): PObject;
  1828. Function SwitchTo (AStream: PStream; Pack: Boolean): PStream;
  1829. Procedure Flush;
  1830. Procedure Delete (Key: String);
  1831. Procedure Put (Item: PObject; Key: String);
  1832. END;
  1833. PResourceFile = ^TResourceFile;
  1834. \end{verbatim}
  1835. \subsection{TResourceFile Fields}
  1836. \var{TResourceFile} has the following fields:
  1837. \begin{description}
  1838. \item[Stream] contains the (file) stream that has the executable image and
  1839. the resources. It can be initialized by the \seepl{Init}{TResourceFile.Init}
  1840. constructor call.
  1841. \item[Modified] is set to \var{True} if one of the resources has been changed.
  1842. It is set by the \seepl{SwitchTo}{TResourceFile.Init},
  1843. \seepl{Delete}{TResourceFile.Delete} and \seepl{Put}{TResourceFile.Put}
  1844. methods. Calling \seepl{Flush}{TResourceFile.Flush} will clear the
  1845. \var{Modified} flag.
  1846. \end{description}
  1847. \begin{procedure}{TResourceFile.Init}
  1848. \Declaration
  1849. Constructor TResourceFile.Init (AStream: PStream);
  1850. \Description
  1851. \var{Init} instantiates a new instance of a \var{TResourceFile} object.
  1852. If \var{AStream} is not nil then it is considered as a stream describing an
  1853. executable image on disk.
  1854. \var{Init} will try to position the stream on the start of the resources section,
  1855. and read all resources from the stream.
  1856. \Errors
  1857. None.
  1858. \SeeAlso
  1859. \seepl{Done}{TResourceFile.Done}
  1860. \end{procedure}
  1861. \begin{procedure}{TResourceFile.Done}
  1862. \Declaration
  1863. Destructor TResourceFile.Done; Virtual;
  1864. \Description
  1865. \var{Done} cleans up the instance of the \var{TResourceFile} Object.
  1866. If \var{Stream} was specified at initialization, then \var{Stream} is
  1867. disposed of too.
  1868. \Errors
  1869. None.
  1870. \SeeAlso
  1871. \seepl{Init}{TResourceFile.Init}
  1872. \end{procedure}
  1873. \begin{function}{TResourceFile.Count}
  1874. \Declaration
  1875. Function TResourceFile.Count: Sw\_Integer;
  1876. \Description
  1877. \var{Count} returns the number of resources. If no resources were
  1878. read, zero is returned.
  1879. \Errors
  1880. None.
  1881. \SeeAlso
  1882. \seepl{Init}{TResourceFile.Init}
  1883. \end{function}
  1884. \begin{function}{TResourceFile.KeyAt}
  1885. \Declaration
  1886. Function TResourceFile.KeyAt (I: Sw\_Integer): String;
  1887. \Description
  1888. \var{KeyAt} returns the key (the name) of the \var{I}-th resource.
  1889. \Errors
  1890. In case \var{I} is invalid, \var{TCollection.Error} will be executed.
  1891. \SeeAlso
  1892. \seefl{Get}{TResourceFile.Get}
  1893. \end{function}
  1894. \begin{function}{TResourceFile.Get}
  1895. \Declaration
  1896. Function TResourceFile.Get (Key: String): PObject;
  1897. \Description
  1898. \var{Get} returns a pointer to a instance of a resource identified by
  1899. \var{Key}. If \var{Key} cannot be found in the list of resources, then
  1900. \var{Nil} is returned.
  1901. \Errors
  1902. Errors returned may be those by \var{TStream.Get}
  1903. \SeeAlso
  1904. \end{function}
  1905. \begin{function}{TResourceFile.SwitchTo}
  1906. \Declaration
  1907. Function TResourceFile.SwitchTo (AStream: PStream; Pack: Boolean): PStream;
  1908. \Description
  1909. \var{SwitchTo} switches to a new stream to hold the resources in.
  1910. \var{AStream} will be the new stream after the call to \var{SwitchTo}.
  1911. If \var{Pack} is true, then all the known resources will be copied from
  1912. the current stream to the new stream (\var{AStream}). If \var{Pack} is
  1913. \var{False}, then only the current resource is copied.
  1914. The return value is the value of the original stream: \var{Stream}.
  1915. The \var{Modified} flag is set as a consequence of this call.
  1916. \Errors
  1917. Errors returned can be those of \seep{TStream.Read} and
  1918. \seep{TStream.Write}.
  1919. \SeeAlso
  1920. \seepl{Flush}{TResourceFile.Flush}
  1921. \end{function}
  1922. \begin{procedure}{TResourceFile.Flush}
  1923. \Declaration
  1924. Procedure TResourceFile.Flush;
  1925. \Description
  1926. If the \var{Modified} flag is set to \var{True}, then \var{Flush}
  1927. writes the resources to the stream \var{Stream}. It sets the \var{Modified}
  1928. flag to true after that.
  1929. \Errors
  1930. Errors can be those by \seep{TStream.Seek} and \seep{TStream.Write}.
  1931. \SeeAlso
  1932. \seefl{SwitchTo}{TResourceFile.SwitchTo}
  1933. \end{procedure}
  1934. \begin{procedure}{TResourceFile.Delete}
  1935. \Declaration
  1936. Procedure TResourceFile.Delete (Key: String);
  1937. \Description
  1938. \var{Delete} deletes the resource identified by \var{Key} from the
  1939. collection. It sets the \var{Modified} flag to true.
  1940. \Errors
  1941. None.
  1942. \SeeAlso
  1943. \seepl{Flush}{TResourceFile.Flush}
  1944. \end{procedure}
  1945. \begin{procedure}{TResourceFile.Put}
  1946. \Declaration
  1947. Procedure TResourceFile.Put (Item: PObject; Key: String);
  1948. \Description
  1949. \var{Put} sets the resource identified by \var{Key} to \var{Item}.
  1950. If no such resource exists, a new one is created. The item is written
  1951. to the stream.
  1952. \Errors
  1953. Errors returned may be those by \seep{TStream.Put} and \var{TStream.Seek}
  1954. \SeeAlso
  1955. \seefl{Get}{TResourceFile.Get}
  1956. \end{procedure}
  1957. \section{TStringList}
  1958. \label{se:TStringList}
  1959. \begin{verbatim}
  1960. TYPE
  1961. TStrIndexRec = Packed RECORD
  1962. Key, Count, Offset: Word;
  1963. END;
  1964. TStrIndex = Array [0..9999] Of TStrIndexRec;
  1965. PStrIndex = ^TStrIndex;
  1966. TStringList = OBJECT (TObject)
  1967. Constructor Load (Var S: TStream);
  1968. Destructor Done; Virtual;
  1969. Function Get (Key: Sw_Word): String;
  1970. END;
  1971. PStringList = ^TStringList;
  1972. \end{verbatim}
  1973. \section{TStrListMaker}
  1974. \label{se:TStrListMaker}
  1975. \begin{verbatim}
  1976. TYPE
  1977. TStrListMaker = OBJECT (TObject)
  1978. Constructor Init (AStrSize, AIndexSize: Sw_Word);
  1979. Destructor Done; Virtual;
  1980. Procedure Put (Key: Sw_Word; S: String);
  1981. Procedure Store (Var S: TStream);
  1982. END;
  1983. PStrListMaker = ^TStrListMaker;
  1984. \end{verbatim}