How about this one? A bit more "functional" with no mutables or side effects...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#light

let (|RegexMatch|_|) regex str =
   let m = System.Text.RegularExpressions.Regex(regex).Match(str)
   if m.Success
   then Some (List.tail [ for x in m.Groups -> x.Value ])
   else None

let loadConfig fn = 
    System.IO.File.ReadAllLines(fn)
    |> Array.fold
        (fun acc l ->
            match l with 
            | RegexMatch "^\[(\S+)\]$" [section] -> (section, []) :: acc            
            | RegexMatch "^(\S+)[\s]*=[\s]*(.*)$" [prop; value] -> (fst acc.Head, (prop, value)::(snd acc.Head))::(acc.Tail)
            | _ -> acc
        )
        List.empty
    |> Map.ofList

 

//Testing

let loadAndPrintConfig =
    loadConfig >>
    Seq.iter (fun (KeyValue(s, pvl)) -> printfn "%s" s; pvl |> Seq.iter (fun (p, v) -> printfn "\t%s=%s" p v))

loadAndPrintConfig "test.ini"

Thanks!
FZSharp

By on 5/18/2011 10:14 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper