Software /
code /
prosody
Annotate
util/paths.lua @ 6164:ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 09 May 2014 19:34:35 +0200 |
child | 6505:2dc8dbd0940e |
rev | line source |
---|---|
6164
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local path_sep = package.config:sub(1,1); |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local path_util = {} |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- Helper function to resolve relative paths (needed by config) |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 function path_util.resolve_relative_path(parent_path, path) |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 if path then |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 -- Some normalization |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 parent_path = parent_path:gsub("%"..path_sep.."+$", ""); |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 path = path:gsub("^%.%"..path_sep.."+", ""); |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local is_relative; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 if path_sep == "/" and path:sub(1,1) ~= "/" then |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 is_relative = true; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 elseif path_sep == "\\" and (path:sub(1,1) ~= "/" and (path:sub(2,3) ~= ":\\" and path:sub(2,3) ~= ":/")) then |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 is_relative = true; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 end |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 if is_relative then |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 return parent_path..path_sep..path; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 end |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 end |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 return path; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 -- Helper function to convert a glob to a Lua pattern |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 function path_util.glob_to_pattern(glob) |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 return "^"..glob:gsub("[%p*?]", function (c) |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 if c == "*" then |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 return ".*"; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 elseif c == "?" then |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 return "."; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 else |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 return "%"..c; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 end |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 end).."$"; |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 end |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 |
ef4024f6bc40
core.configmanager: Move path utility functions into util.paths
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 return path_util; |