ConfSome configuration files areUTF-8AndUnicodeFormat, so that you can well support multiple languages, read and write data from the InternetUTF-8,UnicodeThe file has written several streamlined functions. After the update, the file header function is added to meet more requirements. Note that the function does not provide error protection.
Parameter description:FFile Name,SThe content of the file to be written or read,HSFile Header,BWhether to read or write the file header.
UTF-8File Writing Function
Code
Procedure Saveutf (F: String ; S: String ; B: Boolean = True );
VaR
MS: tmemorystream;
HS: string;
Begin
If S = '' Then Exit;
MS: = Tmemorystream. Create;
If B Then Begin
HS: = # $ EF # $ BB # $ BF;
Ms. Write (HS [ 1 ], 3 );
End ;
S: = Ansitoutf8 (s );
Ms. Write (s [ 1 ], Length (s ));
Ms. Position: = 0 ;
Ms. savetofile (f );
Ms. Free;
End ;
UtF-8File Reading Function
Code
Function Loadutf (F: String ; B: Boolean = True ): String ;
VaR
MS: tmemorystream;
S, HS: String ;
Begin
Result: = '' ;
If Not Fileexists (f) Then Exit;
MS: = Tmemorystream. Create;
Ms. loadfromfile (f );
If B Then Begin
Setlength (HS, 3 );
Ms. Read (HS [ 1 ], 3 );
If HS <> # $ EF # $ BB # $ BF Then Begin Ms. Free; exit; End ;
Setlength (S, ms. Size - 3 );
Ms. Read (s [ 1 ], Ms. Size - 3 );
End Else Begin
Setlength (S, ms. size );
Ms. Read (s [ 1 ], Ms. size );
End ;
Result: = Utf8toansi (s );
Ms. Free;
End ;
UnicodeFile Writing Function
Code
Procedure Saveunicode (F: String ; S: String ; B: Boolean = True );
VaR
MS: tmemorystream;
HS: String ;
WS: widestring;
Begin
If S = '' Then Exit;
MS: = Tmemorystream. Create;
If B Then Begin
HS: = # $ FF # $ Fe;
Ms. Write (HS [ 1 ], 2 );
End ;
WS: = Widestring (s );
Ms. Write (WS [ 1 ], Length (WS) * 2 );
Ms. Position: = 0 ;
Ms. savetofile (f );
Ms. Free;
End ;
UnicodeFile Reading Function
Code
Function Loadunicode (F: String ; B: Boolean = True ): String ;
VaR
MS: tmemorystream;
HS: string;
WS: widestring;
Begin
Result: = '' ;
If Not Fileexists (f) Then Exit;
MS: = Tmemorystream. Create;
Ms. loadfromfile (f );
If B Then Begin
Setlength (HS, 2 );
Ms. Read (HS [ 1 ], 2 );
If HS <> # $ FF # $ fe Then Begin Ms. Free; exit; End ;
Setlength (WS, (Ms. Size - 2 ) Div 2 );
Ms. Read (WS [ 1 ], Ms. Size - 2 );
End Else Begin
Setlength (WS, ms. Size Div 2 );
Ms. Read (WS [ 1 ], Ms. size );
End ;
Result: = Ansistring (WS );
Ms. Free;
End ;