Merge branch 'master' into cairocanvas
[ardour.git] / libs / pbd / pathexpand.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <vector>
21 #include <climits>
22 #include <iostream>
23
24 #include <regex.h>
25
26 #include <glibmm/miscutils.h>
27
28 #include "pbd/pathexpand.h"
29 #include "pbd/strsplit.h"
30
31 using std::string;
32 using std::vector;
33
34 string
35 PBD::path_expand (string path)
36 {
37         if (path.empty()) {
38                 return path;
39         }
40
41         /* tilde expansion */
42
43         if (path[0] == '~') {
44                 if (path.length() == 1) {
45                         return Glib::get_home_dir();
46                 }
47
48                 if (path[1] == '/') {
49                         path.replace (0, 1, Glib::get_home_dir());
50                 } else {
51                         /* can't handle ~roger, so just leave it */
52                 }
53         }
54
55         /* now do $VAR substitution, since wordexp isn't reliable */
56
57         regex_t compiled_pattern;
58         const int nmatches = 100;
59         regmatch_t matches[nmatches];
60         
61         if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
62                 std::cerr << "bad regcomp\n";
63                 return path;
64         }
65
66         while (true) { 
67
68                 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
69                         break;
70                 }
71                 
72                 /* matches[0] gives the entire match */
73                 
74                 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
75                 
76                 /* try to get match from the environment */
77
78                 if (match[1] == '{') {
79                         /* ${FOO} form */
80                         match = match.substr (2, match.length() - 3);
81                 }
82
83                 char* matched_value = getenv (match.c_str());
84
85                 if (matched_value) {
86                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
87                 } else {
88                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
89                 }
90
91                 /* go back and do it again with whatever remains after the
92                  * substitution 
93                  */
94         }
95
96         regfree (&compiled_pattern);
97
98         /* canonicalize */
99
100         char buf[PATH_MAX+1];
101
102         if (realpath (path.c_str(), buf)) {
103                 return buf;
104         } else {
105                 return string();
106         }
107 }
108
109 string
110 PBD::search_path_expand (string path)
111 {
112         if (path.empty()) {
113                 return path;
114         }
115
116         vector<string> s;
117         vector<string> n;
118
119         split (path, s, ':');
120
121         for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
122                 string exp = path_expand (*i);
123                 if (!exp.empty()) {
124                         n.push_back (exp);
125                 }
126         }
127
128         string r;
129
130         for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
131                 if (!r.empty()) {
132                         r += ':';
133                 }
134                 r += *i;
135         }
136
137         return r;
138 }