2. Implementation of Twriter method
The realization of ⑴writelistbegin and writelistend
Both of these methods are used to write a number of consecutive values of the same type. Writelistbegin writes the VALIST flag, Writelistend writes to the VANULL flag.
Procedure Twriter.writelistbegin;
Begin
WriteValue (valist);
End
Procedure Twriter.writelistend;
Begin
WriteValue (Vanull);
End
Both methods call the WriteValue method of the Twriter object, which is used primarily to write values of the Tvaluetype type.
Procedure Twriter.writevalue (Value:tvaluetype);
Begin
Write (value, SizeOf (value));
End
⑵ writes for simple data types
Simple data types refer to integers, character types, string literals, floating-point types, booleans, and so on. The Twriter object defines the corresponding write method.
The Writeinteger method is used to write integer data.
Procedure Twriter.writeinteger (Value:longint);
Begin
if (value >= -128) and (value <= 127) Then
Begin
WriteValue (vaInt8);
Write (Value, SizeOf (Shortint));
End Else
if (value >= -32768) and (value <= 32767) Then
Begin
WriteValue (vaInt16);
Write (Value, SizeOf (Smallint));
End Else
Begin
WriteValue (VaInt32);
Write (Value, SizeOf (Longint));
End
End
The Writeinteger method divides the integer data into 8-bit, 16-bit and 32-bit three, and uses VaInt8, vaInt16 and VaInt32 respectively.
Writeboolean is used to write Boolean data:
Procedure Twriter.writeboolean (Value:boolean);
Begin
If Value Then
WriteValue (vatrue) Else
WriteValue (Vafalse);
End
Unlike other data types, Boolean data uses only a flag bit to store a Boolean value, and there is no data after the flag bit.
The Writefloat method is used to write floating-point data.
Procedure Twriter.writefloat (value:extended);
Begin
WriteValue (vaextended);
Write (Value, SizeOf (Extended));
End
The string "True", "False" and "nil" are passed in as identifiers due to the special needs of Delphi. If "True", "False", and "nil" are written to Vatrue, Vafalse, and Vanil, otherwise the vaident flag is written, and the identifier is written as a string.
WriteString method is used to write strings
Procedure twriter.writestring (const value:string);
Var
L:integer;
Begin
L: = Length (Value);
If L <= 255 Then
Begin
WriteValue (vastring);
Write (L, SizeOf (Byte));
End Else
Begin
WriteValue (valstring);
Write (L, SizeOf (Integer));
End
Write (pointer (Value) ^, L);
End
There are two types of strings for Delphi. One is less than 256 bytes long and the other is less than 65,536 bytes long. The WriteString method distinguishes between these two types of cases of storing strings, one setting VASTIRNG flags and the other setting valstring. The length value of the string is then stored, and the string data is finally stored.
The Writechar method is used to write characters.
Procedure Twriter.writechar (Value:char);
Begin
WriteString (Value);
End
Character type reading and writing is a method of reading and writing a string, when reading, judge the number of bytes is 1 o'clock, then the character type.