allow zero-input (i.e. tone generator) processors to be added
[ardour2.git] / libs / pbd / filesystem.cc
blob9c05962a6b8130c78d84ecdb4cf3ba3f0c52260f
1 /*
2 Copyright (C) 2007 Tim Mayberry
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.
19 #include <sys/stat.h>
21 #include <glib.h>
22 #include <glib/gstdio.h>
24 #include <cerrno>
25 #include <fstream>
27 #include <glibmm/fileutils.h>
28 #include <glibmm/miscutils.h>
30 #include "pbd/filesystem.h"
31 #include "pbd/error.h"
32 #include "pbd/compose.h"
34 #include "i18n.h"
36 using namespace std;
38 namespace PBD {
40 namespace sys {
42 path&
43 path::operator/=(const path& rhs)
45 m_path = Glib::build_filename(m_path, rhs.m_path);
46 return *this;
49 path&
50 path::operator/=(const string& rhs)
52 m_path = Glib::build_filename(m_path, rhs);
53 return *this;
56 path&
57 path::operator/=(const char* rhs)
59 m_path = Glib::build_filename(m_path, rhs);
60 return *this;
63 string
64 path::leaf () const
66 return Glib::path_get_basename(m_path);
69 path
70 path::branch_path () const
72 string dir = Glib::path_get_dirname (m_path);
75 * glib returns "." to signify that the path
76 * has no directory components(branch path)
77 * whereas boost::filesystem returns an empty
78 * string
80 if(dir == ".")
82 return "";
84 return dir;
87 bool
88 exists (const path & p)
90 return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
93 bool
94 is_directory (const path & p)
96 return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
99 bool
100 create_directory(const path & p)
102 if(is_directory(p)) return false;
104 int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
106 if(error == -1)
108 throw filesystem_error(g_strerror(errno), errno);
110 return true;
113 bool
114 create_directories(const path & p)
116 if(is_directory(p)) return false;
118 int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
120 if(error == -1)
122 throw filesystem_error(g_strerror(errno), errno);
124 return true;
127 bool
128 remove(const path & p)
130 if(!exists(p)) return false;
132 int error = g_unlink (p.to_string().c_str());
134 if(error == -1)
136 throw filesystem_error(g_strerror(errno), errno);
138 return true;
141 void
142 rename (const path & from_path, const path & to_path)
144 // g_rename is a macro that evaluates to ::rename on
145 // POSIX systems, without the global namespace qualifier
146 // it would evaluate to a recursive call(if it compiled)
147 if ( ::g_rename( from_path.to_string().c_str(),
148 to_path.to_string().c_str() ) == -1 )
150 throw filesystem_error(g_strerror(errno), errno);
154 // XXX character encoding.
155 void
156 copy_file(const path & from_path, const path & to_path)
158 std::ifstream in(from_path.to_string().c_str());
159 std::ofstream out(to_path.to_string().c_str());
161 if (!in || !out) {
162 throw filesystem_error(string_compose(_("Could not open files %1 and %2 for copying"),
163 from_path.to_string(), to_path.to_string()));
166 out << in.rdbuf();
168 if (!in || !out) {
169 remove (to_path);
170 throw filesystem_error(string_compose(_("Could not copy existing file %1 to %2"),
171 from_path.to_string(), to_path.to_string()));
175 string
176 basename (const path & p)
178 string base(p.leaf());
180 string::size_type n = base.rfind ('.');
182 return base.substr (0, n);
185 string
186 extension (const path & p)
188 string base(p.leaf());
190 string::size_type n = base.rfind ('.');
192 if (n != string::npos)
194 return base.substr(n);
197 return string();
201 } // namespace sys
203 } // namespace PBD