/* * path.h * * Created on: Jul 25, 2012 * Author: gregor */ #ifndef GLTB_PATH_H_ #define GLTB_PATH_H_ #include #include #ifdef WIN32 #include #include #endif #ifdef LINUX #include #endif namespace gltb { class Path { public: Path(); /** * Construct a path from the given string. * If the path is relative, it is assumed to be relative to the current * working directory. */ Path(std::string path); std::string getPath() { return path; }; /** * Get the canonical path to the same file or directory. * The canonical path is the absolute path with all links * resolved and replaced with their targets. Thus, two paths * point to the same file or directory if their canonical paths * are the same. */ Path getCanonicalPath(); /** * If the path points to a file, returns the path * of the containing directory without the file. If * the path points to a directory, it will not be changed. */ Path getDirectoryPath(); /** * Return parent path, unless path is already a root path. * For a file, the parent is the containing directory. * If this path is a root path. the root path itself is returned. * If the path is a relative path with only one element, "." is returned. */ Path getParentPath(); /** * Make the path relative to the given path. */ Path makeRelativeTo(Path startingPath); /** * If the path is relative, turn it into an absolute path with * the current directory as starting point. Note that this function * only concatenates paths. */ Path makeAbsolute(); /** * If the path is relative, turn it into an absolute path with * the given directory as starting point. Note that this function * only concatenates paths. */ Path makeAbsolute(Path startingPath); bool isRelative(); bool isAbsolute(); /** * Get file name (or directory name) that the path points to with any * all other path elements stripped. */ std::string getFilename(); /** * Test if file or directory that path points to exists at all. */ bool exists(); /** * Test if target of path is a regular file. * * Remember that a path may point to an object that is neither a file, * nor a directory nor a link. */ bool isFile(); /** * Test if target of path is a directory. * * Remember that a path may point to an object that is neither a file, * nor a directory nor a link. */ bool isDirectory(); /** * Test if target of path is a symbolic link. * * Remember that a path may point to an object that is neither a file, * nor a directory nor a link. */ bool isLink(); std::vector listContents(); #ifndef WIN32 static const char pathSeparator = '/'; #else static const char driveSeparator = ':'; static const char pathSeparator = '\\'; #endif static Path getCurrentDirectory(); static std::vector getRootPaths(); private: std::string path; #ifdef WIN32 struct _stat statResult; #else struct stat statResult; #endif bool statPath(); }; } #endif /* GLTB_PATH_H_ */