Merge 0.10->trunk
[prosody.git] / util / paths.lua
1 local t_concat = table.concat;
2
3 local path_sep = package.config:sub(1,1);
4
5 local path_util = {}
6
7 -- Helper function to resolve relative paths (needed by config)
8 function path_util.resolve_relative_path(parent_path, path)
9         if path then
10                 -- Some normalization
11                 parent_path = parent_path:gsub("%"..path_sep.."+$", "");
12                 path = path:gsub("^%.%"..path_sep.."+", "");
13
14                 local is_relative;
15                 if path_sep == "/" and path:sub(1,1) ~= "/" then
16                         is_relative = true;
17                 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" and path:sub(2,3) ~= ":/")) then
18                         is_relative = true;
19                 end
20                 if is_relative then
21                         return parent_path..path_sep..path;
22                 end
23         end
24         return path;
25 end
26
27 -- Helper function to convert a glob to a Lua pattern
28 function path_util.glob_to_pattern(glob)
29         return "^"..glob:gsub("[%p*?]", function (c)
30                 if c == "*" then
31                         return ".*";
32                 elseif c == "?" then
33                         return ".";
34                 else
35                         return "%"..c;
36                 end
37         end).."$";
38 end
39
40 function path_util.join(...)
41         return t_concat({...}, path_sep);
42 end
43
44 return path_util;