Jump to content

(personal code dump)


MvGulik
 Share

Recommended Posts

I n d e x


Array_BinReadWrite

Binary dump (write+read) of 1 or 2 dimensional array.

(base variable types only. String and Numbers)

Array_CleanUp

Removal of records from a array based on array field[0] content.

(1 or 2 dimensional array only)

Array_GetDefinition

Generate AutoIt array definition code for a given array.

(any dimensions) (code generation tool) (base variable types only)

AU3_BareSource

Strips given Au3 source down to bare code.

(source code normalizer)

Au3_BareCode_Merge

Generates bare code for a given Au3 file.

(Include's included) (semi source code tool)

RE_Debug()

Personal Regular Expression visualizer/debugger function.

Reserved

Reserved.


PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

PS.2: Code created & tested with AutoIt v3.3.6.1 (-> See Au3-version-History on newer versions for possible breakage reasons, if any.)


Other stuff/links/... (wip)

- October 2010 Array create function. (any dimension)

- December 2010 Last Post, General (minor) updates log.

Edited by singularity

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

  • 10 months later...

Index ___ Array_BinReadWrite

Dump/Load Array to/from file.

- Only for String and Number data type's.

Personal: Needed some way to just dump and reload 2D array's with basic data types.

Last minor code edit: ...

known items: ...

Last update: (2010.07.29)

- fixed: BinWrite bug, int32 type was not stored as int64 type.

- update: function documentation, added error value info.

- added: file is folder exception error. (BinWrite)

- change: BinRead internal error code from 11 to 21. (duplicate error value use.)

- others: return adjustments, Example update.

UDF

#include-once
;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -q

#cs --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    Array_BinReadWrite.au3 :: (2010.07.17)
    ..
    Code Priorities:
    - get the job done: Yes (only one). (with no UDF dependency)
    - speed: no. (not intended for large array's)
    - mem: no. (not intended for large array's)
    - filesize: no. (intended for temporary use)
    ..
    Other var types: (not needed them yet.)
    ..
    update: (2010.07.29)
    - fixed: BinWrite bug, int32 type was not stored as int64 type.
    - update: function documentation, added error value info.
    - added: file is folder exception error. (BinWrite)
    - change: BinRead internal error code from 11 to 21. (duplicate error value use.)
    - others: return adjustments, Example update.
#ce --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

; #FUNCTION# ====================================================================================================
; Name ..........: _Array_BinWrite
; Description ...: Write 1 or 2 dimensional array to file. (binary save)
; Syntax ........: _Array_BinWrite($sFilespec, $avArray, $fIndex = False)
; Parameters ....: $sFilespec - target file.
;                  $avArray - Array to process.
;                  $fIndex - [Optional] Array contains index record. (not saved)
;                  |
; Return ........: Success - 1
;                  Failure - 0 & @error value. (error values still wip)
;                  | e11 - $sFileSpec: not a string.
;                  | e12 - $avArray: not a array.
;                  | e13 - $avArray: not supported array dimensions.
;                  | e14 - $fIndex: not a bool.
;                  | e15 - $sFileSpec: file delete failure. (Recycle)
;                  | e16 - $avArray: no data found. (indexed flagged array only.)
;                  | e17 - $sFileSpec: file is folder.
;                  | e21 - $avArray: unsupported var type encountered.
;                  | e32 - $avArray: none uniform type-use in field.
; Author ........: MvGulik.
; Modified ......: .
; Remarks .......: supported var-types: String, Integer, Float.
;                  String - only tested with ASCII. (UTF results unknown, Uses ZERO-byte as Field data separator in file data.)
;                  Integer - internal int32 type converts to int64 type.
;                  Float - integer float values (doubles type) also saved as float.
;                  Column of field data need to be uniform in type. (or none uniform type use per field is not supported)
;                  Index record - No data from this record is saved. (Index value is only checked, or ignored if check is disabled)
; Related .......: *_Array_BinWrite_1D, *_Array_BinWrite_2D, (*_GetTypeId).
; Link ..........: .
; Example .......: .
; Source ........: http://www.autoitscript.com/forum/index.php?showtopic=100777
; Version .......: 2010.07.29
; ===============================================================================================================
Func _Array_BinWrite($sFileSpec, $avArray, $fIndex = False)
    If 1 Then
        If Not IsString($sFileSpec) Then Return SetError(11)
        If Not IsArray($avArray) Then Return SetError(12)
        If UBound($avArray, 0) > 2 Then Return SetError(13)
        If Not IsBool($fIndex) Then Return SetError(14)
    EndIf
    If B329_Isfolder($sFileSpec) Then Return SetError(17)
    If B329_IsFile($sFileSpec) And Not FileRecycle($sFileSpec) Then Return SetError(15)
    Local $result
    Switch UBound($avArray, 0)
        Case 1
            If $fIndex And _
                    (UBound($avArray, 1) - 1 <> $avArray[0] Or Not $avArray[0]) _
                    Then Return SetError(16)
            $result = B329_Array_BinWrite_1D($sFileSpec, $avArray, $fIndex)
        Case 2
            If $fIndex And _
                    (UBound($avArray, 1) - 1 <> $avArray[0][0] Or Not $avArray[0][0]) _
                    Then Return SetError(16)
            $result = B329_Array_BinWrite_2D($sFileSpec, $avArray, $fIndex)
    EndSwitch
    Return SetError(@error, 0, $result)
EndFunc
; #FUNCTION# ====================================================================================================
; Name ..........: _Array_BinRead
; Description ...: Read _Array_BinWrite array-dump file.
; Syntax ........: _Array_BinRead($sFilespec, $fIndex = False)
; Parameters ....: $sFilespec - target file.
;                  $fIndex - [Optional] Include index record on array.
;                  |
; Return ........: Success - Array
;                  Failure - 0 & @error value. (error values still wip)
;                  | e11 - $sFileSpec: not a string.
;                  | e12 - $fIndex: not a bool.
;                  | e13 - $sFileSpec: file not found.
;                  | e21 - [internal] wrong call, array dimension mismatch.
;                  | e96 - general data-read error. (unknown type value.)
; Author ........: MvGulik.
; Modified ......: .
; Remarks .......: .
; Related .......: *_Array_BinRead_1D, *_Array_BinRead_2D.
; Link ..........: .
; Example .......: .
; Source ........: http://www.autoitscript.com/forum/index.php?showtopic=100777
; Version .......: 2010.07.29
; ===============================================================================================================
Func _Array_BinRead($sFileSpec, $fIndex = False)
    If 1 Then
        If Not IsString($sFileSpec) Then Return SetError(11)
        If Not IsBool($fIndex) Then Return SetError(12)
        If Not B329_IsFile($sFileSpec) Then Return SetError(13)
    EndIf
    Local $hFile = FileOpen($sFileSpec, 16)
    Local $iDims = Int(FileRead($hFile, 8))
    FileClose($hFile)
    Local $result
    Switch $iDims
        Case 1
            $result = B329_Array_BinRead_1D($sFileSpec, $fIndex)
        Case 2
            $result = B329_Array_BinRead_2D($sFileSpec, $fIndex)
        Case Else
            Return SetError(21)
    EndSwitch
    Return SetError(@error, 0, $result)
EndFunc
;; --- internals ---
Func B329_Array_BinWrite_1D($sFileSpec, $avArray, $fIndex)
    Local $iFirst = 0
    If $fIndex Then $iFirst = 1
    Local $iFieldType
    $iFieldType = B329_GetTypeId($avArray[$iFirst])
    If Not $iFieldType Then Return SetError(21)
    If 1 Then
        For $iRec = $iFirst To UBound($avArray, 1) - 1
            If $iFieldType <> B329_GetTypeId($avArray[$iRec]) Then Return SetError(32)
        Next
    EndIf
    Local Const $i64 = Int('1')
    Local $bHeader = Binary(''), $bData = Binary(''), $bNull = Binary(Chr(0))
    $bHeader &= Binary(UBound($avArray, 1) * $i64 - $iFirst)
    $bHeader &= Binary($iFieldType * $i64)
    Local Enum $_err_, $_str_, $_int_, $_float_
    #forceref $_err_
    Switch $iFieldType
        Case $_str_
            $bData &= Binary($avArray[$iFirst])
            For $iRec = $iFirst + 1 To UBound($avArray, 1) - 1
                $bData &= $bNull & Binary($avArray[$iRec])
            Next
        Case $_int_
            For $iRec = $iFirst To UBound($avArray, 1) - 1
                $bData &= Binary($avArray[$iRec] * $i64)
            Next
        Case $_float_
            For $iRec = $iFirst To UBound($avArray, 1) - 1
                $bData &= Binary($avArray[$iRec])
            Next
    EndSwitch
    $bData = Binary(UBound($avArray, 0) * $i64) & Binary(BinaryLen($bHeader) * $i64) & Binary(BinaryLen($bData) * $i64) & $bHeader & $bData
    FileWrite($sFileSpec, $bData)
    Return 1
EndFunc
Func B329_Array_BinWrite_2D($sFileSpec, $avArray, $fIndex)
    Local $iFirst = 0
    If $fIndex Then $iFirst = 1
    Local $aiFieldType[UBound($avArray, 2)]
    For $i = 0 To UBound($aiFieldType, 1) - 1
        $aiFieldType[$i] = B329_GetTypeId($avArray[$iFirst][$i])
        If Not $aiFieldType[$i] Then Return SetError(21)
    Next
    If 1 Then
        Local $iType
        For $ifield = 0 To UBound($aiFieldType, 1) - 1
            $iType = $aiFieldType[$ifield]
            For $iRec = $iFirst To UBound($avArray, 1) - 1
                If $iType <> B329_GetTypeId($avArray[$iRec][$ifield]) Then Return SetError(32)
            Next
        Next
    EndIf
    Local Const $i64 = Int('1')
    Local $bHeader = Binary(''), $bData = Binary(''), $bNull = Binary(Chr(0))
    $bHeader &= Binary(UBound($avArray, 2) * $i64)
    $bHeader &= Binary(UBound($avArray, 1) * $i64 - $iFirst)
    For $i = 0 To UBound($aiFieldType, 1) - 1
        $bHeader &= Binary($aiFieldType[$i] * $i64)
    Next
    Local Enum $_err_, $_str_, $_int_, $_float_
    #forceref $_err_
    Local $bTmp
    For $ifield = 0 To UBound($avArray, 2) - 1
        Switch $aiFieldType[$ifield]
            Case $_str_
                $bTmp = Binary($avArray[$iFirst][$ifield])
                For $iRec = $iFirst + 1 To UBound($avArray, 1) - 1
                    $bTmp &= $bNull & Binary($avArray[$iRec][$ifield])
                Next
                $bData &= Binary(BinaryLen($bTmp) * $i64) & $bTmp
            Case $_int_
                $bTmp = Binary('')
                For $iRec = $iFirst To UBound($avArray, 1) - 1
                    $bTmp &= Binary($avArray[$iRec][$ifield] * $i64)
                Next
                $bData &= Binary(BinaryLen($bTmp) * $i64) & $bTmp
            Case $_float_
                $bTmp = Binary('')
                For $iRec = $iFirst To UBound($avArray, 1) - 1
                    $bTmp &= Binary($avArray[$iRec][$ifield])
                Next
                $bData &= Binary(BinaryLen($bTmp) * $i64) & $bTmp
        EndSwitch
    Next
    $bData = Binary(UBound($avArray, 0) * $i64) & Binary(BinaryLen($bHeader) * $i64) & Binary(BinaryLen($bData) * $i64) & $bHeader & $bData
    FileWrite($sFileSpec, $bData)
    Return 1
EndFunc
Func B329_Array_BinRead_1D($sFileSpec, $fIndex)
    Local $iFirst = 0
    If $fIndex Then $iFirst = 1
    Local $hFile = FileOpen($sFileSpec, 16)
    Local $iDims = Int(FileRead($hFile, 8))
    If $iDims <> 1 Then Return SetError(21)
    Local $iSizeHeader = Int(FileRead($hFile, 8))
    Local $iSizeData = Int(FileRead($hFile, 8))
    Local $tBytesH = DllStructCreate('byte[' & $iSizeHeader & ']')
    DllStructSetData($tBytesH, 1, FileRead($hFile, $iSizeHeader))
    Local $tBytesD = DllStructCreate('byte[' & $iSizeData & ']')
    DllStructSetData($tBytesD, 1, FileRead($hFile, $iSizeData))
    FileClose($hFile)
    Local $sStruct = B329_StringRepeat(Int($iSizeHeader / 8), 'int64', ';')
    Local $tHeader = DllStructCreate($sStruct, DllStructGetPtr($tBytesH))
    Local $iArraySize = DllStructGetData($tHeader, 1)
    Local $iFieldType = DllStructGetData($tHeader, 2)
    Local Enum $_err_, $_str_, $_int_, $_float_
    #forceref $_err_
    Local $ArrayOut[$iArraySize + $iFirst]
    Local $sNull = Chr(0), $tData
    Switch $iFieldType
        Case $_str_
            $ArrayOut = StringSplit(BinaryToString(DllStructGetData($tBytesD, 1)), $sNull, 2 - 2 * $iFirst)
        Case $_int_
            $sStruct = B329_StringRepeat(Int($iSizeData / 8), 'int64', ';')
            $tData = DllStructCreate($sStruct, DllStructGetPtr($tBytesD))
            For $i = 0 To $iArraySize - 1
                $ArrayOut[$i + $iFirst] = DllStructGetData($tData, $i + 1)
            Next
            If $iFirst Then $ArrayOut[0] = $iArraySize
        Case $_float_
            $sStruct = B329_StringRepeat(Int($iSizeData / 8), 'double', ';')
            $tData = DllStructCreate($sStruct, DllStructGetPtr($tBytesD))
            For $i = 0 To $iArraySize - 1
                $ArrayOut[$i + $iFirst] = DllStructGetData($tData, $i + 1)
            Next
            If $iFirst Then $ArrayOut[0] = $iArraySize
        Case Else
            Return SetError(96)
    EndSwitch
    Return $ArrayOut
EndFunc
Func B329_Array_BinRead_2D($sFileSpec, $fIndex)
    Local $iFirst = 0
    If $fIndex Then $iFirst = 1
    Local $hFile = FileOpen($sFileSpec, 16)
    Local $iDims = Int(FileRead($hFile, 8))
    If $iDims <> 2 Then Return SetError(21)
    Local $iSizeHeader = Int(FileRead($hFile, 8))
    Local $iSizeData = Int(FileRead($hFile, 8))
    #forceref $iSizeData
    Local $tBytesH = DllStructCreate('byte[' & $iSizeHeader & ']')
    DllStructSetData($tBytesH, 1, FileRead($hFile, $iSizeHeader))
    Local $sStruct = B329_StringRepeat(Int($iSizeHeader / 8), 'int64', ';')
    Local $tHeader = DllStructCreate($sStruct, DllStructGetPtr($tBytesH))
    Local $ifields = DllStructGetData($tHeader, 1)
    Local $iRecords = DllStructGetData($tHeader, 2)
    Local $aiFieldType[$ifields]
    For $i = 0 To $ifields - 1
        $aiFieldType[$i] = DllStructGetData($tHeader, 3 + $i)
    Next
    Local Enum $_err_, $_str_, $_int_, $_float_
    #forceref $_err_
    Local $ArrayOut[$iRecords + $iFirst][$ifields]
    If $iFirst Then $ArrayOut[0][0] = $iRecords
    Local $aTmp, $sNull = Chr(0), $tData, $iSizeDataPart, $tBytesD
    For $ifield = 0 To $ifields - 1
        $iSizeDataPart = Int(FileRead($hFile, 8))
        $tBytesD = DllStructCreate('byte[' & $iSizeDataPart & ']')
        DllStructSetData($tBytesD, 1, FileRead($hFile, $iSizeDataPart))
        Switch $aiFieldType[$ifield]
            Case $_str_
                $aTmp = StringSplit(BinaryToString(DllStructGetData($tBytesD, 1)), $sNull, 2 - 2 * $iFirst)
                For $iRec = $iFirst To UBound($aTmp) - 1
                    $ArrayOut[$iRec][$ifield] = $aTmp[$iRec]
                Next
                $aTmp = ''
            Case $_int_
                $sStruct = B329_StringRepeat(Int($iSizeDataPart / 8), 'int64', ';')
                $tData = DllStructCreate($sStruct, DllStructGetPtr($tBytesD))
                For $iRec = 0 To $iRecords - 1
                    $ArrayOut[$iRec + $iFirst][$ifield] = DllStructGetData($tData, $iRec + 1)
                Next
            Case $_float_
                $sStruct = B329_StringRepeat(Int($iSizeDataPart / 8), 'double', ';')
                $tData = DllStructCreate($sStruct, DllStructGetPtr($tBytesD))
                For $iRec = 0 To $iRecords - 1
                    $ArrayOut[$iRec + $iFirst][$ifield] = DllStructGetData($tData, $iRec + 1)
                Next
            Case Else
                Return SetError(96)
        EndSwitch
    Next
    FileClose($hFile)
    Return $ArrayOut
EndFunc
Func B329_GetTypeId($v)
    Local Enum $_err_, $_str_, $_int_, $_float_
    Switch VarGetType($v)
        Case 'string'
            Return $_str_
        Case 'int32', 'int64'
            Return $_int_
        Case 'float', 'double'
            Return $_float_
    EndSwitch
    Return $_err_
EndFunc
;; --- sub functions ---
Func B329_IsFile($sFileSpec)
    If FileExists($sFileSpec) And Not StringInStr(FileGetAttrib($sFileSpec), 'D') Then Return True
    Return False
EndFunc
Func B329_Isfolder($sFileSpec)
    If FileExists($sFileSpec) And StringInStr(FileGetAttrib($sFileSpec), 'D') Then Return True
    Return False
EndFunc
Func B329_StringRepeat($iRep, $sRep = ' ', $sDelim = '')
    If 1 Then
        If Not IsInt($iRep) Then Return SetError(1, 0, '')
        If Not IsString($sRep) Then Return SetError(2, 0, '')
        If Not IsString($sDelim) Then Return SetError(3, 0, '')
        If $iRep < 0 Then Return SetError(4, 0, '')
    EndIf
    If $iRep = 0 Then Return ''
    Local $sOut = ''
    For $i = 1 To $iRep
        $sOut &= $sRep & $sDelim
    Next
    If $sDelim Then $sOut = StringTrimRight($sOut, StringLen($sDelim))
    Return $sOut
EndFunc

test/example

;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -q

#include <Debug.au3>
Enum $_logwin_ = 1, $_console_, $_NA_Yet_, $_file_
_DebugSetup(Default, False, $_console_, 'DebugOut.log')
;~ _DebugOut('msg')
;~ _DebugReportVar('msg', 'var/data')

#include "Array_BinReadWrite.au3"

Func TEST()

    Local $sFilespec1 = @ScriptDir & '\' & 'ArrayDumpBin1.tmp'
    Local $sFilespec2 = @ScriptDir & '\' & 'ArrayDumpBin2.tmp'

    Local Enum $_err_, $_str_, $_int32_, $_int64_, $_float_
    #forceref $_err_

    Local $iCase = 2

    Local $bTmp, $result
    #forceref $bTmp
    Switch $iCase

        Case 1 ;; random 1D array.
            Local $avArray[Floor(Random() * 5) + 5]

            Switch Floor(Random() * 4) + 1
                Case $_str_
                    For $i = 0 To UBound($avArray, 1) - 1
                        $avArray[$i] = Hex(Floor(Random() * 2 ^ 32), 8)
                    Next
                Case $_int32_
                    For $i = 0 To UBound($avArray, 1) - 1
                        $avArray[$i] = Number(String(Floor(Random() * 2 ^ 10 - 2 ^ 9)))
                    Next
                Case $_int64_
                    For $i = 0 To UBound($avArray, 1) - 1
                        $avArray[$i] = Floor(Random() * 2 ^ 32)
                    Next
                Case $_float_
                    For $i = 0 To UBound($avArray, 1) - 1
                        $avArray[$i] = Random() * 2 ^ 16 - 2 ^ 15
                    Next
            EndSwitch
            _DebugReportVar('$avArray', $avArray) ;### Debug DebugOut.

            $result = _Array_BinWrite($sFilespec1, $avArray, False)
            _DebugReportVar('$result', $result) ;### Debug DebugOut.
            If @error Then Return SetError(@error)

            $avArray = _Array_BinRead($sFilespec1, False)
            _DebugReportVar('$avArray', $avArray) ;### Debug DebugOut.
            If @error Then Return SetError(@error)

        Case 2 ;; random 2D array.
            Local $avArray[Floor(Random() * 5) + 5][Floor(Random() * 5) + 5]

            For $ifield = 0 To UBound($avArray, 2) - 1
                Switch Floor(Random() * 4) + 1
                    Case $_str_
                        For $iRec = 0 To UBound($avArray, 1) - 1
                            $avArray[$iRec][$ifield] = Hex(Int(Random() * 2 ^ 32), 8)
                        Next
                    Case $_int32_
                        For $iRec = 0 To UBound($avArray, 1) - 1
                            $avArray[$iRec][$ifield] = Number(String(Floor(Random() * 2 ^ 10 - 2 ^ 9)))
                        Next
                    Case $_int64_
                        For $iRec = 0 To UBound($avArray, 1) - 1
                            $avArray[$iRec][$ifield] = Floor(Random() * 2 ^ 32)
                        Next
                    Case $_float_
                        For $iRec = 0 To UBound($avArray, 1) - 1
                            $avArray[$iRec][$ifield] = Random() * 2 ^ 16 - 2 ^ 15
                        Next
                EndSwitch
            Next
            _DebugReportVar('$avArray', $avArray) ;### Debug DebugOut.

            $result = _Array_BinWrite($sFilespec2, $avArray, False)
            _DebugReportVar('$result', $result) ;### Debug DebugOut.
            If @error Then Return SetError(@error)

            $avArray = _Array_BinRead($sFilespec2, False)
            _DebugReportVar('$avArray', $avArray) ;### Debug DebugOut.
            If @error Then Return SetError(@error)
    EndSwitch

EndFunc

TEST()
If @error Then Exit @error

PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Index ___ Array_CleanUp

Delete records from array that contain token string at first field. (1D & 2D array supported)

- Only tested with String and Number data type's. (l8r)

Personal: Might as well make(finish) what I suggest. Two dimensional array delete

Last minor code edit: ...

known items: ...

UDF

#include-once
; #FUNCTION# ====================================================================================================
; Name ..........: Array_CleanUp
; Description ...: Delete records from array that contain token string at first field. (1D & 2D array supported)
; Syntax ........: Array_CleanUp(ByRef $avArray, $fIndex = False, $sToken = '')
; Parameters ....: $avArray - [ByRef]
;                  $fIndex - [Optional] Array has index record/field.
;                  $sToken - [Optional] String to look for. (case sensitive)
;                  |
; Return ........: Success - 1
;                  Failure - 0 & Error value.
;                  | e1 - $avArray: Not a array.
;                  | e2 - $avArray: unsipported array. (more than 2 dimentions.)
;                  | e3 - $fIndex: Not a bool.
;                  | e4 - $sToken: Not a string.
;                  | e5 - index value mismatch. (can be disable.(2x))
; Author ........: MvGulik
; Modified ......: .
; Remarks .......: None indexed arrays that have all there fields/records deleted, are retured as empty string. (no error)
;                  Not (yet) tested on none string or number data types.
; Related .......: .
; Link ..........: .
; Example .......: .
; Source ........: .
; Source ........: http://www.autoitscript.com/forum/index.php?showtopic=100777
; Version .......: 2010.07.21
; ===============================================================================================================
Func Array_CleanUp(ByRef $avArray, $fIndex = False, $sToken = '')
    If 1 Then
        If Not IsArray($avArray) Then Return SetError(1)
        If Not (UBound($avArray, 0) = 1 Or UBound($avArray, 0) = 2) Then Return SetError(2)
        If Not IsBool($fIndex) Then Return SetError(3)
        If Not IsString($sToken) Then Return SetError(4)
    EndIf
    Local Enum $_index_
    Local $iRecords = UBound($avArray, 1), $iFields = UBound($avArray, 2)
    Local $iFirst = 0, $iRecSource = 0
    If $iFields Then
        If $fIndex Then
            If Not IsInt($avArray[$_index_][$_index_]) Or $avArray[$_index_][$_index_] <> $iRecords - 1 Then Return SetError(5)
            $iFirst = 1
        EndIf
        For $iRecTarget = $iFirst To $iRecords - 1
            If $avArray[$iRecTarget][0] == $sToken Then
                For $iRecSource = A07A_Max($iRecSource, $iRecTarget) + 1 To $iRecords - 1
                    If Not ($avArray[$iRecSource][0] == $sToken) Then
                        For $iField = 0 To $iFields - 1
                            $avArray[$iRecTarget][$iField] = $avArray[$iRecSource][$iField]
                        Next
                        $avArray[$iRecSource][0] = $sToken
                        ContinueLoop 2
                    EndIf
                Next
                ExitLoop 1
            EndIf
        Next
        If $iRecTarget Then
            ReDim $avArray[$iRecTarget][$iFields]
            If $fIndex Then $avArray[$_index_][$_index_] = $iRecTarget - 1
        Else
            $avArray = ''
        EndIf
    Else
        If $fIndex Then
            If Not IsInt($avArray[$_index_]) Or $avArray[$_index_] <> $iRecords - 1 Then Return SetError(5)
            $iFirst = 1
        EndIf
        For $iRecTarget = $iFirst To $iRecords - 1
            If $avArray[$iRecTarget] == $sToken Then
                For $iRecSource = A07A_Max($iRecSource, $iRecTarget) + 1 To $iRecords - 1
                    If Not ($avArray[$iRecSource] == $sToken) Then
                        $avArray[$iRecTarget] = $avArray[$iRecSource]
                        $avArray[$iRecSource] = $sToken
                        ContinueLoop 2
                    EndIf
                Next
                ExitLoop 1
            EndIf
        Next
        If $iRecTarget Then
            ReDim $avArray[$iRecTarget]
            If $fIndex Then $avArray[$_index_] = $iRecTarget - 1
        Else
            $avArray = ''
        EndIf
    EndIf
    Return 1
EndFunc
;; --- sub functions ---
Func A07A_Max($n1, $n2)
    If $n1 > $n2 Then Return $n1
    Return $n2
EndFunc

test/example

;; On ToDo. But that don't seems to work yet. :/


ToDo: (19 nov 2010)

- add example. mainly for easy testing purpose.

- see about updating with this code. (seems I already did something similar, in less code. 1d-array)

Local $iTarget = 0
    For $iDim1 = 0 To UBound($avArray, 1) - 1
        If Not ($avArray[$iDim1] == '') Then ;; [edit2]
            If $iTarget < $iDim1 Then $avArray[$iTarget] = $avArray[$iDim1]
            $iTarget += 1 ;; skiped if element is empty. (triggers copy process)
        EndIf
    Next
    If $iTarget < $iDim1 Then ReDim $avArray[$iTarget]

PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Index ___ Array_GetDefinition

Generate AutoIt array definition code for a given array.

- Any array dimensions.

- Code generation tool/function.

- Base variable types only. (String and Number type's)

Personal: Cleanup/Rewrite* of a old UDF.

*removal of array dimension limitation by use of recursion.

Credits:

Heron, Base recursion code. - saving me the time to decipher my own recursion code.

Last minor code edit:

- added missing #include-once line.

known items: ...

UDF

#include-once
;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -q

#cs --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    Array_GetDefinition.au3 :: (2010.07.27)
    .. functions ..
    _Array_GetDef(ByRef $aInput, ByRef $sOut)
    _Array_TrimDef($sDef) - array def cleanup support function.
    .. internals ..
    C6A9_CntString
#ce --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

; #FUNCTION# ====================================================================================================
; Name ..........: _Array_GetDef
; Description ...: Return AutoIt array definition for given array.
; Syntax ........: _Array_GetDef(ByRef $aInput, ByRef $sOut, $d = 0, $cnt = 0)
; Parameters ....: $aInput - [ByRef] Array to process.
;                  $sOut - [ByRef] Returned array definition. (appended to current $sOut string content.)
;                  $d - [internal/recursion] don't use. (dimensional level.)
;                  $cnt - [internal/recursion] don't use. (dimensional position, or current array-cel address.)
;                  |
; Return ........: Success - 1
;                  Failure - 0 & @error value.
;                  | e1 - $aInput: not a array.
;                  | e2 - $sOut: not a string.
;                  | e96 - unsupported data type encountered in array/cel.
; Author ........: MvGulik.
; Modified ......: .
; Remarks .......: Only supports basic data types. (string and numbers)(not sure about others yet.)
;                  Array definition's longer than 4095 characters (split or not) are not supported by some SciTe4AutoIt3 tools. (Tidy, ...)
;                  Recursion code based on example from Heron, http://www.autoitscript.com/forum/index.php?showtopic=104506&view=findpost&p=739385
; Related .......: .
; Link ..........: .
; Example .......: .
; Source ........: http://www.autoitscript.com/forum/index.php?showtopic=100777
; Version .......: 2010.07.27
; ===============================================================================================================
Func _Array_GetDef(ByRef $aInput, ByRef $sOut, $d = 0, $cnt = 0) ;; recursive function.
    If Not IsArray($cnt) Then ;; first call.
        If Not IsArray($aInput) Then Return SetError(1)
        Dim $cnt[UBound($aInput, 0)]
        If Not IsString($sOut) Then Return SetError(2)
        If Not $sOut Then $sOut = '$aArray'
        For $i = 1 To UBound($aInput, 0) ;; build array size-def part.
            $sOut &= '[' & String(UBound($aInput, $i)) & ']'
        Next
        $sOut &= ' = [' ;; start data part.
    EndIf

    Local $vData
    #forceref $vData

    For $i = 0 To UBound($aInput, $d + 1) - 1
        If $d = UBound($aInput, 0) - 1 Then
            For $i = 0 To UBound($aInput, $d + 1) - 1 ;; data scan part. (intentional re-use of $i)
                $cnt[$d] = $i
                $vData = Execute('$aInput' & C6A9_CntString($cnt))
                Switch VarGetType($vData) ;; separate "data type processing" function candidate.
                    Case 'string'
                        $sOut &= "'" & $vData & "'"
                    Case 'int32', 'int64', 'float' ;; float & double will be value approximations.
                        $sOut &= String($vData)
                    Case 'double'
                        $sOut &= String($vData) & '.0' ;; enforce double.
                    ;Case ... others.
                    Case Else ;; unsupported data type's.
                        ;DebugOut('$vData', $vData) ;### Debug DebugOut.
                        ;DebugOut('VarGetType($vData)', VarGetType($vData)) ;### Debug DebugOut.
                        Return SetError(96)
                EndSwitch
                $sOut &= ','
            Next
        Else ;; array nav part.
            $cnt[$d] = $i
            $sOut &= '['
            $d = _Array_GetDef($aInput, $sOut, $d + 1, $cnt)
            if @error Then Return SetError(@error)
            $sOut &= '],'
        EndIf
    Next
    $sOut = StringTrimRight($sOut, 1)
    If $d Then Return $d - 1
    $sOut &= ']' ;; close data part.
    Return 1
EndFunc
Func _Array_TrimDef($sDef)
    ;; remove unneeded "trailing empty string" cases from array definition string.
    Local $sFind1 = ",'']"
    Local $sFind2 = ',""]'
    For $i = 1 To 64 ;; max array dimensions.
        If Not (StringInStr($sDef, $sFind1) Or StringInStr($sDef, $sFind2)) Then ExitLoop
        While StringInStr($sDef, $sFind1) Or StringInStr($sDef, $sFind2)
            $sDef = StringReplace($sDef, $sFind1, ']')
            $sDef = StringReplace($sDef, $sFind2, ']')
        WEnd
        $sFind1 = ',' & '[' & StringTrimLeft($sFind1, 1) & ']'
        $sFind2 = ',' & '[' & StringTrimLeft($sFind2, 1) & ']'
    Next
    Return $sDef
EndFunc
;; --- internals ---
Func C6A9_CntString($cnt)
    Local $sOut = ''
    For $i = 0 To UBound($cnt, 1) - 1
        $sOut &= '[' & String($cnt[$i]) & ']'
    Next
    Return $sOut
EndFunc

test/example

;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -q

#cs --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    Array_GetDefinition_Test.au3 (2010.07.27).
    ...
#ce --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

#include "Array_GetDefinition.au3"

Func TEST()

    Local $avArray[1]
    Local $iMin = 3, $iMax = 5
    Switch _RandomInt($iMin, $iMax)
        Case 1
            $iMin = 1
            $iMax = 2
            ReDim $avArray[_RandomInt($iMin, $iMax)]
        Case 2
            $iMin = 1
            $iMax = 2
            ReDim $avArray[_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)]
        Case 3
            $iMin = 3
            $iMax = 4
            ReDim $avArray[_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)]
        Case 4
            $iMin = 3
            $iMax = 3
            ReDim $avArray[_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)]
        Case 5
            $iMin = 2
            $iMax = 3
            ReDim $avArray[_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)][_RandomInt($iMin, $iMax)]
        Case Else
            Return SetError(96)
    EndSwitch
    _DebugOut('$avArray', $avArray) ;### Debug DebugOut.

    Local $sArrayDef

    ;; empty array. (trimm test)
    $sArrayDef = '$aVarName'
    _Array_GetDef($avArray, $sArrayDef)
    _DebugOut('$sArrayDef:empty', $sArrayDef) ;### Debug DebugOut.
    _DebugOut('$sArrayDef:+trim', _Array_TrimDef($sArrayDef)) ;### Debug DebugOut.

    ;; filled array.
    _ArrayFill($avArray)
    $sArrayDef = '$aArray'
    _Array_GetDef($avArray, $sArrayDef)
    If StringLen($sArrayDef) > 120 Then
        _DebugOut('$sArrayDef:split', _SplitArrayDef($sArrayDef)) ;### Debug DebugOut.
    Else
        _DebugOut('$sArrayDef', $sArrayDef) ;### Debug DebugOut.
    EndIf

EndFunc

;; --- test support functions ---
Func _ArrayFill(ByRef $aInput, $d = 0, $cnt = 0) ;; recursive function.
    If Not IsArray($cnt) Then ;; first call.
        If Not IsArray($aInput) Then Return SetError(1)
        Dim $cnt[UBound($aInput, 0)]
    EndIf

    For $i = 0 To UBound($aInput, $d + 1) - 1
        If $d = UBound($aInput, 0) - 1 Then
            For $i = 0 To UBound($aInput, $d + 1) - 1
                $cnt[$d] = $i
                Execute('__ArrayFill_Cell($aInput' & C6A9_CntString($cnt) & ',"' & __AlphaFieldmask($cnt) & '")')
            Next
        Else
            $cnt[$d] = $i
            $d = _ArrayFill($aInput, $d + 1, $cnt)
        EndIf
    Next
    If $d Then Return $d - 1
    Return 1
EndFunc
Func __ArrayFill_Cell(ByRef $vCell, $vData)
    $vCell = $vData
EndFunc
Func __AlphaFieldMask($cnt) ;; test output case.
    Local $sOut = ''
    For $i = 0 To UBound($cnt, 1) - 1
        $sOut &= String(Chr($cnt[$i] + Asc('A')))
    Next
    Return $sOut
