Use test utility function to find evoral test files
[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/pathexpand.h"
32 #include "pbd/strsplit.h"
33 #include "pbd/tokenizer.h"
34
35 using std::string;
36 using std::vector;
37
38 #ifdef COMPILER_MINGW
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <glibmm.h>
43
44 /****************************************************************
45  * Emulate POSIX realpath() using Win32 _fullpath() since realpath()
46  * is not available.
47  *
48  * Returns:
49  *    On Success: A pointer to the resolved (absolute) path
50  *    On Failure: 0 (NULL)
51  */
52
53 static char* 
54 realpath (const char *original_path, char resolved_path[_MAX_PATH+1])
55 {
56         char *rpath = 0;
57         bool bIsSymLink = false; // We'll probably need to test the incoming path
58                                  // to find out if it points to a Windows shortcut
59                                  // (or a hard link) and set this appropriately.
60
61         if (bIsSymLink) {
62                 // At the moment I'm not sure if Windows '_fullpath()' is directly
63                 // equivalent to POSIX 'realpath()' - in as much as the latter will
64                 // resolve the supplied path if it happens to point to a symbolic
65                 // link ('_fullpath()' probably DOESN'T do this but I'm not really
66                 // sure if Ardour needs such functionality anyway). Therefore we'll
67                 // possibly need to add that functionality here at a later date.
68         } else {
69                 char temp[(_MAX_PATH+1)*6]; // Allow for maximum length of a path in wchar characters
70                 
71                 // POSIX 'realpath()' requires that the buffer size is at
72                 // least PATH_MAX+1, so assume that the user knew this !!
73
74                 rpath = _fullpath (temp, Glib::locale_from_utf8 (original_path).c_str(), _MAX_PATH);
75
76                 if (0 != rpath) {
77                         snprintf (resolved_path, _MAX_PATH+1, "%s", Glib::locale_to_utf8 (temp).c_str());
78                 }
79                         
80         }
81         
82         return (rpath);
83 }
84
85 #endif  // COMPILER_MINGW
86
87 string
88 PBD::canonical_path (const std::string& path)
89 {
90         char buf[PATH_MAX+1];
91
92         if (!realpath (path.c_str(), buf)) {
93                 return path;
94         }
95
96         return string (buf);
97 }
98
99 string
100 PBD::path_expand (string path)
101 {
102         if (path.empty()) {
103                 return path;
104         }
105
106         /* tilde expansion */
107
108         if (path[0] == '~') {
109                 if (path.length() == 1) {
110                         return Glib::get_home_dir();
111                 }
112
113                 if (path[1] == '/') {
114                         path.replace (0, 1, Glib::get_home_dir());
115                 } else {
116                         /* can't handle ~roger, so just leave it */
117                 }
118         }
119
120         /* now do $VAR substitution, since wordexp isn't reliable */
121
122         regex_t compiled_pattern;
123         const int nmatches = 100;
124         regmatch_t matches[nmatches];
125         
126         if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
127                 std::cerr << "bad regcomp\n";
128                 return path;
129         }
130
131         while (true) { 
132
133                 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
134                         break;
135                 }
136                 
137                 /* matches[0] gives the entire match */
138                 
139                 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
140                 
141                 /* try to get match from the environment */
142
143                 if (match[1] == '{') {
144                         /* ${FOO} form */
145                         match = match.substr (2, match.length() - 3);
146                 }
147
148                 char* matched_value = getenv (match.c_str());
149
150                 if (matched_value) {
151                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
152                 } else {
153                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
154                 }
155
156                 /* go back and do it again with whatever remains after the
157                  * substitution 
158                  */
159         }
160
161         regfree (&compiled_pattern);
162
163         /* canonicalize */
164
165         return canonical_path (path);
166 }
167
168 string
169 PBD::search_path_expand (string path)
170 {
171         if (path.empty()) {
172                 return path;
173         }
174
175         vector<string> s;
176         vector<string> n;
177
178         split (path, s, G_SEARCHPATH_SEPARATOR);
179
180         for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
181                 string exp = path_expand (*i);
182                 if (!exp.empty()) {
183                         n.push_back (exp);
184                 }
185         }
186
187         string r;
188
189         for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
190                 if (!r.empty()) {
191                         r += G_SEARCHPATH_SEPARATOR;
192                 }
193                 r += *i;
194         }
195
196         return r;
197 }
198
199 std::vector <std::string>
200 PBD::parse_path(std::string path, bool check_if_exists)
201 {
202         vector <std::string> pathlist;
203         vector <std::string> tmp;
204         PBD::tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp));
205
206         for(vector<std::string>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
207                 if ((*i).empty()) continue;
208                 std::string dir;
209 #ifndef PLATFORM_WINDOWS
210                 if ((*i).substr(0,1) == "~") {
211                         dir = Glib::build_filename(Glib::get_home_dir(), (*i).substr(1));
212                 }
213                 else
214 #endif
215                 {
216                         dir = *i;
217                 }
218                 if (!check_if_exists || Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
219                         pathlist.push_back(dir);
220                 }
221         }
222   return pathlist;
223 }