2

I'm trying to print some stuff to a file from a Rascal program. So I build a location from the working dir using something like |cwd:///myFile|.

However, I could not find the file, so I tried printing this path. But all I get is the same as what I put into the location. Hence I still do not know what the actual path is that will be used.

So how do I obtain the actual path that a location for a file or folder points to?

Something like this:

loc fileLoc = |cwd:///myFile.txt|;
writeFile(fileLoc, veryInterestingDataThatIsTooLargeForConsole);
println(Your file is saved here:);
loc actualFileLoc = getActualFileLoc(fileLoc);
println(actualFileLoc);
  • Or could use a name that does mean something to you, like `home:///myFile.txt` for a file in your personal directory or `tmp:///myFile.txt` for a file in the temp folder of the operating system. – Jurgen Vinju Feb 29 '16 at 10:33

1 Answers1

1

You can use the resolveLocation function in the IO library:

rascal>loc fileLoc = |cwd:///myFile.txt|;
loc: |cwd:///myFile.txt|
rascal>import IO;
ok
rascal>resolveLocation(fileLoc);
loc: |file:///Users/mhills/Development/eclipse.versions/eclipse.45.rcp/Eclipse.app/Contents/MacOS/myFile.txt|
Mark Hills
  • 1,028
  • 5
  • 4
  • Note that this will work for certain URI schemes, but not for all. For `project` for example you will just get the original location back. For `home` and `cwd` it works because these are defined in terms of the local file system. – Jurgen Vinju Feb 29 '16 at 10:34
  • Hi @JurgenVinju, can we use this to obtain full `readlink -f FILE` ([man](https://linux.die.net/man/1/readlink)) functionality in Rascal? For example `resolveLocation(|cwd:///../|)` will still contain `../` in the resolved path. – Teun M. Dec 17 '22 at 20:41
  • Hi Teun. No `..` is not implemented -for security reasons- to go beyond the root of a filesystem. – Jurgen Vinju Dec 19 '22 at 12:12