amitchel_us
2nd April 2002 00:09 UTC
Converting a String ($INSTDIR) from back to forward slash ( \ -> / )
I'm planning to use NSIS (which rocks) to install a Java application on WinNT/2000. The framework includes configuration files with directory listings. For example :
UPLOAD_DIR = d:/InstallDir/subDir
InstallDir is the same as NSIS' $INSTDIR. So if the user decides to install on F:\InstallDir, I want to change my configuration files to F:/InstallDir/subDir.
Java's file are either forward slash (/), or double-backslash (\\). A single backslash (\) is an escape character. So, F:\InstallDir is invalid.
Here's what I've thought of :
1. use NSIS strcmp, strcpy, strlen. I don't think this will work because there's no for..loop to loop through each character in $INSTDIR.
2. use an MS-Dos command through Exec to replace the slashes. It doesn't look like there's an easy way to do it. Win-2000's wsh may have it, but most of my customers are NT. If I could call it, though, I could probably write the result to a file and then read the file back in to get the value
3. Check this forum for similar items. I didn't see any.
Thanks in advance,
Andy
Pomflain
2nd April 2002 05:42 UTC
Look at the utility functions.
Look at the example for parsing $CMDLINE parameters. Instead of delimiting by ' ', just scan until you hit the \ and then do a strcat. Like this:
#Scan for parameters $R1 is the counter which points to the '\'
IntOp $R1 $R1 - 1 ; Move pointer back to point before the '\'
StrCpy $R2 "/"
StrCpy $R0 $R0 "" $R1 ; Copy until the '\'
StrCpy $R0 "$R0$R2" ; Replace the '\' with '/'
I think that should work, but I'm also sure someone else will have a much simpler solution, they always do.
Hope that helps (a little).
Smile2Me
2nd April 2002 13:30 UTC
Since I guess that there will be multiple '\' that need to be replaced, just scan the complete string character by character and replace if recessary will be more easy than waiting with copieng till a '\' is encountered. But that's just my guess. Otherwise you will have a loop with two possibilities and a lot of jumps. With this solution you just copie each character to a new string unless it's equal to '\'.
Take a look at the source. It's a function that replaces in a string any substring by another string (StrReplace).
I hope this will answer the question.
Good luck,
greetz, Hendri.
Smile2Me
2nd April 2002 15:06 UTC
I generalized the ReplChar function to a StrReplace function that replaces any substring by an other string. The statement above was not correct, the previous function only worked for substrings of length 1. That was sufficient for your problem. The new function can take substrings of any length. But anyway, take a look at the general function. I guess more people will like this one...
This function can of course also solve the problem of replacing '\' by '/'.
Good luck, greetz,
-Hendri.
amitchel_us
3rd April 2002 00:30 UTC
That's terrific. It just clicked into place once I saw how you used labels for the loops. I had no idea. Thanks for the cleverness.
Andy
Smile2Me
3rd April 2002 07:07 UTC
Thx for the nice words :D :D :D
-Hendri.
treaz
15th January 2003 19:46 UTC
Hi,
I guess there is a silly person here who doesn't know how to integrate your StrReplace function and put it to use.
I need to replace Strings in a file, but how do I specify a file name or get your StrReplace function read my file?
Could you please advice. Thanks.
:o
Smile2Me
15th January 2003 20:48 UTC
Read this:
http://forums.winamp.com/showthread....hreadid=121562
-Hendri.