27 #include <boost/algorithm/string.hpp> 28 #include <boost/filesystem.hpp> 29 #include <boost/regex.hpp> 38 using boost::filesystem::path;
39 using boost::filesystem::exists;
40 using boost::filesystem::is_regular_file;
41 using boost::filesystem::is_directory;
42 using boost::filesystem::file_size;
43 using boost::filesystem::directory_iterator;
44 using boost::filesystem::create_directories;
53 return (exists(p.
c_str()) && is_regular_file(p.
c_str()));
57 return (exists(p.
c_str()) && is_directory(p.
c_str()));
61 uintmax_t size = (uintmax_t) -1;
64 size = file_size(p.
c_str());
68 if (size == ((uintmax_t) -1)) {
73 if (size > 0x7FFFFFFF) {
84 return file.filename().string();
90 return file.stem().string();
96 return file.extension().string();
100 path file(p.
c_str());
102 file.replace_extension(ext.
c_str());
104 return file.string();
108 path file(p.
c_str());
110 return file.parent_path().string();
114 return boost::filesystem::path(p.
c_str()).is_absolute();
118 const boost::regex bSlash(
"\\\\");
119 const std::string fSlash(
"/");
120 const boost::match_flag_type flags(boost::match_default | boost::format_sed);
122 return path(boost::regex_replace(p.string(), bSlash, fSlash, flags));
129 path::iterator itr = source.begin();
130 if ((itr != source.end()) && (itr->string() ==
"~")) {
133 for (++itr; itr != source.end(); ++itr)
139 return convertToSlash(boost::filesystem::absolute(source)).string().c_str();
156 boost::filesystem::path result;
159 boost::filesystem::path::iterator itr = source.begin();
160 if ((itr != source.end()) && (itr->string() ==
"~")) {
163 for (++itr; itr != source.end(); ++itr)
174 for (itr = source.begin(); itr != source.end(); ++itr) {
176 if (itr->string() ==
".")
180 if (resolveSymLinks && (itr->string() ==
"..")) {
181 if (result != source.root_path())
182 result.remove_filename();
192 if (!resolveSymLinks)
195 boost::system::error_code ec;
196 boost::filesystem::path link = boost::filesystem::read_symlink(result, ec);
199 result.remove_filename();
201 if (link.is_absolute()) {
202 for (++itr; itr != source.end(); ++itr)
208 boost::filesystem::path newSource = result;
212 for (++itr; itr != source.end(); ++itr)
224 if (!result.has_root_directory())
225 result = boost::filesystem::absolute(result);
235 path dirPath(directory.
c_str());
239 directory_iterator itEnd;
240 for (directory_iterator itDir(dirPath); itDir != itEnd; ++itDir) {
241 if (is_directory(itDir->status())) {
242 subDirectories.push_back(itDir->path().generic_string());
263 dirs.push_back(curDir);
275 dirs.push_back(curDir);
279 bool caseInsensitive) {
283 if (subDirectory ==
UString(
"."))
286 const path dirPath(directory.
c_str());
289 if (subDirectory ==
UString(
"..")) {
290 path parent = dirPath.parent_path();
291 if (parent == dirPath)
294 return parent.generic_string();
298 directory_iterator itEnd;
299 for (directory_iterator itDir(dirPath); itDir != itEnd; ++itDir) {
300 if (is_directory(itDir->status())) {
303 if (caseInsensitive) {
304 if (iequals(itDir->path().filename().string(), subDirectory.
c_str()))
305 return itDir->path().generic_string();
307 if (equals(itDir->path().filename().string(), subDirectory.
c_str()))
308 return itDir->path().generic_string();
319 bool caseInsensitive) {
321 if (!exists(directory.
c_str()) || !is_directory(directory.
c_str()))
325 if (subDirectory.
empty())
330 std::list<UString> dirs;
335 for (std::list<UString>::iterator it = dirs.begin(); it != dirs.end(); ++it)
344 return create_directories(path.
c_str());
345 }
catch (std::exception &se) {
351 const boost::regex esc(
"[\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\\\]");
352 const std::string rep(
"\\\\\\1&");
354 return boost::regex_replace(std::string(str.
c_str()), esc, rep, boost::match_default | boost::format_sed);
358 static const char *
const sizes[] = {
"B",
"K",
"M",
"G"};
363 while ((s >= 1024) && ((n + 1) <
ARRAYSIZE(sizes))) {
static UString getUserDataDirectory()
Return the OS-specific path of the user data directory.
static UString getHumanReadableSize(size_t size)
Format this file size into a human readable string.
static void splitDirectories(const UString &directory, std::list< UString > &dirs)
A class holding an UTF-8 string.
static UString absolutize(const UString &p)
Return the absolute path.
static UString getExtension(const UString &p)
Return a file name's extension.
static bool getSubDirectories(const UString &directory, std::list< UString > &subDirectories)
Collect all direct subdirectories of a directory in a list.
bool beginsWith(const UString &with) const
static path convertToSlash(const path &p)
static UString findSubDirectory_internal(const UString &directory, const UString &subDirectory, bool caseInsensitive)
static bool isDirectory(const UString &p)
Does specified path exist and is it a directory?
iterator getPosition(size_t n) const
Convert a numerical position into an iterator.
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Basic exceptions to throw.
UString substr(iterator from, iterator to) const
utf8::iterator< std::string::const_iterator > iterator
const char * c_str() const
Return the (utf8 encoded) string data.
static UString getUserDataFile(UString file)
Return a path suitable for writing into.
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
static bool isRegularFile(const UString &p)
Does specified path exist and is it a regular file?
Utility templates and functions.
static UString escapeStringLiteral(const UString &str)
Escape a string literal for use in a regexp.
static UString canonicalize(const UString &p, bool resolveSymLinks=true)
Return the canonical, absolutized and normalized path.
Utility functions for working with differing string encodings.
bool empty() const
Is the string empty?
static UString getDirectory(const UString &p)
Return a path's directory.
void warning(const char *s,...)
static UString getConfigDirectory()
Return the OS-specific path of the config directory.
static UString getStem(const UString &p)
Return a file name's stem.
size_t size() const
Return the size of the string, in characters.
static bool createDirectories(const UString &path)
Create all directories in this path.
static bool isAbsolute(const UString &p)
Is the given string an absolute path?
static UString relativize(const UString &basePath, const UString &path)
Return the path relative to the base path.
static size_t getFileSize(const UString &p)
Return a file's size.
static UString normalize(const UString &p, bool resolveSymLinks=true)
Normalize a path.
static const size_t kFileInvalid
static UString getHomeDirectory()
Return the OS-specific path of the user's home directory.
void clear()
Clear the string's contents.
static UString getFile(const UString &p)
Return a file name without its path.
Utility class for manipulating file paths.
static UString changeExtension(const UString &p, const UString &ext="")
Change a file name's extension.
static UString findSubDirectory(const UString &directory, const UString &subDirectory, bool caseInsensitive=false)
Find a directory's subdirectory.