Absolute path

Regan Heath regan at netwin.co.nz
Tue Aug 22 16:49:28 PDT 2006


On Sat, 19 Aug 2006 23:06:42 +1000, Reiner Pope  
<reiner.pope at 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



More information about the Digitalmars-d-learn mailing list