Link to home
Start Free TrialLog in
Avatar of 2meathome
2meathomeFlag for Netherlands

asked on

read inifile and take a action

Need some help to beautify this code. I think there is a beter and cleaner way to read and process the infile.

Infile has the following data :

[region]
x   =10
y   =10
xa = 10
ya = 10

[part1]
x=10
y=10
xa=10
ya=10

[part2]
x=10
y=10
xa=10
ya=10

[detail1]
x=10
y=10

[detail2]
x=10
y=10

and so on...

I need to read the part and detail sections but dont know how many times they occure. Could be part1 to part10 for example with detail1 to detail10. The only thing is they always start with part or detail.

Respect for all.


procedure TForm1.Button1Click(Sender: TObject);
var
  I            : Integer;
  FS        : string;
  AppIni  : TIniFile;
begin
  if OpenDialog1.Execute then
  begin
    ScriptFile.LoadFromFile(OpenDialog1.FileName);
    for I := 0 to ScriptFile.Count - 1 do
    begin
      if (Pos('[', ScriptFile[I]) = 1) and (Pos(']', ScriptFile[I]) = Length(ScriptFile[I]))  then
      begin
        FS := ScriptFile[I];
        Delete(FS, 1, 1);                                    //delete first bracket
        Delete(FS, Length(ScriptFile[I]) - 1, 1);  //delete last bracket
        if pos('region', lowercase(ScriptFile[I])) > 0 then
        begin
          try
            AppIni := TIniFile.Create(('c:\software\test.ini');
            // do something here with the data
 
            AppIni.ReadSectionValues(FS, Memo1.Lines);
            showmessage('found : ' + FS);
 
            //
          finally
            AppIni.Free;
          end;
        end;
        if pos('part', lowercase(ScriptFile[I])) > 0 then
        begin
          try
            AppIni := TIniFile.Create('c:\software\test.ini');
           // do something here with the data
 
            AppIni.ReadSectionValues(FS, Memo1.Lines);
            showmessage('found : ' + FS);
 
            //
          finally
            AppIni.Free;
          end;
        end;
        if pos('detail', lowercase(ScriptFile[I])) > 0 then
        begin
          try
            AppIni := TIniFile.Create('c:\software\test.ini');
           // do something here with the data
 
            AppIni.ReadSectionValues(FS, Memo1.Lines);
            showmessage('found : ' + FS);
 
            //          
          finally
            AppIni.Free;
          end;
        end;
      end;
    end;
  end;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of twocandles
twocandles

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 2meathome

ASKER

Hi twocandles,

Is it correct that StartsText (StrUtils) is not available in Delphi 7 ?
Avatar of twocandles
twocandles

It's possible. It's been some time since I switched to Delphi 2007...Then you can try the "old way" (watch out, it's case sensitive):

if( Copy( sections[i]. 1, lenght( 'part' ) ) = 'part' )
Thanks for teaching me the right way.
Why a B? :( I think I gave a very good answer...
Answer was perfect. Sorry for the rating. I thought it was easy for you to solve and not a complex problem. That's why. Youre help was excellent. Thank you very much.
Indeed it was not a difficult problem, but you already assigned few points to it. I wrote that piece of code checking it was right instead of writing a couple of lines explaining how to do it.

Anyway, glad to help! :)

btw, I've been working with Delphi for some years now and I'm still finding out functions that are there but never heard of. If you have the chance to upgrade to Delphi 2007 (or 2009, the current version) you'll find a lot of functions and language features for making life easier. Not to mention the great improvement of the IDE itself.