Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi-line ini file entries 1

Status
Not open for further replies.

Glasgow

IS-IT--Management
Jul 30, 2001
1,669
GB
Is it possible to spread an ini file entry over more than one line - e.g. using a continuation character?

So rather than something like:
Code:
[text inserts]
Name=Fred Bloggs
Phone=888 1234
I'd like to be able to have:
Code:
[text inserts]
Name=Fred Bloggs
Address=121 High St,
        Dullsville,
        Far Away
Phone=888 1234
 
The only "standard" line continuation I know of is for pathnames, as in:

Code:
LastDir = C:\SomeDir\AnotherDir\EvenDeeper\
    DeeperStill\Deepest

I have heard that some INI codecs will treat lines that have leading whitespace (typically spaces or tabs) as value continuations... though not as multiline values.

If you write your own though you can implement any rules you want.

I haven't tried it, but using the INI API calls and writing a value with special characters may end up quoting the value and using C-style escapes:

Code:
keyname = "line 1\nline2"
 
Why wouldn't you just use?:

Street=121 High St
City=Dullsville
State=Far Away
Country=Some Land

You may have posted just an example, but where possible, I would apply the same guidelines here as for a database table....
 
Thanks for replies. In practice I was hoping to use a continuation mechanism that would be recognised by the GetPrivateProfileString API call - so in this context that's what would govern what a valid INI file is. Unfortunately it does not seem to recognise a backslash as a continuation.

On studying the help text for GetPrivateProfileString, it suggests that, by supplying a null string as its second parameter (the key required within a given section), it will return all keys within that section. This might have offered me some kind of workaround where the multi line entry was placed under its own dedicated section in the INI file but I can't get this to work.

All I am really trying to achieve is improved legibility within the INI file rather than force embedded newlines. The entries I am adding are very long and logically belong on separate lines. I guess if necessary I could code each line as a separate INI entry (as in SBerthold's example which was posted as I typed this response) and concatenate them after the different values have been retrieved. The use of an address example was just to illustrate my problem - the actual data is quite different.

 
I don't know if this is what you are looking for but this works for me to get the entire section of an ini file

Code:
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" _
    (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, _
    ByVal lpFileName As String) As Long

Code:
Public Function GetINISection(ByVal strSection As String, Optional ByVal strINI As String = "Default") As String
'returns an ini section based on the section passed and if an ini file is passed it will use that file instead of the default
    Dim strBuffer As String * 32767
    Dim lngLength As Long
    
    On Error GoTo Errhandler
    If strINI = "Default" Then
        strINI = mstrINI
    End If

    lngLength = GetPrivateProfileSection(strSection, strBuffer, 32767, strINI)
    GetINISection = Left$(strBuffer, lngLength)
    
    Exit Function
Errhandler:
    Err.Raise Err.Number, CLASS_NAME & "GetINISection\" & Err.Source, Err.Description
End Function

this would read in all the values in an ini file
Code:
DLLFiles = clINISetting.GetINISection("DLLFiles", App.Path & "\REGDLL.INI")
[DLLFiles]
WSOCKOCX=MSWINSCK.OCX
WSOCKOCA=MSWINSCK.oca
WSOCKDEP=MSWINSCK.DEP
 
Sorry for delay - my efforts were diverted elsewhere.

Thanks jadams0173 - I was not aware of GetPrivateProfileSection which indeed appears to do the job that GetPrivateProfileString does not when passed a null parameter. I will use this approach and thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top