-
1 Attachment(s)
No API Ini Reader Update
This is an example class i made of showing how to read information form an INI file without using any api calls, this maybe usfull for people that want to learn about working with strings
features include:
- GetSelections will return all the selections as a collection
- GetKeyNames will return all the keys in a selection to a collection
- ReadString will return a key from a selection
- ToString will return the whole ini file as one string
- SaveFile saves the ini file
Like to thank LaVolpe for helping me out with this code. and giveing suggestions
-
Re: No API Ini Reader
Two suggestions.
1. When you read the ini file, you can improve retrieval significantly.
a. Your lines can be keyed when adding to the collection: Selection.KeyName
b. Now your ReadString can quickly return value via the collection key: Selection & "." & KeyName; no looping needed
2. Most people that read INIs also need to update them too. Maybe consider adding functions to your class to change the collection items & write collection to an INI file.
As long as you keep your ini sections in order, within the collection, you shouldn't have any problems with any of your other functions.
-
Re: No API Ini Reader
Thanks for the suggestions LaVolpe I also try adding some more things to it.
is there any chance you can show me an example of "Your lines can be keyed when adding to the collection: Selection.KeyName"
-
Re: No API Ini Reader
Something like this... Tweak as needed, but you'll get the idea.
Code:
Public Sub LoadFile(ByVal Filename As String)
Dim sLine As String
Dim fp As Long
Dim sCurSection As String, iPos As Integer
Set Lines = New Collection
fp = FreeFile
Open Filename For Input As #fp
Do Until EOF(fp)
'Read in each line
Line Input #fp, sLine
If Len(sLine) Then
'Add line to the collection
If IsSelection(sLine) Then
sCurSection = Mid$(sLine, 2, Len(sLine) - 2)
Call Lines.Add(sCurSection, vbNullString) ' placeholder in collection
Else
iPos = InStr(sLine, "=")
If iPos = 0 Then
' do what? Not a selection, not a key
Else
Call Lines.Add(Mid$(sLine, iPos + 1), sCurSection & "." & Left$(sLine, iPos - 1))
End If
End If
End If
Loop
Close #fp
End Sub
Edited: The above isn't fool-proof. What happens if someone enters two sections of the same name? What if someone enters the same key twice under a single selection? What happens if someone adds extra spaces before/after section/key names? What if someone adds spaces around the equal sign in a key? As you can guess by what I just asked, there are so many ways a user can hose up the ini file. Windows takes care of this by making decisions. Your routines should also make decisions. Purposely hose up an INI and use APIs to read/write it. Determine how Windows handles duplicate/badly formatted entries & you may want to replicate the logic.
-
Re: No API Ini Reader
Thanks for the code LaVolpe I give it a go