On Apr 11, 3:13 am, mr_unreliable <
kindlyReplyToNewsgr...@notmail.com>
wrote:
> [clsIniFile.vbs.txt]Option Explicit
> '
> ' ================================================
> ' ================================================
> ' A WSH INI (INITIALIZATION) FILE CLASS OBJECT ===
> ' ================================================
> ' ================================================
> '
> ' Acknowledgment: vbParadise provided the "model" (as vb Code)
> ' for this utility. However this is not a one-for-one translation
> ' of the vbParadise code. It was almost entirely re-written,
> ' to conform with the ideosyncracies of vbScript,
> ' (and my own ideosyncracies as well)...
> '
> ' --- Revision History ---------------------------
> ' 20May02: Initial Attempt.
> ' 22May02: added code to deal with comments.
> ' 26May02: revised search pattern, to better detect blank lines,
> ' (courtesy sFulton of wsh ng)...
> ' 03July02: repaired typing mistake...
> ' 06July02: repaired EnumKeys (was returning comment lines)...
> ' 04Feb03: allowed for "commenting out" key=value pair. (thx to: j. ruzewski)
> ' 11Sept03: repaired DeleteKey (vielen Dank, Herr Doktor Reeg)...
> ' --- end of revisions ---------------------------
>
> Class clsIniFile
>
> ' "class level" variables
> Private c_bDebug ' as boolean
> Private c_sMe ' for debugging messages...
> '
> Private c_oRE ' as object (regexp)
> Private c_oDB ' as object (dictionary)
> Private c_sLines ' as collection (ini file lines)
> '
> Private c_sBuffer ' as string (holds entire "resource")
> Private c_iLine ' as integer (holds pointer to NEXT line of text)
> '
> Private ptnIniLine ' as string (pattern for line split)
> Private ptnSecHeader ' as string (pattern for section header)
> Private sEmpty ' as string
> '
> Private i,j ' as integer (for use as loop indexes)
> Private c_sComment ' as string
> Private c_sPreamble ' as string
> ' --- end of declarations and constants ----------
>
> ' ----------------------------------------------
> ' --- ADD / DELETE SECTION ---------------------
> ' ----------------------------------------------
>
> Public Function AddSection(sSectionName) ' returns a boolean value
> Dim saBlank(1,0) ' as string array
>
> ' if there is an existing section by this name, then punt...
> if c_oDB.Exists(sSectionName) then
> AddSection = False : Exit Function
> End If
>
> ' this is a new section name, so add it to dic (er, database)...
> saBlank(0,0) = sEmpty : saBlank(1,0) = sEmpty ' make up dummy key/value pair array
> c_oDB.Add sSectionName, saBlank
>
> AddSection = True ' return success value
> End Function
>
> Public Function DeleteSection(sSectionName) ' returns a boolean value
>
> ' if there is NO section by this name, then punt...
> if NOT c_oDB.Exists(sSectionName) then
> DeleteSection = False : Exit Function
> End If
>
> ' delete this section...
> c_oDB.Remove(sSectionName)
>
> DeleteSection = True ' return success value
> End Function
>
> ' ----------------------------------------------
> ' --- ADD / DELETE KEY (w/corresponding value) -
> ' ----------------------------------------------
>
> Public Function AddKey(sSection, sKey, sValue) ' returns a boolean value
> Const sMe = "[cIni:AddKey], "
> Dim saKeyValuePairs ' as string array (key/value pairs)
> Dim cItems ' as integer
> Dim saNewKeyValuePairs() ' as string array
> Dim sLastKey, sLastValue ' as string(s)
>
> ' if there is NO section by this name, then punt...
> if NOT c_oDB.Exists(sSection) then
> AddKey = False : Exit Function
> End If
>
> saKeyValuePairs = c_oDB(sSection) ' retrieve key/value pairs
> if IsArray(saKeyValuePairs) then ' test valid array of key/value pairs
> cItems = UBound(saKeyValuePairs, 2) + 1 ' add one to expand array
> if c_bDebug then dbPrint sMe & "cItems in section: " & CStr(cItems-1) & "/" & sSection
>
> ' create a new string array...
> ReDim saNewKeyValuePairs(1,cItems)
>
> ' copy the existing key/value pairs...
> For j = 0 to cItems -1
> saNewKeyValuePairs(0,j) = saKeyValuePairs(0,j)
> saNewKeyValuePairs(1,j) = saKeyValuePairs(1,j)
> Next ' j
>
> ' add the new key/value pair at end,
> ' (note: if last key/value is comment, then insert AHEAD of the comment)...
> sLastKey = saKeyValuePairs(0,cItems -1)
> sLastValue = saKeyValuePairs(1,cItems -1)
> if c_bDebug then dbPrint sMe & "sLastKey/sLastValue: " & sLastKey & "/" & sLastValue
>
> ' test for comment line or blank line...
> if sLastKey = c_sComment then ' test for comment line...
>
> saNewKeyValuePairs(0,cItems -1) = sKey
> saNewKeyValuePairs(1,cItems -1) = sValue
> ' dbPrint "[], added: " & CStr(cItems -1) & "/" & sKey & "/" & sValue
>
> saNewKeyValuePairs(0,cItems) = sLastKey ' tack on the blank/comment
> saNewKeyValuePairs(1,cItems) = sLastValue
>
> Else ' last key/value NOT blank/comment...
>
> saNewKeyValuePairs(0,cItems) = sKey ' add new key/value to end...
> saNewKeyValuePairs(1,cItems) = sValue
> End If ' blank/comment test
>
> Else ' not a valid array, so make one...
>
> cItems = 0 ' zero means one, in zero-based arrays
> ReDim saNewKeyValuePairs(1,cItems)
>
> saNewKeyValuePairs(0,cItems) = sKey ' add new key/value to end...
> saNewKeyValuePairs(1,cItems) = sValue
>
> End If ' valid array test
>
> ' replace the key/value pairs in the DB...
> c_oDB.Item(sSection) = saNewKeyValuePairs
>
> AddKey = True
> End Function
>
> Public Function DeleteKey(sSection, sKey) ' returns a boolean value
> Const sMe = "[cIni:DeleteKey], "
> Dim saKeyValuePairs ' as string array (key/value pairs)
> Dim cItems ' as integer
>
> ' if there is NO section by this name, then punt...
> if NOT c_oDB.Exists(sSection) then
> DeleteKey = False : Exit Function
> End If
>
> saKeyValuePairs = c_oDB(sSection) ' retrieve key/value pairs
> if NOT IsArray(saKeyValuePairs) then ' test no key/value pairs
> DeleteKey = False : Exit Function
> End If
>
> cItems = UBound(saKeyValuePairs, 2)
> if c_bDebug then dbPrint sMe & "cItems in section: " & CStr(cItems) & "/" & sSection
>
> ' search for the sKey entry, in this string array...
> For j = 0 to cItems
>
> if saKeyValuePairs(0,j) = sKey then ' found it!
>
> ' set this key/value to empty (rather than REALLY deleting it)...
> saKeyValuePairs(0,j) = sEmpty
> saKeyValuePairs(1,j) = sEmpty
>
> ' update the section contents, as held in the DB...
> c_oDB.Item(sSection) = saKeyValuePairs ' (vielen Dank, Herr Doktor Reeg)
>
> DeleteKey = True ' return success value
> Exit Function
> End If
> Next ' j
>
> ' at this point you searched through all the keys,
> ' without finding the designated sKey value, so TILT!
> DeleteKey = False
> End Function
>
> ' ----------------------------------------------
> ' --- ENUM SECTIONS (return string array) ------
> ' ----------------------------------------------
>
> Public Function EnumSections()
>
> EnumSections = c_oDB.Keys ' Keys method returns a string array...
> End Function
>
> ' ----------------------------------------------
> ' --- ENUM KEYS (return string array) ----------
> ' ----------------------------------------------
>
> Public Function EnumKeys(sSection)
> Const sMe = "[cInI:EnumKeys], "
> Dim saKeyValuePairs ' as string array (key/value pairs)
> Dim saReturn() ' as string array
> Dim cItems, cActual ' as integer
>
> saKeyValuePairs = c_oDB(sSection) ' retrieve key/value pairs
> BugAssert (IsArray(saKeyValuePairs)), sMe & "no key/value pairs for this section: " & sSection
> cItems = UBound(saKeyValuePairs, 2)
> if c_bDebug then dbPrint "[cIni:EnumKeys], cItems in section: " & CStr(cItems) & "/" & sSection
>
> cActual = 0
> ReDim saReturn(cItems) ' includes actual items (plus any comment lines)...
> For j = 0 To cItems
> if saKeyValuePairs(0,j) <> c_sComment then
> saReturn(cActual) = saKeyValuePairs(0,j)
> cActual = cActual + 1
> End If
> Next
> ReDim Preserve saReturn(cActual - 1)
> EnumKeys = saReturn ' set return (string array)
> End Function
>
> ' ----------------------------------------------
> ' --- GET / SET VALUE --------------------------
> ' ----------------------------------------------
>
> Public Function GetValue(sSection, sKey)
> Const sMe = "[cIni:GetValue], "
> Dim saKeyValuePairs ' as string array
> Dim cItems ' as integer
> Dim sThisKey, sThisValue
>
> if c_bDebug then dbPrint sMe & "sSection/sKey is: " & sSection & "/" & sKey
>
> ' get the section's key/value pairs array...
> saKeyValuePairs = c_oDB(sSection) ' retrieve associated item (a string array)
> BugAssert (IsArray(saKeyValuePairs)), sMe & "no key/value pairs for this section: " & sSection & " / " & sKey
>
> cItems = UBound(saKeyValuePairs, 2)
> if c_bDebug then dbPrint sMe & "cItems: " & CStr(cItems)
> BugAssert (cItems >= 0), sMe & "internal error: no key/value pairs for this section: " & sSection & " / " & sKey
>
> ' lookup the value for this key...
> For j = 0 to cItems
> sThisKey = saKeyValuePairs(0,j)
> sThisValue = saKeyValuePairs(1,j)
> ' dbPrint sMe & "sThisKey/sThisValue: " & sThisKey & "/" & sThisValue
>
> if sThisKey = sKey then ' found it!
> GetValue = sThisValue ' set return value
> Exit Function
> End If
> Next ' j
>
> ' use bugassert to throw an error...
> BugAssert False, sMe & "could not find value for: " & sSection & "/" & sKey
> End Function
>
> Public Function SetValue(sSection, sKey, sNewVal) ' returns a boolean value
> Const sMe = "[cIni:SetValue], "
> Dim saKeyValuePairs ' as string array
> Dim cItems ' as integer
> Dim sThisKey, sThisValue
>
> if c_bDebug then dbPrint sMe & "sSection/sKey is: " & sSection & "/" & sKey
>
> ' get the section's key/value pairs array...
> saKeyValuePairs = c_oDB(sSection) ' retrieve associated item (a string array)
> BugAssert (IsArray(saKeyValuePairs)), sMe & "no key/value pairs for this section: " & sSection & " / " & sKey...
>
> read more »
Thanks everybody for replying my query
AD