| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "FsPath.h" |
| 10 |
|
| 11 |
#include "Log.h" |
| 12 |
|
| 13 |
#include "boost/filesystem/operations.hpp" |
| 14 |
#include "boost/filesystem/convenience.hpp" |
| 15 |
#include "boost/filesystem/exception.hpp" |
| 16 |
#include "boost/version.hpp" |
| 17 |
|
| 18 |
//----------------------------------------------------------------- |
| 19 |
inline boost::filesystem::path |
| 20 |
boostPath(const std::string &file) |
| 21 |
{ |
| 22 |
#if BOOST_VERSION < 103100 |
| 23 |
return boost::filesystem::path(file); |
| 24 |
#else |
| 25 |
return boost::filesystem::path(file, |
| 26 |
boost::filesystem::portable_posix_name); |
| 27 |
#endif |
| 28 |
} |
| 29 |
|
| 30 |
//----------------------------------------------------------------- |
| 31 |
/** |
| 32 |
* Get native filename. |
| 33 |
* @param file posix filename |
| 34 |
*/ |
| 35 |
std::string |
| 36 |
FsPath::getNative(const std::string &file) |
| 37 |
{ |
| 38 |
return boostPath(file).native_file_string(); |
| 39 |
} |
| 40 |
//----------------------------------------------------------------- |
| 41 |
/** |
| 42 |
* Returns true when file or directory exists. |
| 43 |
* @param file posix filename |
| 44 |
*/ |
| 45 |
bool |
| 46 |
FsPath::exists(const std::string &file) |
| 47 |
{ |
| 48 |
return boost::filesystem::exists(boostPath(file)); |
| 49 |
} |
| 50 |
//----------------------------------------------------------------- |
| 51 |
/** |
| 52 |
* Joint two paths. |
| 53 |
* @param dir posix filename |
| 54 |
* @param file posix filename |
| 55 |
* @return "dir/file" |
| 56 |
*/ |
| 57 |
std::string |
| 58 |
FsPath::join(const std::string &dir, const std::string &file) |
| 59 |
{ |
| 60 |
return (boostPath(dir) / boostPath(file)).string(); |
| 61 |
} |
| 62 |
//----------------------------------------------------------------- |
| 63 |
/** |
| 64 |
* Create all directories in path (like "mkdir -p"). |
| 65 |
* @param file posix filename |
| 66 |
*/ |
| 67 |
void |
| 68 |
FsPath::createPath(const std::string &file) |
| 69 |
{ |
| 70 |
boost::filesystem::create_directories(boostPath(file).branch_path()); |
| 71 |
} |
| 72 |
|