]> git.tdb.fi Git - libs/core.git/blobdiff - source/path.h
Refactor the API
[libs/core.git] / source / path.h
index 9eacf5199ca1c3fa0363788d698c5c1a14b79e15..8c24f191394e5198d14fd7222f54f3c7bf1a6df6 100644 (file)
@@ -12,13 +12,14 @@ Distributed under the LGPL
 #include <string>
 
 namespace Msp {
+namespace FS {
 
 enum
 {
 #ifdef WIN32
-       DIRCHAR='\\'
+       DIRSEP='\\'
 #else
-       DIRCHAR='/'
+       DIRSEP='/'
 #endif
 };
 
@@ -27,44 +28,63 @@ class Path
 public:
        class Iterator
        {
+               friend class Path;
+
+       private:
+               const Path &path;
+               unsigned   start,end;
+
+               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); }
-       private:
-               const Path &path;
-               unsigned   start,end;
+       };
 
-               Iterator(const Path &);
+private:
+       std::string path;
 
-               friend class Path;
-       };
+public:
+       Path();
+       Path(const std::string &);
+       Path(const char *);
 
-       Path() { }
-       Path(const std::string &p)     { init(p); }
-       Path(const char *p)            { init(p); }
        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<unsigned>(-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<unsigned>(-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<<p.str(); return o; }
 
+} // namespace FS
 } // namespace Msp
 
 #endif