Pathname.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00012 #ifndef ZYPP_PATHNAME_H
00013 #define ZYPP_PATHNAME_H
00014
00015 #include <iosfwd>
00016 #include <string>
00017
00019 namespace zypp
00020 {
00021
00022 class Url;
00023
00025 namespace filesystem
00026 {
00027
00029
00030
00031
00045 class Pathname
00046 {
00047 public:
00049 Pathname()
00050 : prfx_i( 0 )
00051 {}
00052
00054 Pathname( const std::string & name_tv )
00055 { _assign( name_tv ); }
00056
00058 Pathname( const char * name_tv )
00059 { _assign( name_tv ? name_tv : "" ); }
00060
00062 Pathname & operator=( const Pathname & path_tv )
00063 {
00064 prfx_i = path_tv.prfx_i;
00065 name_t = path_tv.name_t;
00066 return *this;
00067 }
00068
00070 Pathname & operator/=( const Pathname & path_tv )
00071 { return( *this = cat( *this, path_tv ) ); }
00072
00076 Pathname & operator+=( const Pathname & path_tv )
00077 { return( *this = cat( *this, path_tv ) ); }
00078
00080 const std::string & asString() const
00081 { return name_t; }
00082
00084 static std::string showRoot( const Pathname & root_r, const Pathname & path_r );
00085
00087 static std::string showRootIf( const Pathname & root_r, const Pathname & path_r );
00088
00090 Url asUrl() const;
00091
00093 const char * c_str() const
00094 { return name_t.c_str(); }
00095
00097 bool empty() const { return name_t.empty(); }
00099 bool absolute() const { return !empty() && name_t[prfx_i] == '/'; }
00101 bool relative() const { return !empty() && name_t[prfx_i] != '/'; }
00102
00104 Pathname dirname() const { return dirname( *this ); }
00105 static Pathname dirname( const Pathname & name_tv );
00106
00108 std::string basename() const { return basename( *this ); }
00109 static std::string basename( const Pathname & name_tv );
00110
00115 std::string extension() const { return extension( *this ); }
00116 static std::string extension( const Pathname & name_tv );
00117
00119 Pathname absolutename() const { return absolutename( *this ); }
00120 static Pathname absolutename( const Pathname & name_tv )
00121 { return name_tv.relative() ? cat( "/", name_tv ) : name_tv; }
00122
00124 Pathname relativename() const { return relativename( *this ); }
00125 static Pathname relativename( const Pathname & name_tv )
00126 { return name_tv.absolute() ? cat( ".", name_tv ) : name_tv; }
00127
00129 static Pathname assertprefix( const Pathname & root_r, const Pathname & path_r );
00130
00139 Pathname cat( const Pathname & r ) const { return cat( *this, r ); }
00140 static Pathname cat( const Pathname & l, const Pathname & r );
00141
00147 Pathname extend( const std::string & r ) const { return extend( *this, r ); }
00148 static Pathname extend( const Pathname & l, const std::string & r );
00149
00150 private:
00151 std::string::size_type prfx_i;
00152 std::string name_t;
00153
00154 void _assign( const std::string & name_tv );
00155 };
00157
00159 inline bool operator==( const Pathname & l, const Pathname & r )
00160 { return l.asString() == r.asString(); }
00161
00163 inline bool operator!=( const Pathname & l, const Pathname & r )
00164 { return l.asString() != r.asString(); }
00165
00167 inline Pathname operator/( const Pathname & l, const Pathname & r )
00168 { return Pathname::cat( l, r ); }
00169
00173 inline Pathname operator+( const Pathname & l, const Pathname & r )
00174 { return Pathname::cat( l, r ); }
00175
00177 inline bool operator<( const Pathname & l, const Pathname & r )
00178 { return l.asString() < r.asString(); }
00179
00181
00183 inline std::ostream & operator<<( std::ostream & str, const Pathname & obj )
00184 { return str << obj.asString(); }
00185
00187 }
00189
00191 using filesystem::Pathname;
00192
00194 }
00196 #endif // ZYPP_PATHNAME_H