Jump to content

_FileReadToArrayEx


DXRW4E
 Share

Recommended Posts

; #CONSTANTS# ====================================================
#Region ;**** Global vars for _FileReadToArrayEx
Global Const $FRTA_NOCOUNT         = 1
Global Const $FRTA_ARRAYFIELD      = 2
Global Const $FRTA_STRIPLEADING    = 4
Global Const $FRTA_STRIPTRAILING   = 8
Global Const $FRTA_STRIPLINEEMPTY  = 16
Global Const $FRTA_CHECKSINGEQUOTE = 32
Global Const $FRTA_STRIP_TRAILINGLEADING  = BitOR($FRTA_STRIPLEADING, $FRTA_STRIPTRAILING) ;INTERNALLY USE ONLY
Global Const $FRTA_STRIPALL        = BitOR($FRTA_STRIPLEADING, $FRTA_STRIPTRAILING, $FRTA_STRIPLINEEMPTY)
#EndRegion ;**** Global vars for _FileReadToArrayEx
; ================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _FileReadToArrayEx
; Description ...: Reads the specified file into an array.
; Parameters ....: $sFilePath - Path and filename of the file to be read.
;                 $sDelim     - Optional, Fiels separator character, Default is ',', This parameter can not be '"' or "'"  or ';' or Whitespace character
;                 $iFlags     - Optional, Flag to indicate the type of stripping that should be performed (add the flags together for multiple operations)
;                 |  Default (0) - Return All line)s and set array count in the 0th index
;                 |$FRTA_NOCOUNT (1)          - Don't return the array count
;                 |$FRTA_ARRAYFIELD (2)       - Array field Mod
;                 |$FRTA_STRIPLEADING (4)     - Strip Line leading white space
;                 |$FRTA_STRIPTRAILING (8)    - Strip Line trailing white space
;                 |$FRTA_STRIPLINEEMPTY (16)  - Don't return Line empty (ignores @CRLF & @CRLF & @CRLF ect ect)
;                 |$FRTA_CHECKSINGEQUOTE (64) - Check also Single Quote "'" for compatibility with AU3 file, Default check Double Quote only
;                                                This Flags will be ignored if the $FRTA_ARRAYFIELD is not set\used,
; Return values .: If the function succeeds, it returns Array (Check @extended for array count); otherwise, 0.
;                  @error  - 0 = No error
;                  |1 = File does not exist (or has not $GENERIC_READ Security Access Rights) or No line\fiels found
; Author ........: DXRW4E
; ===============================================================================================================================
Func _FileReadToArrayEx($sFilePath, $sDelim = "", $iFlags = 0)
    If BitAND($iFlags, $FRTA_ARRAYFIELD) Then
        Local $sData = StringTrimLeft(StringTrimRight(StringRegExpReplace(@LF & FileRead($sFilePath) & @LF, "[\s\x0]*[\r\n][\s\x0]*+", @LF), 1), 1)
        If Not $sData Then Return SetError(1, 0, 0)
        If Not $sDelim Then $sDelim = ","
        If BitAND($iFlags, $FRTA_CHECKSINGEQUOTE) Then
            $sData = StringRegExpReplace($sData, "(?:[^" & $sDelim & '\s"''\x0]+|''[^'']*''|"[^"]*"|[\h\f\xb\x0]*+(?!\n|' & $sDelim & "))*+\K[\h\f\xb\x0]*+" & $sDelim & "[\h\f\xb\x0]*+", @CR)
        Else
            $sData = StringRegExpReplace($sData, "(?:[^" & $sDelim & '\s"\x0]+|"[^"]*"|[\h\f\xb\x0]*+(?!\n|' & $sDelim & "))*+\K[\h\f\xb\x0]*+" & $sDelim & "[\h\f\xb\x0]*+", @CR)
        EndIf
        Local $STR_COUNT = (BitAND($iFlags, $FRTA_NOCOUNT) ? 3 : 1)
        $sData = StringSplit($sData, @LF, $STR_COUNT)
        Local $iaData = ($STR_COUNT = 1 ? $sData[0] : UBound($sData) - 1), $_iaData = ($STR_COUNT = 1 ? 1 : 0)
        For $i = $_iaData To $iaData
            $sData[$i] = StringSplit($sData[$i], @CR, $STR_COUNT)
        Next
        Return SetError(0, $iaData, $sData)
    Else
        Local $iCount = 0, $sData = BitAND($iFlags, $FRTA_NOCOUNT) ? FileRead($sFilePath) : "0" & @LF & FileRead($sFilePath)
        If Not BitAND($iFlags, $FRTA_STRIPALL) Then
            $sData = StringRegExp($sData, "(?s)([^\r\n]*+)\r?+\n?+", 3)
        ElseIf BitAND($iFlags, $FRTA_STRIPALL) = $FRTA_STRIPALL Then
            $sData = StringRegExp($sData, "(?s)[\s\x0]*+((?:[^\s\x0]+|[\h\f\xb\x0]*+(?![\r\n]|$))+)[\s\x0]*+", 3)
        Else
            $sData = StringRegExp($sData, (BitAND($iFlags, $FRTA_STRIPLINEEMPTY) ? "(?s)[\r\n]*+" : "(?s)") & (BitAND($iFlags, $FRTA_STRIPLEADING) ? "[\h\f\xb\x0]*+" : "") & (BitAND($iFlags, $FRTA_STRIPTRAILING) ? "((?:[^\s\x0]+|[\h\f\xb\x0]*+(?![\r\n]|$))*+)[\h\f\xb\x0]*+" : "([^\r\n]*+)") & (BitAND($iFlags, $FRTA_STRIPLINEEMPTY) ? "[\r\n]*+" : ""), 3)
        EndIf
        If @Error Then Return SetError(1, 0, 0)
        $iCount = UBound($sData)
        If Not BitAND($iFlags, $FRTA_NOCOUNT) Then $sData[0] = $iCount - 1
        Return SetError(0, $iCount, $sData)
    EndIf
