fix Window->Common move for show-mixer
[ardour.git] / libs / pbd / pathexpand.cc
1 /*
2     Copyright (C) 2013-2014 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 <iostream>
22 #include <climits>
23 #include <cerrno>
24 #include <cstdlib>
25
26 #include <regex.h>
27
28 #include <glibmm/fileutils.h>
29 #include <glibmm/miscutils.h>
30
31 #include "pbd/compose.h"
32 #include "pbd/debug.h"
33 #include "pbd/pathexpand.h"
34 #include "pbd/strsplit.h"
35 #include "pbd/tokenizer.h"
36
37 using std::string;
38 using std::vector;
39
40 string
41 PBD::path_expand (string path)
42 {
43         if (path.empty()) {
44                 return path;
45         }
46
47         /* tilde expansion */
48
49         if (path[0] == '~') {
50                 if (path.length() == 1) {
51                         return Glib::get_home_dir();
52                 }
53
54                 if (path[1] == '/') {
55                         path.replace (0, 1, Glib::get_home_dir());
56                 } else {
57                         /* can't handle ~roger, so just leave it */
58                 }
59         }
60
61         /* now do $VAR substitution, since wordexp isn't reliable */
62
63         regex_t compiled_pattern;
64         const int nmatches = 100;
65         regmatch_t matches[nmatches];
66
67         if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
68                 std::cerr << "bad regcomp\n";
69                 return path;
70         }
71
72         while (true) {
73
74                 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
75                         break;
76                 }
77
78                 /* matches[0] gives the entire match */
79
80                 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
81
82                 /* try to get match from the environment */
83
84                 if (match[1] == '{') {
85                         /* ${FOO} form */
86                         match = match.substr (2, match.length() - 3);
87                 }
88
89                 char* matched_value = getenv (match.c_str());
90
91                 if (matched_value) {
92                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
93                 } else {
94                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
95                 }
96
97                 /* go back and do it again with whatever remains after the
98                  * substitution
99                  */
100         }
101
102         regfree (&compiled_pattern);
103
104         /* canonicalize */
105
106         return canonical_path (path);
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, G_SEARCHPATH_SEPARATOR);
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 += G_SEARCHPATH_SEPARATOR;
133                 }
134                 r += *i;
135         }
136
137         return r;
138 }
139
140 std::vector <std::string>
141 PBD::parse_path(std::string path, bool check_if_exists)
142 {
143         vector <std::string> pathlist;
144         vector <std::string> tmp;
145         PBD::tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp));
146
147         for(vector<std::string>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
148                 if ((*i).empty()) continue;
149                 std::string dir;
150 #ifndef PLATFORM_WINDOWS
151                 if ((*i).substr(0,1) == "~") {
152                         dir = Glib::build_filename(Glib::get_home_dir(), (*i).substr(1));
153                 }
154                 else
155 #endif
156                 {
157                         dir = *i;
158                 }
159                 if (!check_if_exists || Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
160                         pathlist.push_back(dir);
161                 }
162         }
163   return pathlist;
164 }