char* file_GetAbsolutePathFromRelativePath(const char* path) { // cancel for absolute paths if (!file_IsPathRelative(path)) { return strdup(path); } // cut off unrequired clutter while (path[0] == '.' && path[1] == file_NativeSlash()) { path += 2; } // get current working directory char* currentdir = file_GetCwd(); if (!currentdir) { return NULL; } // process ../ while (path[0] == '.' && path[1] == '.' && path[2] == file_NativeSlash()) { file_CutOffOneElement(currentdir); path += 3; } // allocate space for new path char* newdir = malloc(strlen(currentdir) + 1 + strlen(path) + 1); if (!newdir) { free(currentdir); return NULL; } // assemble new path memcpy(newdir, currentdir, strlen(currentdir)); char slash = file_NativeSlash(); newdir[strlen(currentdir)] = slash; memcpy(newdir + strlen(currentdir) + 1, path, strlen(path)); newdir[strlen(currentdir) + 1 + strlen(path)] = 0; free(currentdir); return newdir; }
/// Get an absolute path to the current working directory // (sometimes(!) this is where the blitwizard binary is located // - but not necessarily) // @function getcwd // @treturn string absolute directory path int luafuncs_getcwd(lua_State* l) { char* p = file_GetCwd(); lua_pushstring(l, p), free(p); return 1; }