The 20th Chapter-Development Delphi Object Type Data management function (four) (4)

Source: Internet
Author: User
Tags data structures

5. The realization of simple data type reading method

Simple data types refer to Boolean, character, Integer, string, floating-point, collection type, and identifier. They are introduced together because they are implemented in a similar way.

Because they are implemented using the ReadValue method, we first introduce the implementation of the ReadValue method:

function TReader.ReadValue:TValueType;

Begin

Read (result, SizeOf (result));

End

The method invokes the private method read, reads a byte from the reader object stream, and moves the position pointer.

The ReadValue method specifically reads the type of the value from the stream, and all data read and write methods call the ReadValue method to determine whether the data is to be read before reading the data. If it is, the Read method is called to read the data, otherwise an exception event is fired, and the reading method of the integer type is shown below:

function TReader.ReadInteger:Longint;

Var

S:shortint;

I:smallint;

Begin

Case ReadValue of

VaInt8:

Begin

Read (S, SizeOf (Shortint));

Result: = S;

End

VaInt16:

Begin

Read (i, SizeOf (i));

Result: = I;

End

VaInt32:

Read (result, SizeOf (result));

Else

Propvalueerror;

End

End

Because of Delphi 2.0, the integral type can be divided into 8 bits, 16 bits and 32 bits, so the reading of integer data is judged separately.

Boolean-type data is placed directly on the value type flag, or True if the type is vatrue, or False if the type is vafalse.

function TReader.ReadBoolean:Boolean;

Begin

Result: = ReadValue = Vatrue;

End

The ReadString method also uses the ReadValue method to determine whether it is a string or a long string.

function TReader.ReadString:string;

Var

L:integer;

Begin

L: = 0;

Case ReadValue of

Vastring:

Read (L, SizeOf (Byte));

Valstring:

Read (L, SizeOf (Integer));

Else

Propvalueerror;

End

SetString (result, Pchar (nil), L);

Read (pointer (result) ^, L);

End

If the vastring type has the length of the string followed by a byte, and if it is the Valstring class, then the string length is followed by the two bytes, then the space is allocated using the SetString procedure based on the length of the string, and the data is read using the Read method.

The Readfloat method allows you to convert an integer value to a floating-point type.

function TReader.ReadFloat:Extended;

Begin

If ReadValue = vaextended then Read (result, SizeOf) Else

Begin

Dec (Fbufpos);

Result: = Readinteger;

End

End

The character type data is provided with a direct flag, which is judged by a byte with an ordinal value of 1 after vastring.

function TReader.ReadChar:Char;

Begin

CheckValue (vastring);

Read (result, 1);

If Ord (Result) <> 1 Then

Begin

Dec (Fbufpos);

READSTR;

Propvalueerror;

End

Read (result, 1);

End

To read the DFM file, the Filer object supports read identifiers.

function TReader.ReadIdent:string;

Var

L:byte;

Begin

Case ReadValue of

Vaident:

Begin

Read (L, SizeOf (Byte));

SetString (result, Pchar (nil), L);

Read (result[1], L);

End

Vafalse:

Result: = ' False ';

Vatrue:

Result: = ' True ';

Vanil:

Result: = ' nil ';

Else

Propvalueerror;

End

End

In general, all kinds of complex data structures are composed of these simple elements, the definition of these methods is equal to the reading of various types of data provides a meta operation, easy to use. For example, when reading data of a string type, it is different to use the ReadString method if the Liu method is used to determine the length of the string. However, it should be noted that the storage format for these types of data is clearly different from the simple data type designed by Delphi. Therefore, the data should be stored in the corresponding method of the writer object, and before reading the data to be judged by the NextValue method, otherwise it will trigger an exception event.

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.