EndFunc ;==>_FileReadToArrayEx
#include <Array.au3>
#include "_FileReadToArrayEx.au3"

FileDelete(@DesktopDir & '\Test.inf')
Local $sData,  $aArray, $fTimerDiff
For $i = 1 to 50000
    $sData &= "Line - " & $i & ', "1  ,  ,  , ",2 ,'',,,3'',4 ,5      ' & @CRLF
Next
$sData &= @CRLF & @CRLF & "       " & @CRLF  & @CRLF & "    "  & @CRLF
For $i = 1 to 50000
    $sData &= "Line - " & $i & ", 1,2,3,4,5,6,7,8,9,10" & @CRLF
Next
FileWrite(@DesktopDir & '\Test.inf', $sData)

Local $aArray
$fTimerDiff = TimerInit()
$aArray = _FileReadToArrayEx(@DesktopDir & '\Test.inf')
$fTimerDiff = TimerDiff($fTimerDiff)
ConsoleWrite("_FileReadToArrayEx() : " & $fTimerDiff & @CRLF)
_ArrayDisplay($aArray)

$fTimerDiff = TimerInit()
$aArray = _FileReadToArrayEx(@DesktopDir & '\Test.inf', ",", $FRTA_ARRAYFIELD + $FRTA_STRIPALL + $FRTA_CHECKSINGEQUOTE)
$fTimerDiff = TimerDiff($fTimerDiff)
ConsoleWrite("_FileReadToArrayEx() : " & $fTimerDiff & @CRLF)
_ArrayDisplay($aArray)
_ArrayDisplay(($aArray[1]))
_ArrayDisplay(($aArray[50055]))

Example, how to read in the loop ArrayField mod, or read arrays of arrays, this is normally done Thanks to the Array access on expression

#include "_FileReadToArrayEx.au3"

Local $sData,  $aArray, $fTimerDiff
FileDelete(@DesktopDir & '\Test.inf')
For $i = 1 to 1000
    $sData &= "Line - " & $i & ', "1  ,  ,  , ",2 ,'',,,3'',4 ,5      ' & @CRLF
Next
$sData &= @CRLF & @CRLF & "       " & @CRLF  & @CRLF & "    "  & @CRLF
For $i = 1 to 1000
    $sData &= "Line - " & $i & ", 1,2,3,4,5,6,7,8,9,10" & @CRLF
Next
FileWrite(@DesktopDir & '\Test.inf', $sData)

$aArray = _FileReadToArrayEx(@DesktopDir & '\Test.inf', ",", BitOR($FRTA_ARRAYFIELD, $FRTA_STRIPALL))
If Not @Error Then
    For $i = 1 To $aArray[0]
        For $y = 1 To ($aArray[$i])[0]
            $sData &= ($aArray[$i])[$y] & ","
        Next
        $sData = StringTrimRight($sData, 1) & @CRLF
    Next
EndIf
ConsoleWrite($sData & @LF)
$sData = ''

$aArray = _FileReadToArrayEx(@DesktopDir & '\Test.inf', ",", BitOR($FRTA_NOCOUNT, $FRTA_ARRAYFIELD, $FRTA_STRIPALL))
If Not @Error Then
    ;;Local $iaArray = UBound($aArray) - 1   ; or $iaArray = @Extended
    For $i = 0 To UBound($aArray) - 1
        For $y = 0 To UBound($aArray[$i]) - 1
            $sData &= ($aArray[$i])[$y] & ","
        Next
        $sData = StringTrimRight($sData, 1) & @CRLF
    Next
EndIf
ConsoleWrite($sData & @LF)

MsgBox(0, ($aArray[1])[0],($aArray[1])[1])

Ciao.

_FileReadToArrayEx.au3

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Link to comment
Share on other sites

  • Moderators

GrowBigTrees,

Those are part of the ternary operator syntax - you need to upgrade to 3.3.10.2. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...