/builddir/build/BUILD/raul-0.4.0/raul/Path.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
00003  * 
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  * 
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  * 
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_PATH_HPP
00019 #define RAUL_PATH_HPP
00020 
00021 #include <iostream>
00022 #include <cctype>
00023 #include <string>
00024 #include <cassert>
00025 
00026 namespace Raul {
00027 
00028     
00043 class Path : public std::basic_string<char> {
00044 public:
00045 
00047     Path() : std::basic_string<char>("/") {}
00048 
00054     Path(const std::basic_string<char>& path)
00055         : std::basic_string<char>(path)
00056     {
00057         assert(is_valid(path));
00058     }
00059     
00060 
00066     Path(const char* cpath)
00067         : std::basic_string<char>(cpath)
00068     {
00069         assert(is_valid(cpath));
00070     }
00071     
00072     static bool is_valid(const std::basic_string<char>& path);
00073     
00074     static bool is_valid_name(const std::basic_string<char>& name) {
00075         return name.length() > 0 && is_valid(std::string("/").append(name));
00076     }
00077 
00078     static std::string pathify(const std::basic_string<char>& str);
00079     static std::string nameify(const std::basic_string<char>& str);
00080 
00081     static void replace_invalid_chars(std::string& str, bool replace_slash = false);
00082     
00083     bool is_child_of(const Path& parent) const;
00084     bool is_parent_of(const Path& child) const;
00085 
00086     
00090     inline std::basic_string<char> name() const {
00091         if ((*this) == "/")
00092             return "";
00093         else
00094             return substr(find_last_of("/")+1);
00095     }
00096     
00097     
00103     inline Path parent() const {
00104         std::basic_string<char> parent = substr(0, find_last_of("/"));
00105         return (parent == "") ? "/" : parent;
00106     }
00107     
00108 
00114     inline const std::string base() const {
00115         if ((*this) == "/")
00116             return *this;
00117         else
00118             return (*this) + "/";
00119     }
00120 
00122     static bool descendant_comparator(const Path& parent, const Path& child) {
00123         return ( child == parent || (child.length() > parent.length() &&
00124                 (!strncmp(parent.c_str(), child.c_str(), parent.length())
00125                         && (parent == "/" || child[parent.length()] == '/'))) );
00126     }
00127 };
00128 
00129 
00130 } // namespace Raul
00131 
00132 #endif // RAUL_PATH_HPP

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1