To allow working with strings also, I suggest following:
Code:
; Created by Tuncay *2009-09-19*
; TextFile can be a path to an existing file or a variable holding the data of a file.
; ErrorLevel holds the line count of TextFile.
; Returns 0 if TextFile is leaved as is and 1 if the content of an existing
; file is read into that variable.
TF_LoadTextFile(ByRef TextFile)
{
StringReplace, TextFile, TextFile, `n, `n, UseErrorLevel
If (ErrorLevel = 0 AND FileExist(TextFile))
{
FileRead, TextFile, %TextFile%
StringReplace, TextFile, TextFile, `n, `n, UseErrorLevel
If (TextFile != "")
ErrorLevel++
Return, 1
}
Else
{
If (TextFile != "")
ErrorLevel++
Return, 0
}
}
; Replaced by Tuncay *2009-09-19*.
TF_CountLines(TextFile) ; was TotalLines in v1
{
TF_LoadTextFile(TextFile)
Return ErrorLevel
}
TF_ReadLines(TextFile, StartLine = 1, EndLine = 0)
{
TF_MatchList:=_MakeMatchList_(TextFile, StartLine, EndLine) ; create MatchList
TF_LoadTextFile(TextFile) ;Edited.
Loop, Parse, TextFile, `n, `r ;Edited.
{
If A_Index in %TF_MatchList%
OutPut .= A_LoopField "`n"
Else if (A_Index => EndLine)
Break
}
StringTrimRight, OutPut, OutPut, 1 ;?Added: Trim last new line from loop.
Return OutPut
}
The TextFile variable is checked if it is a path without new lines, then if it is an existing file, the content will be used as TextFile. That has the advantage that we can use the function with strings also, not only with files on disk.
In a quick review I changed the CountLines to work with strings also. Now it counts correctly (it did not worked correct before I think) and the ReadLines had added a last not needed new line character, which is trimmed by me. The TF_LoadTextFile(ByRef TextFile) can be used at any place where FileRead, Str, %TextFile% is in use.
I did not looked at other functions, they seem to complicated to me to touch them. Hope you adapt this to other functions.
The only problem with this function is, if TextFile should be a string (just after FileRead a textfile) holding just one line of an existing path... that would be loaded. This is very rare, so it is fair enough to use this.
On a sidenote, I am currently not using this set of functions (may be in future I would). So I am not sure currently about the dependency of functions and how far the changed would break it.