Do not expect exact load timing. Hopefull makes buildbot results more stable (see...
[gnash.git] / libbase / NamingPolicy.cpp
blob1384bc012727380e4f64e537112a63d65c004674
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
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.
9 //
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.
14 //
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"
22 #include "rc.h"
23 #include "log.h"
24 #include "URL.h"
26 #include <sstream>
27 #include <string>
28 #include <limits>
29 #include <boost/algorithm/string/replace.hpp>
31 namespace gnash {
34 namespace {
35 std::string urlToDirectory(const std::string& path);
39 std::string
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();
51 return dir + path;
55 IncrementalRename::IncrementalRename(const URL& baseURL)
57 _baseURL(baseURL)
62 std::string
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
71 // initial '/').
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) ? "" :
83 path.substr(dot);
85 // Add a trailing slash.
86 const std::string& hostname = _baseURL.hostname().empty() ? "localhost" :
87 _baseURL.hostname();
89 const std::string& dir = urlToDirectory(hostname + "/");
90 if (dir.empty()) return std::string();
92 std::ostringstream s(dir + pre + suffix);
94 size_t i = 0;
96 const size_t m = std::numeric_limits<size_t>::max();
98 struct stat st;
99 while (stat(s.str().c_str(), &st) >= 0 && i < m) {
100 s.str("");
101 s << dir << pre << i << suffix;
102 ++i;
105 // If there are no options left, return an empty string.
106 if (i == m) {
107 return std::string();
110 return s.str();
114 namespace {
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.
121 std::string
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
130 // directory tree.
131 if (!mkdirRecursive(dir)) {
132 return std::string();
135 return dir;
139 } // anonymous namespace
140 } // namespace gnash