Searching an INI file without knowing section names

Sadge

Sexy Shoeless God of War
Joined
Nov 2, 2007
Messages
567
Reaction score
0
Points
16
Location
Washington, DC area
I've got a plugin that writes to an ini file some code that's similar to this:
Code:
[Day&Time]
1=Name1
2=Name2
3=Name3
[Another Day&Time]
1=Name4
2=Name5
...

Is it possible to "search" that INI file without knowing what the section headers are?

I know GetPrivateProfileString would allow me to "read" from a particular section (if I knew what section it was), but is there a way to reverse it? In other words, could I search the INI file for "Name5" and have it return the "Another Day&Time" section header?

Anyone know if that's possible?
 
Probably the easiest way if you have the plugin source is to add into a known section like [Headings] the specific headings you are writing out. I think you can then loop through all of the entries in there, if they are like heading1=xxxx, heading2=xxxx, etc. Other than that, yeah, you will have to go for direct file access, as Vladus said.
 
No clue how to do it with a plugin, but here's how you can do it with a macro..

Code:
Sub Main(string Search)
    /declare INIFile    string  local   test.ini

    /declare Sections   string  local
    /declare Keys       string  local
    /declare s          int     local
    /declare k          int     local

    /if (!${Defined[Search]}) {
        /echo Syntax: /macro ${Macro.Name} <"Search String">
        /endmacro
    }


    /varset Sections ${Ini[${INIFile}]}
    :TrimSections
        /if (${Sections.Right[1].Equal[|]}) /varset Sections ${Sections.Left[${Math.Calc[${Sections.Length}-1]}]}
    /if (${Sections.Right[1].Equal[|]}) /goto :TrimSections

    /for s 1 to ${Math.Calc[${Sections.Count[|]}+1]}
        /varset Keys ${Ini[${INIFile},${Sections.Arg[${s},|]}]}
        :TrimKeys
            /if (${Keys.Right[1].Equal[|]}) /varset Keys ${Keys.Left[${Math.Calc[${Keys.Length}-1]}]}
        /if (${Keys.Right[1].Equal[|]}) /goto :TrimKeys
        /for k 1 to ${Math.Calc[${Keys.Count[|]}+1]}
            /if (${Ini[${INIFile},${Sections.Arg[${s},|]},${Keys.Arg[${k},|]}].Find[${Search}]}) {
                /echo ${Keys.Arg[${k},|]}=${Ini[${INIFile},${Sections.Arg[${s},|]},${Keys.Arg[${k},|]}]}
                /echo Found in section: ${Sections.Arg[${s},|]}
            }
        /next k
    /next s
/return
 
You can read in the entire ini file, then work with it (search, parse, whatever). This will read in all the sections and print them, then you can look for keys under the section names.

Example:
Code:
CHAR szTmp[MAX_STRING] = {0};
[SIZE=2]CHAR* szSection = 0;
[SIZE=2]CHAR FileName[MAX_STRING] = {0}; 
[/SIZE]int p;
 
[SIZE=2]sprintf(FileName, [/SIZE][SIZE=2][COLOR=#800000]"%s\\MyIniFile.ini"[/COLOR][/SIZE][SIZE=2], gszINIPath[/SIZE][SIZE=2]); 
[/SIZE]GetPrivateProfileString(NULL,NULL,NULL,szTmp,MAX_STRING,FileName);[/SIZE]
[SIZE=2]szSection = szTmp;
[/SIZE][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2] (p=0; p==0 || (szTmp[p-1]!=0 || szTmp[p]!=0) ; p++) {
[/SIZE][SIZE=2][COLOR=#0000ff]  if[/COLOR][/SIZE][SIZE=2] ( szTmp[p]==0 ) {
[/SIZE][SIZE=2]    Writechatf("Section name: %s", szSection);
    szSection = &szTmp[p+1];
  }
}
[/SIZE]

That should work.

htw
 
Last edited:
Great guys thanks for the replies. I may not be able to look at the code again until next week, but everyone's resposes helped.

Thank a lot guys!