EndFunc
Func _RandomInt($1, $2)
    If $1 = $2 Then Return $1 ;; assuming input are int's ...
    Local $result = Random($1, $2, 1)
    Return SetError(@error, @extended, $result)
EndFunc
Func _SplitArrayDef($s)
    Return StringReplace(StringReplace($s, '= [', '= _' & @CRLF & @TAB & '['), '],[', '], _ ' & @CRLF & @TAB & '[')
EndFunc
Func _DebugOut($s, $v = '[NuLL]', $err = @error, $ext = @extended)
    If $v == '[NuLL]' Then
        ConsoleWrite($s)
    ElseIf IsString($v) Then
        ConsoleWrite($s & ' = "' & $v & '"')
    Else
        If IsArray($v) Then
            Local $sTmp = '[ARRAY]:'
            For $i = 1 To UBound($v, 0)
                $sTmp &= '[' & UBound($v, $i) & ']'
            Next
            $v = $sTmp
        EndIf
        ConsoleWrite($s & ' = ' & $v)
    EndIf
    If $err Or $ext Then ConsoleWrite('   [err:' & $err & ', ext:' & $ext & ']')
    ConsoleWrite(@CRLF)
    Return SetError($err, $ext)
EndFunc

TEST()
If @error Then Exit @error

PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Index ___ 'AU3_BareSource'

