objects.tex 78 KB

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