2 Copyright (C) 2013 Paul Davis
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.
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.
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.
28 #include <glibmm/miscutils.h>
30 #include "pbd/pathexpand.h"
31 #include "pbd/strsplit.h"
37 PBD::canonical_path (const std::string& path)
44 if (!realpath (path.c_str(), buf) && (errno != ENOENT)) {
53 PBD::path_expand (string path)
62 if (path.length() == 1) {
63 return Glib::get_home_dir();
67 path.replace (0, 1, Glib::get_home_dir());
69 /* can't handle ~roger, so just leave it */
73 /* now do $VAR substitution, since wordexp isn't reliable */
75 regex_t compiled_pattern;
76 const int nmatches = 100;
77 regmatch_t matches[nmatches];
79 if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
80 std::cerr << "bad regcomp\n";
86 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
90 /* matches[0] gives the entire match */
92 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
94 /* try to get match from the environment */
96 if (match[1] == '{') {
98 match = match.substr (2, match.length() - 3);
101 char* matched_value = getenv (match.c_str());
104 path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
106 path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
109 /* go back and do it again with whatever remains after the
114 regfree (&compiled_pattern);
118 return canonical_path (path);
122 PBD::search_path_expand (string path)
131 split (path, s, ':');
133 for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
134 string exp = path_expand (*i);
142 for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {