'=========================================================================== ' Subject: INI FUNCTIONS FOR PBCC Date: 02-01-00 (22:16) ' Author: Kispen Code: PBCC ' Origin: kispen@mailpanda.com Packet: PBCC.ABC '=========================================================================== $if 0 Uuhh... has something like this been done already? If it has... sorry... you'll probably want to use the other one ;). This contains functions to work with INI files. Any bugs or anything, e-mail me... the address is at the end of this if/endif block. Here are the functions: *********************************************************************************************** OpenINI(INIFile$) as Integer Opens the INI file... call this first, always returns 0 if it's ok, -1 if it's not. ReadKey$(Section$, Key$) Gets the value of Key$ in Section$ returns the value of Key$ (if found) returns "Section not found" if Section$ isn't found returns "Key not found" if Key$ isn't found WriteKey(Section$, Key$, Value$) as Integer Writes Value$ to Key$ in Section$ returns 0 if it's ok, -1 if Section$ wasn't found WriteSection(Section$) (this is a sub) <-- writes a section. DeleteKey(Section$, Key$) as Integer <-- Figure this one out ;-) returns 0 if it's ok returns -1 if Section$ wasn't found returns -2 if Key$ wasn't found DeleteSection(Section$) as Integer <-- hmm... wonder what this does.... returns 0 if it's ok returns -1 if Section$ wasn't found SaveINI(INIFile$) as Integer <-- hmmmm...... returns 0 if it's ok, -1 if it's not *********************************************************************************************** Hopefully, this can help you out... a little :). I should add code to ignore a line (e.g. not put the line into INIArray() if it's blank, but I want the INI file to be written exactly as it was read... so I didn't. Erm... sorry for the messy code, I just sorta stick things in as they come into my head. Oh, btw, I don't like ARRAY SCAN. I really don't. Oh, second btw... I think this thing is pretty slow. Please, please, modify it... make it faster... It'd be cool to get this thing going fast... Oh, third btw... I think there are API calls to write/read from INI files, but from what I saw they only wrote/read to win.ini or the registry... Then again, I could be wrong. I only looked at it for a couple of minutes. Oh yea, final warning: Don't use INIArray(), INISectionIndex(), or INISectionIndexMax in your program. If you do, you could mess this up. Blah... Kispen (kispen@mailpanda.com) $endif global INIArray() as string global INISectionIndex() as integer global INISectionIndexMax as long function OpenINI(INIFile$) as integer $if 0 OK, here's how this little baby works... 1) Check to see if the file exists (if not, return -1) 2) Open the file 3) Put each line of the file into INIArray() 4) If the line begins with [ and ends with ] then it's a section, and we should put the number into INISectionIndex()... hence the name 'index' hehehe 5) End the function. $endif local x as integer local NumberFree as integer INILine$ = "" x = 0 NumberFree = 0 ' Check to see if the file exists if dir$(INIFile$) = "" then function = -1: exit function ' Open the INI File NumberFree = Freefile open INIFile$ for input as NumberFree do until eof(NumberFree) x = x + 1 line input #NumberFree, INILine$ ' Check to see if it's a section, and update accordingly if left$(INILine$, 1) = "[" and right$(INILine$, 1) = "]" then INISectionIndexMax = ubound(INISectionIndex) + 1 redim preserve INISectionIndex(0 to INISectionIndexMax) INISectionIndex(INISectionIndexMax) = x end if ' Add another line to INIArray redim preserve INIArray(1 to x) INIArray(x) = INILine$ loop close NumberFree function = 0 end function function ReadKey$(Section$, Key$) $if 0 Here is how this one works: 1) Add [ or ] to Section$ if necessary 2) Use INISectionIndex() to match the section... if section isn't found, say so. 3) Scan INIArray() from the section to the next section (or the end of the array) for Key$ 4) If Key$ isn't found, say so and exit 5) Get the value of Key$ and send it down... then exit. $endif local FoundSection as integer local FoundKey as integer local x as integer local LenKey as integer local LowerScan as integer local UpperScan as integer local Counter as integer local tempKey as string FoundSection = 0 x = 0 LenKey = 0 tempKey = "" ' Add [ or ] if needed IF LEFT$(Section$, 1) <> "[" THEN Section$ = "[" + Section$ IF RIGHT$(Section$, 1) <> "]" THEN Section$ = Section$ + "]" FOR x = 1 TO INISectionIndexMax IF INIArray(INISectionIndex(x)) = Section$ THEN FoundSection = INISectionIndex(x) ' Ok, we have to check to see if there's another section or not. If there is, then ' we should stop scanning at that number. If there isn't, then we should stop scanning ' at the last number of the file array (since, obviously, if there is no next section, ' it's the end of the file. if x = ubound(INISectionIndex) then UpperScan = ubound(INIArray) else UpperScan = INISectionIndex(x + 1) end if EXIT FOR END IF NEXT x if FoundSection = 0 then function = "Section not found": exit function LenKey = LEN(Key$) LowerScan = FoundSection 'I would love to use ARRAY SCAN, but I don't like its limitations... 'ARRAY SCAN INIArray(FoundSection), FROM 1 TO LenKey, = Key$, TO i& FoundKey = 0 for Counter = LowerScan to UpperScan if left$(INIArray(Counter), LenKey) = Key$ then FoundKey = Counter exit for end if next Counter if FoundKey = 0 then function = "Key not found": exit function tempKey = INIArray(FoundKey) tempKey = trim$(right$(tempKey, Len(tempKey) - LenKey)) ' Get rid of the Key string... ' If there is an "=", then get rid of it, and all surrounding spaces if left$(tempKey, 1) = "=" then tempKey = trim$(right$(tempKey, Len(tempKey) - 1)) ' Send the string.. modified or not. function = tempKey end function function WriteKey(Section$, Key$, Value$) as integer $if 0 Hmm... k, flowchart for this one 1) Add [ and ] to Section$ if necessary 2) Find the section 3) ARRAY INSERT a line into it... and set it equal to Key$=Value$ 4) End the function... $endif local FoundSection as integer local x as integer if left$(Section$, 1) <> "[" then Section$ = "[" + Section$ if right$(Section$, 1) <> "]" then Section$ = Section$ + "]" FOR x = 1 TO INISectionIndexMax IF INIArray(INISectionIndex(x)) = Section$ THEN FoundSection = INISectionIndex(x) EXIT FOR END IF NEXT x if FoundSection = 0 then function = -1: exit function redim preserve INIArray(1 to ubound(INIArray)+1) ' Make room for the new key NewKey$ = Key$ + "=" + Value$ array insert INIArray(FoundSection + 1), NewKey$ function = 0 end function sub WriteSection(Section$) if left$(Section$, 1) <> "[" then Section$ = "[" + Section$ if right$(Section$, 1) <> "]" then Section$ = Section$ + "]" redim preserve INIArray(1 to ubound(INIArray)+1) ' Make room for the new section INIArray(ubound(INIArray)) = Section$ end sub function DeleteKey(Section$, Key$) as integer 'This works surprisingly like ReadKey... I wonder why. :P local FoundSection as integer local FoundKey as integer local x as integer local LenKey as integer local LowerScan as integer local UpperScan as integer local Counter as integer local tempKey as string FoundSection = 0 x = 0 LenKey = 0 tempKey = "" ' Add [ or ] if needed IF LEFT$(Section$, 1) <> "[" THEN Section$ = "[" + Section$ IF RIGHT$(Section$, 1) <> "]" THEN Section$ = Section$ + "]" FOR x = 1 TO INISectionIndexMax IF INIArray(INISectionIndex(x)) = Section$ THEN FoundSection = INISectionIndex(x) ' Ok, we have to check to see if there's another section or not. If there is, then ' we should stop scanning at that number. If there isn't, then we should stop scanning ' at the last number of the file array (since, obviously, if there is no next section, ' it's the end of the file. if x = ubound(INISectionIndex) then UpperScan = ubound(INIArray) else UpperScan = INISectionIndex(x + 1) end if EXIT FOR END IF NEXT x if FoundSection = 0 then function = -1: exit function LenKey = LEN(Key$) LowerScan = FoundSection 'I would love to use ARRAY SCAN, but I don't like its limitations... 'ARRAY SCAN INIArray(FoundSection), FROM 1 TO LenKey, = Key$, TO i& FoundKey = 0 for Counter = LowerScan to UpperScan if left$(INIArray(Counter), LenKey) = Key$ then FoundKey = Counter exit for end if next Counter if FoundKey = 0 then function = -2: exit function ARRAY DELETE INIArray(FoundKey) function = 0 end function function DeleteSection(Section$) as integer ' Look at all the cut-n-paste! Basically, it's a modified ReadKey$ local FoundSection as integer local FoundKey as integer local x as integer local LenKey as integer local LowerScan as integer local UpperScan as integer local Counter as integer local tempKey as string FoundSection = 0 x = 0 LenKey = 0 tempKey = "" ' Add [ or ] if needed IF LEFT$(Section$, 1) <> "[" THEN Section$ = "[" + Section$ IF RIGHT$(Section$, 1) <> "]" THEN Section$ = Section$ + "]" FOR x = 1 TO INISectionIndexMax IF INIArray(INISectionIndex(x)) = Section$ THEN FoundSection = INISectionIndex(x) ' Ok, we have to check to see if there's another section or not. If there is, then ' we should stop scanning at that number. If there isn't, then we should stop scanning ' at the last number of the file array (since, obviously, if there is no next section, ' it's the end of the file. if x = ubound(INISectionIndex) then UpperScan = ubound(INIArray) else UpperScan = INISectionIndex(x + 1) end if EXIT FOR END IF NEXT x if FoundSection = 0 then function = -1: exit function LowerScan = FoundSection for Counter = LowerScan to UpperScan - 1 ' It's gotta be LowerScan since as soon as you delete it, the next one you want to ' delete is at LowerScan.... ARRAY DELETE INIArray(LowerScan) redim preserve INIArray(1 to ubound(INIArray) - 1) ' Get rid of the extra space next Counter function = 0 end function function SaveINI(INIFile$) as integer $if 0 1) Open INIFile$ 2) Write INIArray() to it. 3) Close INIFile$ $endif local NextFree as integer local x as integer NextFree = Freefile Open INIFile$ for output as #NextFree for x = 1 to ubound(INIArray) print #NextFree, INIArray(x) next x Close #NextFree function = 0 end function