jaberwacky Posted August 9, 2013 Share Posted August 9, 2013 (edited) 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 expandcollapse popup#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 May 16, 2014 by jaberwacky mLipok 1 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
guinness Posted August 9, 2013 Share Posted August 9, 2013 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 parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
jaberwacky Posted August 9, 2013 Author Share Posted August 9, 2013 The way I see it, $path could be a path to any file. Thanks for the info about zero. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted August 10, 2013 Author Share Posted August 10, 2013 Updated. More functions, less bugs. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted August 11, 2013 Author Share Posted August 11, 2013 Updated. Actually works now plus a couple more useful constants. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted August 12, 2013 Author Share Posted August 12, 2013 Updated. See op for more detail. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now