Strips given Au3 source down to bare code.

- (split off from Au3 code merger)

- (wip, see todo part)

Personal:

- RE exercise.

- squeezing all the space out of it.

Related/Credits:

- topic: Removing comments with StringRegExpReplace()

- topic: _stripComments

Todo:

- re-think directives part. (as this is separated from au3-merger) (strip all/none/optional) (later: easy user adjustable.)

- same for some other parts. (!:additional code blanks removal)

- see about fixing "trailing spaces fix"

- (?:merge some base steps.)

known items: ...

Last edits:

- (code spacing: string capture fix.)

- (quick fix 'negative number after keyword'.) (?:not picked up by AU3Check)

UDF

;~ #include-once
;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -q

; #FUNCTION# ====================================================================================================
; Name ..........: AU3_BareSource
; Description ...: Strips given Au3 source down to bare code. (del: formating, comments, splits, most directives)
; Syntax ........: AU3_BareSource(ByRef $sCode)
; Parameters ....: $sCode (ByRef) - au3 source code text to be processed.
;                  |
; Return ........: (none)
; Author ........: MvGulik.
; Modified ......: .
; Remarks .......: assuming none faulty au3 source code. (unterminated string, block comment, etc)
;                  assuming ASCII/ANSI. (no UTF)
; Related .......: .
; Link ..........: .
; Example .......: .
; Source ........: http://www.autoitscript.com/forum/topic/100777-personal-code-dump/page__view__findpost__p__820533
; ===============================================================================================================
Func AU3_BareSource(ByRef $sCode)
    $sCode = @LF & $sCode & @LF ;; forcing leading and trailing linefeeds on text. (eliminating the need to test for them)
    $sCode = StringRegExpReplace($sCode, '\s*\R+\s*', @LF) ;; general normalize text. (linesfeeds, empty lines, leading and trailing blanks.)
    $sCode = StringRegExpReplace($sCode, '(?m)^;.*$\n?', '') ;; remove full comment lines.
    $sCode = StringRegExpReplace($sCode, '(?im)^#c(?:omments-(s)tart|omments-(e)nd)', '#c$1$2') ;; prep for next RE.
    $sCode = StringRegExpReplace($sCode, '(?im)^#cs(?<rec>(?<!^)#|[^#]|#cs\g<rec>*#ce)*?^#ce(?-s).*\n', '') ;; remove comment blocks. (+nested)
    $sCode = StringRegExpReplace($sCode, '(?im)^#[^i].*$\n?', '') ;; remove directives, excluding both #include directive's. (?:optional, move out)
    $sCode = StringRegExpReplace($sCode, '(?m)^((?:[^''";]*([''"]).*?\2)*[^;]*);?.*$', '$1') ;; remove trailing code comments. (leaves leading comment tag spacing)
    $sCode = StringRegExpReplace($sCode, '(?m)\h*$', '') ;; trailing spaces fix.
    $sCode = StringRegExpReplace($sCode, '(?m)\h+_$\n', ' ') ;; merge split lines. (note: un-runneble code if line > 4096)
    $sCode = StringRegExpReplace($sCode, '(([''"]).*?\2)|\h*([=<>(\){}[\]*+\-/^,&])\h*|(\h)\h*', '$1$3$4') ;; remove unneeded code spacing. (?:benefits)(!:negative sign problem)
    $sCode = StringRegExpReplace($sCode, '(?i)(?<=[^a-z$]to|[^a-z$]step|[^a-z$]exit|[^a-z$]return|[^a-z$]select|[^a-z$]switch|[^a-z$]case)-', ' -') ;; wip 'negative number after keyword' quick-fix. (?:speed)(!:only target most likely keywords)
    $sCode = StringTrimRight($sCode, 1) ;; remove text leading and trailing linefeeds. (?:potential pitfalls)
    $sCode = StringTrimLeft($sCode, 1) ;; remove text leading and trailing linefeeds.
EndFunc

PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

  • 2 months later...

Index ___ 'Au3_BareCode_Merge'

Generates bare code for a given Au3 file.

- Including #include's. (Obfuscator merge style)

- (considered beta for the moment)

Personal: Just something I seem to get back at from time to time.

Related/Credits: ...

Last minor code edit:

- directive cleanup: merged 3 separate RE's into one.

known items:

- (forgot) includes with absolute paths will probably not work right.

Todo:

- include AutoIt type merge.

- clear up [later] items.

- finalize into UDF form.

- Get RE's, to deal with raw user code, done. ;)

;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -q

;; == info: terminal's == [debug]
;; Exit 96001 ;; [later] ;; unexpected character in #include data.
;; Exit 96002 ;; file not found on file that was allready checked to exist.
;; Exit 96003 ;; locked file or something.

Global Enum $_index_
Global Const $DELIM = '|'

Func main($sFilespec)
    If Not IsFile($sFilespec) Then Exit 1
    Local $sExclude_list = $DELIM
    Local $aInclude_dirs = GetIncludeDirs(GetAutoitLocation())
    Local $sCode = _merge_includes_recursive($sFilespec, $aInclude_dirs, $sExclude_list)
    Return $sCode
EndFunc

;; == main process function's ==
Func _merge_includes_recursive($sFilespec, $aInclude_dirs, ByRef $sExclude_list)
    Local Enum $_icl_file_, $_icl_type_, $iFieldsIncludeList
    Local Enum $_inc_once_ = -1, $_inc_global_first_, $_inc_local_first_
    #forceref $iFieldsIncludeList, $_inc_global_first_, $_inc_local_first_

    Local $aInclude_list
    Local $aCode = _pre_merge_new_file_processing($sFilespec, $sExclude_list, $aInclude_list)
    $aInclude_dirs[$aInclude_dirs[$_index_][$_index_]][$_icl_file_] = Filespec_ClipLast($sFilespec) ;; update local path.

    Local $sFilespec_include
    For $i = 0 To UBound($aInclude_list, 1) - 1
        If $aInclude_list[$i][$_icl_type_] > $_inc_once_ Then
            $sFilespec_include = _scan_include_dirs_for_include_file($aInclude_list[$i][$_icl_file_], $aInclude_list[$i][$_icl_type_], $aInclude_dirs)
            If $sFilespec_include Then
                ;; check for already included include-once file. [Obfucator-MODE]
                If Not StringInStr($sExclude_list, $DELIM & $sFilespec_include & $DELIM) Then _
                    $aCode[$i] &= _merge_includes_recursive($sFilespec_include, $aInclude_dirs, $sExclude_list)
            Else
                $aCode[$i] &= "<<< file NOT found >>> : " & $aInclude_list[$i][$_icl_file_] & @LF ;; [later] not sure yet, trow error or drop include line back.
            EndIf
        Else
            ;; [AutoIt-MODE] [later]
;~          $aCode[$i] &= "<<< include-once >>> : (" & $sFilespec & ')' & @LF
        EndIf
    Next
    Return _merge_code_array($aCode)
EndFunc
Func _pre_merge_new_file_processing($sFilespec, ByRef $sExclude_list, ByRef $aInclude_list)
    If Not IsFile($sFilespec) Then Exit 96002 ;; also takes care of invalid filespec input at function _merge_includes_recursive()

    Local $sCode = FileRead($sFilespec) & @LF
    If @error Then Exit 96003 ;; locked file or something.
    $sCode = StringRegExpReplace($sCode, '\s*\R+\s*', @LF) ;; normelize text. (linefeeds, indentation, etc)
    $sCode = StringRegExpReplace($sCode, '(?m)^;.*$\n?', '') ;; del full comment lines.
    $sCode = StringRegExpReplace($sCode, '(?im)^#c(?:omments-(s)tart|omments-(e)nd)', '#c$1$2') ;; prep for next RE.
    $sCode = StringRegExpReplace($sCode, '(?im)^#cs(?<rec>(?<!^)#|[^#]|#cs\g<rec>*#ce)*?^#ce(?-s).*\n', '') ;; del comment blocks.
    $sCode = StringRegExpReplace($sCode, '(?im)^#[^i].*$\n?', '') ;; del other directive's, excluding #include directive's)
    $sCode = StringRegExpReplace($sCode, '(?m)^((?:[^''";]*([''"]).*?\2)*[^;]*);?.*$', '$1') ;; del trailing code comments.
    $sCode = StringRegExpReplace($sCode, '(?m)\h*$', '') ;; trailing spaces fix.
    $sCode = StringRegExpReplace($sCode, '(?m)\h+_$\n', ' ') ;; merge split lines.

;~  ;; experimental, compress unneeded (code) spaces. (not compatible with split lines(inside code), acts also on string data.)
;~  $sCode = StringRegExpReplace($sCode, '(?x)  \h*  (  [   = < >   ( \)  {}  [ \]   * + \- / ^   ,   &   ] )  \h*  ', '$1')

    ;; is include-once. [Obfucator-MODE] (incompatible with posible [AutoIt-MODE] at the moment)
    If StringRegExp($sCode, '(?im)^#include-once', 0) Then _
            $sExclude_list &= $sFilespec & $DELIM ;; add to exclude list.

    ;; collect include lines.
    $aInclude_list = _get_include_list($sCode)

    ;; split code on #include lines, while not including them. (trainling line feed on code assumed by other parts.)
    $sCode = StringRegExpReplace($sCode, '(?m)(^#).*?$', '$1') ;; get rid of trailing data at # lines.
    Return StringRegExp($sCode, '(?sm)(.*?)(?:#$\n)|(?:.+)', 3) ;; split +remove. (somewhat iffy RE, [later])
EndFunc
#region - secondary's
Func _get_include_list(ByRef Const $sCode) ;; (ByRef Const) not sure if its really needed to cut down memory use. [later]
    Local Enum $_icl_file_, $_icl_type_, $iFieldsIncludeList
    Local $aInclude_raw = StringRegExp($sCode, '(?im)(?:^#include\h*)(.*$)', 3) ;; extract include lines. (all, including '*-once')
    ;; ceate and fill output array
    Local $aInclude_list[UBound($aInclude_raw, 1)][$iFieldsIncludeList]
    For $i = 0 To UBound($aInclude_raw) - 1
        __set_include_list_data($aInclude_raw[$i], $aInclude_list[$i][$_icl_file_], $aInclude_list[$i][$_icl_type_])
    Next
    Return $aInclude_list
EndFunc
Func __set_include_list_data($sInclude_raw, ByRef $sInclude_File_out, ByRef $sInclude_type_out)
    Local Enum $_inc_once_ = -1, $_inc_global_first_, $_inc_local_first_
    Switch StringLeft($sInclude_raw, 1)
        Case '<'
            $sInclude_File_out = StringTrimLeft(StringTrimRight($sInclude_raw, 1), 1)
            $sInclude_type_out = $_inc_global_first_
        Case "'", '"'
            $sInclude_File_out = StringTrimLeft(StringTrimRight($sInclude_raw, 1), 1)
            $sInclude_type_out = $_inc_local_first_
        Case '-'
            $sInclude_type_out = $_inc_once_
        Case Else
            Exit 96001 ;; [later] unexpected character in #include line. (trow error or drop $include back)
    EndSwitch
