Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Treate Posix style FilePath on Windows

Moderators: kris

Posted: 10/18/07 03:52:24

Some time, we recive file path from Posix server, or send file path to Posix file path. but seems no way to treate Posix style file path on Windows :

auto s = FilePath(r"/a/b/c.txt");
Cout(s.parent).newline;  //print nothing

my solution is:

//from dwin.io.FilePath

char[] parent(char[] fullname)
out (result)
{
    assert(result.length <= fullname.length);
}
body
{
    uint i;

    for (i = fullname.length; i > 0; i--)
    {
        version(Win32)
        {
            if (fullname[i - 1] == ':')
                break;
            if (fullname[i - 1] == '\\')
            {   i--;
                break;
            }
            if (fullname[i - 1] == '/')//to support server path, by yidabu.com 20070718
            {   i--;
                break;
            }			
        }
        version(linux)
        {
            if (fullname[i - 1] == '/')
            {   i--;
                break;
            }
        }
    }
    return fullname[0 .. i];
}
//parent
debug(UnitTest) unittest
{
	char[] p = "/a/b/c.txt";
	p = parent(p);
	assert(p == "/a/b");
}
Author Message

Posted: 10/18/07 04:22:19

auto s = FilePath(r"/a/b/c.txt", true);

Posted: 10/18/07 04:55:13

thanks. I got it:

auto s = FilePath(r"/a/b/c.txt", true);
Cout(s.pop.replace('\\', '/')).newline;  //print /a/b

since / can not be part of directory name on Windows, how about treat / as \ on Windows , then s.parent should be "/a/b".

Posted: 10/18/07 05:54:49 -- Modified: 10/18/07 06:00:30 by
kris

the second argument to the ctor should change all '/' to '\', and then there's no problem at all ... if you're then going to send it back, flip the path-separators before you do. The effort to support both styles on Win32 is just not worth it :)

Posted: 02/24/08 03:06:23

Tango always normalizes paths to '/' usage these days, due to pressure from the VFS implementation