[DGD] Resolve Path & Array Efficiency

Robert Forshaw iouswuoibev at hotmail.com
Sat Feb 21 02:22:05 CET 2004


>I have come up with the follow way to resolve ".." in directory paths:

I wrote a similar function about a year ago when I was writing my mudlib in 
ldmud. This function was specifically for changing to another directory 
though, although the for loop can be extracted from it to 'normalize' the 
path:

mixed current_dir(string new_dir)
{
    int count;
    string old_dir = c_dir; // the current directory, set from the c_dir 
global variable

    string * new_path, old_path;

    if(!new_dir) return old_dir;
    if(new_dir == "/") return old_dir = "/";

    new_path = explode(new_dir, "/");
    old_path = explode(old_dir, "/");

    for(count = 0; count < sizeof(new_path); count++)
    {
        if(new_path[count] == "") continue;
        if(new_path[count] == ".") continue;
        if(new_path[count] == "..")
            old_path = old_path[0..sizeof(old_path)];
        else
            old_path += ({ new_path[count] });
    }

    new_dir = "/" + implode(old_path, "/");
    if(new_dir != "/") new_dir += "/";

    if(!get_dir(new_dir)) return 0;

    return c_dir = new_dir;
}

_________________________________________________________________
Express yourself with cool new emoticons http://www.msn.co.uk/specials/myemo

_________________________________________________________________
List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list