objects.tex 78 KB

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