Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 07/10/2020 in all areas

  1. Just something for those that don't know about the Scripting Dictionary Global $oDictionary, $oMyError _Main() Func _Main() ; Create dictionary object $oDictionary = _InitDictionary() $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Initialize a COM error handler Local $vKey, $sItem, $sMsg ; Add keys with items _DebugPrint('_AddItem("One", "Same")' & @TAB & _AddItem("One", "Same")) _DebugPrint('_AddItem("Two", "Car")' & @TAB & _AddItem("Two", "Car")) _DebugPrint('_AddItem("Three", "House")' & @TAB & _AddItem("Three", "House")) _DebugPrint('_AddItem("Three", "House")' & @TAB & _AddItem("Three", "House")) _DebugPrint('_AddItem("Four", "Boat")' & @TAB & _AddItem("Four", "Boat")) If _ItemExists('One') Then ; Display item MsgBox(0x0, 'Item One', _Item('One'), 2) ; Set an item _ChangeItem('One', 'Changed') ; Display item MsgBox(0x20, 'Did Item One Change?', _Item('One'), 3) ; Remove key _ItemRemove('One') ; EndIf ; Store items into a variable For $vKey In $oDictionary $sItem &= $vKey & " : " & _Item($vKey) & @CRLF Next ; Display items MsgBox(0x0, 'Items Count: ' & _ItemCount(), $sItem, 3) ; Add items into an array $aArray = _GetItems() ; Display items in the array For $i = 0 To _ItemCount() - 1 MsgBox(0x0, 'Array [ ' & $i & ' ]', $aArray[$i], 2) Next _DebugPrint('_ItemRemove("Two")' & @TAB & _ItemRemove("Two")) _DebugPrint('_ItemRemove("Three")' & @TAB & _ItemRemove("Three")) _DebugPrint('_ItemRemove("Three")' & @TAB & _ItemRemove("Three")) _DebugPrint('_ItemRemove("Four")' & @TAB & _ItemRemove("Four")) ; use keys like an array index For $x = 1 To 3 _AddItem($x, "") Next $sItem = "" _ChangeItem(2, "My Custom Item") _ChangeItem(1, "This is the 1st item") _ChangeItem(3, "This is the last item") For $vKey In $oDictionary $sItem &= $vKey & " : " & _Item($vKey) & @CRLF Next ; Display items MsgBox(0x0, 'Items Count: ' & _ItemCount(), $sItem, 3) $sItem = "" _ChangeKey(2, "My New Key") For $vKey In $oDictionary $sItem &= $vKey & " : " & _Item($vKey) & @CRLF Next ; Display items MsgBox(0x0, 'Items Count: ' & _ItemCount(), $sItem, 3) $oDictionary.RemoveAll() MsgBox(0x0, 'Items Count',_ItemCount(), 3) EndFunc ;==>_Main Func _InitDictionary() Return ObjCreate("Scripting.Dictionary") EndFunc ;==>_InitDictionary ; Adds a key and item pair to a Dictionary object. Func _AddItem($v_key, $v_item) $oDictionary.ADD ($v_key, $v_item) If @error Then Return SetError(1, 1, -1) EndFunc ;==>_AddItem ; Returns true if a specified key exists in the Dictionary object, false if it does not. Func _ItemExists($v_key) Return $oDictionary.Exists ($v_key) EndFunc ;==>_ItemExists ; Returns an item for a specified key in a Dictionary object Func _Item($v_key) Return $oDictionary.Item ($v_key) EndFunc ;==>_Item ; Sets an item for a specified key in a Dictionary object Func _ChangeItem($v_key, $v_item) $oDictionary.Item ($v_key) = $v_item EndFunc ;==>_ChangeItem ; Sets a key in a Dictionary object. Func _ChangeKey($v_key, $v_newKey) $oDictionary.Key ($v_key) = $v_newKey EndFunc ;==>_ChangeKey ; Removes a key, item pair from a Dictionary object. Func _ItemRemove($v_key) $oDictionary.Remove ($v_key) If @error Then Return SetError(1, 1, -1) EndFunc ;==>_ItemRemove ; Returns the number of items in a collection or Dictionary object. Func _ItemCount() Return $oDictionary.Count EndFunc ;==>_ItemCount ; Returns an array containing all the items in a Dictionary object Func _GetItems() Return $oDictionary.Items EndFunc ;==>_GetItems ; This is my custom defined error handler Func MyErrFunc() Local $err = $oMyError.number If $err = 0 Then $err = -1 SetError($err) ; to check for after this function returns EndFunc ;==>MyErrFunc Func _DebugPrint($s_Text) ConsoleWrite( _ "!===========================================================" & @LF & _ "+===========================================================" & @LF & _ "-->" & $s_Text & @LF & _ "+===========================================================" & @LF) EndFunc ;==>_DebugPrint
    1 point
  2. I already pointed you to the functions you need above -- Read up on these in the help file. Try them out in your own script and then let us know how it goes. 😃
    1 point
  3. @RTFC Thanks so much! I will take a look into this. I think CryptoNG handles UTF8 strings, but from what you have shown it may not handle strings with multi byte characters correctly. If that is true, which it appears that it is, I will see what I can do to add the ability to handle such strings. In typical American fashion and since I don't ever use multi-byte character sets, for the most part, they don't exist. It's sort of like how some of us assume that everyone speaks English. Yes, this is true. By default, the Scite4AutoIt editor's message area does not correctly display multi-byte characters. As I mentioned above, I'm sure I used ConsoleWrites in my examples because I didn't take into account the possibility of strings with multi-byte characters in them. It was definitely short-sighted on my part. I will see if I can come up with a better set of examples that will work universally with either single or multi-byte string output. Those look like Au3Check errors and I do not get them when I run the UDF or the example file through Au3Check. I'm not sure why you are getting them. I did see that I forgot to run the latest version through Au3Check with the strictest settings which allowed some "declared, but not used in func" warnings to get through. I will definitely get those corrected in the next version. As for using dot notation for the dll struct gets & puts, I have read and seen small tests that show it is a little faster but not by much. In this particular case, I don't think speed is a concern because the gets & puts are not occurring in a loop or being done multiple times during encryption or decryption. My primary reason for making the change was that it looked cleaner and required far less code to get the same result. I'm glad that you have found the UDF useful. I definitely plan to maintain it for the foreseeable future. I will start looking into these issue today. Hopefully I can get them resolved quickly. I won't be truly satisfied with this "Praise Sandwich" until all of the fillings are as good as the slices of bread.
    1 point
  4. Thanks CYCho :X love you
    1 point
  5. 1 point
  6. Another example for your amusement. #include <Array.au3> Local $aFiles[][2] = [["1.txt"], ["2.txt"], ["3.txt"]] Local $iMNCols = 0 ; Maximum Number of Columns (MNC) Local $sStr = "" ; This string will be converted into 2D array. ; Files to abbreviated strings For $i = 0 To UBound($aFiles) - 1 $aFiles[$i][1] = StringRegExpReplace(FileRead($aFiles[$i][0]), '(?:=+\R)?(\w+)[^=]+(?:=+\R?)?', "\1|") ; De-construct file format. StringReplace($aFiles[$i][1], "|", "|") $iExt = @extended ; @extended contains number of replacements in StringReplace() function. $iMNCols = ($iExt > $iMNCols ? $iExt : $iMNCols) ; Get Maximum Number of Columns Next ; String to 2D array For $i = 0 To UBound($aFiles) - 1 $sStr &= StringTrimRight($aFiles[$i][1], 1) & @CRLF ; StringTrimRight() removes trailing "|" . Next Local $aData[0][$iMNCols] _ArrayAdd($aData, StringStripWS($sStr, 2)) ; Append the three rows into the $aData array with $iMNCols number of columns. _ArrayTranspose($aData) ; Now array has $iMNCols number of rows, with three columns. ;_ArrayDisplay($aData, "", "", 0, Default, "1.txt|2.txt|3.txt") ; 2D array to file re-constructed string with numbers Local $sRet = "==" & @CRLF Local $r = 1 ; Counter For $j = 0 To UBound($aData) - 1 For $i = 0 To UBound($aData, 2) - 1 If $aData[$j][$i] <> "" Then $sRet &= $r & $aData[$j][$i] & @CRLF & $r & $aData[$j][$i] & @CRLF & "==" & @CRLF EndIf Next $r += 1 ; Increment $r Next ConsoleWrite(StringStripWS($sRet, 2) & @CRLF) ; Parameter flag (2) = strip trailing white spaces (@CRLF). Used the following files with the resultant output. 1.txt: == aa aa == aa aa == 2.txt: == bb bb == bb bb == bb bb == bb bb == bb bb == bb bb == 3.txt: == cc cc == cc cc == cc cc == cc cc == OUTPUT.txt: == 1aa 1aa == 1bb 1bb == 1cc 1cc == 2aa 2aa == 2bb 2bb == 2cc 2cc == 3bb 3bb == 3cc 3cc == 4bb 4bb == 4cc 4cc == 5bb 5bb == 6bb 6bb ==
    1 point
  7. A praise-sandwich for CryptoNG: Top bread slice: Great work; thorough, methodical, consistent, clean code, and well-documented; an excellent contribution all-round. The filling: At the moment, the library does not handle UTF-8 strings correctly. Just adding If $bResultIsText Then $vDecryptedData = BinaryToString($vDecryptedData, $SB_UTF8) in __CryptoNG_BCryptDecrypt is not enough; you need to take wide chars into account at the encryption stage too! For example, in __CryptoNG_BCryptEncrypt, you need to do something like this: ;Create input buffer and move input to the buffer $tInputBuffer = DllStructCreate(StringFormat("byte data[%i]", BinaryLen(StringToBinary($vData,$SB_UTF8)))) DllStructSetData($tInputBuffer,"data",StringToBinary($vData,$SB_UTF8)) Of course, this also affects passwords and salts, and anything else that relies on string inputs. Imagine what would happen if a user changes locale between encryption and decryption... Furthermore, you cannot properly test this using your current example suite, as you use Consolewrite for examining output, which would require a registry hack each time to select the appropriate code page(s) to render non-English UTF8 characters correctly. MsgBox does this out of the box (at least in my tests). For example, replace your test input string in your aes example by this (grabbed from Help's StringToBinary example): Func aes_encrypt_decrypt_example() Const $MESSAGE = "Hello - 你好", _ $SECRET = "Secret Password!", _ $SECRET_SALT = "Secret Salt" and watch it fail (both to encrypt correctly and (when patched) to represent correctly in console output). BTW, your recent "upgrade" of filling struct members using dot notation causes my scripts to error out (on every dot-based struct access) with: "<path>\CryptoNG.v1.5.5.au3" (2743) : ==> Variable must be of type "Object".: $tInputBuffer.data = Binary($vData) Haven't got a reproducer for you to test (and your examples run okay), but I've also read that access using dot notation is slower than using DllstructSetData (again, not tested myself (yet)), so you may want to look into that as well. Personally, I just restore dllstructSet/getData everywhere (neeeeed speeeeed!), but your code of course looks cleaner. Your call. Bottom bread slice: As you know, I've gratefully incorporated your library into my own CodeCrypter environment, and it's proving a real asset, so many, many thanks for your continuing efforts, and I hope you'll keep maintaining it.
    1 point
  8. DutchCoder

    AutoIt Snippets

    Wait for Mouse to move (two ways) Normally when you have a GUI you can use this: Func _MouseWaitMove() Do Sleep(100) Until GuiGetMsg() = -11 EndFunc But sometimes you don't have a GUI. This one always works: Func _MouseWaitMove() $_old = MouseGetPos() Do Sleep(100) $_new = MouseGetPos() Until ($_new[0] - $_old[0]) Or ($_new[1] - $_old[1]) EndFunc
    1 point
  9. Jeez, I now see what you want. The first problem is that you never explained yourself clearly, and your first example matches what would result from a sort, once elements are split out. Another skin for that cat, provided the files have the correct format: Local $iNbFiles = 3 Local $aFiles[$iNbFiles][3] = [["1.txt"], ["2.txt"], ["3.txt"]] Local Static $iMax = 0 For $i = 0 To UBound($aFiles) - 1 $aFiles[$i][1] = StringRegExp(FileRead($aFiles[$i][0]), "==\R(?:.+\R){2}", 3) $aFiles[$i][2] = UBound($aFiles[$i][1]) $iMax = _Max($iMax, UBound($aFiles[$i][1])) Next Local $sOut, $a For $i = 0 To $iMax - 1 For $j = 0 To UBound($aFiles) - 1 If $i < $aFiles[$j][2] Then $sOut &= ($aFiles[$j][1])[$i] Next Next $sOut &= "==" ConsoleWrite($sOut & @LF) Next time, please, make the minimum effort to fully and clearly explain what you want.
    1 point
  10. I don't know how the things work in CodeCrypter, but I suspect some part isn't handling UTF8 correctly. That sould be relatively easy to fix.
    1 point
  11. A new version (v1.5.0) was just published to the Downloads section of the forum. What's new in v1.5.0 Begun adding algorithm-specific encryption/decryption functions starting with: _CryptoNG_3DES_CBC_DecryptData _CryptoNG_3DES_CBC_DecryptFile _CryptoNG_3DES_CBC_EncryptData _CryptoNG_3DES_CBC_EncryptFile _CryptoNG_AES_CBC_DecryptData _CryptoNG_AES_CBC_DecryptFile _CryptoNG_AES_CBC_EncryptData _CryptoNG_AES_CBC_EncryptFile Updated the compiled Help file with the new functions and a new page that lists common named-value constants in the appendix. Added new internal functions to support the new algorithm-specific functions above. Cleaned up a few function headers. Added new examples, for the new functions, to the examples file. Fixed a small bug in how the size of the IV buffer was being determined when doing block cipher encryption and decryption. The length of the IV should match the algorithm's block size, not its key size. NOTE: The bug did NOT affect the accuracy of the results, just the size of the buffer. Corrected $CNG_KEY_BIT_LENGTH_3DES constant value. The value was changed from 168 to 192. 168 was the usable/logical bit length but the actual bit length is 192.
    1 point
  12. Hi, #cs ============================= OBJEKT 'Scripting.Dictionary' ======================================= *** Funktionssammlung *** _ObjDictCreate(Modus) Erzeugt ein Dictionary Objekt im Binär- (default) oder Textmodus Gibt das Handle des Objektes zurück _ObjDictAdd(Objekt, Schlüssel, Wert) Fügt einem Dictionary Objekt ein Schlüssel-Wert Paar hinzu _ObjDictGetValue(Objekt, Schlüssel) Liest den Wert für einen Schlüssel aus _ObjDictSetValue(Objekt, Schlüssel) Setzt einen neuen Wert für einen Schlüssel _ObjDictCount(Objekt) Gibt die Anzahl der Schlüssel-Wert Paare zurück _ObjDictSearch(Objekt, Schlüssel) Prüft ob der Schlüssel existiert, liefert 'TRUE' wenn gefunden _ObjDictDeleteKey(Objekt, Schlüssel) Löscht den angegebenen Schlüssel (und dessen Wert) Wird statt eines Schlüsselnamens '' übergeben, werden alle Schlüssel gelöscht (Standard) _ObjDictList(Objekt) Eine GUI mit einem ListView zeigt alle Schlüssel-Wert Paare _IniReadSectionToObjDict(Objekt, INI-Pfad, Sektion) Liest die angegebene INI-Sektion in das Objekt Gibt die Anzahl der gelesenen Schlüssel-Wert Paare zurück _IniWriteSectionFromObjDict(Objekt, INI-Pfad, Sektion) Schreibt die Schlüssel-Wert Paare des Objekts in die angegebene Sektion der INI-Datei Gibt die Anzahl der geschriebenen Schlüssel-Wert Paare zurück _FileReadToObjDict(Objekt, TextDatei-Pfad, Seperator) Liest aus einer Textdatei alle Schlüssel-Wert Paare zeilenweise ein (1 Paar/Zeile) Standard-Seperator ist '|', kann hier angepaßt werden Gibt die Anzahl der gelesenen Schlüssel-Wert Paare zurück _FileWriteFromObjDict(Objekt, TextDatei-Pfad, Seperator, Overwrite) Schreibt die Schlüssel-Wert Paare des Objekts in die angegebene Datei (1 Paar/Zeile). Mit Overwrite='TRUE' (Standard) wird die angegebene Datei, falls sie bereits existiert, überschrieben. Mit 'FALSE' wird an eine bestehende Datei angehängt. Standard-Seperator ist '|', kann hier angepaßt werden. Gibt die Anzahl der geschriebenen Schlüssel-Wert Paare zurück Autor: BugFix ( bugfix@autoit.de ) =================================================================================================== #ce #include <GuiConstants.au3> #include <GuiListView.au3> ;================================================================================================== ; Parameter: $MODE ; 0 Binär (default) ; 1 Text ; Return: Objekt Handle ;================================================================================================== Func _ObjDictCreate($MODE=0) $oDICT = ObjCreate('Scripting.Dictionary') If $MODE <> 0 Then $oDICT.CompareMode = 1 Return $oDICT EndFunc ;==>_ObjDictCreate ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $KEY - Schlüssel ; $VALUE - Wert ; Return: Erfolg: 0 ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; 2 Schlüssel ohne Inhalt übergeben ; 3 Wert hat keinen Inhalt ; 4 Schlüssel bereits vorhanden ;================================================================================================== Func _ObjDictAdd(ByRef $oDICT, $KEY, $VALUE) If Not IsObj($oDICT) Then SetError(1) Return -1 ElseIf $KEY = '' Then SetError(2) Return -1 ElseIf $VALUE = '' Then SetError(3) Return -1 ElseIf $oDICT.Exists($KEY) Then SetError(4) Return -1 EndIf $oDICT.Add($KEY, $VALUE) Return 0 EndFunc ;==>_ObjDictAdd ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $KEY - Schlüssel ; Return: Erfolg: Wert des Schlüssels ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; 2 Schlüssel ohne Inhalt übergeben ; 5 Schlüssel nicht vorhanden ;================================================================================================== Func _ObjDictGetValue(ByRef $oDICT, $KEY) If Not IsObj($oDICT) Then SetError(1) Return -1 ElseIf $KEY = '' Then SetError(2) Return -1 ElseIf Not $oDICT.Exists($KEY) Then SetError(5) Return -1 EndIf Return $oDICT.Item($KEY) EndFunc ;==>_ObjDictGetValue ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $KEY - Schlüssel ; $VALUE - Wert ; Return: Erfolg: 0 ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; 2 Schlüssel ohne Inhalt übergeben ; 3 Wert hat keinen Inhalt ; 5 Schlüssel nicht vorhanden ;================================================================================================== Func _ObjDictSetValue(ByRef $oDICT, $KEY, $VALUE) If Not IsObj($oDICT) Then SetError(1) Return -1 ElseIf $KEY = '' Then SetError(2) Return -1 ElseIf $VALUE = '' Then SetError(3) Return -1 ElseIf Not $oDICT.Exists($KEY) Then SetError(5) Return -1 EndIf $oDICT.Item($KEY) = $VALUE Return 0 EndFunc ;==>_ObjDictSetValue ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; Return: Erfolg: Anzahl der Schlüssel-Wert Paare ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ;================================================================================================== Func _ObjDictCount(ByRef $oDICT) If Not IsObj($oDICT) Then SetError(1) Return -1 EndIf Return $oDICT.Count EndFunc ;==>_ObjDictCount ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $KEY - Schlüssel ; Return: Erfolg: TRUE Schlüssel vorhanden ; FALSE Schlüssel nicht vorhanden ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; 2 Schlüssel ohne Inhalt übergeben ;================================================================================================== Func _ObjDictSearch(ByRef $oDICT, $KEY) If Not IsObj($oDICT) Then SetError(1) Return -1 ElseIf $KEY = '' Then SetError(2) Return -1 ElseIf Not $oDICT.Exists($KEY) Then Return False Else Return True EndIf EndFunc ;==>_ObjDictSearch ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $KEY - Schlüssel (default='') ; mit $KEY = '' werden alle Schlüssel gelöscht ; Return: Erfolg: 0 ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ;================================================================================================== Func _ObjDictDeleteKey(ByRef $oDICT, $KEY='') If Not IsObj($oDICT) Then SetError(1) Return -1 EndIf If $KEY = '' Then $oDICT.RemoveAll Return 0 ElseIf Not $oDICT.Exists($KEY) Then SetError(5) Return -1 EndIf $oDICT.Remove($KEY) Return 0 EndFunc ;==>_ObjDictDeleteKey ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $TITLE - Fenstertitel (optional) ; Return: Erfolg: GUI mit ListView ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; Requirements: #include <GuiConstants.au3> ; #include <GuiListView.au3> ;================================================================================================== Func _ObjDictList(ByRef $oDICT, $TITLE='Elemente: Objekt Dictionary') If Not IsObj($oDICT) Then SetError(1) Return -1 EndIf Local $count = $oDICT.Count Local $SaveMode = Opt("GUIOnEventMode",0), $ListGUI, $oDictLV, $btnClose, $msg $ListGUI = GUICreate($TITLE, 600, 400, (@DesktopWidth - 600)/2, (@DesktopHeight - 400)/2) $btnClose = GUICtrlCreateButton('&Ende', 40, 360, 70, 22) GUICtrlSetResizing($btnClose, BitOR($GUI_DockRight, $GUI_DockBottom, $GUI_DockSize)) GUICtrlDelete($oDictLV) $oDictLV = GUICtrlCreateListView('Schlüssel|Wert', 10, 10, 580, 340, BitOR($LVS_SHOWSELALWAYS, _ $LVS_EDITLABELS), BitOR($LVS_EX_GRIDLINES, $LVS_EX_HEADERDRAGDROP, $LVS_EX_FULLROWSELECT, $LVS_EX_REGIONAL)) If $count > 0 Then Local $strKey, $colKeys = $oDICT.Keys For $strKey In $colKeys GUICtrlCreateListViewItem($strKey & '|' & $oDICT.Item($strKey), $oDictLV) Next Else WinSetTitle($ListGUI, '', 'Das Objekt Dictionary enthält keine Elemente!') EndIf GUISetState(@SW_SHOW, $ListGUI) While 1 $msg = GUIGetMsg(1) If $msg[1] = $ListGUI And _ ($msg[0] = $GUI_EVENT_CLOSE Or $msg[0] = $btnClose) Then ExitLoop WEnd GUIDelete($ListGUI) Opt("GUIOnEventMode",$SaveMode) EndFunc ;==>ObjDictList ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $PathINI - Pfad der INI-Datei ; $SECTION - Sektion die gelesen werden soll ; Return: Erfolg: Anzahl der eingelesenen Schlüssel-Wert Paare ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; 6 INI-Pfad nicht vorhanden ; 7 kein Sektionsname ; 8 INI-Sektion konnte nicht gelesen werden oder leer ;================================================================================================== Func _IniReadSectionToObjDict(ByRef $oDICT, $PathINI, $SECTION) If Not IsObj($oDICT) Then SetError(1) Return -1 ElseIf Not FileExists($PathINI) Then SetError(6) Return -1 ElseIf $SECTION = '' Then SetError(7) Return -1 EndIf Local $arSECTION = IniReadSection($PathINI, $SECTION) If Not IsArray($arSECTION) Then SetError(8) Return -1 EndIf For $i = 1 To $arSECTION[0][0] $oDICT.Add($arSECTION[$i][0], $arSECTION[$i][1]) Next Return $arSECTION[0][0] EndFunc ;==>_IniReadSectionToObjDict ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $PathINI - Pfad der INI-Datei ; $SECTION - Sektion die gelesen werden soll ; Return: Erfolg: Anzahl der geschriebenen Schlüssel-Wert Paare ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; 7 kein Sektionsname ; 9 INI-Sektion konnte nicht geschrieben werden ;================================================================================================== Func _IniWriteSectionFromObjDict(ByRef $oDICT, $PathINI, $SECTION) If Not IsObj($oDICT) Then SetError(1) Return -1 ElseIf $SECTION = '' Then SetError(7) Return -1 EndIf Local $arSECTION[$oDICT.Count][2], $i = 0 Local $strKey, $colKeys = $oDICT.Keys For $strKey In $colKeys $arSECTION[$i][0] = $strKey $arSECTION[$i][1] = $oDICT.Item($strKey) $i += 1 Next If IniWriteSection($PathINI, $SECTION, $arSECTION, 0) = 1 Then Return $oDICT.Count Else SetError(9) Return -1 EndIf EndFunc ;==>_IniWriteSectionFromObjDict ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $PathFile - Pfad der Datei ; $SEPERATOR - Trennzeichen zwischen Wert u. Schlüssel, Standard: '|' ; Return: Erfolg: Anzahl der eingelesenen Schlüssel-Wert Paare ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; 6 Datei-Pfad nicht vorhanden ; 7 Leerstring als Seperator übergeben ; 8 Datei konnte nicht gelesen werden ;================================================================================================== Func _FileReadToObjDict(ByRef $oDICT, $PathFile, $SEPERATOR='|') If Not IsObj($oDICT) Then SetError(1) Return -1 ElseIf Not FileExists($PathFile) Then SetError(6) Return -1 ElseIf $SEPERATOR = '' Then SetError(7) Return -1 EndIf Local $fh = FileOpen($PathFile, 0), $line Local $val If $fh = -1 Then SetError(8) Return -1 EndIf While 1 $line = FileReadLine($fh) If @error = -1 Then ExitLoop $val = StringSplit($line, $SEPERATOR) If $val[0] > 1 Then $oDICT.Add($val[1], $val[2]) EndIf Wend FileClose($fh) Return $oDICT.Count EndFunc ;==>_FileReadToObjDict ;================================================================================================== ; Parameter: $oDICT - Handle des Dictionary-Objektes ; $PathFile - Pfad der Datei ; $SEPERATOR - Trennzeichen zwischen Wert u. Schlüssel, Standard: '|' ; $OVERWRITE - Falls Datei existiert, wird sie überschrieben (Standard). ; Mit 'FALSE' wird an bestehende Datei angehängt. ; Return: Erfolg: Anzahl der geschriebenen Schlüssel-Wert Paare ; Fehler: -1 ; Fehlerwert: 1 Objekt existiert nicht ; 7 Leerstring als Seperator übergeben ; 8 Datei konnte nicht geschrieben werden ;================================================================================================== Func _FileWriteFromObjDict(ByRef $oDICT, $PathFile, $SEPERATOR='|', $OVERWRITE=True) If Not IsObj($oDICT) Then SetError(1) Return -1 ElseIf $SEPERATOR = '' Then SetError(7) Return -1 EndIf If $OVERWRITE Then Local $fh = FileOpen($PathFile, 34) Else Local $fh = FileOpen($PathFile, 33) EndIf Local $strKey, $colKeys = $oDICT.Keys For $strKey In $colKeys FileWriteLine($fh, $strKey & $SEPERATOR & $oDICT.Item($strKey)) If @error Then SetError(8) Return -1 EndIf Next FileClose($fh) Return $oDICT.Count EndFunc ;==>_FileWriteFromObjDict Mega
    1 point
  13. A dictionary object example. Global $vKey, $sItem, $sMsg, $oDictionary ; Create dictionary object $oDictionary = ObjCreate("Scripting.Dictionary") If @error Then MsgBox(0, '', 'Error creating the dictionary object') Else ; Add keys with items $oDictionary.Add ("One", "Same" ) $oDictionary.Add ("Two", "Car" ) $oDictionary.Add ("Three", "House" ) $oDictionary.Add ("Four", "Boat" ) If $oDictionary.Exists('One') Then ; Display item MsgBox(0x0, 'Item One', $oDictionary.Item('One'), 2) ; Set an item $oDictionary.Item('One') = 'Changed' ; Display item MsgBox(0x20, 'Did Item One Change?', $oDictionary.Item('One'), 3) ; Remove key $oDictionary.Remove('One') ; $oDictionary.Key ('Two') = 'Bike' EndIf Sleep(1000) ; Store items into a variable For $vKey In $oDictionary $sItem &= $oDictionary.Item($vKey) & @CRLF Next ; Display items MsgBox(0x0, 'Items Count: ' & $oDictionary.Count, $sItem, 3) ; Add items into an array $aItems = $oDictionary.Items ; Display items in the array For $i = 0 To $oDictionary.Count -1 MsgBox(0x0, 'Items [ ' & $i & ' ]', $aItems[$i], 2) Next Sleep(1000) ; Add keys into an array $aKeys = $oDictionary.Keys ; Display keys in the array For $i = 0 To $oDictionary.Count -1 MsgBox(0x0, 'Keys [ ' & $i & ' ]', $aKeys[$i], 2) Next EndIf
    1 point
×
×
  • Create New...