00001 /**************************************************************************** 00002 Copyright (C)1996 David Jung <opensim@pobox.com> 00003 00004 This program/file is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. (http://www.gnu.org) 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 $Id: PathName 1029 2004-02-11 20:45:54Z jungd $ 00019 $Revision: 1.3 $ 00020 $Date: 2004-02-11 15:45:54 -0500 (Wed, 11 Feb 2004) $ 00021 $Author: jungd $ 00022 00023 ****************************************************************************/ 00024 00025 #ifndef _BASE_PATHNAME_ 00026 #define _BASE_PATHNAME_ 00027 00028 #include <base/base> 00029 #include <base/array> 00030 #include <base/Object> 00031 #include <base/Cloneable> 00032 #include <base/PathComponent> 00033 00034 #include <stdexcept> 00035 00036 namespace base { 00037 00038 00039 class PathName : public Object, public Cloneable 00040 { 00041 public: 00042 PathName(); 00043 PathName(const String& name) throw(std::invalid_argument); 00044 PathName(const PathName& pn); 00045 virtual ~PathName(); 00046 00047 virtual String className() const { return String("PathName"); } 00048 00049 static const char separator = '/'; 00050 static const String currentDirectory; // "." 00051 static const String parentDirectory; // ".." 00052 00053 static bool validName(const String& name) { return true; } //!!! should check for no '/'s, only one '.', first is alpha etc. 00054 static bool validPath(const String& name) { return true; } //!!! should check for valid dir name 00055 00056 virtual void parent(); 00057 virtual void canonical(); // resolves path to contain no '..' path components 00058 virtual String str() const; 00059 virtual PathName path() const; 00060 virtual PathName name() const; 00061 virtual String extension() const; 00062 virtual bool isRelative() const; 00063 virtual bool isAbsolute() const; 00064 virtual bool isSimple() const; 00065 00066 virtual PathName& prepend(const String& prefix); 00067 00068 virtual Int size() const; 00069 virtual bool empty() const; 00070 virtual String operator[](Int i) const; 00071 virtual String operator()(Int i) const; 00072 00073 virtual bool operator==(const PathName& pn) const; 00074 virtual bool operator!=(const PathName& pn) const; 00075 virtual bool operator==(const String& pn) const; 00076 virtual bool operator!=(const String& pn) const; 00077 virtual PathName& operator=(const PathName& pn); 00078 virtual PathName& operator+=(const PathName& pn); 00079 virtual Object& clone() const; 00080 00081 protected: 00082 bool relative; 00083 typedef array<PathComponent> PathComponents; 00084 PathComponents _path; 00085 }; 00086 00087 00088 // Operations 00089 00090 inline bool PathName::isRelative() const 00091 { return relative; } 00092 00093 inline bool PathName::isAbsolute() const 00094 { return !relative; } 00095 00096 inline bool PathName::isSimple() const 00097 { return relative && (_path.size()==1); } 00098 00099 inline PathName& PathName::prepend(const String& prefix) 00100 { 00101 PathName newpn(prefix); newpn += *this; 00102 *this = newpn; 00103 return *this; 00104 } 00105 00106 inline Int PathName::size() const 00107 { return _path.size(); } 00108 00109 inline bool PathName::empty() const 00110 { return (_path.size()==0); } 00111 00112 inline String PathName::operator[](Int i) const 00113 { 00114 if ((i < 0) || (i>=size())) 00115 throw std::out_of_range(Exception("path component index out of range")); 00116 return _path[i].name(); 00117 } 00118 00119 inline String PathName::operator()(Int i) const 00120 { return operator[](i-1); } 00121 00122 inline PathName operator+(const PathName& p1, const PathName& p2) 00123 { PathName r(p1); return r+=p2; } 00124 00125 inline PathName operator+(const PathName& p, const String& s) 00126 { PathName r(p); return r+=PathName(s); } 00127 00128 00129 00130 00131 00132 } // base 00133 00134 #endif