~ubuntu-branches/ubuntu/dapper/fpc/dapper

« back to all changes in this revision

Viewing changes to docs/objects.tex

  • Committer: Bazaar Package Importer
  • Author(s): Carlos Laviola
  • Date: 2005-05-30 11:59:10 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050530115910-x5pbzm4qqta4i94h
Tags: 2.0.0-2
debian/fp-compiler.postinst.in: forgot to reapply the patch that
correctly creates the slave link to pc(1).  (Closes: #310907)

Show diffs side-by-side

added added

removed removed

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