3
// This file is part of Bombono DVD project.
5
// Copyright (c) 2009-2010 Ilya Murav'jov
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.
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.
17
// You should have received a copy of the GNU General Public License
18
// along with this program; if not, write to the Free Software
19
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "filesystem.h"
26
// :TRICKY: со временем может измениться (в бусте), но создавать
27
// свою хлопотнее пока, см. boost/detail/utf8_codecvt_facet.hpp
28
#include <boost/filesystem/detail/utf8_codecvt_facet.hpp>
29
#include <boost/filesystem/v3/path_traits.hpp> // boost::filesystem3::convert()
31
#include <string.h> // strstr()
33
namespace boost { namespace filesystem {
35
bool is_empty_directory(const path & dir_path)
37
static const directory_iterator end_itr;
38
return directory_iterator(dir_path) == end_itr;
41
std::string operator / (const path& f, to_string_enum /*to_str*/)
48
std::string name_str(const path& pth)
50
return pth.filename().string();
55
std::string name_str(const path& pth)
60
#endif // BFS_VERSION_3
62
std::string name_str(const std::string& pth)
64
return name_str(path(pth));
67
} } // namepspace filesystem, boost
69
const char* FindExtDot(const char* name)
71
return strrchr(name, '.');
76
std::string get_basename(const fs::path& pth)
78
return pth.stem().string();
81
std::string get_extension(const fs::path& pth)
83
return pth.extension().string();
88
std::string get_basename(const fs::path& pth)
90
std::string name_s = pth.leaf();
91
const char* name = name_s.c_str();
93
//if( const char* dot = strstr(name, ".") )
94
if( const char* dot = FindExtDot(name) )
95
return std::string(name, dot);
99
std::string get_extension(const fs::path& pth)
101
std::string name_s = pth.leaf();
103
if( const char* dot = FindExtDot(name_s.c_str()) )
104
return std::string(dot+1);
105
return std::string();
108
#endif // BFS_VERSION_3
110
class tune_boost_filesystem
113
tune_boost_filesystem()
115
// B.FS: отказались от проверок в ver>=2
116
//// глобальная установка проверки имен файлов
117
//// чтоб любые символы в именах файлов позволялись, для utf8
118
//fs::path::default_name_check(fs::native);
120
#if defined(_WIN32) && defined(BFS_VERSION_3)
121
// внутри используем utf-8 => меняем конвертор
122
std::locale utf8_loc(std::locale(), new fs::detail::utf8_codecvt_facet);
123
fs::path::imbue(utf8_loc);
126
} tune_boost_filesystem_obj;
128
std::wstring Utf8ToUcs16(const char* utf8_str)
132
// можно напрямую использовать utf8_codecvt_facet, но так проще
133
// (будет работать при fs::path::imbue(utf8_loc);)
134
boost::filesystem3::path_traits::convert(utf8_str, 0, res, fs::path::codecvt());
144
fs::path MakeAbsolutePath(const fs::path& pth, const fs::path& cur_dir)
148
if( pth.is_complete() )
152
fs::path dir = cur_dir.empty() ? fs::current_path() : cur_dir ;
155
return res.normalize();
158
// оба аргумента должны быть абсолютными путями
159
bool MakeRelativeToDir(fs::path& pth, fs::path dir)
163
ASSERT( pth.is_complete() );
164
ASSERT( dir.is_complete() );
166
fs::path::iterator p_itr = pth.begin(), p_end = pth.end();
167
fs::path::iterator d_itr = dir.begin(), d_end = dir.end();
169
if( *p_itr != *d_itr )
172
for( ; (p_itr != p_end) && (d_itr != d_end) && (*p_itr == *d_itr);
177
for( ; d_itr != d_end; ++d_itr )
179
for( ; p_itr != p_end; ++p_itr )
186
bool HaveFullAccess(const fs::path& path)
188
ASSERT( fs::exists(path) );
189
return 0 == access(path.string().c_str(), R_OK|W_OK|X_OK);
192
bool ClearAllFiles(const fs::path& dir_path, std::string& err_str)
197
static const fs::directory_iterator end_itr;
198
for( fs::directory_iterator itr(dir_path);
199
itr != end_itr; ++itr )
200
fs::remove_all(*itr);
202
catch( const fs::filesystem_error& fe )
204
err_str = FormatFSError(fe);
210
} // namespace Project
212
std::string AppendPath(const std::string& dir, const std::string& path)
214
return (fs::path(dir)/path).string();
217
std::string FormatFSError(const fs::filesystem_error& fe)
219
std::string what = fe.code().message();
220
// бывает и второй путь, path2(), но пока не требуется
221
std::string fpath = fe.path1().string();
223
what = boost::format("%1%: \"%2%\"") % what % fpath % bf::stop;