php 2008-1-23 10:31
Delphi读写UTF-8、Unicode格式文本文件
在研究PvPGN时发现conf配置文件一些为UTF-8和Unicode格式,这样便可良好的支持多语言,从网上查阅资料后,将读写UTF-8、Unicode文件写了几个最精简的函数,更新后加了是否写文件头的功能,以适应更多需要,注意函数未加防错保护。
参数说明:f文件名、s写入或读取的文件内容、hs文件头、b是否读写文件头。
[color=#000000]UTF-8文件写入函数
程序代码 程序代码
[color=#0000ff]procedure[/color] SaveUTF[color=#0000cc]([/color]f[color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]s[color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]b[color=#0000cc]:[/color][color=#ff0000]boolean[/color][color=#0000cc]=[/color]true[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]var[/color]
ms[color=#0000cc]:[/color]TMemoryStream[color=#0000cc];[/color]
hs[color=#0000cc]:[/color][color=#ff0000]String[/color][color=#0000cc];[/color]
[color=#0000ff]begin[/color]
[color=#0000ff]if[/color] s[color=#0000cc]=[/color][color=#ff00ff]''[/color] [color=#0000ff]then[/color] exit[color=#0000cc];[/color]
ms[color=#0000cc]:[/color][color=#0000cc]=[/color]TMemoryStream[color=#0000cc].[/color]Create[color=#0000cc];[/color]
[color=#0000ff]if[/color] b [color=#0000ff]then[/color] [color=#0000ff]begin[/color]
hs[color=#0000cc]:[/color][color=#0000cc]=[/color][color=#0000cc]#[/color][color=#0000cc]$[/color]EF[color=#0000cc]#[/color][color=#0000cc]$[/color]BB[color=#0000cc]#[/color][color=#0000cc]$[/color]BF[color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Write[/color][color=#0000cc]([/color]hs[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]3[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]end[/color][color=#0000cc];[/color]
s[color=#0000cc]:[/color][color=#0000cc]=[/color]AnsiToUtf8[color=#0000cc]([/color]s[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Write[/color][color=#0000cc]([/color]s[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]Length[color=#0000cc]([/color]s[color=#0000cc])[/color][color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color]Position[color=#0000cc]:[/color][color=#0000cc]=[/color]0[color=#0000cc];[/color]
ms[color=#0000cc].[/color]SaveToFile[color=#0000cc]([/color]f[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color]Free[color=#0000cc];[/color]
[color=#0000ff]end[/color][color=#0000cc];[/color]
UtF-8文件读取函数
程序代码 程序代码
[color=#0000ff]function[/color] LoadUTF[color=#0000cc]([/color]f[color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]b[color=#0000cc]:[/color][color=#ff0000]boolean[/color][color=#0000cc]=[/color]true[color=#0000cc])[/color][color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]
[color=#0000ff]var[/color]
ms[color=#0000cc]:[/color]TMemoryStream[color=#0000cc];[/color]
s[color=#0000cc],[/color]hs[color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]
[color=#0000ff]begin[/color]
Result[color=#0000cc]:[/color][color=#0000cc]=[/color][color=#ff00ff]''[/color][color=#0000cc];[/color]
[color=#0000ff]if[/color] [color=#0000ff]not[/color] FileExists[color=#0000cc]([/color]f[color=#0000cc])[/color] [color=#0000ff]then[/color] exit[color=#0000cc];[/color]
ms[color=#0000cc]:[/color][color=#0000cc]=[/color]TMemoryStream[color=#0000cc].[/color]Create[color=#0000cc];[/color]
ms[color=#0000cc].[/color]LoadFromFile[color=#0000cc]([/color]f[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]if[/color] b [color=#0000ff]then[/color] [color=#0000ff]begin[/color]
SetLength[color=#0000cc]([/color]hs[color=#0000cc],[/color]3[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Read[/color][color=#0000cc]([/color]hs[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]3[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]if[/color] hs[color=#0000cc]<[/color][color=#0000cc]>[/color][color=#0000cc]#[/color][color=#0000cc]$[/color]EF[color=#0000cc]#[/color][color=#0000cc]$[/color]BB[color=#0000cc]#[/color][color=#0000cc]$[/color]BF [color=#0000ff]then[/color] [color=#0000ff]begin[/color] ms[color=#0000cc].[/color]Free[color=#0000cc];[/color] exit[color=#0000cc];[/color] [color=#0000ff]end[/color][color=#0000cc];[/color]
SetLength[color=#0000cc]([/color]s[color=#0000cc],[/color]ms[color=#0000cc].[/color]Size-3[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Read[/color][color=#0000cc]([/color]s[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]ms[color=#0000cc].[/color]Size-3[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]end[/color] [color=#0000ff]else[/color] [color=#0000ff]begin[/color]
SetLength[color=#0000cc]([/color]s[color=#0000cc],[/color]ms[color=#0000cc].[/color]Size[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Read[/color][color=#0000cc]([/color]s[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]ms[color=#0000cc].[/color]Size[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]end[/color][color=#0000cc];[/color]
Result[color=#0000cc]:[/color][color=#0000cc]=[/color]Utf8ToAnsi[color=#0000cc]([/color]s[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color]Free[color=#0000cc];[/color]
[color=#0000ff]end[/color][color=#0000cc];[/color]
Unicode文件写入函数
程序代码 程序代码
[color=#0000ff]procedure[/color] SaveUnicode[color=#0000cc]([/color]f[color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]s[color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]b[color=#0000cc]:[/color][color=#ff0000]boolean[/color][color=#0000cc]=[/color]true[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]var[/color]
ms[color=#0000cc]:[/color]TMemoryStream[color=#0000cc];[/color]
hs[color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]
ws[color=#0000cc]:[/color]WideString[color=#0000cc];[/color]
[color=#0000ff]begin[/color]
[color=#0000ff]if[/color] s[color=#0000cc]=[/color][color=#ff00ff]''[/color] [color=#0000ff]then[/color] exit[color=#0000cc];[/color]
ms[color=#0000cc]:[/color][color=#0000cc]=[/color]TMemoryStream[color=#0000cc].[/color]Create[color=#0000cc];[/color]
[color=#0000ff]if[/color] b [color=#0000ff]then[/color] [color=#0000ff]begin[/color]
hs[color=#0000cc]:[/color][color=#0000cc]=[/color][color=#0000cc]#[/color][color=#0000cc]$[/color]FF[color=#0000cc]#[/color][color=#0000cc]$[/color]FE[color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Write[/color][color=#0000cc]([/color]hs[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]2[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]end[/color][color=#0000cc];[/color]
ws[color=#0000cc]:[/color][color=#0000cc]=[/color]WideString[color=#0000cc]([/color]s[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Write[/color][color=#0000cc]([/color]ws[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]Length[color=#0000cc]([/color]ws[color=#0000cc])[/color][color=#0000cc]*[/color]2[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color]Position[color=#0000cc]:[/color][color=#0000cc]=[/color]0[color=#0000cc];[/color]
ms[color=#0000cc].[/color]SaveToFile[color=#0000cc]([/color]f[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color]Free[color=#0000cc];[/color]
[color=#0000ff]end[/color][color=#0000cc];[/color]
Unicode文件读取函数
程序代码 程序代码
[color=#0000ff]function[/color] LoadUnicode[color=#0000cc]([/color]f[color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]b[color=#0000cc]:[/color][color=#ff0000]boolean[/color][color=#0000cc]=[/color]true[color=#0000cc])[/color][color=#0000cc]:[/color][color=#ff0000]string[/color][color=#0000cc];[/color]
[color=#0000ff]var[/color]
ms[color=#0000cc]:[/color]TMemoryStream[color=#0000cc];[/color]
hs[color=#0000cc]:[/color][color=#ff0000]String[/color][color=#0000cc];[/color]
ws[color=#0000cc]:[/color]WideString[color=#0000cc];[/color]
[color=#0000ff]begin[/color]
Result[color=#0000cc]:[/color][color=#0000cc]=[/color][color=#ff00ff]''[/color][color=#0000cc];[/color]
[color=#0000ff]if[/color] [color=#0000ff]not[/color] FileExists[color=#0000cc]([/color]f[color=#0000cc])[/color] [color=#0000ff]then[/color] exit[color=#0000cc];[/color]
ms[color=#0000cc]:[/color][color=#0000cc]=[/color]TMemoryStream[color=#0000cc].[/color]Create[color=#0000cc];[/color]
ms[color=#0000cc].[/color]LoadFromFile[color=#0000cc]([/color]f[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]if[/color] b [color=#0000ff]then[/color] [color=#0000ff]begin[/color]
SetLength[color=#0000cc]([/color]hs[color=#0000cc],[/color]2[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Read[/color][color=#0000cc]([/color]hs[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]2[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]if[/color] hs[color=#0000cc]<[/color][color=#0000cc]>[/color][color=#0000cc]#[/color][color=#0000cc]$[/color]FF[color=#0000cc]#[/color][color=#0000cc]$[/color]FE [color=#0000ff]then[/color] [color=#0000ff]begin[/color] ms[color=#0000cc].[/color]Free[color=#0000cc];[/color] exit[color=#0000cc];[/color] [color=#0000ff]end[/color][color=#0000cc];[/color]
SetLength[color=#0000cc]([/color]ws[color=#0000cc],[/color][color=#0000cc]([/color]ms[color=#0000cc].[/color]Size-2[color=#0000cc])[/color] [color=#0000ff]div[/color] 2[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Read[/color][color=#0000cc]([/color]ws[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]ms[color=#0000cc].[/color]Size-2[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]end[/color] [color=#0000ff]else[/color] [color=#0000ff]begin[/color]
SetLength[color=#0000cc]([/color]ws[color=#0000cc],[/color]ms[color=#0000cc].[/color]Size [color=#0000ff]div[/color] 2[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color][color=#0000ff]Read[/color][color=#0000cc]([/color]ws[color=#0000cc][[/color]1[color=#0000cc]][/color][color=#0000cc],[/color]ms[color=#0000cc].[/color]Size[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]end[/color][color=#0000cc];[/color]
Result[color=#0000cc]:[/color][color=#0000cc]=[/color]AnsiString[color=#0000cc]([/color]ws[color=#0000cc])[/color][color=#0000cc];[/color]
ms[color=#0000cc].[/color]Free[color=#0000cc];[/color]
[color=#0000ff]end[/color][color=#0000cc];[/color][/color]