Archive: Linefind text insert issue


Linefind text insert issue
Has anyone else noticed that inserting text using linefind into a file doesn't work correctly anymore. I figured it was just me so I ran the example in the documentation (Example4 (insert lines)) and found that it does not work as well. It seems to occur only when you want to write to the file and output the changes to the same filename. This did work before. I plan on looking into this but wanted to throw it out there to see if anyone noticed. Any help would be appreciated.


The first example works fine for me and the tests pass. What exactly doesn't work for you?



Section
${LineFind} "C:\a.log" "" "10" "Example4"
IfErrors 0 +2
MessageBox MB_OK "Error"
SectionEnd

Function Example4
FileWrite $R4 "---First Line---$\r$\n"
FileWrite $R4 "---Second Line ...---$\r$\n"

Push $0
FunctionEnd



This code does not insert the the two lines into c:\a.log. I ran the code and look in c:\a.log that I created before hand and nothing is inserted. If I change the output to a2.log the text is inserted into a2.log.

Code works fine for me. It inserts those two lines of text right before the 10th line.


I know why I was seeing the issue with the example. If the file is completely empty it doesn't insert the text. If there is an actual 10th line in the file it will insert.

The thing I don't get is I had some functionality that used the linefind that worked and just stopped working. It had a linefind call within a linefind (not sure if this matters) call. Like this:



Section test

${LineFind} "c:\file.txt" "c:\file.txt" "1:-1" "fileReplace"

SectionEnd

Function fileReplace

;String to search for
StrCpy $R0 "test="

${LineFind} "c:\file.txt" "/NUL" "1:-1" "FindStringInString"

;String to insert
StrCpy $R0 "test=test$\r$\n"

${LineFind} "c:\file.txt" "" "$R1" "FileTextInsert"

Push $0

FunctionEnd

;Writes the string provided in $R0
function FileTextInsert

;Write the specified string ($R0) to the file specified by linefind ($R4)
FileWrite $R4 $R0

Push $0

functionend

;Searches a given string and assignes $R1 the line number where it was found + 1
Function FindStringInString

;$R9 is the path name return by locate. It equals the path name for the current file that was found
${TrimNewLines} '$R9' $R9

;Search the current line to see if specified value is there
${WordFind} "$R9" "$R0" "+1{" $1

;If string to search is not equal to output string then line was not found.
${IfNot} $R9 == $1

;Sets the $0 to contain a string that stops
StrCpy $0 StopLineFind

;Sets $R1 with the line number found
StrCpy $R1 $R8

;Increment $R1 to the next line number
IntOp $R1 $R1 + 1

${EndIf}

;Search the next line
StrCpy $R9 "$R9$\r$\n"

Push $0

FunctionEnd




I have excluded some code to make it cleaner. The problem I have is this code worked for me before and now it doesn't. I was able to get it working by calling fileReplace directly. I do realize that the way above is unnecessary and it isn't really necessary to fix.