Convert .ini to map

Started by stanl, April 22, 2021, 01:04:41 PM

Previous topic - Next topic

stanl

I was thinking of a UDF to that would create a WB map from an .ini file and output references to each element. This would act as a stand-alone to prepare existing scripts that use .ini files to be replaced with maps for lookups.  I would think some sort of regex would be needed. Has anyone thought about or pursued this?

td

Assuming I understand what you want to do, why not use IniItemizePvt and IniReadPvt to load the contents of an .ini file into a map? You can use IniItemizePvt to both get a list of section names and key names for each section.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

Something like this?

Code (winbatch) Select


ini  = DirScript():"your.ini"
section = "YOUR_SECTION"
keys = ""
default = ""

keys = IniItemizePvt(section, ini)
keys = StrReplace(keys,@TAB,',""':@TAB):',""'
imap = MapCreate(keys)
Foreach i in imap
  imap[i] = IniReadPvt(section, i, default, ini)
  Message(i,imap[i])
Next



stanl

Quote from: td on April 22, 2021, 01:13:37 PM
Assuming I understand what you want to do, why not use IniItemizePvt and IniReadPvt to load the contents of an .ini file into a map? You can use IniItemizePvt to both get a list of section names and key names for each section.


I was thinking more of FileGet([file]) and a straight map create.


[EDIT]: But thanks, Tony, Jim.  Makes sense.

stanl

a basic WIP: the attached .ini is an example of the kind of .ini have have used for complex scripts for data transformations from SQL Server/Access/MySQL into Excel .xlsb workbooks with pivots and slicers and all that jazz. Looking at replacing multiple dips into the .ini with one map/hash lookup. Pretty sure that is more for a single section .ini, but using Tony and Jim's suggestions, gave it a shot.... [use the attached .ini if you have spare time to test - just change the extension from .txt to .ini]
Code (WINBATCH) Select


;Winbatch 2020b convert .ini to map
;====================================================================================
IntControl(73,1,0,0,0)
gosub udfs
types="Ini Files|*.ini|"
cINI=AskFilename("Select INI File",Dirscript(), types, "", 101)
cTXT = Dirscript():FileRoot(cINI):".txt"
Lkup = Dirscript():FileRoot(cINI):"_lkup.wbt"
If FileExist(cTXT) Then FileDelete(cTXT)
clkup = ""
sections = IniItemizePvt("", cINI)
n = ItemCount(sections,@TAB)
map = MapCreate()


For i = 1 To n
   section = ItemExtract(i,sections,@TAB)
   keys = ""
   default = ""
   keys = IniItemizePvt(section, cINI)
   n1 = ItemCount(keys,@TAB)
   For k = 1 To n1
     subsection = ItemExtract(k,keys,@TAB)
     newmap = section:"_":subsection
     entry = "c":newmap:"=map['":newmap:"']"
     clkup=clkup:entry:@LF
     map[newmap] = IniReadPvt(section,subsection,default,cINI)
   Next
Next
MapFilePutCSV(cTXT, map, "=",@true,0)
If FileExist(cTXT)
   cData = Fileget(cTXT)
   cData = StrReplace(cData,'"','')
   FilePut(cTXT,cData)
   Message("Lookup",clkup)
   map = MapFileGetCsv(cTXT,"=")
   first = 'map = MapFileGetCsv("%cTXT%","=")':@LF
   nitem = Random(ItemCount(clkup,@LF))
   item = ItemExtract(nitem,clkup,@LF)
   key = ItemExtract(1,item,"=")
   value = ItemExtract(2,item,"=")
   Message("Test Item Key: ":key,"Key Value: ":%value%)
   ;just for fun
   ;===================================================
   Execute %item%
   Message("Result for ":item,%key%)
   ;===================================================
   ;write result out as script
   clkup = first:clkup
   FilePut(Lkup,clkup)
Endif


exit


;======================================================================================================
:WBERRORHANDLER
Terminate(@TRUE,"Error Encountered",geterror())
;======================================================================================================


:CANCEL
Display(2,"Operation Canceled","Goodbye...")
Exit


:udfs
#DefineSubRoutine geterror()
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errmsg = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Return(errmsg)
#EndSubRoutine


Return



JTaylor

Works for me.   I started to do a multiple section example but figured that was easily accomplished if you had that need and was limited on time at that moment.   

Jim