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);
62 exists (const path & p)
64 return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
68 is_directory (const path & p)
70 return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
74 create_directory(const path & p)
76 if(is_directory(p)) return false;
78 int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
82 throw filesystem_error(g_strerror(errno), errno);
88 create_directories(const path & p)
90 if(is_directory(p)) return false;
92 int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
96 throw filesystem_error(g_strerror(errno), errno);
102 remove(const path & p)
104 if(!exists(p)) return false;
106 int error = g_unlink (p.to_string().c_str());
110 throw filesystem_error(g_strerror(errno), errno);
116 copy_file(const path & from_path, const path & to_path)
118 // this implementation could use mucho memory
120 std::ifstream in(from_path.to_string().c_str());
121 std::ofstream out(to_path.to_string().c_str());
124 throw filesystem_error(string_compose(_("Could not open files %1 and %2 for copying"),
125 from_path.to_string(), to_path.to_string()));
131 throw filesystem_error(string_compose(_("Could not copy existing file %1 to %2"),
132 from_path.to_string(), to_path.to_string()));
138 basename (const path & p)
140 // I'm not sure if this works quite the same as boost::filesystem::basename
141 return Glib::path_get_basename (p.to_string ());