/*
* path.c
*
* SFUN: path management
*
* Taken from VikingMUD, created by Auronthas
*
*/
#include <std.h>
/* parse a path to a player*/
static varargs string parse_path(string str, string cwd, string name) {
string path, *comp, *res;
int i, j, endslash;
endslash = (str[strlen(str)-1] == '/');
if (str[0] == '/')
path = str[1..];
else if (str[0] == '~') {
if (str == "~")
path = PLAYER_DIR[1..]+name;
else {
if (str[1] == '/')
path = master()->home_directory(name) + str[1..];
else {
if (str[1..1] == capitalize(str[1..1]))
path = DOMAIN_DIR[1..] + str [1..];
else
path = PLAYER_DIR[1..] + str[1..];
}
}
}
else
path = ((cwd == "") ? str : cwd + "/" + str);
comp = explode(path+"/","/");
if (!a_sizeof(comp))
path="";
else {
res = allocate(a_sizeof(comp));
j = 0;
for (i = 0; i < a_sizeof(comp); i++) {
if (comp[i] == "" || comp[i] == ".")
continue;
else if (comp[i] == "..") {
if (j > 0)
j--;
} else
res[j++] = comp[i];
}
path = (j-1 < 0) ? "" : implode(res[0..j-1], "/");
}
if (path != "/")
path = "/" + path;
if (endslash && path != "")
return path + "/";
else
return path;
}
#if 0
/* resolve a wizard's path
(needs query_path() and query_real_name() in player object) */
varargs string resolve_path(string str, string cwd, object player) {
string path, *comp, *res, name, home, current_path;
int i, j, endslash;
if (!player)
player = this_player();
if (player)
home = PLAYER_DIR + (string)player->query_real_name();
else
home = "/";
if (player)
current_path = "/" + (string)player->query_path();
else
current_path = "/";
if (!cwd)
cwd = current_path;
endslash=(extract(str, strlen(str)-1) == "/");
if (str[0] == '/')
path = str[1..];
else if (str[0] == '~') {
if (str == "~")
path = PLAYER_DIR+name;
else {
if (str[1] == '/')
path=PLAYER_DIR+name+extract(str, 1);
else
path=PLAYER_DIR+extract(str, 1);
}
}
else {
if (current_path == "")
path=str;
else
path=current_path+"/"+str;
}
comp=explode(path+"/","/");
if (comp == 0)
path="";
else {
res=allocate(a_sizeof(comp)); j=0;
for (i=0; i<a_sizeof(comp); i++) {
if (comp[i]=="" || comp[i]==".")
continue;
else if (comp[i]=="..") {
if (j>0)
j--;
}
else
res[j++]=comp[i];
}
path=implode(res[0..j-1],"/");
}
if (endslash && path != "")
return path+"/";
else
return path;
}
#endif