| 1 |
/* |
| 2 |
Copyright (C) 2009/2010 Kai Sterker <[email protected]> |
| 3 |
Part of the Adonthell Project <http://adonthell.nongnu.org> |
| 4 |
|
| 5 |
Dlgedit 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 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
Dlgedit 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 Dlgedit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @file common/util.h |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief Helper methods. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef COMMON_UTIL_H |
| 27 |
#define COMMON_UTIL_H |
| 28 |
|
| 29 |
#include <string> |
| 30 |
|
| 31 |
#ifdef WIN32 |
| 32 |
#define MK_UNIX_PATH(path) util::to_unix_path(path) |
| 33 |
#else |
| 34 |
#define MK_UNIX_PATH(path) path |
| 35 |
#endif |
| 36 |
|
| 37 |
/** |
| 38 |
* Some helper methods used across the different tools. |
| 39 |
*/ |
| 40 |
class util |
| 41 |
{ |
| 42 |
public: |
| 43 |
/** |
| 44 |
* Create a path that is relative to either the user supplied or builtin |
| 45 |
* data directory. If the given path points into neither of those, try to |
| 46 |
* remove anything before the specified target_dir (so that the returned |
| 47 |
* relative path starts with target_dir). |
| 48 |
* @param path the path to make relative. |
| 49 |
* @param target_dir starting directory for a relative path used as fallback. |
| 50 |
* @return a relative path on success, or a canonical absolute path on error. |
| 51 |
*/ |
| 52 |
static std::string get_relative_path (const std::string & path, const std::string & target_dir); |
| 53 |
|
| 54 |
/** |
| 55 |
* Convert a relative path to an absolute path. |
| 56 |
* @param path the path to convert. |
| 57 |
* @return an absolute path pointing to the location of path. |
| 58 |
*/ |
| 59 |
static std::string get_absolute_path (const std::string & path); |
| 60 |
|
| 61 |
/** |
| 62 |
* Convert windows directory names to unix directory names. All paths in |
| 63 |
* data files are supposed to be in unix-style notation. |
| 64 |
* @param path the pathname to convert. |
| 65 |
* @return path where '\\' is replaced with '/'. |
| 66 |
*/ |
| 67 |
static std::string to_unix_path (const std::string & path); |
| 68 |
|
| 69 |
private: |
| 70 |
/** |
| 71 |
* Try to create a path relative to the given base path. |
| 72 |
* This is used so we can store objects relative to the data directory. |
| 73 |
* |
| 74 |
* @note Note that path will be modified by the method to be a relative |
| 75 |
* path on success or a canonical absolute path on failure. |
| 76 |
* |
| 77 |
* @param path full path to make relative. |
| 78 |
* @param base_path path to the data directory. |
| 79 |
* @return true if base path has been subtracted from the path. |
| 80 |
*/ |
| 81 |
static bool remove_common_path (std::string & path, const std::string & base_path); |
| 82 |
}; |
| 83 |
|
| 84 |
#endif |