Fix XML-writer edge-case (empty content)
[ardour.git] / libs / pbd / pathexpand.cc
1 /*
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>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <vector>
23 #include <iostream>
24 #include <climits>
25 #include <cerrno>
26 #include <cstdlib>
27
28 #include <regex.h>
29
30 #include <glibmm/fileutils.h>
31 #include <glibmm/miscutils.h>
32
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"
38
39 using std::string;
40 using std::vector;
41
42 string
43 PBD::path_expand (string path)
44 {
45         if (path.empty()) {
46                 return path;
47         }
48
49         /* tilde expansion */
50
51         if (path[0] == '~') {
52                 if (path.length() == 1) {
53                         return Glib::get_home_dir();
54                 }
55
56                 if (path[1] == '/') {
57                         path.replace (0, 1, Glib::get_home_dir());
58                 } else {
59                         /* can't handle ~roger, so just leave it */
60                 }
61         }
62
63         /* now do $VAR substitution, since wordexp isn't reliable */
64
65         regex_t compiled_pattern;
66         const int nmatches = 100;
67         regmatch_t matches[nmatches];
68
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";
71                 return path;
72         }
73
74         while (true) {
75
76                 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
77                         break;
78                 }
79
80                 /* matches[0] gives the entire match */
81
82                 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
83
84                 /* try to get match from the environment */
85
86                 if (match[1] == '{') {
87                         /* ${FOO} form */
88                         match = match.substr (2, match.length() - 3);
89                 }
90
91                 char* matched_value = getenv (match.c_str());
92
93                 if (matched_value) {
94                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
95                 } else {
96                         path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
97                 }
98
99                 /* go back and do it again with whatever remains after the
100                  * substitution
101                  */
102         }
103
104         regfree (&compiled_pattern);
105
106         /* canonicalize */
107
108         return canonical_path (path);
109 }
110
111 string
112 PBD::search_path_expand (string path)
113 {
114         if (path.empty()) {
115                 return path;
116         }
117
118         vector<string> s;
119         vector<string> n;
120
121         split (path, s, G_SEARCHPATH_SEPARATOR);
122
123         for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
124                 string exp = path_expand (*i);
125                 if (!exp.empty()) {
126                         n.push_back (exp);
127                 }
128         }
129
130         string r;
131
132         for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
133                 if (!r.empty()) {
134                         r += G_SEARCHPATH_SEPARATOR;
135                 }
136                 r += *i;
137         }
138
139         return r;
140 }
141
142 std::vector <std::string>
143 PBD::parse_path(std::string path, bool check_if_exists)
144 {
145         vector <std::string> pathlist;
146         vector <std::string> tmp;
147         PBD::tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp));
148
149         for(vector<std::string>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
150                 if ((*i).empty()) continue;
151                 std::string dir;
152 #ifndef PLATFORM_WINDOWS
153                 if ((*i).substr(0,1) == "~") {
154                         dir = Glib::build_filename(Glib::get_home_dir(), (*i).substr(1));
155                 }
156                 else
157 #endif
158                 {
159                         dir = *i;
160                 }
161                 if (!check_if_exists || Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
162                         pathlist.push_back(dir);
163                 }
164         }
165   return pathlist;
166 }