2 * Copyright (C) 2013-2014 John Emmas <john@creativepost.co.uk>
3 * Copyright (C) 2013-2015 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2013-2016 Tim Mayberry <mojofunk@gmail.com>
5 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <glibmm/fileutils.h>
31 #include <glibmm/miscutils.h>
33 #include "pbd/compose.h"
34 #include "pbd/debug.h"
35 #include "pbd/pathexpand.h"
36 #include "pbd/strsplit.h"
37 #include "pbd/tokenizer.h"
43 PBD::path_expand (string path)
52 if (path.length() == 1) {
53 return Glib::get_home_dir();
57 path.replace (0, 1, Glib::get_home_dir());
59 /* can't handle ~roger, so just leave it */
63 /* now do $VAR substitution, since wordexp isn't reliable */
65 regex_t compiled_pattern;
66 const int nmatches = 100;
67 regmatch_t matches[nmatches];
69 if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
70 std::cerr << "bad regcomp\n";
76 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
80 /* matches[0] gives the entire match */
82 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
84 /* try to get match from the environment */
86 if (match[1] == '{') {
88 match = match.substr (2, match.length() - 3);
91 char* matched_value = getenv (match.c_str());
94 path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
96 path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
99 /* go back and do it again with whatever remains after the
104 regfree (&compiled_pattern);
108 return canonical_path (path);
112 PBD::search_path_expand (string path)
121 split (path, s, G_SEARCHPATH_SEPARATOR);
123 for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
124 string exp = path_expand (*i);
132 for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
134 r += G_SEARCHPATH_SEPARATOR;
142 std::vector <std::string>
143 PBD::parse_path(std::string path, bool check_if_exists)
145 vector <std::string> pathlist;
146 vector <std::string> tmp;
147 PBD::tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp));
149 for(vector<std::string>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
150 if ((*i).empty()) continue;
152 #ifndef PLATFORM_WINDOWS
153 if ((*i).substr(0,1) == "~") {
154 dir = Glib::build_filename(Glib::get_home_dir(), (*i).substr(1));
161 if (!check_if_exists || Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
162 pathlist.push_back(dir);