<commit_before>{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      This descendant adds ReadOnly support and can read/write Form state
      information.
}

unit fpg_iniutils;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  IniFiles,
  fpg_base,
  fpg_form;

type

  TfpgINIFile = class(TINIFile)
  private
    FReadOnly: Boolean;
  public
    constructor CreateExt(const AFileName: TfpgString = ''; AReadOnly: Boolean = False);
    function    ReadString(const ASection, AIdent, ADefault: string): string; override;
    function    ReadInteger(const ASection, AIdent: string; ADefault: longint): longint; override;
    function    ReadBool(const ASection, AIdent: string; ADefault: Boolean): Boolean; override;
    function    ReadDate(const ASection, AName: string; ADefault: TDateTime): TDateTime; override;
    function    ReadDateTime(const ASection, AName: string; ADefault: TDateTime): TDateTime; override;
    function    ReadFloat(const ASection, AName: string; ADefault: double): double; override;
    function    ReadTime(const ASection, AName: string; ADefault: TDateTime): TDateTime; override;
    procedure   ReadFormState(AForm: TfpgForm; AHeight: integer = -1; AWidth: integer = -1; const ASkipDimensions: Boolean = False);
    procedure   WriteFormState(AForm: TfpgForm);
  end;

// singleton
function gINI(const AFileName: TfpgString = ''): TfpgINIFile;

implementation

uses
  fpg_main,
  fpg_constants,
  fpg_utils;

var
  uINI: TfpgINIFile;


function gINI(const AFileName: TfpgString): TfpgINIFile;
begin
  if uINI = nil then
    uINI := TfpgINIFile.CreateExt(AFileName);
  Result := uINI;
end;

{ TfpgINIFile }

constructor TfpgINIFile.CreateExt(const AFileName: TfpgString; AReadOnly: Boolean);
var
  lDir: TfpgString;
  lFileName: TfpgString;
begin
  FReadOnly := AReadOnly;
  lDir      := fpgExtractFileDir(AFileName);
  lFileName := fpgExtractFileName(AFileName);

  if lDir = '' then
    lDir := GetAppConfigDir(False);
  if not (lDir[Length(lDir)] = PathDelim) then
    lDir := lDir + PathDelim;

  { We used a non-Global config dir, so should be able to create the dir }
  if not fpgForceDirectories(lDir) then
    raise Exception.CreateFmt(rsErrFailedToCreateDir, [lDir]);


  if lFileName = '' then
    lFileName := ApplicationName + '.ini'
  else if fpgExtractFileExt(lFileName) = '' then
    lFileName := lFileName + '.ini';

  lFileName := lDir + lFileName;
  Create(lFileName);
end;

function TfpgINIFile.ReadString(const ASection, AIdent, ADefault: string): string;
begin
  Result := inherited ReadString(ASection, AIdent, ADefault);
  if (not ValueExists(ASection, AIdent)) and
    (not FReadOnly) then
    WriteString(ASection, AIdent, ADefault);<commit_msg>TfpgINIFile called the SysUtils.GetAppConfigDir() instead of fpgGetAppConfigDir()<commit_after>{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      This descendant adds ReadOnly support and can read/write Form state
      information.
}

unit fpg_iniutils;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  IniFiles,
  fpg_base,
  fpg_form;

type

  TfpgINIFile = class(TINIFile)
  private
    FReadOnly: Boolean;
  public
    constructor CreateExt(const AFileName: TfpgString = ''; AReadOnly: Boolean = False);
    function    ReadString(const ASection, AIdent, ADefault: string): string; override;
    function    ReadInteger(const ASection, AIdent: string; ADefault: longint): longint; override;
    function    ReadBool(const ASection, AIdent: string; ADefault: Boolean): Boolean; override;
    function    ReadDate(const ASection, AName: string; ADefault: TDateTime): TDateTime; override;
    function    ReadDateTime(const ASection, AName: string; ADefault: TDateTime): TDateTime; override;
    function    ReadFloat(const ASection, AName: string; ADefault: double): double; override;
    function    ReadTime(const ASection, AName: string; ADefault: TDateTime): TDateTime; override;
    procedure   ReadFormState(AForm: TfpgForm; AHeight: integer = -1; AWidth: integer = -1; const ASkipDimensions: Boolean = False);
    procedure   WriteFormState(AForm: TfpgForm);
  end;

// singleton
function gINI(const AFileName: TfpgString = ''): TfpgINIFile;

implementation

uses
  fpg_main,
  fpg_constants,
  fpg_utils;

var
  uINI: TfpgINIFile;


function gINI(const AFileName: TfpgString): TfpgINIFile;
begin
  if uINI = nil then
    uINI := TfpgINIFile.CreateExt(AFileName);
  Result := uINI;
end;

{ TfpgINIFile }

constructor TfpgINIFile.CreateExt(const AFileName: TfpgString; AReadOnly: Boolean);
var
  lDir: TfpgString;
  lFileName: TfpgString;
begin
  FReadOnly := AReadOnly;
  lDir      := fpgExtractFileDir(AFileName);
  lFileName := fpgExtractFileName(AFileName);

  if lDir = '' then
    lDir := fpgGetAppConfigDir(False);
  if not (lDir[Length(lDir)] = PathDelim) then
    lDir := lDir + PathDelim;

  { We used a non-Global config dir, so should be able to create the dir }
  if not fpgForceDirectories(lDir) then
    raise Exception.CreateFmt(rsErrFailedToCreateDir, [lDir]);


  if lFileName = '' then
    lFileName := ApplicationName + '.ini'
  else if fpgExtractFileExt(lFileName) = '' then
    lFileName := lFileName + '.ini';

  lFileName := lDir + lFileName;
  Create(lFileName);
end;

function TfpgINIFile.ReadString(const ASection, AIdent, ADefault: string): string;
begin
  Result := inherited ReadString(ASection, AIdent, ADefault);
  if (not ValueExists(ASection, AIdent)) and
    (not FReadOnly) then
    WriteString(ASection, AIdent, ADefault);
{'fraction_non_alphanumeric': 0.08794481893713749, 'fraction_numerical': 0.0031352876626430477, 'mean_word_length': 4.246710526315789, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3916476', 'n_tokens_mistral': 2048, 'n_tokens_neox': 2016, 'n_words': 615}