Thread overview
Absolute path
Aug 19, 2006
Reiner Pope
Aug 22, 2006
Regan Heath
August 19, 2006
Hi there,

I can't find any function which will convert a file path from relative to absolute. C# and Java both have one: Path.GetAbsolutePath or something similar.

Is there one?

Do you think this is a good enough solution?

char[] toabs(char[] filepath)
out (c)
{
	assert (isabs(c));
}
body
{
	return (join(getcwd(), filepath));
}

Cheers,

Reiner
August 19, 2006
"Reiner Pope" <reiner.pope@REMOVE.THIS.gmail.com> wrote in message news:ec72hr$hd7$1@digitaldaemon.com...

> Is there one?

I can't find any in phobos.  I usually end up doing what you do..

> Do you think this is a good enough solution?
>
> char[] toabs(char[] filepath)
> out (c)
> {
> assert (isabs(c));
> }
> body
> {
> return (join(getcwd(), filepath));
> }

That's almost exactly what I do, but I check to see that it isn't absolute already:

if(isabs(filepath))
    return filepath;
else
    return join(getcwd(), filepath);


August 22, 2006
On Sat, 19 Aug 2006 23:06:42 +1000, Reiner Pope <reiner.pope@REMOVE.THIS.gmail.com> wrote:
> Hi there,
>
> I can't find any function which will convert a file path from relative to absolute. C# and Java both have one: Path.GetAbsolutePath or something similar.
>
> Is there one?
>
> Do you think this is a good enough solution?
>
> char[] toabs(char[] filepath)
> out (c)
> {
> 	assert (isabs(c));
> }
> body
> {
> 	return (join(getcwd(), filepath));
> }

There are some C functions _fullpath and _wfullpath (unicode) which can take a relative path and give the full path. I do not think they are ANSI functions however they do appear to exist in the DMC libraries (and thus you can use them in D).

eg.

import std.stdio;    //writefln
import std.string;   //toStringz
import std.c.string; //strlen

extern(C) char *_fullpath(char *buf,char *path,size_t buflen);

void main()
{
	char[] abs = new char[100]; //allocate space
	char[] rel = "a\\b\\c";
		
	_fullpath(abs.ptr,toStringz(rel),abs.length);
	abs.length = strlen(abs.ptr); //correct array length
	writefln(abs);
}

This should output <cwd>\a\b\c where <cwd> is the path in which you run it.

Also of note are these functions:
  _makepath, _wmakepath
  _splitpath, _wsplitpath

:)

Regan