At 10:46 AM -0700 2/11/1999, Leston Drake wrote:

>I have a data file (text) that is formatted in a standard Windows INI format.
>
>[Section 1 Header]
>Item1=<Value>
>Item2=<Value>
>Item3=<Value>
>
>[Section 2 Header]
>Item1=<Value>
>Item2=<Value>
>Item3=<Value>
>
>etc...
>
>I am reading the contents of this file into a container, and I want
>to parse it to pull out the Values. So I need to *search* to a
>section header, then seach (from that point) for the "Item1=" line
>below it (for example), then finally, pull out the value.
>I am looking for some direction for someone about what MC commands,
>functions, etc are best suited for this task.

Hi Leston

Here are a couple of functions I use to do this. The first one pulls 
all data from a section. The second one gets a single key value. As 
the second one calls the first one, you'll need both. I haven't 
looked at them for a while (but I use them all the time), so I can't 
remember why I wrote them as I did. (So much for promises to myself 
to document everything!)

Some notes
Only the section name inside square brackets should be on the section 
title line. No other text,including spaces, is allowed. The square 
brackets shouldn't be passed in the pGroup parameter:

get iniGroup("OPTIONS", myData) -- NOT -- get iniGroup("[OPTIONS]", myData)

lines beginning with ";" are not returned by iniGroup

The local declarations were necessary I think to make "matchText" 
work properly.

The functions return "no_group" and "no_key" respectively if the 
search fails. Be sure to check for that when calling the functions. 
They should probably return empty instead, but I may have had a 
reason at the time.

-------------------------------------
function iniGroup pGroup,pData
   local tMatched, tMatchedx
   put cr before pdata
   put cr after pdata
   if matchText(pData,cr & "\[" & pGroup & "\]" & cr & "(.*)",tMatched) then
     get matchText(tMatched, "(" & cr & "\[)", tMatchedx)
     get offset(tmatchedx, tMatched)
     if it > 0 then
       delete char it to -1 of tMatched
     end if
     put 1 into tCount
     repeat for each line tLine in tMatched
       if char 1 of tLine <> ";" and tLine <> empty then
         put tLine into line tCount of tResult
         add 1 to tCount
       end if
     end repeat
     return tResult
   else
     return "no_group"
   end if
end iniGroup
--------------------------------
function iniKey pKey,pGroup,pData
   local tResult
   if pGroup is not empty then
     put iniGroup(pGroup,pData) into tGroup
   else
     put pData into tGroup
   end if
   if tGroup is not "no_group" then
     put cr before tGroup
     if  matchText(tGroup,cr & pKey & "=" & "([^" & cr & "]*)", tResult) then
       return tResult
     else
       return "no_key"
     end if
   else
     return "no_key"
   end if
end iniKey
-----------------------------------


Cheers
Dave Cragg

Reply via email to