gpath.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
20 
21 #ifndef G_PATH_H
22 #define G_PATH_H
23 
24 #include "gdef.h"
25 #include "gstrings.h"
26 #include <string>
27 #include <iostream>
28 
30 namespace G
31 {
32  class Path ;
33 }
34 
44 class G::Path
45 {
46 public:
47  Path() ;
50 
51  Path( const std::string & path ) ;
53 
54  Path( const char * path ) ;
56 
57  Path( const Path & path , const std::string & tail ) ;
59 
60  Path( const Path & path , const std::string & tail_1 , const std::string & tail_2 ) ;
62 
63  Path( const Path & other ) ;
65 
66  ~Path() ;
68 
69  bool simple() const ;
73 
74  std::string str() const ;
76 
77  std::string basename() const ;
80 
81  Path dirname() const ;
99 
100  std::string extension() const ;
103 
104  void removeExtension() ;
106 
107  bool isAbsolute() const ;
109 
110  bool isRelative() const ;
112 
113  bool hasDriveLetter() const ;
116 
117  Path & operator=( const Path & other ) ;
119 
120  void pathAppend( const std::string & tail ) ;
123 
124  static G::Path join( const G::Path & p1 , const G::Path & p2 ) ;
127 
128  Strings split( bool no_dot = true ) const ;
132 
133  bool operator==( const Path & path ) const ;
135 
136  bool operator!=( const Path & path ) const ;
138 
139 private:
140  void set( const std::string & path ) ;
141  void normalise() ;
142  void clear() ;
143  static std::string slashString() ;
144  static std::string doubleSlashString() ;
145  std::string driveString() const ;
146  std::string::size_type slashAt() const ;
147  bool hasNoSlash() const ;
148  std::string withoutTail() const ;
149  bool hasNetworkDrive() const ;
150  std::string dirnameImp() const ;
151 
152 private:
153  std::string m_str ;
154  std::string m_extension ;
155  std::string::size_type m_dot ;
156 } ;
157 
159 namespace G
160 {
161  inline
162  std::ostream & operator<<( std::ostream & stream , const Path & path )
163  {
164  return stream << path.str() ;
165  }
166 
167  inline
168  Path & operator+=( Path & p , const std::string & str )
169  {
170  p.pathAppend( str ) ;
171  return p ;
172  }
173 
174  inline
175  Path operator+( const Path & p , const std::string & str )
176  {
177  Path result( p ) ;
178  result.pathAppend( str ) ;
179  return result ;
180  }
181 }
182 
183 #endif
std::string str() const
Returns the path string.
Definition: gpath.cpp:135
Path & operator=(const Path &other)
Assignment operator.
Definition: gpath.cpp:380
Path()
Default constructor.
Definition: gpath.cpp:30
std::string basename() const
Returns the path, excluding drive/directory parts.
Definition: gpath.cpp:162
Strings split(bool no_dot=true) const
Spits the path into a list of component parts.
Definition: gpath.cpp:345
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
bool operator!=(const Path &path) const
Comparison operator.
Definition: gpath.cpp:375
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
bool hasDriveLetter() const
Returns true if the path has a leading drive letter (and the operating system uses drive letters)...
Definition: gpath.cpp:267
std::string extension() const
Returns the path's filename extension.
Definition: gpath.cpp:320
bool simple() const
Returns true if the path is just a file/directory name without any separators.
Definition: gpath.cpp:140
bool isRelative() const
Returns true if the path is a relative path.
Definition: gpath.cpp:145
Low-level classes.
Path operator+(const Path &p, const std::string &str)
Definition: gpath.h:175
bool isAbsolute() const
Returns !isRelative().
Definition: gpath.cpp:150
bool operator==(const Path &path) const
Comparison operator.
Definition: gpath.cpp:368
Path dirname() const
Returns the drive/directory parts of the path.
Definition: gpath.cpp:182
void pathAppend(const std::string &tail)
Appends a filename to the path.
Definition: gpath.cpp:301
std::ostream & operator<<(std::ostream &stream, const G::Identity &identity)
Definition: gidentity.h:138
Path & operator+=(Path &p, const std::string &str)
Definition: gpath.h:168
A Path object represents a file system path.
Definition: gpath.h:44
void removeExtension()
Modifies the path by removing any extension.
Definition: gpath.cpp:291
static G::Path join(const G::Path &p1, const G::Path &p2)
Joins two paths together.
Definition: gpath.cpp:325
~Path()
Destructor.
Definition: gpath.cpp:35