|
Ein Programm in der Programmiersprache | A program in the programming language | |||
vbScript | ||||
zum Lesen und Schreiben von Daten in INI-Dateien mit Gruppen und Obergruppen | for reading and writing of data in INI-files with groups and supgroups |
Das ist eine weiter abgespeckte (minimalisierte) Version von Read-Write von INI-Daten mit Gruppen und Obergruppen, getrennt durch Ausdrücke in eckigen Klammern und Doppelklammern, so dass die INI-Datei von mehreren Programmen verwendet werden kann, obwohl der Code muss in allen diesen sein. Das Meiste des gezeigten Programm-Beispiels besteht aus Code für den Test der verschiedenen Arten von INI-Daten. Für die Minimalisierung ist angenommen, dass die Default-Daten und die INI-Daten fehlerfrei sind und unnötige Leerzeichen und Leerzeilen nicht vorkommen. Das Programm-Beispiel zeigt das Prinzip, dass für die Verarbeitungs-Prozedur keine Delimiters verwendet werden, so dass diese in der INI-Datei verwendet werden können wie folgt: ParameterXYZ$etc=1,2,3;4,5,6:x,y,z,50%,2&3,'e23',abc etc or [Groupname1(index);xyz:abc/\{} ] etc Hier ist die Dictionary-Object-Technik verwendet, um Objekte mit Eigenschafts-Namen zu erhalten, welche vom Programmierer verwendet werden können wie Variable, welche automatisch den gleichen Namen haben wie die Parameter in der INI-Datei auf der Festplatte, ohne dass man Variablen-Deklarationen mit Zuweisungen zu den Parameter-Namen schreiben muss. Es ist gezeigt, dass Listen von Parametern geschrieben werden können vor und ohne dass eine Gruppe oder Obergruppe vorkommt. Es ist extra gezeigt, dass Parameter-Namen in verschiedenen Gruppen gleich sein können. Im Fall von einer irrtümlichen Verwendung von Parametern im Programm, welche nicht in der INI-Datei vorkommen, wird das ignoriert ohne eine Fehlermeldung. Für den Test, ob das Programm richtig arbeitet, sind 6 Beispiele gezeigt und das Ergebnis wird angezeigt in einem von 6 Notepad-Seiten, wenn sie weg geklickt wird, wird die nächste angezeigt. |
|
This is a futher slimmed (minimalised) version of Read-Write of INI-data with groups and supgroups, separated by expressions with squared brackets and double-brackets, so that the INI-file can be used by several programs, although the code must be in all of them. Most of the shown program-example consists of the code for testing different kinds of INI-data. For the minimalisation it is assumed, that the default-data and the data of the INI-file is free of errors and no unneeded blanks and empty lines occor. For this there is no error-detect. The program-example shows the principle, that for the processing procedure there are used no delimiters, so that they can be used in the INI-file like this: parameterXYZ$etc=1,2,3;4,5,6:x,y,z,50%,2&3,'e23',abc etc or [Groupname1(index);xyz:abc/\{} ] etc Here it is used the Dictionary-Object-Technique to get objects with property names, which can be used by the programmer as variables which have automaticly the same names as the parameters in the INI-file on disk without having to write declarations and assignments of the variables to the parameter-names It is shown, that list of parameters can be written before or without a supgroup or a group occurs It is extra shown, that parameter-names in different groups can be equal In case of a faulty use of a parameter in the program, which does not occur in the INI-file, this is ignored without giving an error-message. For the test if the program works properly, 6 examples are shown and the result is displayed in one out of 6 notepad-pages, if clicked away the next is displayed. | ||||
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 | |||||
16. April 2014 | April 16th 2014 |
' ReadWriteINI with Sup-Groups
Option Explicit ' -------------- For this program -------------- Dim GpON ' Boolean Dim INIFileSpec ' Strings Dim DefaultINI, ItemList, ItemList1, ItemList2, GroupList ' Arrays Dim INI ' Objects Set INI = CreateObject("Scripting.Dictionary") ' -------------- For all programs -------------- Const ForReading = 1, ForWriting = 2 Dim A0: A0 = Array() ' Arrays Dim fso, WshShell ' Objects Set fso = CreateObject("Scripting.FileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") ' -------------- For Testing -------------- Dim INIexample ' INI-File in CurrentDirectory INIFileSpec = fso.BuildPath(WshShell.CurrentDirectory,_ fso.GetBaseName(WScript.ScriptName) & ".INI") ' If SupGroup, Group and correct key, used by the programmer, ' are not found in the INI-file, then all value-assignment is ignored ' GroupClose can be ommitted before next group is opened (is automatisized), ' but must be written before writing the ItemList on disk ' For the test if the program works properly, 6 examples are shown and the ' result is displayed in one out of 6 notepad-pages, if clicked away the next ' is displayed. ' Job For INIexample = 1 To 6 Select Case INIexample Case 1: DefaultINI = Array( "Param1=01","Param2=02","Param3=03") Case 2: DefaultINI = Array( "Param1=01","Param2=02","Param3=03", _ "[Group1]","Param1=11","Param2=12","Param3=13", _ "[Group2]","Param1=21","Param2=22","Param3=23", _ "[Group3]","Param1=31","Param2=32","Param3=33") Case 3: DefaultINI = Array( "[Group1]","Param1=11","Param2=12","Param3=13", _ "[Group2]","Param1=21","Param2=22","Param3=23", _ "[Group3]","Param1=31","Param2=32","Param3=33") Case 4: DefaultINI = Array( "Param1=001","Param2=002","Param3=003", _ "[[SupGroup1]]", _ "[Group11]","Param1=11","Param2=12","Param3=13", _ "[Group12]","Param1=21","Param2=22","Param3=23", _ "[Group13]","Param1=31","Param2=32","Param3=33") Case 5: DefaultINI = Array( "Param1=001","Param2=002","Param3=003", _ "[[SupGroup1]]", _ "Param1=101","Param2=102","Param3=103", _ "[Group11]","Param1=11","Param2=12","Param3=13", _ "[Group12]","Param1=21","Param2=22","Param3=23", _ "[Group13]","Param1=31","Param2=32","Param3=33") Case 6: DefaultINI = Array( "[[SupGroup1]]", _ "[Group11]","Param1=11","Param2=12","Param3=13", _ "[Group12]","Param1=21","Param2=22","Param3=23", _ "[Group13]","Param1=31","Param2=32","Param3=33", _ "[[SupGroup2]]", _ "[Group21]","Param1=11","Param2=12","Param3=13", _ "[Group22]","Param1=21","Param2=22","Param3=23", _ "[Group23]","Param1=31","Param2=32","Param3=33", _ "[[SupGroup3]]", _ "[Group31]","Param1=11","Param2=12","Param3=13", _ "[Group32]","Param1=21","Param2=22","Param3=23", _ "[Group33]","Param1=31","Param2=32","Param3=33") End Select ' Write Default INI on disk ReadWriteListFile ForWriting, DefaultINI, INIFileSpec ' Read INI from harddisk into Array ReadWriteListFile ForReading, ItemList, INIFileSpec ' Changing the value of "Variables" per using the parameter-name With INI Select Case INIexample Case 1: GroupOpen "", "": .Item("Param2") = .Item("Param2") & "333" Case 2: GroupOpen "", "": .Item("Param2") = .Item("Param2") & "99" GroupOpen "", "Group2": .Item("Param2") = .Item("Param2") & "55" Case 3: GroupOpen "", "": .Item("Param1") = .Item("Param1") & "88" .Item("Param2") = .Item("Param2") & "44" GroupOpen "", "Group2": .Item("Param2") = .Item("Param2") & "666" Case 4: GroupOpen "", "": .Item("Param2") = .Item("Param2") & "333" GroupOpen "SupGroup1", "Group12": .Item("Param2") = .Item("Param2") & "555" Case 5: GroupOpen "", "": .Item("Param2") = .Item("Param2") & "44" GroupOpen "SupGroup1", "": .Item("Param2") = .Item("Param2") & "66" GroupOpen "SupGroup1", "Group12": .Item("Param2") = .Item("Param2") & "88" Case 6: GroupOpen "SupGroup2", "Group22": .Item("Param1") = .Item("Param1") & "111" .Item("Param2") = .Item("Param2") & "44" End Select End With GroupClose ' Write INI from Array to harddisk ReadWriteListFile ForWriting, ItemList, INIFileSpec ' Display Result on Screen WshShell.Run Join(Array("notepad",INIFileSpec)," "), 3, True Next ' --------------- Procedures --------------- Sub GroupOpen(anySup, anyGrp): If GpON Then GroupClose GroupsSelect ForReading, anySup, anyGrp: GpON = True: End Sub Sub GroupClose: GroupsSelect ForWriting, "", "": GpON = False: End Sub Sub GroupsSelect(Direction, byVal AnySupGroup, byVal AnyGroup) Dim i, d: d = 1 + 2 * (1 - Direction): If Abs(d) <> 1 Then Exit Sub For i = 1 To 2 Select Case i * d + ( 3 And d < 0) Case 1: ReadWriteItemList Direction, AnySupGroup, AnyGroup ' ItemList <-> GroupList Case 2: INIObjVars Direction End Select Next End Sub Sub ReadWriteItemList(Direction, byVal SupGroupName, byVal GroupName) ' ForReading: ItemList -> GroupList ' ForWriting: GroupList -> ItemList Dim SupGroup, Group, GroupFound, GroupBeen, item, debr Do: If Direction <> ForWriting Then Exit Do PUSH ItemList,ItemList1: PUSH ItemList,GroupList: PUSH ItemList,ItemList2: Exit Sub Loop Until True: If Direction <> ForReading Then Exit Sub ItemList1 = A0: GroupList = A0: ItemList2 = A0 SupGroup = "": Group = "": GroupBeen = False If SupGroup = SupGroupName And Group = GroupName Then _ GroupFound = True Else GroupFound = False For Each item In ItemList debr = Debrack(item) ' debr(0) ... Nr of Brackets, debr(1) ... expression If debr(0) > 0 Then If debr(0) > 1 Then SupGroup = debr(1): Group = "" Else Group = debr(1) If SupGroup = SupGroupName And Group = GroupName Then _ GroupFound = True Else GroupFound = False If GroupBeen Then PUSH ItemList2, item Else PUSH ItemList1, item ElseIf GroupFound Then PUSH GroupList, item: GroupBeen = True ElseIf GroupBeen Then PUSH ItemList2, item Else PUSH ItemList1, item End If Next: ItemList = A0 End Sub Function Debrack(AnyItem) ' Debrack(0) ... Nr of Brackets, Debrack(1) ... expression Dim expr: expr = Replace(Replace(AnyItem,"[",""),"]","") Debrack = Array((Len(AnyItem) - Len(expr))\2,expr) End Function Sub INIObjVars(ByVal Direction) ' AnyGroupList ... Array ' ForReading: Direction=IN, AnyGroupList=IN --> INI.key.items=created for global use ' ForWriting: Direction=IN, INI.key.items --> AnyGroupList=OUT Dim item, a, key, j: With INI If Direction = ForReading Then .RemoveAll: For Each item In GroupList: a = Split(item, "="): .Add a(0), a(1): Next ElseIf Direction = ForWriting And UBound(GroupList) > -1 Then A = A0: For Each item In GroupList: PUSH A, Split(item, "=")(0) : Next For Each key In .Keys: j = Find(A, key, 1) If j > -1 Then GroupList(j) = key & "=" & .Item(key) Next End If: End With End Sub ' ----------- General Used Procedures ----------- Function Find(AnyArray, AnyString, AnyComp) Dim item: Find = -1: If AnyComp <> 0 And AnyComp <> 1 Then Exit Function If UBound(AnyArray) < 0 Or AnyString = "" Then Exit Function For Each item In AnyArray: INC Find If StrComp (item, AnyString, AnyComp) = 0 Then Exit Function Next: Find = -1 End Function 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 Sub ReadWriteListFile(ByVal Direction, ByRef AnyList, ByVal AnyFileSpec) ' ForReading: Direction=IN, AnyList=OUT, AnyFileSpec=IN ' ForWriting: Direction=IN, AnyList=IN, AnyFileSpec=IN Dim f If Direction = ForReading Then 'returns lines in an array AnyList = A0: If Not fso.FileExists(AnyFileSpec) Then Exit Sub Set f = fso.OpenTextFile(AnyFileSpec, ForReading) While Not f.AtEndOfStream: PUSH AnyList, f.ReadLine: Wend: f.Close() 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 would make ein CrLf behind If fso.FileExists (AnyFileSpec) Then fso.DeleteFile(AnyFileSpec) If UBound(AnyList) < 0 Then Exit Sub Set f = fso.OpenTextFile(AnyFileSpec, ForWriting, True) f.Write Join(AnyList,vbCrLf): f.Close End If End Sub | ||