ArrayFromINIfile

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search

A User Function for use with Oblivion Script Extender

Syntax:

(Array :array) ArrayFromINIfile Filename :string

Reads a Pluggy INI file into an array.

The arguments are:

  • Filename - a string with the name of the file


Notes[edit | edit source]

  • The INI file must be a standard INI format with section names within brackets followed by lines formatted as "Key=Value"
  • The returned array will be a StringMap type array with the Section names as the first level keys.
  • Pluggy INI files are read from "C:\Documents and Settings\C\My Documents\My Games\Oblivion\Pluggy\User Files"


Usage[edit | edit source]

Let MyArray := Call ArrayFromINIfile "MyFilename.ini"

The INI file like this:

[Section51]
EntryA_Attr1='A' 
EntryA_Attr2_0_Value1=11
EntryA_Attr2_0_Value2=22
EntryA_Attr2_1_Value1=1111
EntryA_Attr2_1_Value2=2222
EntryB_Attr1='B' 
[Section52]
. . . etc 

Will return an array like this:

["Section51"] 
= ["EntryA"]
= = ["Attr1"] = 'A' 
= = ["Attr2"]
= = = [0]
= = = = ["Value1"] = 11
= = = = ["Value2"] = 22
= = = [1]
= = = = ["Value1"] = 1111
= = = = ["Value2"] = 2222
= ["EntryB"]
= = ["Attr1"] = "A" 
["Section52"] 
. . . etc


Code[edit | edit source]

scn zzzArrayFromINIfile

;---------------------------------------
;    Read an INI file into an array
;---------------------------------------
array_var arr
array_var asSections
array_var asWork
long L
long len
long p1
long p1a
long p2
short ix
string_var s
string_var sFile 

long psFile             ;===Pluggy===
long psSep            ;===Pluggy===
long psText            ;===Pluggy===
long psFileText       ;===Pluggy===

begin Function {sFile}

   let asSections := ar_construct stringmap
   let psFile    := createstring -1 0 1 1
   let psSep:= createstring -1 "|" 1 1
   let psText := createstring -1 0 1 1
   let psFileText    := createstring -1 0 1 1

   ;=== Read file ===
   setString psFile  $sFile    
   FileToString psFile psFileText  
   let len := StringLen psFileText   
   StringRep -1310 psSep psFileText 1 len 0 1   ; Replace LFCR with "|"

   setString psSep "["
   let p1 := 1
   set p1 to  StringPos psSep psFileText  
   if p1 > 0
       let p1 := p1 +1
       let p2 := p1 

       while p2 < len 
           ;=== Find Section boundaries ===
           set p2 to  StringPos psSep psFileText  p1
           if p2 == 0
               let p2 := len + 1
           endif
           let L := p2 -p1

           ;=== Copy to OBSE ===
           CopyString psText psFileText  p1 L
           set s to toobse psText

           ;=== Split by section ===
           let asWork := ar_construct stringmap
           let arr := sv_split s "]"
           let asWork[zsKey] := arr[0]
           let asWork[zsSection] := s := arr[1]

           ;=== Split by line ===
           let arr := sv_split s "|"
           let asWork[zsLines] := arr

           let asWork[zasParms] := ar_construct stringmap
           let ix := -1 
           while ( ix += 1 ) <  ar_size asWork[zsLines]        
               let arr := sv_split asWork[zsLines][ix] "="
               if eval (ar_size arr) != 2
                   Message "ERROR"
               else
                   let asWork[zasParms][arr[0]] := arr[1]
               endif
           loop

           let asSections[asWork[zsKey]] := call zzzArrayDeserialize asWork[zasParms]

           let p1 := p2 + 1
       loop
   endif

   SetFunctionValue asSections 

   DestroyString psFile             ;===Pluggy===
   DestroyString psSep            ;===Pluggy===
   DestroyString psText        ;===Pluggy===
   DestroyString psFileText       ;===Pluggy===
   sv_destruct s sFile
end

See Also[edit | edit source]