EndFunc
Func _scan_include_dirs_for_include_file($sFilespec_in, $iLocalFirst, $aInclude_dirs)
    ;; returns filespec (of existing file). empty string if not found.
    Local Enum $_icl_file_, $_icl_type_, $iFieldsIncludeList
    #forceref $_icl_type_, $iFieldsIncludeList
    Local $iFirst = 1, $iLast = $aInclude_dirs[$_index_][$_index_], $iStep = 1
    ;; switch scan direction.
    If $iLocalFirst Then
        $iFirst = $iLast
        $iLast = 1
        $iStep = -1
    EndIf
    ;; scan, find first file match.
    Local $sFilespec_out
    For $i = $iFirst To $iLast Step $iStep
        $sFilespec_out = $aInclude_dirs[$i][$_icl_file_] & '\' & $sFilespec_in
        If IsFile($sFilespec_out) Then ExitLoop
        $sFilespec_out = ''
    Next
    Return $sFilespec_out
EndFunc
Func _merge_code_array(ByRef Const $aCode) ;; (ByRef Const) not sure if its really needed to cut down memory use. [later]
    Local $sCode = ''
    For $i = 0 To UBound($aCode, 1) - 1
        $sCode &= $aCode[$i]
    Next
    Return $sCode
EndFunc
#endregion - secondary's
#region - setup
Func GetIncludeDirs($AutoItExe = '') ;; ...
    ;; ! indexed array's.
    Local Enum $_path_, $_type_, $iFieldsIncludeDirs
    Local Enum $_icd_local_, $_icd_user_, $_icd_global_
    Local $iCount = 0
    ;; get/process User.Include.folders.
    Local $sUserDirs = RegRead('HKCU\Software\AutoIt v3\Autoit', 'Include')
    Local $asUserDirs = StringSplit($sUserDirs, ';')
    Local $aInclude_dirs[UBound($asUserDirs) + 2][$iFieldsIncludeDirs]
    ;; add global AutoIt3 directory. (first)
    If $AutoItExe And IsFile($AutoItExe) Then ;; (file exist check not really needed. old habit.)
        $iCount += 1
        $aInclude_dirs[$iCount][$_path_] = Filespec_ClipLast($AutoItExe) & '\include'
        $aInclude_dirs[$iCount][$_type_] = $_icd_global_
    EndIf
    ;; add user directory's.
    For $i = 1 To $asUserDirs[$_index_]
        If Not (StringStripWS($asUserDirs[$i], 3) == '') And Isfolder($asUserDirs[$i]) Then
            $iCount += 1
            $aInclude_dirs[$iCount][$_path_] = StringReplace($asUserDirs[$i], '"', '')
            $aInclude_dirs[$iCount][$_type_] = $_icd_user_
        EndIf
    Next
    ;; preset local entry. (dynamic updated)
    $iCount += 1
    $aInclude_dirs[$iCount][$_path_] = ''
    $aInclude_dirs[$iCount][$_type_] = $_icd_local_
    ;; update index.
    $aInclude_dirs[$_index_][$_path_] = $iCount
    Return $aInclude_dirs
EndFunc
Func GetAutoitLocation() ;; (source: AutoIt3Wrapper.au3.)
    Local $sAutoIt_Exe_Location
    If @Compiled Then
        If StringInStr(@OSArch, '64') Then $sAutoIt_Exe_Location = RegRead('HKLM64\Software\Microsoft\Windows\CurrentVersion\App Paths\AutoIt3.exe', '')
        If Not $sAutoIt_Exe_Location Then $sAutoIt_Exe_Location = RegRead('HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\AutoIt3.exe', '')
    Else
        $sAutoIt_Exe_Location = @AutoItExe
    EndIf
    If Not $sAutoIt_Exe_Location Then
        Return SetError(1, 0, '')
    ElseIf Not IsFile($sAutoIt_Exe_Location) Then
        Return SetError(2, 0, '')
    EndIf
    Return $sAutoIt_Exe_Location
EndFunc
#endregion - setup
#region - general's
Func IsFile($sFilespec)
    Return (FileExists($sFilespec) And Not StringInStr(FileGetAttrib($sFilespec), 'D'))
EndFunc
Func Isfolder($sFilespec)
    Return (FileExists($sFilespec) And StringInStr(FileGetAttrib($sFilespec), 'D'))
