diff options
author | tobigun <tobigun@b956fd51-792f-4845-bead-9b4dfca2ff2c> | 2009-07-23 20:19:42 +0000 |
---|---|---|
committer | tobigun <tobigun@b956fd51-792f-4845-bead-9b4dfca2ff2c> | 2009-07-23 20:19:42 +0000 |
commit | e941bd65cecb31c84202bbbe14db882a906a0663 (patch) | |
tree | 34425a857b7883d3766c886f142abc32d3731cd4 /unicode | |
parent | 95e2044583db3b8dbe0b7d859385ac4fe164f921 (diff) | |
download | usdx-e941bd65cecb31c84202bbbe14db882a906a0663.tar.gz usdx-e941bd65cecb31c84202bbbe14db882a906a0663.tar.xz usdx-e941bd65cecb31c84202bbbe14db882a906a0663.zip |
TUnicodeMemIniFile added
git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/branches/experimental@1905 b956fd51-792f-4845-bead-9b4dfca2ff2c
Diffstat (limited to '')
-rw-r--r-- | unicode/src/base/UPath.pas | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/unicode/src/base/UPath.pas b/unicode/src/base/UPath.pas index 32e1bc02..a8743470 100644 --- a/unicode/src/base/UPath.pas +++ b/unicode/src/base/UPath.pas @@ -36,6 +36,7 @@ interface uses SysUtils, Classes, + IniFiles, {$IFDEF MSWINDOWS} TntClasses, {$ENDIF} @@ -53,6 +54,23 @@ type procedure LoadFromFile(const FileName: IPath); procedure SaveToFile(const FileName: IPath); end; + + {** + * Unicode capable IniFile implementation. + * TMemIniFile and TIniFile are not able to handle INI-files with + * an UTF-8 BOM. This implementation checks if an UTF-8 BOM exists + * and removes it from the internal string-list. + * UTF8Encoded is set accordingly. + *} + TUnicodeMemIniFile = class(TMemIniFile) + private + FFilename: IPath; + FUTF8Encoded: boolean; + public + constructor Create(const FileName: IPath; UTF8Encoded: boolean = false); reintroduce; + procedure UpdateFile; override; + property UTF8Encoded: boolean READ FUTF8Encoded WRITE FUTF8Encoded; + end; {** * TBinaryFileStream (inherited from THandleStream) @@ -363,6 +381,7 @@ implementation uses RTLConsts, + UTextEncoding, UFilesystem; {* @@ -1313,6 +1332,66 @@ begin end; end; +{ TUnicodeMemIniFile } + +constructor TUnicodeMemIniFile.Create(const FileName: IPath; UTF8Encoded: boolean); +var + List: TStringList; + Stream: TBinaryFileStream; + BOMBuf: array[0..2] of AnsiChar; +begin + inherited Create(''); + FFilename := FileName; + FUTF8Encoded := UTF8Encoded; + + if FileName.Exists() then + begin + List := nil; + Stream := nil; + try + List := TStringList.Create; + Stream := TBinaryFileStream.Create(FileName, fmOpenRead); + if (Stream.Read(BOMBuf[0], SizeOf(BOMBuf)) = 3) and + (CompareMem(PChar(UTF8_BOM), @BomBuf, Length(UTF8_BOM))) then + begin + // truncate BOM + FUTF8Encoded := true; + end + else + begin + // rewind file + Stream.Seek(0, soBeginning); + end; + List.LoadFromStream(Stream); + SetStrings(List); + finally + Stream.Free; + List.Free; + end; + end; +end; + +procedure TUnicodeMemIniFile.UpdateFile; +var + List: TStringList; + Stream: TBinaryFileStream; +begin + List := nil; + Stream := nil; + try + List := TStringList.Create; + GetStrings(List); + Stream := TBinaryFileStream.Create(FFileName, fmCreate); + if UTF8Encoded then + Stream.Write(UTF8_BOM, Length(UTF8_BOM)); + List.SaveToStream(Stream); + finally + List.Free; + Stream.Free; + end; +end; + + var PATH_NONE_Singelton: IPath; |