1. Useful code snippet for finding parameters in a file
- Posted by AlanO Sep 09, 2008
- 869 views
My code often reads in a file containing a few "parm=value" statements. Here is my solution for this, its deliberately compatible with old Euphoria versions. Just read the file into data and call this function.
----------------------------------------------------------------- function resolve_parameter(sequence cfgverb, sequence data) integer i1, i2, i3 sequence s1, s2, s3 object o1 s3 = "not found" i1 = 0 i2 = length(data) -- how many records i3 = length(cfgverb) while i1 < i2 do i1 = i1 + 1 o1 = data[i1] if sequence(o1) then s1 = o1 if length(s1) > (i3 -1) then s2 = s1[1..i3] if compare(s2,cfgverb) = 0 then s2 = s1[i3+1..length(s1)] s2 = drop_cr(s2) s3 = s2 exit end if end if end if end while return s3 end function ----------------------------------------------------------------- function drop_cr(sequence data) integer i1 i1 = length(data) if compare(data[i1..i1],"\n") = 0 then data = data[1..i1-1] end if return(data) end function ------------------------------------------------------------------- -- calling it s1 = resolve_parameter("Logfile DSN=",licence_text) if compare(s1,"not found") = 0 then logfile_dsn = "SAMPLE.LOG.FILE" doPCLOG("Logfile DSN not specified, using default SAMPLE.LOG.FILE") else logfile_dsn = s1 end if