objects.tex 76 KB

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