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.
22 #include <glib/gstdio.h>
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"
41 path::operator/=(const path& rhs)
43 m_path = Glib::build_filename(m_path, rhs.m_path);
48 path::operator/=(const string& rhs)
50 m_path = Glib::build_filename(m_path, rhs);
55 path::operator/=(const char* rhs)
57 m_path = Glib::build_filename(m_path, rhs);
64 return Glib::path_get_basename(m_path);
68 path::branch_path () const
70 string dir = Glib::path_get_dirname (m_path);
73 * glib returns "." to signify that the path
74 * has no directory components(branch path)
75 * whereas boost::filesystem returns an empty
86 exists (const path & p)
88 return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
92 is_directory (const path & p)
94 return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
98 create_directory(const path & p)
100 if(is_directory(p)) return false;
102 int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
106 throw filesystem_error(g_strerror(errno), errno);
112 create_directories(const path & p)
114 if(is_directory(p)) return false;
116 int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
120 throw filesystem_error(g_strerror(errno), errno);
126 remove(const path & p)
128 if(!exists(p)) return false;
130 int error = g_unlink (p.to_string().c_str());
134 throw filesystem_error(g_strerror(errno), errno);
140 rename (const path & from_path, const path & to_path)
142 // g_rename is a macro that evaluates to ::rename on
143 // POSIX systems, without the global namespace qualifier
144 // it would evaluate to a recursive call(if it compiled)
145 if ( ::g_rename( from_path.to_string().c_str(),
146 to_path.to_string().c_str() ) == -1 )
148 throw filesystem_error(g_strerror(errno), errno);
152 // XXX character encoding.
154 copy_file(const path & from_path, const path & to_path)
156 std::ifstream in(from_path.to_string().c_str());
157 std::ofstream out(to_path.to_string().c_str());
160 throw filesystem_error(string_compose(_("Could not open files %1 and %2 for copying"),
161 from_path.to_string(), to_path.to_string()));
168 throw filesystem_error(string_compose(_("Could not copy existing file %1 to %2"),
169 from_path.to_string(), to_path.to_string()));
174 basename (const path & p)
176 string base(p.leaf());
178 string::size_type n = base.rfind ('.');
180 return base.substr (0, n);
184 extension (const path & p)
186 string base(p.leaf());
188 string::size_type n = base.rfind ('.');
190 if (n != string::npos)
192 return base.substr(n);