Old 16th December 2002, 13:44   #1
michiel
Junior Member
 
Join Date: Dec 2002
Posts: 17
Angry 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
michiel is offline   Reply With Quote
Old 16th December 2002, 13:56   #2
Joost Verburg
NSIS MUI Dev
 
Join Date: Nov 2001
Posts: 3,717
http://forums.winamp.com/showthread....hreadid=109670
Joost Verburg is offline   Reply With Quote
Old 16th December 2002, 15:03   #3
michiel
Junior Member
 
Join Date: Dec 2002
Posts: 17
replacing $INSTDIR for ServerRoot

replacing $INSTDIR for ServerRoot as an string that mutiple times must be done help!!!
michiel is offline   Reply With Quote
Old 16th December 2002, 20:38   #4
rarefluid
Guest
 
Posts: n/a
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
  Reply With Quote
Old 16th December 2002, 20:41   #5
Joost Verburg
NSIS MUI Dev
 
Join Date: Nov 2001
Posts: 3,717
A string search function has been included in the NSIS 2 documentation (Useful functions appendix).
Joost Verburg is offline   Reply With Quote
Old 16th December 2002, 20:51   #6
rarefluid
Guest
 
Posts: n/a
I don't think it is very good for this purpose, but you could modify it...
  Reply With Quote
Old 17th December 2002, 08:22   #7
michiel
Junior Member
 
Join Date: Dec 2002
Posts: 17
????

Quote:
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?
michiel is offline   Reply With Quote
Old 17th December 2002, 16:11   #8
rarefluid
Guest
 
Posts: n/a
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
  Reply With Quote
Old 20th December 2002, 15:29   #9
michiel
Junior Member
 
Join Date: Dec 2002
Posts: 17
thanks It worked
michiel is offline   Reply With Quote
Old 9th January 2003, 09:07   #10
dsvedchenko
Junior Member
 
Join Date: Jan 2003
Posts: 7
Send a message via ICQ to dsvedchenko
some rework and no register are modified

PHP Code:
#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 #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 #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 #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 
dsvedchenko is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump