Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread RTT

 It is not that easy!
 The code above might work if String maps to an AnsiString,
 however fails if String maps to a UnicodeString ( Delpi 2009 ).
   
This little modification does the job in D2009

uses  rtlconsts;
...

 procedure TMyUnicodeIniFile.WriteString(const Section, Ident, Value: 
string);
 var s:Ansistring;
 begin
 s:=Utf8Encode(Value);
  if not WritePrivateProfileStringA(PAnsiChar(AnsiString((Section))), 
PAnsiChar(AnsiString((Ident))),
   PAnsiChar(s), 
PAnsiChar(AnsiString((FileName then
raise EIniFileException.CreateResFmt(@SIniFileWriteError, [FileName]);
 end;
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread Arno Garrels
RTT wrote:
 It is not that easy!
 The code above might work if String maps to an AnsiString,
 however fails if String maps to a UnicodeString ( Delpi 2009 ).
 
 This little modification does the job in D2009
 
 uses  rtlconsts;
 ...
 
 procedure TMyUnicodeIniFile.WriteString(const Section, Ident, Value:
 string);
 var s:Ansistring;
 begin
 s:=Utf8Encode(Value);
  if not WritePrivateProfileStringA(PAnsiChar(AnsiString((Section))),
 PAnsiChar(AnsiString((Ident))),
   PAnsiChar(s),
 PAnsiChar(AnsiString((FileName then
raise EIniFileException.CreateResFmt(@SIniFileWriteError,
 [FileName]); end;

Yes, that should work and is the bare minimum required for use
in the demos. Though it cannot handle a possible BOM.

--
Arno Garrels
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread RTT

 Yes, that should work and is the bare minimum required for use
 in the demos. Though it cannot handle a possible BOM.
   
With a little trick and we just need to rewrite the create constructor.

unit UnicodeIniFile;

interface
type
  TMyUnicodeIniFile = class(TIniFile)
  public
constructor Create(const aFileName: string);
  end;

implementation
uses Classes, sysutils, Inifiles;

constructor TMyUnicodeIniFile.Create(const aFileName: string);
var f: TfileStream;
const UnicodeBOM: word = $FEFF;
begin
  if not fileexists(aFileName) then
  try
f.Write(UnicodeBOM, 2);
  finally
f.Free;
  end;
  inherited;
end;

end.

The D2009 TInitFile already uses the WritePrivateProfileStringW and 
GetPrivateProfileStringW versions of the functions, but only work if the 
ini file already exists and is Unicode. This code does the trick adding 
the Unicode BOM if files does not exist already.


-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread Arno Garrels
RTT wrote:
 Yes, that should work and is the bare minimum required for use
 in the demos. Though it cannot handle a possible BOM.
 
 With a little trick and we just need to rewrite the create
 constructor. 
 
 unit UnicodeIniFile;
 
 interface
 type
  TMyUnicodeIniFile = class(TIniFile)
  public
constructor Create(const aFileName: string);
  end;
 
 implementation
 uses Classes, sysutils, Inifiles;
 
 constructor TMyUnicodeIniFile.Create(const aFileName: string);
 var f: TfileStream;
 const UnicodeBOM: word = $FEFF;
 begin
  if not fileexists(aFileName) then
  try
f.Write(UnicodeBOM, 2);
  finally
f.Free;
  end;
  inherited;
 end;
 
 end.
 
 The D2009 TInitFile already uses the WritePrivateProfileStringW and
 GetPrivateProfileStringW versions of the functions, but only work if
 the ini file already exists and is Unicode. 

Surprise! That works indeed with UTF-16, however if there is a UTF-8 
BOM and data I cannot read a string, have you tested with UTF-8 as well?

--
Arno


-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread RTT

 The D2009 TInitFile already uses the WritePrivateProfileStringW and
 GetPrivateProfileStringW versions of the functions, but only work if
 the ini file already exists and is Unicode. 
 

 Surprise! That works indeed with UTF-16, however if there is a UTF-8 
 BOM and data I cannot read a string, have you tested with UTF-8 as well?
   
I tested right now and unfortunately don't work, but we can easily 
extend the class to support UTF8 BOM marked files.

interface
uses Inifiles;
type
  TMyUnicodeIniFile = class(TIniFile)
  private
fIsUTF8: Boolean;
  public
function ReadString(const Section, Ident, Default: string): string; 
override;
procedure WriteString(const Section, Ident, Value: string); override;
constructor Create(const aFileName: string);
  end;

implementation
uses Windows, Classes, sysutils, RTLConsts;

constructor TMyUnicodeIniFile.Create(const aFileName: string);
var f: TfileStream;
  i: integer;
const UnicodeBOM: word = $FEFF;
  UTF8BOM: Integer = $00BFBBEF;
begin
  if not fileexists(aFileName) then
  begin
f := Tfilestream.Create(aFilename, fmCreate);
try
  f.Write(UnicodeBOM, 2);
finally
  f.Free;
end
  end else
  begin
f := Tfilestream.Create(aFilename, fmOpenRead);
try
  i := 0;
  f.read(i, sizeof(i));
  fIsUTF8 := i and $00FF = UTF8BOM;
finally
  f.Free;
end;
  end;
  inherited;
end;

function TMyUnicodeIniFile.ReadString(const Section, Ident, Default: 
string): string;
begin
  result := inherited;
  if fIsUTF8 then
result := utf8decode(result)
end;

procedure TMyUnicodeIniFile.WriteString(const Section, Ident, Value: 
string);
var s: Ansistring;
begin
  if fIsUTF8 then
  begin
s := Utf8Encode(Value);
if not WritePrivateProfileStringA(PAnsiChar(AnsiString((Section))), 
PAnsiChar(AnsiString((Ident))),
  PAnsiChar(s), PAnsiChar(AnsiString((FileName then
  raise EIniFileException.CreateResFmt(@SIniFileWriteError, [FileName]);
  end else
inherited;
end;
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread Arno Garrels
RTT wrote:
 The D2009 TInitFile already uses the WritePrivateProfileStringW and
 GetPrivateProfileStringW versions of the functions, but only work if
 the ini file already exists and is Unicode.
 
 
 Surprise! That works indeed with UTF-16, however if there is a UTF-8
 BOM and data I cannot read a string, have you tested with UTF-8 as
 well? 
 
 I tested right now and unfortunately don't work, but we can easily
 extend the class to support UTF8 BOM marked files.
 
 
 function TMyUnicodeIniFile.ReadString(const Section, Ident, Default:
 string): string;
 begin
  result := inherited;
  if fIsUTF8 then
result := utf8decode(result)
 end;

As I wrote in my previous message, this does not work for me.
Whenever Windows finds an UTF-8 BOM it does not return a single byte.

--
Arno

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread RTT

 As I wrote in my previous message, this does not work for me.
 Whenever Windows finds an UTF-8 BOM it does not return a single byte.
   
Sorry, I have not understood.
For me, the previous code works fine in Vista and XP.
I change the encode of the ini file using Notepad from UTF16 to UTF8, 
and vice-versa, and class can read/write both encodings without problems.
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread Arno Garrels
Arno Garrels wrote:
 function TMyUnicodeIniFile.ReadString(const Section, Ident, Default:
 string): string;
 begin
  result := inherited;
  if fIsUTF8 then
result := utf8decode(result)
 end;
 
 As I wrote in my previous message, this does not work for me.
 Whenever Windows finds an UTF-8 BOM it does not return a single byte.

My fault, now it works. But it won't be the same level of backwards
compatibility that can be achieved with a class derived from TCustomIniFile.

--
Arno

 
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread RTT


 My fault, now it works. But it won't be the same level of backwards
 compatibility that can be achieved with a class derived from TCustomIniFile.
   
Can you, please, point one possible problem?
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread Francois PIETTE
 It is also important that the INI file generated by D2009 compiled
 application remains compatible with the same application compiled with 
 older
 Delphi as long as it is possible (This is not possible if a value stored 
 in
 the INI file cannot be represented properly as an ansi character. See a
 message I wrote ealier).

 This compatibility will avoid a lot of headache to all ICS developpers 
 when
 testing ICS accross many compilers. It would be a nuisance to fix the 
 INI
 file each time you switch from D2009 to a previous version. It is already
 enough a nuisance to have the dproj file broken by D2009 !

 Obviously, if the ini file contain a string that can not be codepage
 mapped, will show up with ??? in old Delphi compiled demos, but this is
 a consequence of non Unicode applications.


That is acceptable IMO.
This is the best compatibility level we can achieve.

--
[EMAIL PROTECTED]
The author of the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread RTT

 Old Delphi saved ini files will open correctly in D2009 compiled demo too.
 

 It is also important that the INI file generated by D2009 compiled 
 application remains compatible with the same application compiled with older 
 Delphi as long as it is possible (This is not possible if a value stored in 
 the INI file cannot be represented properly as an ansi character. See a 
 message I wrote ealier).

 This compatibility will avoid a lot of headache to all ICS developpers when 
 testing ICS accross many compilers. It would be a nuisance to fix the INI 
 file each time you switch from D2009 to a previous version. It is already 
 enough a nuisance to have the dproj file broken by D2009 !
   
This code already provide old Delphi versions compiled demos the 
possibility to read BOM marked files created by D2009 ICS demos.
To make it completely compatible there is only the need to not BOM mark 
new files when using non Unicode Delphi

constructor TMyUnicodeIniFile.Create(const aFileName: string);
var f: TfileStream;
i:integer;
const UnicodeBOM: word = $FEFF;
  UTF8BOM:Integer=$00BFBBEF;
begin
 {$IFDEF UNICODE}
 if not fileexists(aFileName) then
 begin
 f:=Tfilestream.Create(aFilename,fmCreate);
 try
   f.Write(UnicodeBOM, 2);
 finally
   f.Free;
 end
 end else
{$ENDIF}
 begin
 f:=Tfilestream.Create(aFilename,fmOpenRead);
 try
   i:=0;
   f.read(i, sizeof(i));
   fIsUTF8:=i and $00ff=UTF8BOM;
 finally
   f.Free;
 end;
 end;
 inherited;
end;

Obviously, if the ini file contain a string that can not be codepage 
mapped, will show up with ??? in old Delphi compiled demos, but this is 
a consequence of non Unicode applications.


-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread Francois PIETTE
 Old Delphi saved ini files will open correctly in D2009 compiled demo too.

It is also important that the INI file generated by D2009 compiled 
application remains compatible with the same application compiled with older 
Delphi as long as it is possible (This is not possible if a value stored in 
the INI file cannot be represented properly as an ansi character. See a 
message I wrote ealier).

This compatibility will avoid a lot of headache to all ICS developpers when 
testing ICS accross many compilers. It would be a nuisance to fix the INI 
file each time you switch from D2009 to a previous version. It is already 
enough a nuisance to have the dproj file broken by D2009 !

--
[EMAIL PROTECTED]
The author of the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread Arno Garrels
RTT wrote:
 My fault, now it works. But it won't be the same level of backwards
 compatibility that can be achieved with a class derived from
 TCustomIniFile. 
 
 Can you, please, point one possible problem?

If you derive something like the TMemInFile that writes all data in
one call to a method UpdateFile it's possible to add a BOM or not 
depending on the data itself. You can read a BOM-marked UTF-8 INI +
file, change it and write it back as Ansi or plain Ascii without BOM
if encoding is no longer required.

--
Arno

  
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] Proposal to replace TIniFile byTRegIniFile in all v7 demo applications

2008-11-09 Thread RTT

 If you derive something like the TMemInFile that writes all data in
 one call to a method UpdateFile it's possible to add a BOM or not 
 depending on the data itself. You can read a BOM-marked UTF-8 INI +
 file, change it and write it back as Ansi or plain Ascii without BOM
 if encoding is no longer required.
   
For the simple use in the ICS demos this simple class is sufficient and 
backward compatibility with early Delphi  versions is simple as adding 
{$IFDEF UNICODE} to the override functions.

 TMyUnicodeIniFile = class(TIniFile)
 private
 fIsUTF8:Boolean;
 public
 {$IFDEF UNICODE}
   function ReadString(const Section, Ident, Default: string): string; 
override;
   procedure WriteString(const Section, Ident, Value: String); override;
   constructor Create(const aFileName: string);
 {$ENDIF}
 end;

In old Delphi versions use regular ini files, in D2009 check for the BOM 
presence, and when creating new ini file, add the BOM.
Old Delphi saved ini files will open correctly in D2009 compiled demo too.

In what situation you need to convert these files from code?
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be