X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fpath.h;h=8c24f191394e5198d14fd7222f54f3c7bf1a6df6;hp=6662a8c20a875856c347d6488747f9c515ecdb5f;hb=a17d6ad286e19e2222ab8b6c9a762a83bf2c6c56;hpb=05ea50ffd15fa74ab1059df2c63442fdf83dec96 diff --git a/source/path.h b/source/path.h index 6662a8c..8c24f19 100644 --- a/source/path.h +++ b/source/path.h @@ -1,70 +1,90 @@ -/* -This file is part of libmsppath -Copyright © 2006 Mikko Rasa, Mikkosoft Productions +/* $Id$ + +This file is part of libmspfs +Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ -#ifndef MSP_PATH_PATH_H_ -#define MSP_PATH_PATH_H_ + +#ifndef MSP_FS_PATH_H_ +#define MSP_FS_PATH_H_ #include #include namespace Msp { -namespace Path { +namespace FS { enum { #ifdef WIN32 - DIRCHAR='\\' + DIRSEP='\\' #else - DIRCHAR='/' + DIRSEP='/' #endif }; class Path { public: - class iterator + class Iterator { - public: - iterator &operator++(); - iterator &operator--(); - std::string operator*() const; - bool operator==(const iterator &i) const { return (start==i.start && end==i.end); } - bool operator!=(const iterator &i) const { return !(*this==i); } + friend class Path; + private: const Path &path; unsigned start,end; - iterator(const Path &); - - friend class Path; + Iterator(const Path &); + public: + Iterator &operator++(); + Iterator &operator--(); + std::string operator*() const; + bool operator==(const Iterator &i) const { return (start==i.start && end==i.end); } + bool operator!=(const Iterator &i) const { return !(*this==i); } }; - Path() { } - Path(const std::string &p) { init(p); } - Path(const char *p) { init(p); } +private: + std::string path; + +public: + Path(); + Path(const std::string &); + Path(const char *); + const std::string &str() const { return path; } + + /// Returns the number of components in the path. unsigned size() const; + bool empty() const { return path.empty(); } - bool is_absolute() const; - Path subpath(unsigned, unsigned =static_cast(-1)) const; - Path operator/(const Path &p) const { Path a=*this; a/=p; return a; } - Path &operator/=(const Path &); + + /// Determines whether the path starts from the root directory + bool is_absolute() const; + + /// Extracts a range of components from the path. + Path subpath(unsigned start, unsigned count=static_cast(-1)) const; + + /// Concatenates this path with another one, with usual filesystem semantics + Path operator/(const Path &p) const; + Path &operator/=(const Path &); + + /** + Extracts a single component from the path. Negative indices count from the + end of the path. + */ std::string operator[](int) const; + bool operator==(const Path &) const; - iterator begin() const { return iterator(*this); } - iterator end() const { iterator i(*this); i.start=i.end=std::string::npos; return i; } + Iterator begin() const; + Iterator end() const; private: - std::string path; - void init(const std::string &); void add_component(const std::string &); }; inline std::ostream &operator<<(std::ostream &o, const Path &p) { o<