EndFunc
Func Filespec_ClipLast($sFilespec)
    Return StringLeft($sFilespec, StringInStr($sFilespec, '\', 0, -1) - 1)
EndFunc
#endregion - general's

PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Index ___ 'RE_Debug'

Showing RE behavior by showing RE capture results inside source text. (see 'test/example' code below.)

Personal: Personal Regular Expression visualizer/debugger function.

Related:

- [solved] StringRegExp, weird case. (spoiler in first message.)

Last minor code edit: ...

known items: ...

Todo:

- Take additional look at stats.

base function (needs updating)

;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -q

Func RE_Debug($sText, $sPattern, $fBreak = False, $fDelEmptyZero = False)

    ;; Default keyword parameter support.
    If IsKeyword($fBreak) And $fBreak = Default Then $fBreak = False
    If IsKeyword($fDelEmptyZero) And $fDelEmptyZero = Default Then $fDelEmptyZero = False

    Local Const $NL = @CRLF
    Local Const $sDelim1 = Chr(1), $sDelim2 = Chr(2) ;; internal used delimiter characters.
    Local Const $sDelim1_out = '[', $sDelim2_out = ']' ;; visual delimiters for final output result.

    Local $iLogDivide = 10, $iLogRepl = -1, $iLogUnused = -1 ;; stats vars

    Local $sResult = $sText, $sReplace
    $sText = ''

    ;; set capture feedback replace-pattern. (targeting only up to case/level 9)
    If $fBreak Then ;; - with added additional line-break per capture case. ;; '@CRLF[0:\0]@CRLF[1:\1]...[8:\8]@CRLF[9:\9]@CRLF'
        $sReplace = $NL
        For $i = 0 To 9
            $sReplace &= $sDelim1 & String($i) & ':$' & String($i) & $sDelim2 & $NL
        Next
    Else ;; - without additional line-breaks. ;; '[0:\0][1:\1][2:\2][3:\3][4:\4][5:\5][6:\6][7:\7][8:\8][9:\9]'
        $sReplace = ''
        For $i = 0 To 9
            $sReplace &= $sDelim1 & String($i) & ':$' & String($i) & $sDelim2
        Next
    EndIf

    ;; run it, get raw RE result.
    $sResult = StringRegExpReplace($sResult, $sPattern, $sReplace)
    If Not @error Then $iLogRepl = @extended ;; stats collection.
    If @error Then Return SetError(@error, @extended, $sResult) ;; report to caller. (presuming original RE pattern error)

    ;; removal of empty capture cases.
    ;; - potential issue: no way to tell if a empty case was actual a end-result or just a empty(not used/active) case. (lacking a example where this might matter.)
    ;;
    ;; - leaving '[0:]' cases. These are separately removed for RE-stats purpose.
    $sPattern = $sDelim1 & '[1-9]:' & $sDelim2
    If $fBreak Then $sPattern &= '(?>\r\n)'
    $sReplace = '\1'
    $sResult = StringRegExpReplace($sResult, $sPattern, $sReplace)
    If Not @error Then $iLogUnused = @extended ;; stats collection.
    If @error Then Return SetError(@error, @extended, $sResult) ;; report to caller. (internal code error(?) though)

    ;;  remove empty '[0:]' cases.
    If $fDelEmptyZero Then
        $sPattern = StringReplace($sPattern, '1', '0')
        $sPattern = StringReplace($sPattern, '9', '0') ;; just to make it a single step process.
        $sResult = StringRegExpReplace($sResult, $sPattern, $sReplace)
    EndIf

    ;; process stats.
    ;; - add some documentation about the stats. to abstract. (also 'Al' and 'No' not really needed.)
    ;; RE_Debug: Al=90, No=81, Re=9, \0=9, Ot=0 ... [0:][0:d][0:][0:a][0:][0:t][0:][0:a][0:],  data
    ;; RE_Debug: Al=90, No=86, Re=4, \0=9, Ot=-5 ... [0:d][0:a][0:t][0:a],  data
    Local $sStats = ''
    If $iLogRepl > 0 And $iLogUnused > 0 Then _
            $sStats = '- RE_Debug: Al=' & $iLogRepl & _
            ', No=' & $iLogUnused & _
            ', Re=' & $iLogRepl - $iLogUnused & _
            ', \0=' & $iLogRepl / $iLogDivide & _
            ', Ot=' & ($iLogRepl - $iLogUnused) - ($iLogRepl / $iLogDivide)

    ;; finalize output result. (change internal used delimiters to visual delimiters)
    $sResult = StringReplace($sResult, $sDelim1, $sDelim1_out)
    $sResult = StringReplace($sResult, $sDelim2, $sDelim2_out)

    Return $sStats & $NL & $sResult
EndFunc

test/example

Global $sText, $sPattern, $fBreak = False, $fDelEmptyZero = False, $sResult
$sText = '12345'
$sPattern = '3'         ;; Al=10, No=9,  Re=1, \0=1, Ot=0 ;; 12[0:3]45
;~ $sPattern = '[^3]'   ;; Al=40, No=36, Re=4, \0=4, Ot=0 ;; [0:1][0:2]3[0:4][0:5]
;~ $sPattern = '[^3]+'  ;; Al=20, No=18, Re=2, \0=2, Ot=0 ;; [0:12]3[0:45]
;~ $sPattern = '[^3]*'  ;; Al=40, No=36, Re=4, \0=4, Ot=0 ;; [0:12][0:]3[0:45][0:]
;~ $sPattern = '9*'     ;; Al=60, No=54, Re=6, \0=6, Ot=0 ;; [0:]1[0:]2[0:]3[0:]4[0:]5[0:]
;~ $fBreak = True
;~ $fDelEmptyZero = True
$sResult = RE_Debug($sText, $sPattern, $fBreak, $fDelEmptyZero)
MsgBox(0, 'MsgBox', $sResult)
;~ ConsoleWrite($sResult & @CRLF)


Single file RE testing setup

;~ #AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 6 -q

RE_code_test_area()

Func RE_code_test_area()

    Local $1, $2, $3, $4, $5, $6, $7, $8, $9, $0
    Local $sText, $sPattern, $fBreak = False, $fDelEmptyZero = False
;~  #forceref $1, $2, $3, $4, $5, $6, $7, $8, $9, $0
;~  #forceref $sText, $sPattern, $fBreak, $fDelEmptyZero
    DebugOut()

;~  $fBreak = True
;~  $fDelEmptyZero = True

    ;; --- --- ---

    $0 = '(?imx)' ;; ?x -> escape '#' -> '\#'
    $1 = ' \#cs  (?<rec>  [^#]*  |  \#cs  \g<rec>*  \#ce  )*  \#ce '

    $sPattern = $0 & $1

    $sText = ''
    $sText &= 'aaa\n'
    $sText &= '#cs bbb\n'
    $sText &= '#cs bbb\n'
    $sText &= '#ce bbb\n'
    $sText &= '#ce bbb\n'
    $sText &= 'aaa\n'

    RE_Out($sText, $sPattern, $fBreak, $fDelEmptyZero)

EndFunc

;; support functions.
Func RE_Out($sText, $sPattern, $fBreak = False, $fDelEmptyZero = False, $fLFshort = True)
    $sText = StringReplace(StringReplace(StringReplace($sText, '\t', @TAB), '\r', @CR), '\n', @LF)
    DebugOut2('- $sPattern = ' & QuotIt($sPattern))
    Local $result = RE_Debug2($sText, $sPattern, $fBreak, $fDelEmptyZero)
    If @error Then
        DebugOut2(RE_ErrorPos(@extended, '- $sPattern = "') & @CRLF & '! err: ' & @error & ', ext: ' & @extended)
    Else
        DebugOut2()
        DebugOut2('$sText = ' & QuotIt(ShowLinefeeds($sText, $fLFshort)))
        DebugOut2()
        DebugOut2('$result: ' & ShowLinefeeds($result, $fLFshort))
    EndIf
    DebugOut2()
EndFunc
Func DebugOut2($msg = '', $err = @error, $ext = @extended)
    ConsoleWrite($msg & @CRLF)
    Return SetError($err, $ext, 0)
EndFunc
Func QuotIt($sText, $sQuot = '"')
    If $sQuot = "'" Or $sQuot = 1 Then Return "'" & StringReplace($sText, "'", "''") & "'"
    Return '"' & StringReplace($sText, '"', '""') & '"' ;; If $sQuot = '"' Or $sQuot = 2 Then
EndFunc
Func ShowLinefeeds($text, $fShort = True)
    Local Const $R = @CR, $N = @LF, $T = @TAB, $RN = @CRLF
    If $fShort Then Return StringReplace(StringReplace(StringReplace($text, $R, '\r'), $N, '\n'), $T, '\t')
    Local Const $D1 = Chr(1), $D2 = Chr(2), $D1out = '<', $D2out = '>'
    $text = StringReplace(StringReplace($text, $R, $D1 & 'r' & $D2), $N, $D1 & 'n' & $D2)
    $text = StringReplace($text, $D1 & 'r' & $D2 & $D1 & 'n' & $D2, $D1out & 'CRLF' & $D2out & $R & $N)
    $text = StringReplace($text, $D1 & 'r' & $D2, $D1out & 'CR' & $D2out & $R)
    $text = StringReplace($text, $D1 & 'n' & $D2, $D1out & 'LF' & $D2out & $N)
    $text = StringReplace($text, $T, $D1out & 'VT' & $D2out & $T)
    Return $text
EndFunc
Func RE_Debug2($sText, $sPattern, $fBreak = False, $fDelEmptyZero = False)
    Local Const $NL = @CRLF, $sDelim1 = Chr(1), $sDelim2 = Chr(2)
    Local $iMaxCap = 10, $iLogRepl = -1, $iLogUnused = -1
    Local $sReplace = '[0:$0][1:$1][2:$2][3:$3][4:$4][5:$5][6:$6][7:$7][8:$8][9:$9]'
    If $fBreak Then $sReplace = StringReplace($sReplace, '[', $NL & '[') & $NL
    $sReplace = StringReplace(StringReplace($sReplace, '[', $sDelim1), ']', $sDelim2)
    $sText = StringRegExpReplace($sText, $sPattern, $sReplace)
    If @error Then Return SetError(@error, @extended, $sText)
    $iLogRepl = @extended
    $sPattern = $sDelim1 & '[1-9]:' & $sDelim2
    If $fBreak Then $sPattern &= '(?>\r\n)'
    If $fDelEmptyZero Then $sPattern = StringReplace($sPattern, '[1-9]', '0')
    $sText = StringRegExpReplace($sText, $sPattern, '\1')
    If @error Then Return SetError(@error, @extended, $sText)
    $iLogUnused = @extended
    DebugOut2('+ series=' & Round($iLogRepl / $iMaxCap, 2) & ', singles=' & $iLogRepl - $iLogUnused)
    Return StringReplace(StringReplace($sText, $sDelim1, '['), $sDelim2, ']')
EndFunc
Func RE_ErrorPos($iPos, $sLead, $err = @error, $ext = @extended) ;; single line RE code. (no RE comments)
    Local $sOut = StringRegExpReplace($sLead, '(?Us).', '-'), $scode = '.', $sErr = '^'
    For $i = 1 To $iPos - 1
        $sOut &= $scode
    Next
    Return SetError($err, $ext, $sOut & $sErr)
EndFunc

PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

  • 1 month later...

Index ___ 'Reserved'

<short description>.

Personal: ...

Related/Credits: ...

Last edit: ...

known items: ...

Todo: ...

<type>

;~ #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -q

PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

update

Well kinda ...

Add: AU3_BareSource. (split of from Au3_BareCode_Merge into separate general udf.)

- Strips given Au3 source down to bare code. (code normalizer)


General (minor) updates log.

- 16 Dec 2010 Fixed internal topic links to messages. (odd, I'm sure I tested them.)


PS: Topic/Message/Code no longer maintained. (-> goto help section for questions)

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...