2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "NamingPolicy.h"
21 #include "GnashFileUtilities.h"
29 #include <boost/algorithm/string/replace.hpp>
35 std::string
urlToDirectory(const std::string
& path
);
40 OverwriteExisting::operator()(const URL
& url
) const
42 std::string path
= url
.path().substr(1);
44 // Replace all slashes with a _ for a flat directory structure.
45 boost::replace_all(path
, "/", "_");
47 const std::string
& dir
= urlToDirectory(url
.hostname() + "/");
49 if (dir
.empty()) return std::string();
55 IncrementalRename::IncrementalRename(URL baseURL
)
57 _baseURL(std::move(baseURL
))
63 IncrementalRename::operator()(const URL
& url
) const
66 const std::string
& path
= url
.path();
67 assert(!path
.empty());
68 assert(path
[0] == '/');
70 // Find the last dot, but not if it's first in the path (after the
72 std::string::size_type dot
= path
.rfind('.');
73 if (dot
== 1) dot
= std::string::npos
;
75 // Take the path from after the initial '/' to the last '.' for
76 // manipulation. It doesn't matter if dot is npos.
77 std::string pre
= path
.substr(1, dot
- 1);
79 // Replace all slashes with a _ for a flat directory structure.
80 boost::replace_all(pre
, "/", "_");
82 const std::string
& suffix
= (dot
== std::string::npos
) ? "" :
85 // Add a trailing slash.
86 const std::string
& hostname
= _baseURL
.hostname().empty() ? "localhost" :
89 const std::string
& dir
= urlToDirectory(hostname
+ "/");
90 if (dir
.empty()) return std::string();
92 std::ostringstream
s(dir
+ pre
+ suffix
);
96 const size_t m
= std::numeric_limits
<size_t>::max();
99 while (stat(s
.str().c_str(), &st
) >= 0 && i
< m
) {
101 s
<< dir
<< pre
<< i
<< suffix
;
105 // If there are no options left, return an empty string.
107 return std::string();
116 /// Transform a URL into a directory and create it.
118 /// @return an empty string if the directory cannot be created, otherwise
119 /// the name of the created directory with a trailing slash.
120 /// @param url The path to transform. Anything after the last '/' is ignored.
122 urlToDirectory(const std::string
& path
)
125 const RcInitFile
& rcfile
= RcInitFile::getDefaultInstance();
126 const std::string
& dir
= rcfile
.getMediaDir() + "/" + path
;
128 // Create the user-specified directory if possible.
129 // An alternative would be to use the 'host' part and create a
131 if (!mkdirRecursive(dir
)) {
132 return std::string();
139 } // anonymous namespace