<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>RAUL: Path.hpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.7.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> </ul> </div> <div class="header"> <div class="headertitle"> <h1>Path.hpp</h1> </div> </div> <div class="contents"> <div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* This file is part of Raul.</span> <a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2007-2009 David Robillard <http://drobilla.net></span> <a name="l00003"></a>00003 <span class="comment"> *</span> <a name="l00004"></a>00004 <span class="comment"> * Raul is free software; you can redistribute it and/or modify it under the</span> <a name="l00005"></a>00005 <span class="comment"> * terms of the GNU General Public License as published by the Free Software</span> <a name="l00006"></a>00006 <span class="comment"> * Foundation; either version 2 of the License, or (at your option) any later</span> <a name="l00007"></a>00007 <span class="comment"> * version.</span> <a name="l00008"></a>00008 <span class="comment"> *</span> <a name="l00009"></a>00009 <span class="comment"> * Raul is distributed in the hope that it will be useful, but WITHOUT ANY</span> <a name="l00010"></a>00010 <span class="comment"> * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS</span> <a name="l00011"></a>00011 <span class="comment"> * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.</span> <a name="l00012"></a>00012 <span class="comment"> *</span> <a name="l00013"></a>00013 <span class="comment"> * You should have received a copy of the GNU General Public License along</span> <a name="l00014"></a>00014 <span class="comment"> * with this program; if not, write to the Free Software Foundation, Inc.,</span> <a name="l00015"></a>00015 <span class="comment"> * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA</span> <a name="l00016"></a>00016 <span class="comment"> */</span> <a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="preprocessor">#ifndef RAUL_PATH_HPP</span> <a name="l00019"></a>00019 <span class="preprocessor"></span><span class="preprocessor">#define RAUL_PATH_HPP</span> <a name="l00020"></a>00020 <span class="preprocessor"></span> <a name="l00021"></a>00021 <span class="preprocessor">#include <iostream></span> <a name="l00022"></a>00022 <span class="preprocessor">#include <cctype></span> <a name="l00023"></a>00023 <span class="preprocessor">#include <string></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <cstring></span> <a name="l00025"></a>00025 <span class="preprocessor">#include <cassert></span> <a name="l00026"></a>00026 <a name="l00027"></a>00027 <span class="preprocessor">#include "raul/Symbol.hpp"</span> <a name="l00028"></a>00028 <span class="preprocessor">#include "raul/URI.hpp"</span> <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="keyword">namespace </span>Raul { <a name="l00031"></a>00031 <a name="l00032"></a>00032 <a name="l00046"></a><a class="code" href="classRaul_1_1Path.html">00046</a> <span class="keyword">class </span><a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a> : <span class="keyword">public</span> <a class="code" href="classRaul_1_1URI.html" title="Simple wrapper around standard string with useful URI-specific methods.">URI</a> { <a name="l00047"></a>00047 <span class="keyword">public</span>: <a name="l00048"></a>00048 <span class="keyword">class </span>BadPath : <span class="keyword">public</span> std::exception { <a name="l00049"></a>00049 <span class="keyword">public</span>: <a name="l00050"></a>00050 BadPath(<span class="keyword">const</span> std::string& path) : _path(path) {} <a name="l00051"></a>00051 ~BadPath() <span class="keywordflow">throw</span>() {} <a name="l00052"></a>00052 <span class="keyword">const</span> <span class="keywordtype">char</span>* what() <span class="keyword">const</span> <span class="keywordflow">throw</span>() { <span class="keywordflow">return</span> _path.c_str(); } <a name="l00053"></a>00053 <span class="keyword">private</span>: <a name="l00054"></a>00054 std::string _path; <a name="l00055"></a>00055 }; <a name="l00056"></a>00056 <a name="l00063"></a>00063 <span class="keyword">static</span> <span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a> <a class="code" href="classRaul_1_1Path.html#ac4a4e06be4318672ff9238fdf824a2b9" title="Return the root path.">root</a>(); <a name="l00064"></a>00064 <a name="l00078"></a>00078 <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="classRaul_1_1Path.html#ab9f5e3239dca6bd2c81b893584a59630" title="Set the root path.">set_root</a>(<span class="keyword">const</span> <a class="code" href="classRaul_1_1URI.html" title="Simple wrapper around standard string with useful URI-specific methods.">Raul::URI</a>& uri); <a name="l00079"></a>00079 <a name="l00080"></a>00080 <span class="keyword">static</span> <span class="keywordtype">bool</span> is_path(<span class="keyword">const</span> <a class="code" href="classRaul_1_1URI.html" title="Simple wrapper around standard string with useful URI-specific methods.">Raul::URI</a>& uri); <a name="l00081"></a>00081 <a name="l00083"></a><a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536">00083</a> <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>() : <a class="code" href="classRaul_1_1URI.html" title="Simple wrapper around standard string with useful URI-specific methods.">URI</a>(root()) {} <a name="l00084"></a>00084 <a name="l00090"></a>00090 <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>(<span class="keyword">const</span> std::basic_string<char>& path); <a name="l00091"></a>00091 <a name="l00097"></a>00097 <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* cpath); <a name="l00098"></a>00098 <a name="l00099"></a>00099 <a name="l00105"></a><a class="code" href="classRaul_1_1Path.html#a47641db3a8b367b038afcf01afc9996c">00105</a> <a class="code" href="classRaul_1_1Path.html#a47641db3a8b367b038afcf01afc9996c" title="Construct a Path from another path.">Path</a>(<span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a>& copy) : <a class="code" href="classRaul_1_1URI.html" title="Simple wrapper around standard string with useful URI-specific methods.">URI</a>(copy) {} <a name="l00106"></a>00106 <a name="l00107"></a>00107 <span class="keyword">static</span> <span class="keywordtype">bool</span> is_valid(<span class="keyword">const</span> std::basic_string<char>& path); <a name="l00108"></a>00108 <a name="l00109"></a>00109 <span class="keyword">static</span> <span class="keywordtype">bool</span> is_valid_name(<span class="keyword">const</span> std::basic_string<char>& name) { <a name="l00110"></a>00110 <span class="keywordflow">return</span> name.length() > 0 && name.find(<span class="stringliteral">"/"</span>) == std::string::npos <a name="l00111"></a>00111 && is_valid(std::string(<span class="stringliteral">"/"</span>).append(name)); <a name="l00112"></a>00112 } <a name="l00113"></a>00113 <a name="l00114"></a>00114 <span class="keyword">static</span> std::string <a class="code" href="classRaul_1_1Path.html#a897a6299821f2db5a4d7f08db12fc74f" title="Convert a string to a valid full path.">pathify</a>(<span class="keyword">const</span> std::basic_string<char>& str); <a name="l00115"></a>00115 <span class="keyword">static</span> std::string <a class="code" href="classRaul_1_1Path.html#a82d234fd91e09ae19a1b31940ec06263" title="Convert a string to a valid name (or &quot;method&quot; - tokens between slashes).">nameify</a>(<span class="keyword">const</span> std::basic_string<char>& str); <a name="l00116"></a>00116 <a name="l00117"></a>00117 <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="classRaul_1_1Path.html#ad1ffe394b2a4cb2c065a4cd50d55f63b" title="Replace any invalid characters in str with a suitable replacement.">replace_invalid_chars</a>(std::string& str, <span class="keywordtype">size_t</span> start, <span class="keywordtype">bool</span> replace_slash = <span class="keyword">false</span>); <a name="l00118"></a>00118 <a name="l00119"></a>00119 <span class="keywordtype">bool</span> is_root()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> (*<span class="keyword">this</span>) == <a class="code" href="classRaul_1_1Path.html#ac4a4e06be4318672ff9238fdf824a2b9" title="Return the root path.">root</a>(); } <a name="l00120"></a>00120 <a name="l00121"></a>00121 <span class="keywordtype">bool</span> is_child_of(<span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>& <a class="code" href="classRaul_1_1Path.html#a12fae32068c6164c4a2b37f9b144f28b" title="Return the parent&#39;s path.">parent</a>) <span class="keyword">const</span>; <a name="l00122"></a>00122 <span class="keywordtype">bool</span> is_parent_of(<span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>& child) <span class="keyword">const</span>; <a name="l00123"></a>00123 <a name="l00124"></a>00124 <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a> child(<span class="keyword">const</span> std::string& s)<span class="keyword"> const </span>{ <a name="l00125"></a>00125 <span class="keywordflow">if</span> (is_valid(s)) <a name="l00126"></a>00126 <span class="keywordflow">return</span> <a class="code" href="classRaul_1_1Path.html#af8c11bcf9033eaab59843d8a16146084" title="Return path with a trailing &quot;/&quot;.">base</a>() + <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>(s).chop_scheme().substr(1); <a name="l00127"></a>00127 <span class="keywordflow">else</span> <a name="l00128"></a>00128 <span class="keywordflow">return</span> <a class="code" href="classRaul_1_1Path.html#af8c11bcf9033eaab59843d8a16146084" title="Return path with a trailing &quot;/&quot;.">base</a>() + s; <a name="l00129"></a>00129 } <a name="l00130"></a>00130 <a name="l00131"></a>00131 <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a> child(<span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>& p)<span class="keyword"> const </span>{ <a name="l00132"></a>00132 <span class="keywordflow">return</span> <a class="code" href="classRaul_1_1Path.html#af8c11bcf9033eaab59843d8a16146084" title="Return path with a trailing &quot;/&quot;.">base</a>() + p.chop_scheme().substr(1); <a name="l00133"></a>00133 } <a name="l00134"></a>00134 <a name="l00135"></a>00135 <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a> operator+(<span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>& p)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> child(p); } <a name="l00136"></a>00136 <a name="l00142"></a><a class="code" href="classRaul_1_1Path.html#aea7d19e02e65bb2d26008477fa6198b8">00142</a> <span class="keyword">inline</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="classRaul_1_1Path.html#aea7d19e02e65bb2d26008477fa6198b8" title="Return the symbol of this path (everything after the last &#39;/&#39;).">symbol</a>()<span class="keyword"> const </span>{ <a name="l00143"></a>00143 <span class="keywordflow">if</span> ((*<span class="keyword">this</span>) != <a class="code" href="classRaul_1_1Path.html#ac4a4e06be4318672ff9238fdf824a2b9" title="Return the root path.">root</a>()) { <a name="l00144"></a>00144 <span class="keyword">const</span> <span class="keywordtype">char</span>* last_slash = strrchr(c_str(), <span class="charliteral">'/'</span>); <a name="l00145"></a>00145 <span class="keywordflow">if</span> (last_slash) { <a name="l00146"></a>00146 <span class="keywordflow">return</span> last_slash + 1; <a name="l00147"></a>00147 } <a name="l00148"></a>00148 } <a name="l00149"></a>00149 <span class="keywordflow">return</span> <span class="stringliteral">""</span>; <a name="l00150"></a>00150 } <a name="l00151"></a>00151 <a name="l00157"></a><a class="code" href="classRaul_1_1Path.html#a12fae32068c6164c4a2b37f9b144f28b">00157</a> <span class="keyword">inline</span> <a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a> <a class="code" href="classRaul_1_1Path.html#a12fae32068c6164c4a2b37f9b144f28b" title="Return the parent&#39;s path.">parent</a>()<span class="keyword"> const </span>{ <a name="l00158"></a>00158 <span class="keywordflow">if</span> ((*<span class="keyword">this</span>) == <a class="code" href="classRaul_1_1Path.html#ac4a4e06be4318672ff9238fdf824a2b9" title="Return the root path.">root</a>()) { <a name="l00159"></a>00159 <span class="keywordflow">return</span> *<span class="keyword">this</span>; <a name="l00160"></a>00160 } <span class="keywordflow">else</span> { <a name="l00161"></a>00161 <span class="keyword">const</span> std::string str(this->str()); <a name="l00162"></a>00162 <span class="keyword">const</span> <span class="keywordtype">size_t</span> first_slash = str.find(<span class="charliteral">'/'</span>); <a name="l00163"></a>00163 <span class="keyword">const</span> <span class="keywordtype">size_t</span> last_slash = str.find_last_of(<span class="charliteral">'/'</span>); <a name="l00164"></a>00164 <span class="keywordflow">return</span> (first_slash == last_slash) ? <a class="code" href="classRaul_1_1Path.html#ac4a4e06be4318672ff9238fdf824a2b9" title="Return the root path.">root</a>() : str.substr(0, last_slash); <a name="l00165"></a>00165 } <a name="l00166"></a>00166 } <a name="l00167"></a>00167 <a name="l00168"></a>00168 <a name="l00171"></a><a class="code" href="classRaul_1_1Path.html#aca906c3be23db1597de497df7ed4e522">00171</a> <span class="keyword">inline</span> <a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a> <a class="code" href="classRaul_1_1Path.html#aca906c3be23db1597de497df7ed4e522" title="Return the path&#39;s child with the given name (symbol).">child</a>(<span class="keyword">const</span> <a class="code" href="classRaul_1_1Symbol.html" title="A restricted string (C identifier, which is a component of a Path).">Raul::Symbol</a>& <a class="code" href="classRaul_1_1Path.html#aea7d19e02e65bb2d26008477fa6198b8" title="Return the symbol of this path (everything after the last &#39;/&#39;).">symbol</a>)<span class="keyword"> const </span>{ <a name="l00172"></a>00172 <span class="keywordflow">return</span> <a class="code" href="classRaul_1_1Path.html#af8c11bcf9033eaab59843d8a16146084" title="Return path with a trailing &quot;/&quot;.">base</a>() + symbol.c_str(); <a name="l00173"></a>00173 } <a name="l00174"></a>00174 <a name="l00175"></a>00175 <a name="l00178"></a><a class="code" href="classRaul_1_1Path.html#a72181c79457416e799f0d02d7dec3066">00178</a> <span class="keyword">inline</span> <a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a> <a class="code" href="classRaul_1_1Path.html#a72181c79457416e799f0d02d7dec3066" title="Return path relative to some base path (chop prefix).">relative_to_base</a>(<span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a>& <a class="code" href="classRaul_1_1Path.html#af8c11bcf9033eaab59843d8a16146084" title="Return path with a trailing &quot;/&quot;.">base</a>)<span class="keyword"> const </span>{ <a name="l00179"></a>00179 <span class="keywordflow">if</span> ((*<span class="keyword">this</span>) == base) { <a name="l00180"></a>00180 <span class="keywordflow">return</span> <span class="stringliteral">"/"</span>; <a name="l00181"></a>00181 } <span class="keywordflow">else</span> { <a name="l00182"></a>00182 assert(length() > base.length()); <a name="l00183"></a>00183 <span class="keywordflow">return</span> substr(base.length() - 1); <a name="l00184"></a>00184 } <a name="l00185"></a>00185 } <a name="l00186"></a>00186 <a name="l00187"></a>00187 <a name="l00193"></a><a class="code" href="classRaul_1_1Path.html#af8c11bcf9033eaab59843d8a16146084">00193</a> <span class="keyword">inline</span> <span class="keyword">const</span> std::string <a class="code" href="classRaul_1_1Path.html#af8c11bcf9033eaab59843d8a16146084" title="Return path with a trailing &quot;/&quot;.">base</a>()<span class="keyword"> const </span>{ <a name="l00194"></a>00194 std::string ret = str(); <a name="l00195"></a>00195 <span class="keywordflow">if</span> ((*<span class="keyword">this</span>) == <a class="code" href="classRaul_1_1Path.html#ac4a4e06be4318672ff9238fdf824a2b9" title="Return the root path.">root</a>() && ret[ret.length() - 1] == <span class="charliteral">'/'</span>) <a name="l00196"></a>00196 <span class="keywordflow">return</span> ret; <a name="l00197"></a>00197 <span class="keywordflow">else</span> <a name="l00198"></a>00198 <span class="keywordflow">return</span> ret + <span class="charliteral">'/'</span>; <a name="l00199"></a>00199 } <a name="l00200"></a>00200 <a name="l00206"></a><a class="code" href="classRaul_1_1Path.html#a962975b07ebe2acee4d99e4f738655c4">00206</a> <span class="keyword">inline</span> <span class="keyword">const</span> std::string <a class="code" href="classRaul_1_1Path.html#a962975b07ebe2acee4d99e4f738655c4" title="Return path with a trailing &quot;/&quot;.">base_no_scheme</a>()<span class="keyword"> const </span>{ <a name="l00207"></a>00207 <span class="keywordflow">return</span> <a class="code" href="classRaul_1_1Path.html#af8c11bcf9033eaab59843d8a16146084" title="Return path with a trailing &quot;/&quot;.">base</a>().substr(find(<span class="stringliteral">":"</span>) + 1); <a name="l00208"></a>00208 } <a name="l00209"></a>00209 <a name="l00210"></a>00210 <a name="l00212"></a><a class="code" href="classRaul_1_1Path.html#a4e2864b28e04fa32430efc2eccf0e1b9">00212</a> <span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classRaul_1_1Path.html#a4e2864b28e04fa32430efc2eccf0e1b9" title="Return true if child is equal to, or a descendant of parent.">descendant_comparator</a>(<span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a>& <a class="code" href="classRaul_1_1Path.html#a12fae32068c6164c4a2b37f9b144f28b" title="Return the parent&#39;s path.">parent</a>, <span class="keyword">const</span> <a class="code" href="classRaul_1_1Path.html" title="A URI which is a path (for example a filesystem or OSC path).">Path</a>& child) { <a name="l00213"></a>00213 <span class="keywordflow">return</span> ( child == parent || (child.length() > parent.length() && <a name="l00214"></a>00214 (!std::strncmp(parent.c_str(), child.c_str(), parent.length()) <a name="l00215"></a>00215 && (parent == <a class="code" href="classRaul_1_1Path.html#ac4a4e06be4318672ff9238fdf824a2b9" title="Return the root path.">root</a>() || child.str()[parent.length()] == <span class="charliteral">'/'</span>))) ); <a name="l00216"></a>00216 } <a name="l00217"></a>00217 <a name="l00218"></a>00218 <span class="keyword">private</span>: <a name="l00219"></a>00219 <span class="keyword">inline</span> <a class="code" href="classRaul_1_1Path.html#ad69dae35fcdefe7278edca40c7e65536" title="Construct an uninitialzed path, because the STL is annoying.">Path</a>(<span class="keywordtype">bool</span> unchecked, <span class="keyword">const</span> <a class="code" href="classRaul_1_1URI.html" title="Simple wrapper around standard string with useful URI-specific methods.">URI</a>& uri) : <a class="code" href="classRaul_1_1URI.html" title="Simple wrapper around standard string with useful URI-specific methods.">URI</a>(uri) {} <a name="l00220"></a>00220 }; <a name="l00221"></a>00221 <a name="l00222"></a>00222 <a name="l00223"></a>00223 } <span class="comment">// namespace Raul</span> <a name="l00224"></a>00224 <a name="l00225"></a>00225 <span class="preprocessor">#endif // RAUL_PATH_HPP</span> </pre></div></div> </div> <hr class="footer"/><address class="footer"><small>Generated on Wed Oct 6 2010 for RAUL by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.1 </small></address> </body> </html>