Re: INI Files by de
de
Fri Mar 11 21:40:18 CST 2005
Anything here good for you? BTW - People ask me why have the trailing
quotes in comments. It's because my editor shows commented lines as black
text on a light grey background and thr trailing quote keeps the line from
being blank trimmed on file save (which would display a nasty ragged right
edge). My desk may be messy but my code is neat.
set ini = New IniFile
ini.Read("c:\vista.ini")
ini.Dump
Display ini,"[reports]","EXE"
Sub Display ( ini , section , variable )
dim value: value = ini.GetValue(section,variable)
if isNull(value) then
wscript.echo variable,"is not defined in",section
else
wscript.echo section,variable,"=",value
end if
End Sub
'
'
' Name:
'
'
'
' IniFile.cls
'
'
'
' Description:
'
'
'
' This class implements a basic ini file interface. Using this class you
can read and '
' parse an ini file. The methods give easy access to the variables by
section. '
'
'
' Ini File Format
'
'
'
' An ini file contains variables that are defined in sections. A section
is defined by '
' entering [sectionname] on a line by itself where sectionname is any
name you want to '
' use (when you specift the section name in GetValue, include the [ and ]
characters). '
' Any variables that are defined prior to the start of a section are put
into the default '
' section which is named "".
'
'
'
' A variable is defined by creating a line with variable=value. You can
include quotes or '
' not as your choice. The quotes will not get stored as part of the
value. '
'
'
' Section and variable names are case-insensitive.
'
'
'
' Properties:
'
'
'
' none
'
'
'
' Methods:
'
'
'
' Read(filename)
'
'
'
' Reads and parses the ini file with the given name. Returns true if
the file exists, '
' false otherwise. Any ini file entry that does not begin with "[" (a
section name) or '
' contain a variable=value pair is ignored.
'
'
'
' GetValue(section,variable)
'
'
'
' Returns the value of the variable in the given section. If the
section or variable '
' is not defined then the returned value is null (not the null string,
but null). '
'
'
' Dump
'
'
'
' Returns the parsed contents of the ini file as a long string with
lines terminated '
' by vbCrLf.
'
'
'
' Implementation:
'
'
'
' Each section (including the default section) is stored as a dictionary
entry. Each '
' entry in this dictionary is itself a dictionary where each entry
contains a variable '
' and value. The variable is the key and the value is the contents of the
location with '
' that key.
'
'
'
' Future:
'
'
'
' I may add a Write method to allow updates to the ini file. This will
require some fancy '
' coding to preserve comments.
'
'
'
' Audit:
'
'
'
' 2005-03-11 jdeg original code
'
'
'
Class IniFile
Private m_ini
'
'
' Create the root dictionary and a default (no name section
'
'
'
Private Sub Class_Initialize ()
set m_ini = CreateObject("Scripting.Dictionary")
m_ini.CompareMode = vbTextCompare
set m_ini("") = CreateObject("Scripting.Dictionary")
End Sub
'
'
' Delete all variable and section entries then delete the dictionary
'
'
'
Private Sub Class_Terminate ()
dim entry
for each entry in m_ini.Keys
m_ini(entry).RemoveAll
set m_ini(entry) = Nothing
next
m_ini.RemoveAll
set m_ini = Nothing
End Sub
'
'
' Return the value of the variable in the given section (or null if not
found) '
'
'
Public Function GetValue ( section , variable )
GetValue = null
if not m_ini.Exists(section) then exit Function
if not m_ini(section).Exists(variable) then exit function
GetValue = m_ini(section)(variable)
End Function
'
'
' Read the ini file into the dictionary. Return True if there were no
errors. '
'
'
Public Function Read ( inifileName )
dim fso 'file system object for file access
'
dim all 'all of the cntents of the ini file
'
dim var 'the variable name from variable=value
'
dim val 'the variable value from variable=value
'
dim section 'the current section name
'
dim line 'the current line in the ini file
'
set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(inifileName) then
'read the entire contents of the ini file
all = fso.OpenTextFile(inifileName).ReadAll
all = Split(all,vbCrLf)
'start wiht the default section
section = ""
for each line in all
line = Trim(line)
'get line type and parse
if not Comment(line,var,val) then
'not a comment - add section (maybe) or variable
if left(line,1) = "[" then
section = line
if not m_ini.Exists(section) then
set m_ini(section) =
CreateObject("Scripting.Dictionary")
m_ini(section).CompareMode = vbtextCompare
end if
else
m_ini(section)(var) = val
end if
end if
next
Read = True
else
Read = False
End if
End Function
'
'
' Returns the entire contents of the parsed ini file as a string with
lines terminated by '
' vbCrLf.
'
'
'
Public Function Dump ()
dim section
dim variable
Dump = ""
for each section in m_ini.Keys
Dump = Dump & section & vbCrLf
for each variable in m_ini(section).Keys
Dump = Dump & " " & variable & " = " &
m_ini(section)(variable)
next
next
End Function
'
'
' Returns True if the line is a comment. A line is considered to be a
comment if '
'
'
' 1) It is empty
'
' 2) It starts with a semi-colon
'
' 3) It is not a section or variable definition
'
'
'
' If the line is a variable definition, the variable name and value are
parsed and '
' returned. Quotes are removed from the value if appearing in the first
or last position '
'
'
Private Function Comment ( line , var , val )
Comment = True
if Len(line) = 0 then Exit Function
if Left(line,1) = ";" then Exit Function
if instr(line,"=") = 0 and left(line,1) <> "[" then Exit Function
Comment = False
if left(line,1) <> "[" then
var = Trim(Left(line,Instr(line,"=")-1))
val = Trim(Mid(line, Instr(line,"=")+1))
if Left(val,1) = """" then val = Mid(val,2)
if Right(val,1) = """" then val = Mid(val,1,Len(val)-1)
end if
End Function
End Class