File:  [mozdev] / mozptch / preffile.pas
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Wed Mar 22 11:29:12 2006 UTC (13 years, 7 months ago) by oertel
Branches: MAIN
CVS tags: HEAD
bug fix: patching the pref.js new lines where inserted before the last line instead to be appended

(* Copyright (c) uib umwelt informatik buero gmbh (www.uib.de)
 This sourcecode is owned by uib
 and published under the Terms of the General Public License.
#*###CVS_HEAD_START###******************************************************
# $Id: preffile.pas,v 1.4 2006/03/22 11:29:12 oertel Exp $
#
#*###EXCLUDE_CVS_LOG###*****************************************************
# History:
# ========
#
# $Log: preffile.pas,v $
# Revision 1.4  2006/03/22 11:29:12  oertel
# bug fix: patching the pref.js new lines where inserted before the last line instead to be appended
#
# Revision 1.3  2004/05/30 19:51:15  oertel
# -Linux support ready for delivery (beta)
# -New Variables for EmailAddress, ncoming and outgoing server
# -INI-Files debuged
# - new bin zip file
#
# Revision 1.1  2004/03/14 12:49:07  oertel
# Initial commit version 9.29
#
*)
unit preffile;

interface

uses
{$IFDEF MSWINDOWS}
WinTypes, WinProcs,
{$ENDIF}
SysUtils, Classes;

type
  TPrefFile = class(TStringList)
  private
    PrefFileName: string;
    FSuccess : boolean;
    function ExtractKeyString (CompleteString: string) : string;
  public
    problem: string;
    property success : boolean read FSuccess write FSuccess;
    constructor Create(const FileName: string);
    procedure UpdateFile;
    procedure AddPrefLine(ChangeLine: string);
end;


implementation

uses
datamodul;

function TPrefFile.ExtractKeyString (CompleteString: string) : string;
var
   i : integer;
   TempString: string;
begin
  i := 1;
  result := '';
  TempString := CompleteString;
  if TempString = '' then exit;
  while (TempString[i] <> '"') and (i <> Length(tempString)) do Inc(i);
  if i = Length(tempString)then exit;
  Inc(i);
  while tempstring[i] <> '"' do Inc(i);
  System.Delete (tempString, i, Length(tempString));
  result := tempString;
end;



constructor TPrefFile.Create(const FileName: string);
begin
  inherited create;
  problem := '';
  success := true;
  PrefFileName := FileName;
  try
     LoadFromFile(FileName);
  except
     problem := 'Could not load file ' + PrefFileName;
     datamodul.writelog('Could not load file ' + PrefFileName);
     success := false;
  end;
end;



procedure TPrefFile.AddPrefLine(ChangeLine : string);
var
   LineInFile : Boolean;
   PrefKey : string;
   ChangeKey : string;
   i : integer;
 begin
   LineInFile := False;
   ChangeKey := ExtractKeyString(ChangeLine);
   i := 0;
   while ((LineInFile = False) and (i <> Count)) do
   begin
       PrefKey := ExtractKeyString(Strings[i]);
       if LowerCase(PrefKey) = LowerCase(ChangeKey) then
           begin
               LineInFile := True;
               Strings[i] := ChangeLine;
               datamodul.writelog('Found and changed: '+ChangeLine);
           end;
   inc(i);
   end;
   if not LineInFile then
      begin
         Append(ChangeLine);
         datamodul.writelog('Appended: '+ChangeLine);
      end;
 end;



procedure TPrefFile.UpdateFile;
begin
     //Sort;
     // del leading spaces - detlef...
     while Strings[0] = '' do delete(0);
     try
          SaveToFile(PrefFileName);
          datamodul.writelog('saved: '+PrefFileName);
     except
          problem := 'New File ' + PrefFileName + 'could not be saved';
          datamodul.writelog('New File ' + PrefFileName + 'could not be saved');
          success := false;
     end;
 end;

end.








FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>