Ausgabe einer Variable als String, rekursiv für Arrays/Maps/DllStructs/... mit der _toString Methode.
Es gibt zwei Modi, einmal wird nur der Inhalt der Variablen ausgegeben, im anderen auch Details wie der Typ. Die Einrückung/Formatierung kann ebenfalls mit Parametern angepasst werden.
Leichteres behandeln von Errorcodes mit der _errCheck Methode, sodass nur noch diese Funktion aufgerufen werden muss und der Fehler automatisch in die Konsole geschrieben wird, ohne ständiges If @error,... .
AutoIt
#include-once
; ==> Beispiel
ConsoleWrite("----------------- String Output -----------------"&@crlf)
Local $mData[]
Local $ar1D = [1, 2, 3, 4]
$mData.arr1D = $ar1D
Local $ar2D = [[1, 2], [3, 4]]
$mData.arr2D = $ar2D
Local $ar3D = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
$mData.arr3D = $ar3D
$mData.binary = Binary(0xFF2312302343221)
$mData.bool = True
Local $tStruct = DllStructCreate("struct;int var1;byte var2;uint var3;char var4[128];endstruct")
DllStructSetData($tStruct, "var1", -1)
DllStructSetData($tStruct, 2, 255)
DllStructSetData($tStruct, "var3", -1)
DllStructSetData($tStruct, "var4", "Hello")
$mData.dllStruct = $tStruct
$mData.func = _toString
$mData.float = 0.341234
$mData.hwnd = WinGetHandle("[ACTIVE]")
$mData.int = 3213
$mData.objDictionary = ObjCreate('Scripting.Dictionary')
$mData.objDictionary.add("1", "eins")
$mData.objDictionary.add(2, "zwei")
$mData.object = ObjCreate("MediaPlayer.MediaPlayer.1")
$mData.pointer = DllStructGetPtr($tStruct)
$mData.string = "Something"
ConsoleWrite(_toString($mData, True)&@crlf)
ConsoleWrite("----------------- Error Handling -----------------"&@crlf)
StringRegExp("error", "\d+", 2)
_errCheck()
StringRegExp("error", "\d+", 2)
_errCheck("Error using StringRegExp")
ConsoleWrite(_errorFunc()&@crlf)
StringRegExp("Fatal Error", "\d+", 2)
_errCheck("Fatal RegExp Error", True)
ConsoleWrite("Well, we will not be getting here :("&@crlf)
Func _errorFunc()
If True Then return _errCheck("Error to be forwarded", False, 10, 1, 5)
EndFunc
Func _exit()
ConsoleWrite("Exit"&@crlf)
Exit
EndFunc
; #FUNCTION# =======================================================================================
; Name ..........: _toString
; Description ...: Returns the content of a variable as String
; Syntax ........: _toString($data, $bInformative = True, $sPrefixSep = @TAB, $sSpaceOuter = @crlf, $sSpaceInner = @crlf, $iDepth = 0)
; Parameters ....: $data - The variable to be converted.
; $bInformative - The level of detail. When True, the type of all variables is returned and not only the content
; $sPrefixSep - The prefix value used for indentation
; $sSpaceOuter - The space between levels of dimensions (Array) or at the beginning of a Map/DllStruct
; $sSpaceInner - The space between items of an Array/Map/DllStruct
; $iDepth - INTERNAL: The depth level of the call. Used in recursive calls to indent items
; Return values .: String
; Author ........: Kanashius
; ==================================================================================================
Func _toString(ByRef $data, $bInformative = True, $sPrefixSep = @TAB, $sSpaceOuter = @crlf, $sSpaceInner = @crlf, $iDepth = 0)
Local $sPrefix = ""
For $i=0 to $iDepth-1
$sPrefix &= $sPrefixSep
Next
If IsArray($data) Then
Local $bPrefix = False
If StringInStr($sSpaceOuter, @crlf) or StringInStr($sSpaceInner, @crlf) Then
$bPrefix = True
Else
$sPrefixSep = ""
EndIf
Local $iDimensions = UBound($data, 0)
Local $sDim = ""
For $i = 1 to $iDimensions Step 1
$sDim &= "["&UBound($data, $i)&"]"
Next
Local $sOut = $sPrefix & ($bInformative ? "Array"&$sDim&" [" : "[") & $sSpaceOuter
For $i=0 To UBound($data, 1)-1 Step 1
If $iDimensions<2 Then
$sOut &= _toString($data[$i], $bInformative, $sPrefixSep, $sSpaceOuter, $sSpaceInner, $iDepth+1) & (($i<>UBound($data, 1)-1)?"," & $sSpaceInner:$sSpaceOuter)
Else
$sOut &= ($bPrefix?$sPrefix & $sPrefixSep:"") & "[" & $sSpaceOuter
For $j=0 To UBound($data, 2)-1 Step 1
If $iDimensions<3 Then
$sOut &= _toString($data[$i][$j], $bInformative, $sPrefixSep, $sSpaceOuter, $sSpaceInner, $iDepth+2) & (($j<>UBound($data, 2)-1)?"," & $sSpaceInner:$sSpaceOuter)
Else
$sOut &= ($bPrefix?$sPrefix & $sPrefixSep & $sPrefixSep:"") & "[" & $sSpaceOuter
For $k=0 To UBound($data, 3)-1 Step 1
$sOut &= _toString($data[$i][$j][$k], $bInformative, $sPrefixSep, $sSpaceOuter, $sSpaceInner, $iDepth+3) & (($k<>UBound($data, 3)-1)?"," & $sSpaceInner:$sSpaceOuter)
Next
$sOut &= ($bPrefix?$sPrefix & $sPrefixSep & $sPrefixSep:"") & "]" & (($j<>UBound($data, 2)-1)?"," & $sSpaceInner:"") & $sSpaceOuter
EndIf
Next
$sOut &= ($bPrefix?$sPrefix & $sPrefixSep:"") & "]" & (($i<>UBound($data, 1)-1)?"," & $sSpaceInner:$sSpaceOuter)
EndIf
Next
$sOut &= ($bPrefix?$sPrefix:"") & "]"
return $sOut
ElseIf IsBinary($data) Then
return $sPrefix & ($bInformative ? 'binary "'&String($data)&'"' : String($data))
ElseIf IsBool($data) Then
return $sPrefix & ($bInformative ? 'bool "'&StringLower(String($data))&'"' : StringLower(String($data)))
ElseIf IsDllStruct($data) Then
Local $bPrefix = StringInStr($sSpaceInner, @crlf)
Local $sOut = $sPrefix & ($bInformative ? "DllStruct[b"&DllStructGetSize($data)&"]{" : "{") & $sSpaceOuter
Local $iCount = 1
While True
Local $oTmp = DllStructGetData($data, $iCount)
If @error Then ExitLoop
$sOut &= _toString($oTmp, $bInformative, $sPrefixSep, $sSpaceOuter, $sSpaceInner, $iDepth+1)
$iCount += 1
$sOut &= "," & $sSpaceInner
WEnd
$sOut &= ($bPrefix?$sPrefix:"") & "}"
return $sOut
ElseIf IsFunc($data) Then
return $sPrefix & ($bInformative ? 'function "'&FuncName($data)&'"' : FuncName($data))
ElseIf IsFloat($data) Then
return $sPrefix & ($bInformative ? 'float "'&String($data)&'"' : String($data))
ElseIf IsHWnd($data) Then
return $sPrefix & ($bInformative ? 'hwnd "'&String($data)&'"' : String($data))
ElseIf IsInt($data) Then
return $sPrefix & ($bInformative ? 'int "'&String($data)&'"' : String($data))
ElseIf IsKeyword($data) Then
If $data=Default Then
return $sPrefix & ($bInformative ? 'keyword "Default"' : "default")
ElseIf $data=Null Then
return $sPrefix & ($bInformative ? 'keyword "Null"' : "null")
EndIf
ElseIf IsMap($data) Then
Local $sOut = $sPrefix & ($bInformative ? "Map["&UBound($data)&"] {" : "{") & $sSpaceOuter
For $key in MapKeys($data)
$sOut &= _toString($key, $bInformative, $sPrefixSep, $sSpaceOuter, $sSpaceInner, $iDepth+1) & ": " & $sSpaceOuter & _toString($data[$key], $bInformative, $sPrefixSep, $sSpaceOuter, $sSpaceInner, $iDepth+2)
$sOut &= "," & $sSpaceInner
Next
$sOut &= $sPrefix & "}"
return $sOut
ElseIf IsNumber($data) Then
return $sPrefix & ($bInformative ? 'number "'&String($data)&'"' : String($data))
ElseIf IsObj($data) Then
If ObjName($data, 1) = "Dictionary" Then
_hasErrorOccurred(False)
Local $oErrHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
Local $iCount = $data.count, $arKeys = $data.keys(), $test = $data.item("")
If Not _hasErrorOccurred() Then
Local $sOut = $sPrefix & ($bInformative ? 'Object "'&ObjName($data, 1)&""" Description: """&ObjName($data, 2)&'" ' : ObjName($data, 1)) & "["&$iCount&"]" & "{" &$sSpaceOuter
For $key in $arKeys
Local $item = $data.item($key)
$sOut &= _toString($key, $bInformative, $sPrefixSep, $sSpaceOuter, $sSpaceInner, $iDepth+1) & ": " & $sSpaceOuter & _toString($item, $bInformative, $sPrefixSep, $sSpaceOuter, $sSpaceInner, $iDepth+2)
$sOut &= "," & $sSpaceInner
Next
$sOut &= $sPrefix & "}"
return $sOut
EndIf
EndIf
return $sPrefix & ($bInformative ? 'Object "'&ObjName($data, 1)&""" Description: """&ObjName($data, 2)&'"' : ObjName($data, 1))
ElseIf IsPtr($data) Then
return $sPrefix & ($bInformative ? 'pointer "'&String($data)&'"' : String($data))
ElseIf IsString($data) Then
return $sPrefix & ($bInformative ? 'string "'&$data&'"' : '"'&$data&'"')
Else
return $sPrefix & ($bInformative ? 'unsupported ('&VarGetType($data)&') "'&String($data)&'"' : String($data))
EndIf
EndFunc
Func _ErrFunc($oError)
_hasErrorOccurred(True)
EndFunc
Func _hasErrorOccurred($bError = Default)
Local static $bCurrentError = False
If $bError<>Default Then
$bCurrentError = $bError
EndIf
return $bCurrentError
EndFunc
; #FUNCTION# =======================================================================================
; Name ..........: _errCheck
; Description ...: Checks if the @error is set (function call before this returned an error) and writes the error to the console.
; Syntax ........: _errCheck($sMsg = Default, $bFatal = False, $return = -1, $iError = @error, $iExtended = @extended, $sExitCallback = "_exit", $iLine = @ScriptLineNumber)
; Parameters ....: $sMsg - The message to show in case of an error
; $bFatal - If True, the $sExitCallback will be called to exit the application gracefully
; $return - The value to be returned by this function
; $iError - The @error code
; $iExtended - The @extended code
; $sExitCallback - The function to call on exit
; $iLine - The ScriptLineNumber, where the error occurred
; Return values .: The value specified as $return and it will throw the same @error and @extended codes, the function was called with.
; Author ........: Kanashius
; ==================================================================================================
Func _errCheck($sMsg = Default, $bFatal = False, $return = -1, $iError = @error, $iExtended = @extended, $sExitCallback = "_exit", $iLine = @ScriptLineNumber)
$sMsg = $sMsg<>Default ? " >> "&$sMsg:""
Local $sExtended = $iExtended<>0 ? ":"&$iExtended:""
If $iError<>0 Then ConsoleWrite("Error (line "&$iLine&"): "&$iError&$sExtended&$sMsg&@crlf)
If $bFatal Then
Call($sExitCallback)
If @error Then _errCheck("Error using the ""Call"" function, while handling another error: "&@crlf&@Tab&"Error (line "&$iLine&"): "&$iError&$sExtended&$sMsg&@crlf& _
"Check if the function """&$sExitCallback&""" (used for $bFatal errors) exists and has no parameters!"&@crlf)
EndIf
Return SetError(@error, @extended, $return)
EndFunc
Alles anzeigen
Ich hoffe, ihr könnt das (gerade zum Debuggen) gut gebrauchen
Output:
Code
----------------- String Output -----------------
Map[14] {
string "arr1D":
Array[4] [
int "1",
int "2",
int "3",
int "4"
],
string "arr2D":
Array[2][2] [
[
int "1",
int "2"
],
[
int "3",
int "4"
]
],
string "arr3D":
Array[2][2][2] [
[
[
int "1",
int "2"
],
[
int "3",
int "4"
]
],
[
[
int "5",
int "6"
],
[
int "7",
int "8"
]
]
],
string "binary":
binary "0x213234022331F20F",
string "bool":
bool "true",
string "dllStruct":
DllStruct[b140]{
int "-1",
int "255",
int "4294967295",
string "Hello",
},
string "func":
function "_TOSTRING",
string "float":
float "0.341234",
string "hwnd":
hwnd "0x00562EC4",
string "int":
int "3213",
string "objDictionary":
Object "Dictionary" Description: "Scripting.Dictionary" [2]{
string "1":
string "eins",
int "2":
string "zwei",
},
string "object":
Object "MediaPlayer" Description: "Windows Media Player",
string "pointer":
pointer "0x015FDDA0",
string "string":
string "Something",
}
----------------- Error Handling -----------------
Error (line 34): 1
Error (line 37): 1 >> Error using StringRegExp
Error (line 47): 1:5 >> Error to be forwarded
10
Error (line 42): 1 >> Fatal RegExp Error
Exit
Alles anzeigen