20th Chapter-Development Delphi Object Type Data management function (III.) (4)

Source: Internet
Author: User
Tags definition

Implementation of 20.2.2.2 Twriter object

The Twriter object provides a number of ways to write various types of data into the stream, which is an important feature for programmers. The Twrite object writes data to the stream in a different format depending on the data. Therefore, in order to master the implementation and application methods of Twriter objects, we must understand the format of writer object storage data.

The first thing to note is that each filer object's stream contains a filer object tag. The label occupies four bytes and its value is "TPF0." The Filer object accesses the label for the Writesignature and Readsignature methods. This label is used primarily for reader object reading data (parts, etc.) to guide the read operation.

Second, the writer object leaves a byte flag bit before storing the data to indicate what type of data is stored behind it. The byte is a value of type Tvaluetype. The Tvaluetype is an enumeration type that takes up a byte space and is defined as follows:

Tvaluetype = (Vanull, valist, VaInt8, VaInt16, VaInt32, vaentended, vastring, Vaident,

Vafalse, Vatrue, Vabinary, Vaset, valstring, Vanil, vacollection);

Therefore, the writer object of each write data method, in the implementation, must first write the flag bit and write the corresponding data, and reader object of every read data method must read the mark bit to judge, if conforms to the study data, otherwise will produce a reading data invalid exception event. The VALIST flag has a special purpose, it is used to identify the following will have a series of the same type of items, while the logo for the end of consecutive items is vanull. Therefore, when the writer object writes a number of identical items consecutively, first write the valist flag with Writelistbegin, then write the vanull mark after the data item is written, and when reading the data, the Readlistbegin begins, the readlistend ends, The middle uses the Endoflist function to determine whether there are vanull signs.

Here's how they are implemented.

1. Implementation of Twriter object properties

The Twriter object inherits directly from the Tfiler object, and it only adds the position and Rootancestor attributes.

The Rootancestor property has a data field frootancestor to its value in the private part. The value is read directly on the read and control of the property definition.

The definition of the Position property contains two read-write control methods: GetPosition and SetPosition:

Twriter = Class (Tfiler)

Private

Frootancestor:tcomponent;

...

function Getposition:longint;

Procedure SetPosition (Value:longint);

Public

...

Property Position:longint read GetPosition write setposition;

Property Rootancestor:tcomponent read Frootancestor write frootancestor;

End

The GetPosition and SetPosition methods are implemented as follows:

function TWriter.GetPosition:Longint;

Begin

Result: = Fstream.position + Fbufpos;

End

Procedure Twriter.setposition (Value:longint);

Var

Streamposition:longint;

Begin

Streamposition: = fstream.position;

{Clear only buffers that are out of bounds}

if (value < streamposition) or (Value > streamposition + fbufpos) Then

Begin

WriteBuffer;

Fstream.position: = Value;

End

else Fbufpos: = value-streamposition;

End

WriteBuffer is a private method of Twriter object definition that is used to write valid data in the writer object's internal buffer into the stream and to set the Fbufpos to 0. The Flushbuffer object of the writer object is to flush the buffer with the WriteBuffer method.

In the SetPosition method, if the value exceeds the bounds (fstream.position,fstream.position + fbufpos), the contents of the buffer are written to the stream, and the buffer is reset relative to the stream; Just move the Fbufpos pointer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.