Use std::string instead of PBD::sys::path in pbd/search_path.h, pbd/file_utils.h...
[ardour.git] / libs / ardour / filesystem_paths.cc
1 /*
2     Copyright (C) 2007 Tim Mayberry
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 #include <cstdlib>
20 #include <iostream>
21
22 #include "pbd/error.h"
23 #include "pbd/compose.h"
24 #include "pbd/strsplit.h"
25 #include "pbd/filesystem.h"
26
27 #include <glibmm/miscutils.h>
28 #include <glibmm/fileutils.h>
29
30 #include "ardour/directory_names.h"
31 #include "ardour/filesystem_paths.h"
32
33 #include "i18n.h"
34
35 using namespace PBD;
36
37 namespace ARDOUR {
38
39 using std::string;
40
41 std::string
42 user_config_directory ()
43 {
44         sys::path p;
45
46 #ifdef __APPLE__
47         p = Glib::get_home_dir();
48         p /= "Library/Preferences";
49
50 #else
51         const char* c = 0;
52
53         /* adopt freedesktop standards, and put .ardour3 into $XDG_CONFIG_HOME or ~/.config
54          */
55
56         if ((c = getenv ("XDG_CONFIG_HOME")) != 0) {
57                 p = c;
58         } else {
59                 const string home_dir = Glib::get_home_dir();
60
61                 if (home_dir.empty ()) {
62                         const string error_msg = "Unable to determine home directory";
63
64                         // log the error
65                         error << error_msg << endmsg;
66
67                         throw sys::filesystem_error(error_msg);
68                 }
69
70                 p = home_dir;
71                 p /= ".config";
72         }
73 #endif
74
75         p /= user_config_dir_name;
76
77         std::string ps (p.to_string());
78
79         if (!Glib::file_test (ps, Glib::FILE_TEST_EXISTS)) {
80                 if (g_mkdir_with_parents (ps.c_str(), 0755)) {
81                         error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
82                                                    ps) << endmsg;
83                         exit (1);
84                 }
85         } else if (!Glib::file_test (ps, Glib::FILE_TEST_IS_DIR)) {
86                 error << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
87                                            ps) << endmsg;
88                 exit (1);
89         }
90
91         return p.to_string();
92 }
93
94 std::string
95 ardour_dll_directory ()
96 {
97         std::string s = Glib::getenv("ARDOUR_DLL_PATH");
98         if (s.empty()) {
99                 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
100                 ::exit (1);
101         }       
102         return s;
103 }
104
105 SearchPath
106 ardour_config_search_path ()
107 {
108         static bool have_path = false;
109         static SearchPath search_path;
110
111         if (!have_path) {
112                 SearchPath sp (user_config_directory());
113                 
114                 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
115                 if (s.empty()) {
116                         std::cerr << _("ARDOUR_CONFIG_PATH not set in environment - exiting\n");
117                         ::exit (1);
118                 }
119                 
120                 std::vector<string> ss;
121                 split (s, ss, ':');
122                 for (std::vector<string>::iterator i = ss.begin(); i != ss.end(); ++i) {
123                         sp += *i;
124                 }
125                 
126                 search_path = sp;
127                 have_path = true;
128         }
129
130         return search_path;
131 }
132
133 SearchPath
134 ardour_data_search_path ()
135 {
136         static bool have_path = false;
137         static SearchPath search_path;
138
139         if (!have_path) {
140                 SearchPath sp (user_config_directory());
141                 
142                 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
143                 if (s.empty()) {
144                         std::cerr << _("ARDOUR_DATA_PATH not set in environment - exiting\n");
145                         ::exit (1);
146                 }
147                 
148                 std::vector<string> ss;
149                 split (s, ss, ':');
150                 for (std::vector<string>::iterator i = ss.begin(); i != ss.end(); ++i) {
151                         sp += *i;
152                 }
153                 
154                 search_path = sp;
155                 have_path = true;
156         }
157
158         return search_path;
159 }
160
161 } // namespace ARDOUR