?
Solved

read inifile and take a action

Posted on 2009-04-21
7
Medium Priority
?
208 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:2meathome
  • 4
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
twocandles earned 300 total points
ID: 24201982
TIniFile has all the functionality for parsing an Ini File, you don't have to do it. Look at the code below to see if it's ok for you.

procedure TForm1.Button1Click(Sender: TObject);
var
  I            : Integer;
  FS        : string;
  AppIni  : TIniFile;
  fileName : string;
  sections : TStrings;
begin
  fileName := 'c:\software\test.ini';
  // Check if inifile exists
  if( not FileExists( fileName ) )then
    exit;
  // Create inifile
  AppIni := TIniFile.Create( fileName );
  // Browse sections inside inifile
  sections := TStringList.Create;
  try
    // Read all sections at once
    AppIni.ReadSections( sections );
    // Browse sections
    for i := 0 to sections.Count - 1 do
    begin
      // Process "part" section. StartsText belongs to StrUtils
      if( StartsText( 'part', sections[i] ) )then
      begin
        AppIni.ReadSectionValues( sections[i], Memo1.Lines );
      end
      // Process "detail" section
      else if( StartsText( 'detail', sections[i] ) )then
      begin
        AppIni.ReadSectionValues( sections[i], Memo1.Lines );
      end;
    end;
  finally
    FreeAndNil( sections );
    FreeAndNil( AppIni );
  end;
end;

Open in new window

0
 

Author Comment

by:2meathome
ID: 24202020
Hi twocandles,

Is it correct that StartsText (StrUtils) is not available in Delphi 7 ?
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24202037
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' )
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Closing Comment

by:2meathome
ID: 31573133
Thanks for teaching me the right way.
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24203799
Why a B? :( I think I gave a very good answer...
0
 

Author Comment

by:2meathome
ID: 24213684
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.
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24214037
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question