~e7appew/ubuntu/vivid/bombono-dvd/mux-files-with-spaces-vivid

« back to all changes in this revision

Viewing changes to .pc/89949cc527f95c8f9ea07559b2a801d783359c03.patch/src/mlib/filesystem.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-05-02 22:09:31 UTC
  • Revision ID: package-import@ubuntu.com-20130502220931-pui7uqs6b468z8sb
Tags: 1.2.1-0ubuntu5
Cherry-pick patches from upstream to build against boost1.53.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// mlib/filesystem.cpp
 
3
// This file is part of Bombono DVD project.
 
4
//
 
5
// Copyright (c) 2009-2010 Ilya Murav'jov
 
6
//
 
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.
 
11
//
 
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.
 
16
//
 
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
 
20
// 
 
21
 
 
22
#include "filesystem.h"
 
23
#include "tech.h"
 
24
#include "format.h"
 
25
 
 
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()
 
30
 
 
31
#include <string.h> // strstr()
 
32
 
 
33
namespace boost { namespace filesystem {
 
34
 
 
35
bool is_empty_directory(const path & dir_path)
 
36
{
 
37
    static const directory_iterator end_itr;
 
38
    return directory_iterator(dir_path) == end_itr;
 
39
}
 
40
 
 
41
std::string operator / (const path& f, to_string_enum /*to_str*/)
 
42
{
 
43
    return f.string();
 
44
}
 
45
 
 
46
#ifdef BFS_VERSION_3
 
47
 
 
48
std::string name_str(const path& pth)
 
49
{
 
50
    return pth.filename().string();
 
51
}
 
52
 
 
53
#else
 
54
 
 
55
std::string name_str(const path& pth)
 
56
{
 
57
    return pth.leaf();
 
58
}
 
59
 
 
60
#endif // BFS_VERSION_3
 
61
 
 
62
std::string name_str(const std::string& pth)
 
63
{
 
64
    return name_str(path(pth));
 
65
}
 
66
 
 
67
} } // namepspace filesystem, boost
 
68
 
 
69
const char* FindExtDot(const char* name)
 
70
{
 
71
    return strrchr(name, '.');
 
72
}
 
73
 
 
74
#ifdef BFS_VERSION_3
 
75
 
 
76
std::string get_basename(const fs::path& pth)
 
77
{
 
78
    return pth.stem().string();
 
79
}
 
80
 
 
81
std::string get_extension(const fs::path& pth)
 
82
{
 
83
    return pth.extension().string();
 
84
}
 
85
 
 
86
#else
 
87
 
 
88
std::string get_basename(const fs::path& pth)
 
89
{
 
90
    std::string name_s = pth.leaf();
 
91
    const char* name = name_s.c_str();
 
92
 
 
93
    //if( const char* dot = strstr(name, ".") )
 
94
    if( const char* dot = FindExtDot(name) )
 
95
        return std::string(name, dot);
 
96
    return name_s;
 
97
}
 
98
 
 
99
std::string get_extension(const fs::path& pth)
 
100
{
 
101
    std::string name_s = pth.leaf();
 
102
 
 
103
    if( const char* dot = FindExtDot(name_s.c_str()) )
 
104
        return std::string(dot+1);
 
105
    return std::string();
 
106
}
 
107
 
 
108
#endif // BFS_VERSION_3
 
109
 
 
110
class tune_boost_filesystem
 
111
{
 
112
    public:
 
113
    tune_boost_filesystem()
 
114
    {
 
115
        // B.FS: отказались от проверок в ver>=2
 
116
        //// глобальная установка проверки имен файлов
 
117
        //// чтоб любые символы в именах файлов позволялись, для utf8
 
118
        //fs::path::default_name_check(fs::native);
 
119
 
 
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);
 
124
#endif
 
125
    }
 
126
} tune_boost_filesystem_obj;
 
127
 
 
128
std::wstring Utf8ToUcs16(const char* utf8_str)
 
129
{
 
130
    std::wstring res;
 
131
#ifdef _WIN32
 
132
    // можно напрямую использовать utf8_codecvt_facet, но так проще
 
133
    // (будет работать при fs::path::imbue(utf8_loc);)
 
134
    boost::filesystem3::path_traits::convert(utf8_str, 0, res, fs::path::codecvt());
 
135
#else
 
136
    ASSERT(0);
 
137
#endif
 
138
    return res;
 
139
}
 
140
 
 
141
namespace Project
 
142
{
 
143
 
 
144
fs::path MakeAbsolutePath(const fs::path& pth, const fs::path& cur_dir)
 
145
{
 
146
    fs::path res;
 
147
 
 
148
    if( pth.is_complete() )
 
149
        res = pth;
 
150
    else
 
151
    {
 
152
        fs::path dir = cur_dir.empty() ? fs::current_path() : cur_dir ;
 
153
        res = dir/pth;
 
154
    }
 
155
    return res.normalize();
 
156
}
 
157
 
 
158
// оба аргумента должны быть абсолютными путями
 
159
bool MakeRelativeToDir(fs::path& pth, fs::path dir)
 
160
{
 
161
    pth.normalize();
 
162
    dir.normalize();
 
163
    ASSERT( pth.is_complete() );
 
164
    ASSERT( dir.is_complete() );
 
165
 
 
166
    fs::path::iterator p_itr = pth.begin(), p_end = pth.end();
 
167
    fs::path::iterator d_itr = dir.begin(), d_end = dir.end();
 
168
    // только под Win
 
169
    if( *p_itr != *d_itr )
 
170
        return false;
 
171
 
 
172
    for( ; (p_itr != p_end) && (d_itr != d_end) && (*p_itr == *d_itr); 
 
173
         ++p_itr, ++d_itr )
 
174
        ;
 
175
 
 
176
    fs::path res;
 
177
    for( ; d_itr != d_end; ++d_itr )
 
178
        res /= "..";
 
179
    for( ; p_itr != p_end; ++p_itr )
 
180
        res /= *p_itr;
 
181
 
 
182
    pth = res;
 
183
    return true;
 
184
}
 
185
 
 
186
bool HaveFullAccess(const fs::path& path)
 
187
{
 
188
    ASSERT( fs::exists(path) );
 
189
    return 0 == access(path.string().c_str(), R_OK|W_OK|X_OK);
 
190
}
 
191
 
 
192
bool ClearAllFiles(const fs::path& dir_path, std::string& err_str)
 
193
{
 
194
    bool res = true;
 
195
    try
 
196
    {
 
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);
 
201
    }
 
202
    catch( const fs::filesystem_error& fe )
 
203
    {
 
204
        err_str = FormatFSError(fe);
 
205
        res = false;
 
206
    }
 
207
    return res;
 
208
}
 
209
 
 
210
} // namespace Project
 
211
 
 
212
std::string AppendPath(const std::string& dir, const std::string& path)
 
213
{
 
214
    return (fs::path(dir)/path).string();
 
215
}
 
216
 
 
217
std::string FormatFSError(const fs::filesystem_error& fe)
 
218
{
 
219
    std::string what  = fe.code().message();
 
220
    // бывает и второй путь, path2(), но пока не требуется
 
221
    std::string fpath = fe.path1().string();
 
222
    if( !fpath.empty() )
 
223
        what = boost::format("%1%: \"%2%\"") % what % fpath % bf::stop;
 
224
    return what;
 
225
}
 
226