Results 1 to 5 of 5

Thread: No API Ini Reader Update

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    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
    Attached Files Attached Files
    Last edited by BenJones; Apr 6th, 2010 at 01:02 PM.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    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"
    Last edited by BenJones; Apr 6th, 2010 at 09:47 AM.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Last edited by LaVolpe; Apr 6th, 2010 at 10:45 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: No API Ini Reader

    Thanks for the code LaVolpe I give it a go

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width