|
Ein Programm in der Programmiersprache | A program in the programming language | |||
vbScript | ||||
zum Lesen und Schreiben von Daten in INI-Dateien | for reading and writing of data in INI-files | |||
Das ist eine abgespeckte (minimalisierte) Version, die davon ausgeht, dass keine Fehler in den Default-Daten und den Daten der INI-Datei vorkommen und das Programmbeispiel soll das Prinzip zeigen, wie man das Datenformat ändern muss, damit man vom Programm her einen einfachen und bequemen Zugriff auf die gewünschten Parameter hat, sodass der Code insgesamt verkleinert wird. Weiters werden die Möglichkeiten des Dictionary-Objekts verwendet, um automatisch Variablen-Namen durch Eigenschaftsnamen eines Dictionary-Objekts zu ersetzen, sodass diese der Programmierer wie Variablen-Namen verwenden kann, die automatisch gleich sind wie die Parameter-Namen in der INI-Datei. Weiters sind alle Formatumwandlungs-Prozeduren für beide Richtungen gemacht, sodass man bei der Abspeicherung der Daten des INI-Arrays nur die Reihenfolge der Prozeduraufrufe umkehren muss und es kann schliesslich mit einer einzigen Prozedur alles in beide Richtungen konvertiert werden. | This is a slimmed (minimalised) version, which emanates from no errors in the default-data and the data of the INI-file and the program-example is to show the principle, how one must change the data-format, that one has from the program a simple and comfortable access to the desired parameters, so that the code in total can be made smaller. Further there are used the possibilities of the dictionary-object, in order to replace the variable-names by propertynames of a dictionary-object, so that the programmer can use them like variable-names, which are automaticly equal with the parameter-names in the INI-file. Further there are all format-conversion-procedures made for both directions, so that one by the saving of the data of the INI-array only needs to reverse the order of the procedure-calls and it can finally be converted all with one single procedure in both directions. | |||
Das Programm ist getestet vor der Publikation, aber es kann keine Garantie gegeben werden, dass es fehlerfrei ist | The program ist tested before publication, but there can be given no guarantee, that it is free of errors | |||
7. Dez. 2013 | Dec 7th 2013 |
' ReadWriteINI ' Here it is used the Dictionary-Object-Technique to get objects with ' property names, which can be used by the programmer like variables ' which have the same names as the parameters in the INI-file on disk ' It is extra shown, that Parameter-Names in different groups can be equal ' All is done without cleaning INI-lines, because it is assumed, ' that the DefaultINI and the INI-Data on disk are error-free Option Explicit Const ForReading = 1, ForWriting = 2 Dim FileSpec ' Strings Dim DefaultINI, ItemList, GroupList, Params ' Arrays Dim fso, f, INI ' Objects Set fso = CreateObject("Scripting.FileSystemObject") Set INI = CreateObject("Scripting.Dictionary") FileSpec = "c:\Users\UserName\Documents\Scripts\ReadWriteINI.INI" DefaultINI = Array( "[Group1]","Param1=11","Param2=12","Param3=13",_ "[Group2]","Param1=21","Param2=22","Param3=23",_ "[Group3]","Param1=31","Param2=32","Param3=33") ' Job ReadWriteListFile ForWriting, DefaultINI, FileSpec ' Write Default INI on disk ReadWriteINI ForReading, FileSpec, "Group2" ' Loads INI-Dictionary-Obj from disk INI.Item("Param2")= 2255 ' Changing the value of one "Variable" per using the parameter-name ReadWriteINI ForWriting, FileSpec, "Group2" ' Saves INI-Dictionary-Obj to disk ' Procedures Sub ReadWriteINI(ByVal Direction, ByVal AnyFileSpec, ByVal AnyGroup) ' ForReading: Direction=IN, AnyFileSpec=IN, AnyGroup=IN ' ForWriting: Direction=IN, AnyFileSpec=IN, AnyGroup=IN ' ForWriting: Procedures worked down in the opposite order to ' restore original INI-file-format on disk Dim i, d: d = 0 Select Case Direction Case ForReading: d = 1 Case ForWriting: d = -1 End Select For i = 1 To 4 Select Case i * d + ( 5 And d < 0) Case 1: ReadWriteListFile Direction, ItemList, FileSpec Case 2: GroupUngroupParams Direction, ItemList, GroupList Case 3: AccessParams Direction, GroupList, Params, AnyGroup Case 4: INIObjVars Direction, Params End Select Next End Sub Sub INIObjVars(ByVal Direction, ByRef AnyParams) ' AnyParams ... Array ' ForReading: Direction=IN, AnyParams=IN --> INI.key.items=created for global use ' ForWriting: Direction=IN, INI.key.items --> AnyParams=OUT Dim item, a, key If Direction = ForReading Then For Each item In AnyParams a = Split(item, "="): INI.Add a(0), a(1) Next ElseIf Direction = ForWriting Then AnyParams = Array() For Each key In INI.Keys PUSH AnyParams, key & "=" & INI.Item(key) Next End If End Sub Sub AccessParams(ByVal Direction, ByRef AnyGroupList, ByRef AnyParamList, ByVal AnyGroupName) ' ForReading: Direction=IN, AnyGroupList=IN, AnyParamList=OUT, AnyGroupName=IN ' ForWriting: Direction=IN, AnyGroupList=OUT, AnyParamList=unchanged, AnyGroupName=IN Dim item, i: i = -1 For Each item In AnyGroupList: INC i If UCase(Split(item, ",")(0)) = UCase(AnyGroupName) Then If Direction = ForReading Then AnyParamList = Split(Mid(item, Len(AnyGroupName) + 2),",") ElseIf Direction = ForWriting Then AnyGroupList(i) = AnyGroupName & "," & Join(AnyParamList, ",") End If End If Next End Sub Sub GroupUngroupParams(ByVal Direction, ByRef AnyItems, ByRef AnyGroupChains) ' ForReading: Direction=IN, AnyItems=IN, AnyGroupChains=OUT ' ForWriting: Direction=IN, AnyItems=OUT, AnyGroupChains=IN Dim item, A, sTmp: sTmp = "" If Direction = ForReading Then AnyGroupChains = Array() For Each item In AnyItems If Left(item,1) = "[" And Right(item,1) = "]" Then If sTmp <> "" Then PUSH AnyGroupChains, sTmp sTmp = Mid(item, 2, Len(item)- 2) Else sTmp = sTmp & "," & item End If Next: If sTmp <> "" Then PUSH AnyGroupChains, sTmp ElseIf Direction = ForWriting Then AnyItems = Array() For Each item In AnyGroupChains A = Split(item, ","): A(0) = "[" & A(0) & "]" PUSH AnyItems, A Next End If End Sub Sub ReadWriteListFile(ByVal Direction, ByRef AnyList, ByVal AnyFileSpec) ' ForReading: Direction=IN, AnyList=OUT, AnyFileSpec=IN ' ForWriting: Direction=IN, AnyList=IN, AnyFileSpec=IN Dim MyFile, Line, LastLine If Direction = ForReading Then 'returns lines in an array ReDim AnyList(-1) If fso.FileExists(AnyFileSpec) Then Set MyFile = fso.OpenTextFile(AnyFileSpec, ForReading) While Not MyFile.AtEndOfStream ReDim Preserve AnyList(UBound(AnyList)+1) AnyList(UBound(AnyList)) = MyFile.ReadLine Wend: MyFile.Close() End If ElseIf Direction = ForWriting Then ' If AnyList not defined (DIM AnyList(-1)), ' then save no file with lenght 0 and ' if such a previous extant, then delete. ' writes lines without quotation marks ' True overwrites already existant files ' WriteLine makes ein CrLf behind If UBound(AnyList) > -1 Then If fso.FileExists (AnyFileSpec) Then fso.DeleteFile(AnyFileSpec) Set f = fso.OpenTextFile(AnyFileSpec, ForWriting, True) f.Write Join(AnyList,vbCrLf): f.Close ElseIf fso.FileExists(AnyFileSpec) Then Set f = fso.GetFile(AnyFileSpec): f.Delete End If End If End Sub Sub PUSH(ByRef AnyArr, ByVal AnyVar) ' AnyVar can be a String, Numeric or a Variant Array Dim item: If IsEmpty(AnyArr) Then ReDim AnyArr(-1) If Not IsArray(AnyVar) Then AnyVar = Array(AnyVar) For Each item In AnyVar ReDim Preserve AnyArr(UBound(AnyArr)+1): AnyArr(UBound(AnyArr)) = item Next End Sub Function INC(ByRef AnyNr) AnyNr = AnyNr + 1: INC = AnyNr End Function | ||