Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 12/27/2018 in all areas

  1. mmm... don't think that is possible yet. Maybe we should add a recursive check to Json_ObjGet() and Json_ObjExists() like this: #include <Json.au3> $sJson = '{"d": {"5": {"4": {"6": {"h": [{"id": "1286","status": "sunny","earth": true}]}}}}}' Json_dump($sJson) $oTemp = Json_Decode($sJson) If Json_IsObject($oTemp) Then ConsoleWrite('@@ Json_ObjExists($oTemp, "d") = ' & Json_ObjExistsR($oTemp, "d") & @CRLF) ;### Debug Console ConsoleWrite('@@ Json_ObjExists($oTemp, "d.5") = ' & Json_ObjExistsR($oTemp, "d.5") & @CRLF) ;### Debug Console ConsoleWrite('@@ Json_ObjExists($oTemp, "d.5.4") = ' & Json_ObjExistsR($oTemp, "d.5.4") & @CRLF) ;### Debug Console ConsoleWrite('@@ Json_ObjExists($oTemp, "d.5.4.6") = ' & Json_ObjExistsR($oTemp, "d.5.4.6") & @CRLF) ;### Debug Console ConsoleWrite('@@ Json_ObjExists($oTemp, "d.5.4.6.h") = ' & Json_ObjExistsR($oTemp, "d.5.4.6.h") & @CRLF) ;### Debug Console ConsoleWrite('@@ Json_ObjExists($oTemp, "d.5.4.6.x") = ' & Json_ObjExistsR($oTemp, "d.5.4.6.x") & @CRLF) ;### Debug Console $oTempb = Json_ObjGetR($oTemp, "d.5.4.6.h") ConsoleWrite('@@ json_get($oTempb,''[0].id'') = ' & json_get($oTempb, '[0].id') & ' >Error code: ' & @error & @CRLF) ;### Debug Console $oTempb = Json_ObjGetR($oTemp, "d.5.4.6.x") ConsoleWrite('@@ json_get($oTempb,''[0].id'') = ' & json_get($oTempb, '[0].id') & ' >Error code: ' & @error & @CRLF) ;### Debug Console EndIf Func Json_ObjExistsR(ByRef $Object, $Key) Local $DynObject = $Object Local $Keys = StringSplit($Key, ".") For $x = 1 To $Keys[0] If $DynObject.Exists($Keys[$x]) Then If $x = $Keys[0] Then Return True Else $DynObject = Json_ObjGet($DynObject, $Keys[$x]) EndIf Else Return False EndIf Next Return $Object.Exists(String($Key)) EndFunc ;==>Json_ObjExistsR Func Json_ObjGetR(ByRef $Object, $Key) Local $DynObject = $Object Local $Keys = StringSplit($Key, ".") For $x = 1 To $Keys[0] If $DynObject.Exists($Keys[$x]) Then If $x = $Keys[0] Then Return $DynObject.Item($Keys[$x]) Else $DynObject = Json_ObjGet($DynObject, $Keys[$x]) EndIf EndIf Next Return SetError(1, 0, '') EndFunc ;==>Json_ObjGetR Thoughts? Jos
    2 points
  2. Introduction JSON (Javascript Object Notation) is a popular data-interchange format and supported by a lot of script languages. On AutoIt, there is already a >JSON UDF written by Gabriel Boehme. It is good but too slow, and not supports unicode and control characters very well. So I write a new one (and of course, fast one as usual). I use a machine code version of JSON parser called "jsmn". jsmn not only supports standard JSON, but also accepts some non-strict JSON string. See below for example. Important Update!! I rename the library from jsmn.au3 to json.au3. All function names are changed, too. Decoding Function Json_Decode($Json) $Json can be a standard or non-standard JSON string. For example, it accepts: { server: example.com port: 80 message: "this looks like a config file" } The most JSON data type will be decoded into corresponding AutoIt variable, including 1D array, string, number, true, false, and null. JSON object will be decoded into "Windows Scripting Dictionary Object" retuned from ObjCreate("Scripting.Dictionary"). AutoIt build-in functions like IsArray, IsBool, etc. can be used to check the returned data type. But for Object and Null, Json_IsObject() and Json_IsNull() should be used. If the input JSON string is invalid, @Error will be set to $JSMN_ERROR_INVAL. And if the input JSON string is not finish (maybe read from stream?), @Error will be set to $JSMN_ERROR_PART. Encoding Function Json_Encode($Data, $Option = 0, $Indent = "\t", $ArraySep = ",\r\n", $ObjectSep = ",\r\n", $ColonSep = ": ") $Data can be a string, number, bool, keyword(default or null), 1D arrry, or "Scripting.Dictionary" COM object. Ptr will be converted to number, Binary will be converted to string in UTF8 encoding. Other unsupported types like 2D array, dllstruct or object will be encoded into null. $Option is bitmask consisting following constant: $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) $JSON_UNESCAPED_UNICODE ; Encode multibyte Unicode characters literally $JSON_UNESCAPED_SLASHES ; Don't escape / $JSON_HEX_TAG ; All < and > are converted to \u003C and \u003E $JSON_HEX_AMP ; All &amp;amp;amp;amp;s are converted to \u0026 $JSON_HEX_APOS ; All ' are converted to \u0027 $JSON_HEX_QUOT ; All " are converted to \u0022 $JSON_PRETTY_PRINT ; Use whitespace in returned data to format it $JSON_STRICT_PRINT ; Make sure returned JSON string is RFC4627 compliant $JSON_UNQUOTED_STRING ; Output unquoted string if possible (conflicting with $JSMN_STRICT_PRINT) Most encoding option have the same means like PHP's json_enocde() function. When $JSON_PRETTY_PRINT is set, output format can be change by other 4 parameters ($Indent, $ArraySep, $ObjectSep, and $ColonSep). Because these 4 output format parameters will be checked inside Jsmn_Encode() function, returned string will be always accepted by Jsmn_Decode(). $JSON_UNQUOTED_STRING can be used to output unquoted string that also accetped by Jsmn_Decode(). $JSON_STRICT_PRINT is used to check output format setting and avoid non-standard JSON output. So this option is conflicting with $JSON_UNQUOTED_STRING. Get and Put Functions Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False) Json_Get(ByRef $Var, $Notation) These functions helps user to access object or array more easily. Both dot notation and square bracket notation can be supported. Json_Put() by default will create non-exists objects and arrays. For example: Local $Obj Json_Put($Obj, ".foo", "foo") Json_Put($Obj, ".bar[0]", "bar") Json_Put($Obj, ".test[1].foo.bar[2].foo.bar", "Test") Local $Test = Json_Get($Obj, '["test"][1]["foo"]["bar"][2]["foo"]["bar"]') ; "Test" Object Help Functions Json_ObjCreate() Json_ObjPut(ByRef $Object, $Key, $Value) Json_ObjGet(ByRef $Object, $Key) Json_ObjDelete(ByRef $Object, $Key) Json_ObjExists(ByRef $Object, $Key) Json_ObjGetCount(ByRef $Object) Json_ObjGetKeys(ByRef $Object) Json_ObjClear(ByRef $Object) These functions are just warps of "Scripting.Dictionary" COM object. You can use these functions if you are not already familiar with it. == Update 2013/05/19 == * Add Jsmn_Encode() option "$JSMN_UNESCAPED_ASCII". Now the default output of Json_Encode() is exactly the same as PHP's json_encode() function (for example, chr(1) will be encoded into u0001). $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) == Update 2015/01/08 == * Rename the library from jsmn.au3 to json.au3. All function names are changed, too. * Add Json_Put() and Json_Get() * Add Null support * Using BinaryCall.au3 to loading the machine code. == Update 2018/01/13== (Jos) * Add JsonDump() to list all Json Keys and their values to easily figure out what they are. == Update 2018/10/01== (Jos) * Fixed JsonDump() some fields and values were not showing as discussed here - tnx @TheXman . == Update 2018/10/01b== (Jos) * Added Json_ObjGetItems, Tidied source and fixed au3check warnings - tnx @TheXman . == Update 2018/10/28== (Jos) * Added declaration for $value to avoid au3check warning - tnx @DerPensionist == Update 2018/12/16== (Jos) * Added another declaration for $value to avoid au3check warning and updated the version at the top - tnx @maniootek == Update 2018/12/29== (Jos) * Changed Json_ObjGet() and Json_ObjExists() to allow for multilevel object in string. == Update 2019/01/17== (Jos) * Added support for DOT notation in JSON functions. == Update 2019/07/15== (Jos) * Added support for reading keys with a dot inside when using a dot as separator (updated) == Update 2021/11/18== (TheXman) * Update details in below post: == Update 2021/11/20== (TheXman) * Minor RegEx update, no change to the functionality or result._Json(2021.11.20).zip
    1 point
  3. mLipok

    Chilkat.au3 UDF

    This is modest beginning for UDF Chilkat component: https://www.chilkatsoft.com/downloads_ActiveX.asp Some of AcitveX object bundled into this component (dll file) are Free (you can verify it here), some other are commercial. You can downolad this UDF here: ; #INDEX# ======================================================================== ; Title .........: Chilkat.au3 ; AutoIt Version : 3.3.10.2++ ; Language ......: English ; Description ...: A collection of Function for use with Chilkat component ; Author ........: mLipok ; Modified ......: ; URL ...........: https://www.chilkatsoft.com/refdoc/activex.asp ; URL ...........: https://www.chilkatsoft.com/downloads_ActiveX.asp ; Date ..........: 2017/02/01 ; Version .......: 0.1.1 BETA - Work in progress ; ================================================================================ #cs 2017/02/01 . First official version - mLipok #ce http://www.chilkatforum.com/questions/1306/is-the-chilkat-upload-component-freeware Examples: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7 ;~ #AutoIt3Wrapper_Run_Debug_Mode=Y #Tidy_Parameters=/sort_funcs /reel #include <Array.au3> #include <StringConstants.au3> #include "Chilkat.au3" _Example() Func _Example() _ErrorLog_ChilkatWrapper(ConsoleWrite) _Chilkat_StartUp(@ScriptDir & '\ChilkatAx-9.5.0-win32.dll') _Chilkat_DllVersion($CHILKATOBJ_VERSION_950) Local $oGLOBAL = _Chilkat_GLOBAL_ObjCreate() If @error Then MsgBox($MB_ICONERROR, 'GLOBAL Declartion', '@error = ' & @error & @CRLF & '@extended = ' & @extended) _Chilkat_GLOBAL_UnlockBundle($oGLOBAL, "Anything for 30-day trial") If @error Then MsgBox($MB_ICONERROR, 'UnlockBundle', '@error = ' & @error & @CRLF & '@extended = ' & @extended) ; Just comment / uncomment one of this followin Examples functions: ;~ _Example_12_JsonObject_PrettyPrint() ;~ _Example_13_JsonObject_IterateMembers() ;~ _Example_14_JsonObject_CreateDocument() ;~ _Example_15_CSV_ColumnNames() ;~ _Example_16_CSV_ToArray() _Example_17_CSV_ToArray('(?i).*?Pierwszy.*?Urz.*?Skarb.*?', '(?i).*Katowice.*?') EndFunc ;==>_Example Func _Example_12_JsonObject_PrettyPrint() _Log_ChilkatExample('_Example_12_JsonObject_PrettyPrint') ; https://www.example-code.com/vbscript/json_pretty_print.asp Local $oJSON = _Chilkat_JSON_ObjCreate() If @error Then Return SetError(@error, @extended, $CHILKAT_RET_FAILURE) Local $sJSON_String = "{""name"": ""donut"",""image"":{""fname"": ""donut.jpg"",""w"": 200,""h"": 200},""thumbnail"":{""fname"": ""donutThumb.jpg"",""w"": 32,""h"": 32}}" Local $iSuccess = $oJSON.Load($sJSON_String) If ($iSuccess <> 1) Then ConsoleWrite($oJSON.LastErrorText & @CRLF) Return SetError(@error, @extended, $CHILKAT_RET_FAILURE) EndIf ; To pretty-print, set the EmitCompact property equal to 0 $oJSON.EmitCompact = 0 ; If bare-LF line endings are desired, turn off EmitCrLf ; Otherwise CRLF line endings are emitted. $oJSON.EmitCrLf = 0 ; Emit the formatted JSON: ConsoleWrite($oJSON.Emit() & @CRLF) EndFunc ;==>_Example_12_JsonObject_PrettyPrint Func _Example_13_JsonObject_IterateMembers() _Log_ChilkatExample('_Example_13_JsonObject_IterateMembers') ; https://www.example-code.com/vbscript/json_iterate_members.asp Local $oJSON = _Chilkat_JSON_ObjCreate() If @error Then Return SetError(@error, @extended, $CHILKAT_RET_FAILURE) Local $sJSON_String = "{ ""id"": 1, ""name"": ""A green door"", ""tags"": [""home"", ""green""], ""price"": 125 }" Local $iSuccess = $oJSON.Load($sJSON_String) If ($iSuccess <> 1) Then ConsoleWrite($oJSON.LastErrorText & @CRLF) Return SetError($CHILKAT_ERR_LOAD, @extended, $CHILKAT_RET_FAILURE) EndIf Local $sName = '', $sValue = '' Local $iNumMembers = $oJSON.Size, $iValue = 0 For $iMember_idx = 0 To $iNumMembers - 1 $sName = $oJSON.NameAt($iMember_idx) $sValue = $oJSON.StringAt($iMember_idx) ConsoleWrite($sName & ": " & $sValue & @CRLF) $iValue = $oJSON.IntAt($iMember_idx) ConsoleWrite($sName & " as integer: " & $iValue & @CRLF) Next EndFunc ;==>_Example_13_JsonObject_IterateMembers Func _Example_14_JsonObject_CreateDocument() _Log_ChilkatExample('_Example_14_JsonObject_CreateDocument') ; https://www.example-code.com/vbscript/create_json.asp Local $oJSON = _Chilkat_JSON_ObjCreate() If @error Then Return SetError(@error, @extended, $CHILKAT_RET_FAILURE) Local $iSuccess $iSuccess = $oJSON.AddStringAt(-1, "Title", "Pan's Labyrinth") ConsoleWrite("- $iSuccess=" & $iSuccess & @CRLF) $iSuccess = $oJSON.AddStringAt(-1, "Director", "Guillermo del Toro") ConsoleWrite("- $iSuccess=" & $iSuccess & @CRLF) $iSuccess = $oJSON.AddStringAt(-1, "Original_Title", "El laberinto del fauno") ConsoleWrite("- $iSuccess=" & $iSuccess & @CRLF) $iSuccess = $oJSON.AddIntAt(-1, "Year_Released", 2006) ConsoleWrite("- $iSuccess=" & $iSuccess & @CRLF) $oJSON.EmitCompact = 0 ConsoleWrite($oJSON.Emit() & @CRLF) EndFunc ;==>_Example_14_JsonObject_CreateDocument Func _Example_15_CSV_ColumnNames() _Log_ChilkatExample('_Example_15_CSV_ColumnNames') ; https://www.example-code.com/vbscript/csv_columnNames.asp Local $sURL = 'http://www.chilkatsoft.com/testData/sample.csv' InetGet($sURL, @ScriptDir & '\sample.csv') Local $oCSV = _Chilkat_CSV_ObjCreate() If @error Then Return SetError(@error, @extended, $CHILKAT_RET_FAILURE) ; Prior to loading the CSV file, indicate that the 1st row ; should be treated as column names: $oCSV.HasColumnNames = 1 ; Load the $oCSV records from the file: Local $iSuccess = $oCSV.LoadFile(@ScriptDir & "\sample.csv") If ($iSuccess <> 1) Then ConsoleWrite($oCSV.LastErrorText & @CRLF) Return SetError($CHILKAT_ERR_LOADFILE, @extended, $CHILKAT_RET_FAILURE) EndIf Local $iIndex = 0 Local $sColName = '' ; Display the column names: For $iCol_idx = 0 To $oCSV.NumColumns - 1 $sColName = $oCSV.GetColumnName($iCol_idx) ConsoleWrite($iCol_idx & ": " & $sColName & @CRLF) ; The following line demonstrates to to get the column ; index given a column name: $iIndex = $oCSV.GetIndex($sColName) ConsoleWrite($sColName & " is at column index " & $iIndex & @CRLF) Next EndFunc ;==>_Example_15_CSV_ColumnNames Func _Example_16_CSV_ToArray() _Log_ChilkatExample('_Example_16_CSV_ToArray') Local $sURL = 'http://www.chilkatsoft.com/testData/sample.csv' InetGet($sURL, @ScriptDir & '\sample.csv') Local $oCSV = _Chilkat_CSV_ObjCreate() If @error Then Return SetError(@error, @extended, $CHILKAT_RET_FAILURE) ; Prior to loading the CSV file, indicate that the 1st row ; should be treated as column names: $oCSV.HasColumnNames = 1 ; Load the $oCSV records from the file: Local $iSuccess = $oCSV.LoadFile(@ScriptDir & "\sample.csv") If ($iSuccess <> 1) Then ConsoleWrite($oCSV.LastErrorText & @CRLF) Return SetError($CHILKAT_ERR_LOADFILE, @extended, $CHILKAT_RET_FAILURE) EndIf Local $iNumColumns = $oCSV.NumColumns Local $iNumRows = $oCSV.NumRows Local $aResult[$iNumRows + 1][$iNumColumns] ; First get the column names For $iCol_idx = 0 To $iNumColumns - 1 $aResult[0][$iCol_idx] = $oCSV.GetColumnName($iCol_idx) Next For $iRow_idx = 0 To $iNumRows - 1 For $iCol_idx = 0 To $iNumColumns - 1 $aResult[$iRow_idx + 1][$iCol_idx] = $oCSV.GetCell($iRow_idx, $iCol_idx) Next Next _ArrayDisplay($aResult, '$aResult') EndFunc ;==>_Example_16_CSV_ToArray Func _Example_17_CSV_ToArray($sName_RegExpPattern, $sCity_RegExpPattern) _Log_ChilkatExample('_Example_16_CSV_ToArray') ; https://www.example-code.com/vbscript/csv_columnNames.asp Local $sURL = 'https://epuap.gov.pl/wps/wcm/connect/61e062b9-d981-4526-9f63-c2569263775a/RESP_2016-10-07.csv?MOD=AJPERES' InetGet($sURL, @ScriptDir & '\ePUAP.csv') ConsoleWrite("- Download completed" & @CRLF) Local $oCSV = _Chilkat_CSV_ObjCreate() If @error Then Return SetError(@error, @extended, $CHILKAT_RET_FAILURE) ; Prior to loading the CSV file, indicate that the 1st row ; should be treated as column names: $oCSV.HasColumnNames = 1 ; Load the $oCSV records from the file: Local $sCSVContent = FileRead(@ScriptDir & "\ePUAP.csv") ConsoleWrite("- Load File completed" & @CRLF) Local $iSuccess = $oCSV.LoadFromString($sCSVContent) If ($iSuccess <> 1) Then ConsoleWrite($oCSV.LastErrorText & @CRLF) Return SetError($CHILKAT_ERR_LOADFILE, @extended, $CHILKAT_RET_FAILURE) EndIf ConsoleWrite("- LoadFromString completed" & @CRLF) Local $iNumColumns = $oCSV.NumColumns Local $iNumRows = $oCSV.NumRows Local $aResult[$iNumRows + 1][$iNumColumns] ; First get the column names For $iCol_idx = 0 To $iNumColumns - 1 $aResult[0][$iCol_idx] = $oCSV.GetColumnName($iCol_idx) Next Local $iResult_RowCount = 1 ; First is ColumnName For $iRow_idx = 0 To $iNumRows - 1 If _ StringRegExp($oCSV.GetCell($iRow_idx, 0), $sName_RegExpPattern, $STR_REGEXPMATCH) _ And _ StringRegExp($oCSV.GetCell($iRow_idx, 4), $sCity_RegExpPattern, $STR_REGEXPMATCH) _ Then For $iCol_idx = 0 To $iNumColumns - 1 $aResult[$iResult_RowCount][$iCol_idx] = $oCSV.GetCell($iRow_idx, $iCol_idx) Next $iResult_RowCount += 1 EndIf Next ; strip array size to Number of founds rows ReDim $aResult[$iResult_RowCount][$iNumColumns] ConsoleWrite("- Parsing completed" & @CRLF) _ArrayDisplay($aResult, '$aResult') EndFunc ;==>_Example_17_CSV_ToArray Func _Log_ChilkatExample($sData) ConsoleWrite('>+ ' & $sData & @CRLF) EndFunc ;==>_Log_ChilkatExample REMARK: This UDF is a continuation of this thread:
    1 point
  4. 1 point
  5. Use GUICtrlSetData($input1, StringReplace($Readfloor1, @CRLF, "|"),1)
    1 point
  6. Makes sense as you are testing for the success of the Local $Find = $sAnswer line, not the success of the InputBox() statement. Jos
    1 point
  7. I think you are the one being rude here and advice to change the tone you are singing when you want help from anybody around here. Jos
    1 point
  8. jchd

    Unicode point

    I followed your Msft link, hit "how to get the font" then installed MDL2 as usual from the zip file offered.
    1 point
  9. trancexx

    WinHTTP functions

    Yes thanks. But the only valid warning is with unreferenced parameter inside __WinHttpFormUpload. I'll fix that.
    1 point
  10. Thanks for the report ... I've update the file and made a new update available in the first post. Jos
    1 point
  11. Jos

    Get data from json

    One other update to fix an bug in case the JSON data starts with an Array: $debug = 0 #include <Inet.au3> #include <json.au3> $data = FileRead("demo_json4.htm") Global $ObjLevel = -1 ;~ Global $ObjPath = "" Global $object = Json_Decode($data) Json_Dump($data) ;[0].id Func Json_Dump($Json, $InitTokenCount = 1000) Static $Jsmn_Init = __Jsmn_RuntimeLoader("jsmn_init"), $Jsmn_Parse = __Jsmn_RuntimeLoader("jsmn_parse") If $Json = "" Then $Json = '""' Local $TokenList, $Ret Local $Parser = DllStructCreate("uint pos;int toknext;int toksuper") Do DllCallAddress("none:cdecl", $Jsmn_Init, "ptr", DllStructGetPtr($Parser)) $TokenList = DllStructCreate("byte[" & ($InitTokenCount * 20) & "]") $Ret = DllCallAddress("int:cdecl", $Jsmn_Parse, "ptr", DllStructGetPtr($Parser), "wstr", $Json, "ptr", DllStructGetPtr($TokenList), "uint", $InitTokenCount) $InitTokenCount *= 2 Until $Ret[0] <> $JSMN_ERROR_NOMEM Local $Next = 0 _Json_TokenDump($Json, DllStructGetPtr($TokenList), $Next) EndFunc ;==>Json_Dump Func _Json_TokenDump(ByRef $Json, $Ptr, ByRef $Next, $ObjPath = "") If $Next = -1 Then Return Null Local $Token = DllStructCreate("int;int;int;int", $Ptr + ($Next * 20)) Local $Type = DllStructGetData($Token, 1) Local $Start = DllStructGetData($Token, 2) Local $End = DllStructGetData($Token, 3) Local $Size = DllStructGetData($Token, 4) $Next += 1 If $Type = 0 And $Start = 0 And $End = 0 And $Size = 0 Then ; Null Item $Next = -1 Return Null EndIf Switch $Type Case 0 ; Json_PRIMITIVE Local $Primitive = StringMid($Json, $Start + 1, $End - $Start) Switch $Primitive Case "true" Return True Case "false" Return False Case "null" Return Null Case Else If StringRegExp($Primitive, "^[+\-0-9]") Then Return Number($Primitive) Else Return Json_StringDecode($Primitive) EndIf EndSwitch Case 1 ; Json_OBJECT $ObjLevel += 1 If $debug Then ConsoleWrite('###>- Start objectlevel=' & $ObjLevel & " $Next=" & $Next & " $ObjPath:" & $ObjPath & " Size:" & $Size & @CRLF) ;### Debug Console For $i = 0 To $Size - 1 Step 2 Local $Key = _Json_TokenDump($Json, $Ptr, $Next) $cObjPath = $ObjPath & "." & $Key If $debug Then ConsoleWrite('+>- Start objectlevel=' & $ObjLevel & " $Next=" & $Next & " $cObjPath:" & $cObjPath & " Size:" & $Size & @CRLF) ;### Debug Console Local $Value = _Json_TokenDump($Json, $Ptr, $Next, $ObjPath & "." & $Key) If $debug Then ConsoleWrite('-<- Return objectlevel=' & $ObjLevel & " $Next=" & $Next & " $cObjPath:" & $ObjPath & " Size:" & $Size & @CRLF) ;### Debug Console If Not ($Value = False) Then If Not IsString($Key) Then $Key = Json_Encode($Key) EndIf If $ObjLevel <= 0 Then $CurJSONKey = "." & $Key EndIf ; show the key and its value ConsoleWrite("+-> " & $cObjPath & ' =' & $Value & @CRLF) If json_get($object, $cObjPath) <> $Value Then ConsoleWrite("! Error JSONGET() different! " & $cObjPath & '=' & $Value & ' JSONGET("' & $cObjPath & '")=' & json_get($object, $cObjPath) & @CRLF) EndIf EndIf Next If $debug Then ConsoleWrite('###< End object ' & $Next & @CRLF) ;### Debug Console $ObjLevel -= 1 Return False Case 2 ; Json_ARRAY Local $sObjPath = $ObjPath If $debug Then ConsoleWrite('$$$> Start Json_ARRAY:' & $ObjPath & " Size:" & $Size & @CRLF) ;### Debug Console Local $Array[$Size] For $i = 0 To $Size - 1 $sObjPath = $ObjPath & "[" & $i & "]" If $debug Then ConsoleWrite('$$$> TokenDump _ARRAY:' & $sObjPath & " Size:" & $Size & @CRLF) ;### Debug Console $Value = _Json_TokenDump($Json, $Ptr, $Next, $sObjPath) If $Value <> False Then ; show the key and its value ConsoleWrite("+=> " & $sObjPath & "=>" & $Value & @CRLF) EndIf Next If $debug Then ConsoleWrite('$$$< End Json_ARRAY:' & $ObjPath & @CRLF) ;### Debug Console $ObjPath = $sObjPath Return False Case 3 ; Json_STRING Local $LastKey = Json_StringDecode(StringMid($Json, $Start + 1, $End - $Start)) If $debug Then ConsoleWrite("=>" & $ObjPath & "." & $LastKey & ' (JSON STRING)' & @CRLF) Return $LastKey EndSwitch EndFunc ;==>_Json_TokenDump Jos
    1 point
  12. When processing files, especially large ones, you will always lose battle if riding AutoIt. Not to mention RAM suffocation. So, what if I leave the whole process on a level that is few steps below me and just collect the cream? Script includes functions and small example: Opt("MustDeclareVars", 1) Global $sFile = FileOpenDialog("Choose file", "", "All files (*)") If @error Then Exit Global $hTimer, $iTimer, $sData ;------------------------------------------------------------------------ ; CRC32: $hTimer = TimerInit() $sData = _CRC32ForFile($sFile) $iTimer = TimerDiff($hTimer) ConsoleWrite("! CRC32 took " & $iTimer & " ms" & @CRLF) ConsoleWrite("Result: " & $sData & @CRLF & @CRLF) ;------------------------------------------------------------------------ ; MD4: $hTimer = TimerInit() $sData = _MD4ForFile($sFile) $iTimer = TimerDiff($hTimer) ConsoleWrite("! MD4 took " & $iTimer & " ms" & @CRLF) ConsoleWrite("Result: " & $sData & @CRLF & @CRLF) ;------------------------------------------------------------------------ ; MD5: $hTimer = TimerInit() $sData = _MD5ForFile($sFile) $iTimer = TimerDiff($hTimer) ConsoleWrite("! MD5 took " & $iTimer & " ms" & @CRLF) ConsoleWrite("Result: " & $sData & @CRLF & @CRLF) ;------------------------------------------------------------------------ ; SHA1: $hTimer = TimerInit() $sData = _SHA1ForFile($sFile) $iTimer = TimerDiff($hTimer) ConsoleWrite("! SHA1 took " & $iTimer & " ms" & @CRLF) ConsoleWrite("Result: " & $sData & @CRLF & @CRLF) ;------------------------------------------------------------------------ ; Functions... ; #FUNCTION# ;=============================================================================== ; ; Name...........: _CRC32ForFile ; Description ...: Calculates CRC32 value for the specific file. ; Syntax.........: _CRC32ForFile ($sFile) ; Parameters ....: $sFile - Full path to the file to process. ; Return values .: Success - Returns CRC32 value in form of hex string ; - Sets @error to 0 ; Failure - Returns empty string and sets @error: ; |1 - CreateFile function or call to it failed. ; |2 - CreateFileMapping function or call to it failed. ; |3 - MapViewOfFile function or call to it failed. ; |4 - RtlComputeCrc32 function or call to it failed. ; Author ........: trancexx ; ;========================================================================================== Func _CRC32ForFile($sFile) Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _ "wstr", $sFile, _ "dword", 0x80000000, _ ; GENERIC_READ "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE "ptr", 0, _ "dword", 3, _ ; OPEN_EXISTING "dword", 0, _ ; SECURITY_ANONYMOUS "ptr", 0) If @error Or $a_hCall[0] = -1 Then Return SetError(1, 0, "") EndIf Local $hFile = $a_hCall[0] $a_hCall = DllCall("kernel32.dll", "ptr", "CreateFileMappingW", _ "hwnd", $hFile, _ "dword", 0, _ ; default security descriptor "dword", 2, _ ; PAGE_READONLY "dword", 0, _ "dword", 0, _ "ptr", 0) If @error Or Not $a_hCall[0] Then DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Return SetError(2, 0, "") EndIf DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Local $hFileMappingObject = $a_hCall[0] $a_hCall = DllCall("kernel32.dll", "ptr", "MapViewOfFile", _ "hwnd", $hFileMappingObject, _ "dword", 4, _ ; FILE_MAP_READ "dword", 0, _ "dword", 0, _ "dword", 0) If @error Or Not $a_hCall[0] Then DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(3, 0, "") EndIf Local $pFile = $a_hCall[0] Local $iBufferSize = FileGetSize($sFile) Local $a_iCall = DllCall("ntdll.dll", "dword", "RtlComputeCrc32", _ "dword", 0, _ "ptr", $pFile, _ "int", $iBufferSize) If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(4, 0, "") EndIf DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Local $iCRC32 = $a_iCall[0] Return SetError(0, 0, Hex($iCRC32)) EndFunc ;==>_CRC32ForFile ; #FUNCTION# ;=============================================================================== ; ; Name...........: _MD4ForFile ; Description ...: Calculates MD4 value for the specific file. ; Syntax.........: _MD4ForFile ($sFile) ; Parameters ....: $sFile - Full path to the file to process. ; Return values .: Success - Returns MD4 value in form of hex string ; - Sets @error to 0 ; Failure - Returns empty string and sets @error: ; |1 - CreateFile function or call to it failed. ; |2 - CreateFileMapping function or call to it failed. ; |3 - MapViewOfFile function or call to it failed. ; |4 - MD4Init function or call to it failed. ; |5 - MD4Update function or call to it failed. ; |6 - MD4Final function or call to it failed. ; Author ........: trancexx ; ;========================================================================================== Func _MD4ForFile($sFile) Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _ "wstr", $sFile, _ "dword", 0x80000000, _ ; GENERIC_READ "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE "ptr", 0, _ "dword", 3, _ ; OPEN_EXISTING "dword", 0, _ ; SECURITY_ANONYMOUS "ptr", 0) If @error Or $a_hCall[0] = -1 Then Return SetError(1, 0, "") EndIf Local $hFile = $a_hCall[0] $a_hCall = DllCall("kernel32.dll", "ptr", "CreateFileMappingW", _ "hwnd", $hFile, _ "dword", 0, _ ; default security descriptor "dword", 2, _ ; PAGE_READONLY "dword", 0, _ "dword", 0, _ "ptr", 0) If @error Or Not $a_hCall[0] Then DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Return SetError(2, 0, "") EndIf DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Local $hFileMappingObject = $a_hCall[0] $a_hCall = DllCall("kernel32.dll", "ptr", "MapViewOfFile", _ "hwnd", $hFileMappingObject, _ "dword", 4, _ ; FILE_MAP_READ "dword", 0, _ "dword", 0, _ "dword", 0) If @error Or Not $a_hCall[0] Then DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(3, 0, "") EndIf Local $pFile = $a_hCall[0] Local $iBufferSize = FileGetSize($sFile) Local $tMD4_CTX = DllStructCreate("dword i[2];" & _ "dword buf[4];" & _ "ubyte in[64];" & _ "ubyte digest[16]") DllCall("advapi32.dll", "none", "MD4Init", "ptr", DllStructGetPtr($tMD4_CTX)) If @error Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(4, 0, "") EndIf DllCall("advapi32.dll", "none", "MD4Update", _ "ptr", DllStructGetPtr($tMD4_CTX), _ "ptr", $pFile, _ "dword", $iBufferSize) If @error Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(5, 0, "") EndIf DllCall("advapi32.dll", "none", "MD4Final", "ptr", DllStructGetPtr($tMD4_CTX)) If @error Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(6, 0, "") EndIf DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Local $sMD4 = Hex(DllStructGetData($tMD4_CTX, "digest")) Return SetError(0, 0, $sMD4) EndFunc ;==>_MD4ForFile ; #FUNCTION# ;=============================================================================== ; ; Name...........: _MD5ForFile ; Description ...: Calculates MD5 value for the specific file. ; Syntax.........: _MD5ForFile ($sFile) ; Parameters ....: $sFile - Full path to the file to process. ; Return values .: Success - Returns MD5 value in form of hex string ; - Sets @error to 0 ; Failure - Returns empty string and sets @error: ; |1 - CreateFile function or call to it failed. ; |2 - CreateFileMapping function or call to it failed. ; |3 - MapViewOfFile function or call to it failed. ; |4 - MD5Init function or call to it failed. ; |5 - MD5Update function or call to it failed. ; |6 - MD5Final function or call to it failed. ; Author ........: trancexx ; ;========================================================================================== Func _MD5ForFile($sFile) Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _ "wstr", $sFile, _ "dword", 0x80000000, _ ; GENERIC_READ "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE "ptr", 0, _ "dword", 3, _ ; OPEN_EXISTING "dword", 0, _ ; SECURITY_ANONYMOUS "ptr", 0) If @error Or $a_hCall[0] = -1 Then Return SetError(1, 0, "") EndIf Local $hFile = $a_hCall[0] $a_hCall = DllCall("kernel32.dll", "ptr", "CreateFileMappingW", _ "hwnd", $hFile, _ "dword", 0, _ ; default security descriptor "dword", 2, _ ; PAGE_READONLY "dword", 0, _ "dword", 0, _ "ptr", 0) If @error Or Not $a_hCall[0] Then DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Return SetError(2, 0, "") EndIf DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Local $hFileMappingObject = $a_hCall[0] $a_hCall = DllCall("kernel32.dll", "ptr", "MapViewOfFile", _ "hwnd", $hFileMappingObject, _ "dword", 4, _ ; FILE_MAP_READ "dword", 0, _ "dword", 0, _ "dword", 0) If @error Or Not $a_hCall[0] Then DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(3, 0, "") EndIf Local $pFile = $a_hCall[0] Local $iBufferSize = FileGetSize($sFile) Local $tMD5_CTX = DllStructCreate("dword i[2];" & _ "dword buf[4];" & _ "ubyte in[64];" & _ "ubyte digest[16]") DllCall("advapi32.dll", "none", "MD5Init", "ptr", DllStructGetPtr($tMD5_CTX)) If @error Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(4, 0, "") EndIf DllCall("advapi32.dll", "none", "MD5Update", _ "ptr", DllStructGetPtr($tMD5_CTX), _ "ptr", $pFile, _ "dword", $iBufferSize) If @error Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(5, 0, "") EndIf DllCall("advapi32.dll", "none", "MD5Final", "ptr", DllStructGetPtr($tMD5_CTX)) If @error Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(6, 0, "") EndIf DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Local $sMD5 = Hex(DllStructGetData($tMD5_CTX, "digest")) Return SetError(0, 0, $sMD5) EndFunc ;==>_MD5ForFile ; #FUNCTION# ;=============================================================================== ; ; Name...........: _SHA1ForFile ; Description ...: Calculates SHA1 value for the specific file. ; Syntax.........: _SHA1ForFile ($sFile) ; Parameters ....: $sFile - Full path to the file to process. ; Return values .: Success - Returns SHA1 value in form of hex string ; - Sets @error to 0 ; Failure - Returns empty string and sets @error: ; |1 - CreateFile function or call to it failed. ; |2 - CreateFileMapping function or call to it failed. ; |3 - MapViewOfFile function or call to it failed. ; |4 - CryptAcquireContext function or call to it failed. ; |5 - CryptCreateHash function or call to it failed. ; |6 - CryptHashData function or call to it failed. ; |7 - CryptGetHashParam function or call to it failed. ; Author ........: trancexx ; ;========================================================================================== Func _SHA1ForFile($sFile) Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _ "wstr", $sFile, _ "dword", 0x80000000, _ ; GENERIC_READ "dword", 3, _ ; FILE_SHARE_READ|FILE_SHARE_WRITE "ptr", 0, _ "dword", 3, _ ; OPEN_EXISTING "dword", 0, _ ; SECURITY_ANONYMOUS "ptr", 0) If @error Or $a_hCall[0] = -1 Then Return SetError(1, 0, "") EndIf Local $hFile = $a_hCall[0] $a_hCall = DllCall("kernel32.dll", "ptr", "CreateFileMappingW", _ "hwnd", $hFile, _ "dword", 0, _ ; default security descriptor "dword", 2, _ ; PAGE_READONLY "dword", 0, _ "dword", 0, _ "ptr", 0) If @error Or Not $a_hCall[0] Then DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Return SetError(2, 0, "") EndIf DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Local $hFileMappingObject = $a_hCall[0] $a_hCall = DllCall("kernel32.dll", "ptr", "MapViewOfFile", _ "hwnd", $hFileMappingObject, _ "dword", 4, _ ; FILE_MAP_READ "dword", 0, _ "dword", 0, _ "dword", 0) If @error Or Not $a_hCall[0] Then DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(3, 0, "") EndIf Local $pFile = $a_hCall[0] Local $iBufferSize = FileGetSize($sFile) Local $a_iCall = DllCall("advapi32.dll", "int", "CryptAcquireContext", _ "ptr*", 0, _ "ptr", 0, _ "ptr", 0, _ "dword", 1, _ ; PROV_RSA_FULL "dword", 0xF0000000) ; CRYPT_VERIFYCONTEXT If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) Return SetError(4, 0, "") EndIf Local $hContext = $a_iCall[1] $a_iCall = DllCall("advapi32.dll", "int", "CryptCreateHash", _ "ptr", $hContext, _ "dword", 0x00008004, _ ; CALG_SHA1 "ptr", 0, _ ; nonkeyed "dword", 0, _ "ptr*", 0) If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hContext, "dword", 0) Return SetError(5, 0, "") EndIf Local $hHashSHA1 = $a_iCall[5] $a_iCall = DllCall("advapi32.dll", "int", "CryptHashData", _ "ptr", $hHashSHA1, _ "ptr", $pFile, _ "dword", $iBufferSize, _ "dword", 0) If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) DllCall("advapi32.dll", "int", "CryptDestroyHash", "ptr", $hHashSHA1) DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hContext, "dword", 0) Return SetError(6, 0, "") EndIf Local $tOutSHA1 = DllStructCreate("byte[20]") $a_iCall = DllCall("advapi32.dll", "int", "CryptGetHashParam", _ "ptr", $hHashSHA1, _ "dword", 2, _ ; HP_HASHVAL "ptr", DllStructGetPtr($tOutSHA1), _ "dword*", 20, _ "dword", 0) If @error Or Not $a_iCall[0] Then DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) DllCall("advapi32.dll", "int", "CryptDestroyHash", "ptr", $hHashSHA1) DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hContext, "dword", 0) Return SetError(7, 0, "") EndIf DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile) DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject) DllCall("advapi32.dll", "int", "CryptDestroyHash", "ptr", $hHashSHA1) Local $sSHA1 = Hex(DllStructGetData($tOutSHA1, 1)) DllCall("advapi32.dll", "int", "CryptReleaseContext", "ptr", $hContext, "dword", 0) Return SetError(0, 0, $sSHA1) EndFunc ;==>_SHA1ForFile Results are in form of hex strings, but that is easily changed to fit your needs. First time is the hardest - you will see what I mean if you run it (file mapping related). Try it on something big.
    1 point
×
×
  • Create New...