Jump to content
Sign in to follow this  
jaberwacky

File Stream Functions 08/12/2013

Recommended Posts

jaberwacky

I cooked up this file stream thingy.  I don't like continually dealing with the file stuff.  I found it helpful, so I figured someone else might too.   It doesn't display any amazing code acrobatics but whatever.

To use it just feed it a path to a file and call it repeatedly.  The next line in the file is returned.  To close a file before the EOF is reached then call the same function with $file_stream_close.

Update: [05/15/2014] -- Reduced code duplication.

[unknown date] -- Added string stream read functions and their corresponding constants.  Didn't do the string write functions because it's kind of even less pointless than these already are.

Functions:
    file_stream_read_line
    file_stream_read_char
    file_stream_append_line
    file_stream_append_char
    file_stream_overwrite_line
    file_stream_overwrite_char
    string_stream_read_line
    string_stream_read_char

Constants: 
    $file_stream_close
    $file_stream_eof 
    $file_stream_other_error 
    $string_stream_close 
    $string_stream_eos 
    $string_stream_delim_not_found

Stream.au3

#AutoIt3Wrapper_AU3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 6 -w 7 -d

#include-once

#include <File.au3>

#region ; String Stream

Global Const $string_stream_close = -2
Global Const $string_stream_eos = -1
Global Const $string_stream_delim_not_found = -1

; Internal
Global Const $string_stream_ready = -3

Func __string_stream_test(Const $string, ByRef $string_array)
    Switch $string
        Case $string_stream_close
            $string_array = $string_stream_ready
            Return True
  EndSwitch
 
  Return False
EndFunc

Func string_stream_read_line(Const $string)
    Local Static $string_array = StringSplit($string, @LF)

    If __string_stream_test($string, $string_array) Then Return True

    Local Static $element = 0

    Select
        Case $string_array = $string_stream_ready
            $string_array = StringSplit($string, @LF)

            $element = 0

            ContinueCase

        Case Not @error
            $element += 1

            Switch $element > $string_array[0]
                Case True
                    $string_array = $string_stream_ready
                    $element = 0
                    Return SetError($string_stream_eos, 0, False)
            EndSwitch

            Return $string_array[$element]

        Case $string_stream_delim_not_found
            $string_array = $string_stream_ready
            $element = 0
            Return SetError($string_stream_delim_not_found, 0, False)
    EndSelect
EndFunc

Func string_stream_read_char(Const $string)
    Local Static $string_array = StringSpLit($string, '')

    If __string_stream_test($string, $string_array) Then Return True

    Local Static $element = 0

    Select
        Case $string_array = $string_stream_ready
            $string_array = StringSplit($string, '')

            $element = 0

            ContinueCase

        Case Not @error
            $element += 1

            Switch $element > $string_array[0]
                Case True
                    $string_array = $string_stream_ready
                    $element = 0
                    Return SetError($string_stream_eos, 0, False)
            EndSwitch

            Return $string_array[$element]

        Case $string_stream_delim_not_found
            $string_array = $string_stream_ready
            $element = 0
            Return SetError($string_stream_delim_not_found, 0, False)
    EndSelect
EndFunc

#endregion

#region ; File Stream

Global Const $file_stream_close = -2
Global Const $file_stream_eof = -1
Global Const $file_stream_other_error = 1

; Internal
Global Const $file_stream_ready = -3
Global Const $file_stream_open_error = -1

Func __file_stream_test(Const $path, ByRef $file_open)
    Switch $path
        Case $file_stream_close
      FileClose($file_open)
      $file_open = $file_stream_ready
      Return True
  EndSwitch
 
  Return False
EndFunc

Func file_stream_read_line(Const $path)
  Local Static $file_open = FileOpen($path, $FO_READ)
 
  If __file_stream_test($path, $file_open) Then Return True

  Select
        Case $file_open = $file_stream_open_error
            $file_open = $file_stream_ready
            Return SetError(2, 1, False)

        Case $file_open = $file_stream_ready
      $file_open = FileOpen($path, $FO_READ)

      Switch $file_open
                Case $file_stream_open_error
                    $file_open = $file_stream_ready
          Return SetError(2, 2, False)
      EndSwitch

      ContinueCase

    Case $file_open >= 0
      Local Const $file_line = FileReadLine($file_open)

      Switch @error
        Case 0
          Return $file_line

        Case $file_stream_eof
          FileClose($file_open)
          $file_open = $file_stream_ready
          Return SetError($file_stream_eof, 0, False)

        Case $file_stream_other_error
          FileClose($file_open)
          $file_open = $file_stream_ready
          Return SetError($file_stream_other_error, 0, False)
            EndSwitch
  EndSelect
EndFunc

Func file_stream_read_char(Const $path)
  Local Static $file_open = FileOpen($path, $FO_READ)
 
  If __file_stream_test($path, $file_open) Then Return True

  Select
        Case $file_open = $file_stream_open_error
            $file_open = $file_stream_ready
            Return SetError(2, 1, False)

    Case $file_open = $file_stream_ready
      $file_open = FileOpen($path, $FO_READ)

      Switch $file_open
                Case $file_stream_open_error
                    $file_open = $file_stream_ready
          Return SetError(2, 2, False)
      EndSwitch

      ContinueCase

    Case $file_open >= 0
      Local Const $file_line = FileRead($file_open, 1)

      Switch @error
        Case 0
          Return $file_line

        Case $file_stream_eof
          FileClose($file_open)
          $file_open = $file_stream_ready
          Return SetError($file_stream_eof, 0, False)

        Case $file_stream_other_error
          $file_open = $file_stream_ready
          Return SetError($file_stream_other_error, 0, False)
      EndSwitch
  EndSelect
EndFunc

Func file_stream_overwrite_line(Const $line, Const $path)
  Local Static $file_open = FileOpen($path, $FO_OVERWRITE)

    Switch $path
        Case $file_stream_close
      FileClose($file_open)
      $file_open = $file_stream_ready
      Return True
    EndSwitch

  Select
        Case $file_open = $file_stream_open_error
            $file_open = $file_stream_ready
            Return SetError(2, 1, False)

    Case $file_open = $file_stream_ready
      $file_open = FileOpen($path, $FO_READ)

      Switch $file_open
        Case $file_stream_open_error
          Return SetError(2, 2, False)
      EndSwitch

      ContinueCase

    Case $file_open >= 0
      Local Const $file_line = FileWriteLine($file_open, $line)

      Switch @error
        Case 0
          Return $file_line

        Case $file_stream_eof
          FileClose($file_open)
          $file_open = $file_stream_ready
          Return SetError($file_stream_eof, 0, False)

        Case $file_stream_other_error
          $file_open = $file_stream_ready
          Return SetError($file_stream_other_error, 0, False)
      EndSwitch
  EndSelect
EndFunc

Func file_stream_overwrite_char(Const $char, Const $path)
  Local Static $file_open = FileOpen($path, $FO_OVERWRITE)

    Switch $path
        Case $file_stream_close
      FileClose($file_open)
      $file_open = $file_stream_ready
      Return True
    EndSwitch

  Select
        Case $file_open = $file_stream_open_error
            $file_open = $file_stream_ready
            Return SetError(2, 1, False)

    Case $file_open = $file_stream_ready
      $file_open = FileOpen($path, $FO_READ)

      Switch $file_open
        Case $file_stream_open_error
          $file_open = $file_stream_ready
          Return SetError(2, 2, False)
      EndSwitch

      ContinueCase

    Case $file_open >= 0
      Local Const $file_line = FileWrite($file_open, $char)

      Switch @error
        Case 0
          Return $file_line

        Case $file_stream_eof
          FileClose($file_open)
          $file_open = $file_stream_ready
          Return SetError($file_stream_eof, 0, False)

        Case $file_stream_other_error
          $file_open = $file_stream_ready
          Return SetError($file_stream_other_error, 0, False)
      EndSwitch
  EndSelect
EndFunc

Func file_stream_append_line(Const $line, Const $path)
  Local Static $file_open = FileOpen($path, $FO_APPEND)

    Switch $path
        Case $file_stream_close
      FileClose($file_open)
      $file_open = $file_stream_ready
      Return True
    EndSwitch

  Select
        Case $file_open = $file_stream_open_error
            $file_open = $file_stream_ready
            Return SetError(2, 1, False)

    Case $file_open = $file_stream_ready
      $file_open = FileOpen($path, $FO_READ)

      Switch $file_open
        Case $file_stream_open_error
          $file_open = $file_stream_ready
          Return SetError(2, 2, False)
      EndSwitch

      ContinueCase

    Case $file_open >= 0
      Local Const $file_line = FileWriteLine($file_open, $line)

      Switch @error
        Case 0
          Return $file_line

        Case $file_stream_eof
          FileClose($file_open)
          $file_open = $file_stream_ready
          Return SetError($file_stream_eof, 0, False)

        Case $file_stream_other_error
          $file_open = $file_stream_ready
          Return SetError($file_stream_other_error, 0, False)
      EndSwitch
  EndSelect
EndFunc

Func file_stream_append_char(Const $char, Const $path)
  Local Static $file_open = FileOpen($path, $FO_APPEND)

    Switch $path
        Case $file_stream_close
      FileClose($file_open)
      $file_open = $file_stream_ready
      Return True
    EndSwitch

  Select
        Case $file_open = $file_stream_open_error
            $file_open = $file_stream_ready
            Return SetError(2, 1, False)

    Case $file_open = $file_stream_ready
      $file_open = FileOpen($path, $FO_READ)

      Switch $file_open
        Case $file_stream_open_error
          $file_open = $file_stream_ready
          Return SetError(2, 2, False)
      EndSwitch

      ContinueCase

    Case $file_open >= 0
      Local Const $file_line = FileWrite($file_open, $char)

      Switch @error
        Case 0
          Return $file_line

        Case $file_stream_eof
          FileClose($file_open)
          $file_open = $file_stream_ready
          Return SetError($file_stream_eof, 0, False)

        Case $file_stream_other_error
          $file_open = $file_stream_ready
          Return SetError($file_stream_other_error, 0, False)
      EndSwitch
  EndSelect
EndFunc

#endregion

Example:

Local Const $path = "[YOUR PATH HERE]"

Local $file_stream = ''

Do
  $file_stream = file_stream_read_line($path)

    Switch @error
        Case $file_stream_close, $file_stream_other_error
            ExitLoop
        
        Case Else
            ConsoleWrite("File: " & $file_stream & @CRLF)
    EndSwitch
Until False

Enjoy!

Edited by jaberwacky

Share this post


Link to post
Share on other sites
guinness

Good example usage of using Static. By the way 0 could be a "valid handle" (not the correct term, as it's an int) as AutoIt uses this to reference the iopen handle in (what I can only presume) is an array of sorts.

I'm just curious, is there a huge difference to say using FileReadLine(@ScriptFullPath) and letting AutoIt internally deal with the handle?


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
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
Sign in to follow this  

×