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