Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   ini file , readstring UTF vs. ASCII (https://www.delphipraxis.net/208415-ini-file-readstring-utf-vs-ascii.html)

bernhard_LA 26. Jul 2021 22:41

ini file , readstring UTF vs. ASCII
 
bin mit dem Code unten gerade ziemlich aufgelaufen,
auf einem PC und einer ini Datei im UTF-8 Format lief der code ins leere,
erst nachdem ich das Format der DAtei auf ASCII umgestellt habe gings,
ist diese Problem bekannt ???




Delphi-Quellcode:

var
  IniFile: TIniFile;
begin

  if FileExists(filename) then
  begin
    IniFile := TIniFile.Create(filename);
    try
      Result := IniFile.ReadString(SectionStr, IdentStr, '<none>');
    finally
      IniFile.Free;
    end;
  end
  else
  begin
    Result := '<no file found>';
  end;

Dalai 26. Jul 2021 22:51

AW: ini file , readstring UTF vs. ASCII
 
Hat(te) die Datei vielleicht einen BOM (Byte Order Mark)? Wenn ja, ist das wahrscheinlich keine offiziell unterstützte Geschichte, denn die INI-Funktionen von Windows können nur ANSI und UTF-16.

Grüße
Dalai

venice2 26. Jul 2021 22:53

AW: ini file , readstring UTF vs. ASCII
 
Ist doch kein Fehler oder?
Ini Files unterstützt kein Unicode.

Dir wird also nichts anderes übrigbleiben zu prüfen in welchem Format die INI geschrieben wurde.
Und dann entsprechend Utf8ToString verwenden.

Oder Versuchs mal mit TMemIniFile

bernhard_LA 26. Jul 2021 23:03

AW: ini file , readstring UTF vs. ASCII
 
geschrieben wurde mit einer anderen Delphi Anwendung ,
auch wieder alles mit der Klasse Inifile ......
auf einem anderen PC hat die Anwendung ohne Probleme funktioniert :-)

meine aktuelle Zwischenlösung :
Datei mit Notepad öffnen und dann als ASCII Format abspeichern, dann scheint alles zu passen

venice2 26. Jul 2021 23:15

AW: ini file , readstring UTF vs. ASCII
 
Zitat:

geschrieben wurde mit einer anderen Delphi Anwendung ,
auch wieder alles mit der Klasse Inifile ......
Nein wohl eher mit der TMemIniFile denn nur diese unterstützt Utf8

Die Klasse TInifile ist ein Wrapper für die Windows API IniFiles und die kann nun mal nur EASCII (ANSI) und UTF-16.
So wie @Dalai schon sagte.

himitsu 27. Jul 2021 03:04

AW: ini file , readstring UTF vs. ASCII
 
Zitat:

Zitat von venice2 (Beitrag 1492850)
Ini Files unterstützt kein Unicode.

Doch, tut es.

Aber eben nur UTF-16 ohne BOM.

TMemIniFiles von Delphi kann, wie eine TStringList, eben noch viel mehr, was aber die "offizielle" INI-API von Windows eben nicht "kann".


Wie beim XML gibt es hier eine automatische Erkennung durch die #0
quasi '[...' oder '['#0'...' am Anfang.

bernhard_LA 12. Okt 2021 08:48

AW: ini file , readstring UTF vs. ASCII
 
a) wenn ich die Ini files Klasse weiterhin verwenden will, benötige ich einen Check auf BOM , ich habe diesen code hierzu gefunden https://www.delphi-treff.de/tipps-tr...rung-erkennen/
eine extra Zeile Code und eine Exception werfen wenn das BOM Flag gefunden wird ?


b) bei dieser Option , von https://docwiki.embarcadero.com/Code...niFile_(Delphi),
wie übergebe ich den Filenamen an meine Settingsklasse ?
Die Funktion
Delphi-Quellcode:
OpenIniFileInstance()
ist leider nicht erklärt


Delphi-Quellcode:

var
  SettingsFile : TCustomIniFile;
begin
  { Open an instance }
  SettingsFile := OpenIniFileInstance();

  try
    {
    Read all saved values from the last session. The section name
    is the name of the form. Also use the form's properties as defaults.
    }
    Top    := SettingsFile.ReadInteger(Name, 'Top', Top );
    Left   := SettingsFile.ReadInteger(Name, 'Left', Left );
    Width  := SettingsFile.ReadInteger(Name, 'Width', Width );
    Height := SettingsFile.ReadInteger(Name, 'Height', Height );
    Caption := SettingsFile.ReadString (Name, 'Caption', Caption);

    { Load last window state. }
    case SettingsFile.ReadBool(Name, 'InitMax', WindowState = wsMaximized) of
      true : WindowState := wsMaximized;
      false: WindowState := wsNormal;
    end;

  finally
    SettingsFile.Free;
  end;
end;
PS : diesmal wurde die Datei mit dem Notepad++ erstellt :-(

himitsu 12. Okt 2021 09:28

AW: ini file , readstring UTF vs. ASCII
 
Erstmal, INI-Dateiteien der WinAPI gibt es ausschließlich in ANSI oder UTF-16 (ohne BOM).

Dein Problem dürfte sich aber durch die TStringList lösen lassen, also mit RadioGroup1.ItemIndex=2.


Zitat:

PS : diesmal wurde die Datei mit dem Notepad++ erstellt
Das darf man gern machen, aber beim Speichern wählt man dann gefälligst auch das richtige Format.

Zitat:

Check auf BOM
Da gibt es auch was von Ratiof TEncoding.

Zitat:

Die Funktion OpenIniFileInstance() ist leider nicht erklärt
Die ist doch selbsterklärend?

bernhard_LA 12. Okt 2021 12:10

AW: ini file , readstring UTF vs. ASCII
 
nicht ganz, weil meiene naheliegende Lösung hier dann einen Abstract Fehler zurückliefert ....



Delphi-Quellcode:
 
var
  IniFile: TCustomIniFile;
  if FileExists(filename) then
  begin
    IniFile := TCustomIniFile.Create(filename);
    try
      Result := IniFile.ReadString(SectionStr, IdentStr, '<none>');
    finally
      IniFile.Free;
    end;
  end
  else
  begin
    Result := '<no file found>';
  end;

Klaus01 12. Okt 2021 12:58

AW: ini file , readstring UTF vs. ASCII
 
.. aus der Hilfe..
Zitat:

Note: Use TCustomIniFile as a base class when defining components that access ini files. Ini files are text files that are divided into sections with variable assignments in each section. TCustomIniFile introduces properties and methods to handle storage and retrieval of application-specific information and settings in a standard ini file.

..

Do not create instances of TCustomIniFile. Instead, use or create descendants of TCustomIniFile, such as TIniFile, TRegistryIniFile, and TMemIniFile
Grüße
Klaus


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:28 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz