Archive: replacing a str


replacing a str
  I need a script or function that searches an string on each line of an file and replaces that string to another string. And the file is not an *.ini file

Can someone help me with this


http://forums.winamp.com/showthread....hreadid=109670


replacing $INSTDIR for ServerRoot
  replacing $INSTDIR for ServerRoot as an string that mutiple times must be done help!!!


I would do it that way :

-open the file (source)
-open another file (target)
-while you don't find the string, copy the characters to the new file
-if you encounter the string, write the string you want as a replacement to the target file, and start reading from the sourcefile after the end of the string you encountered...
-do that until you have reached the end of the sourcefile...

--> It uses a lot of strcmp's and you should maybe come up with a good string searching or mathing routine...

KIM


A string search function has been included in the NSIS 2 documentation (Useful functions appendix).


I don't think it is very good for this purpose, but you could modify it...


????
 

Originally posted by rarefluid
I would do it that way :

-open the file (source)
-open another file (target)
-while you don't find the string, copy the characters to the new file
-if you encounter the string, write the string you want as a replacement to the target file, and start reading from the sourcefile after the end of the string you encountered...
-do that until you have reached the end of the sourcefile...

--> It uses a lot of strcmp's and you should maybe come up with a good string searching or mathing routine...

KIM
Can you give me an example because i am just a beginner and i don't get the point with the third and the fourth thing what to do?

So here it is. More like brute force... ;-)


;I didn't check if this compiles, and I guess it won't.
;But you'll get the idea...
;It is VERY unefficient, but I wanted to keep it easy.
;it saves no variables to the stack and so on...
Function read_file
FileOpen $R9 "sourcefilename" "r"
StrCpy $R8 0 ;position in sourcefile

FileOpen $R7 "targetfilename" "w"

Strcpy $R1 "stringtosearchfor"
StrLen $R0 $R1 ;get length of searchstring
StrCpy $R2 $R1 1 ;get first character of searchstring

Strcpy $R4 "replacementstring_xyz"

goto searchloop
writechar:
FileWrite $R7 $R6
searchloop:
Fileread $R9 $R6 1 ;read character from file
IntOp $R8 $R8 + 1 ;increase position counter
StrCmp $R6 "" "" fileend ;have we reached the end of the file?
StrCmp $R6 $R2 "" writechar ;compare character to first character from searchstring
;HERE, the first character of the string has been found!
IntOp $R8 $R8 - 1 ;decrease position
FileSeek $R9 -1 CUR ;seek back in file
FileRead $R9 $R5 $R0 ;Read string with length of search string from file
StrCmp $R5 $R1 found ;is the string there in the file?
;HERE, the string has NOT been found
Intop $R8 $R8 + 1 ;increase original position +1
FileSeek $R9 $R8 ;seek original position +1 in file
goto writechar ;and continue searching
;HERE, the string has been found!
found:
FileWrite $R7 $R4 ;write replacement string to file
IntOp $R8 $R8 + $R0 ;set file postition to
Intop $R8 $R8 + 1 ;1 character behind the string we've found
goto searchloop
;HERE, the end of the file has been reached
fileend:
FileClose $R7
FileClose $R9
FunctionEnd


So, after this you have a new file "targetfilename" which contains the strings "searchstring" replaced by "replacementstring_xyz".
Hope it helps you!

P.S. : If you have a file with CRLF (linebreaks), you may want to do this differently, but i guess it will still work...

KIM


thanks It worked


some rework and no register are modified


#Function Replace String in File

# Push "<File with string to replace>"
# Push "old string"
# Push "new string"
# Call ReplaceInFile
#
#It is VERY unefficient, but I wanted to keep it easy.
#it saves no variables to the stack and so on...
>Function ReplaceInFile
Push $R4
Push $R1
Push $R3
Exch 5
Pop $R3
Exch 3
Pop $R1
Exch 1
Pop $R4
Push $R0
Push $R2
Push $R5
Push $R6
Push $R7
Push $R8
Push $R9
FileOpen $R9 $R3 "r"
StrCpy $R8 0 #position in sourcefile
GetTempFileName $R0
Push $R0
FileOpen $R7 $R0 "w"
StrLen $R0 $R1 #get length of searchstring
StrCpy $R2 $R1 1 #get first character of searchstring

goto searchloop
writechar:
FileWrite $R7 $R6
searchloop:
Fileread $R9 $R6 1 #read character from file
IntOp $R8 $R8 + 1 #increase position counter
StrCmp $R6 "" fileend #have we reached the end of the file?
StrCmp $R6 $R2 "" writechar #compare character to first character from searchstring
#HERE, the first character of the string has been found!
IntOp $R8 $R8 - 1 #decrease position
FileSeek $R9 -1 CUR #seek back in file
FileRead $R9 $R5 $R0 #Read string with length of search string from file

StrCmp $R5 $R1 found #is the string there in the file?
#HERE, the string has NOT been found
Intop $R8 $R8 + 1 #increase original position +1
FileSeek $R9 $R8 #seek original position +1 in file
goto writechar #and continue searching
#HERE, the string has been found!
found:
FileWrite $R7 $R4 #write replacement string to file
IntOp $R8 $R8 + $R0 #set file postition to
#BUG ###Intop $R8 $R8 + 1 #1 character behind the string we've found
goto searchloop
#HERE, the end of the file has been reached
fileend:
FileClose $R7
FileClose $R9
Pop $R0
CopyFiles/FILESONLY $R0 $R3
Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R2
Pop $R0
Pop $R4
Pop $R1
Pop $R3
FunctionEnd
>