GEOS
Path.hpp
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
16 #ifndef GEOS_COMMON_PATH_HPP
17 #define GEOS_COMMON_PATH_HPP
18 
19 // System includes
20 #include <string>
21 #include <sstream>
22 #include <vector>
23 
24 namespace geos
25 {
32 class Path : public std::string
33 {
34 public:
35 
36  using std::string::string;
37 
41  Path()
42  {}
43 
48  Path( Path const & rhs ): std::string( rhs )
49  {}
50 
55  Path( Path && rhs ) noexcept: std::string( std::move( rhs ) )
56  {}
57 
63  Path & operator=( Path const & rhs )
64  {
65  std::string::operator=( rhs );
66  return *this;
67  }
68 
74  Path & operator=( Path && rhs ) noexcept
75  {
76  std::string::operator=( std::move( rhs ) );
77  return *this;
78  }
79 
84  {}
85 
92  {
93  pathPrefix() = p;
94  }
95 
102  {
103  return pathPrefix();
104  }
105 
110  {
111  size_type const pos = find_last_of( '/' );
112  return pos == npos ? static_cast< std::string >( *this ) : substr( pos + 1 );
113  }
114 
119  {
120  std::string const fname = filename();
121  size_type const pos = fname.find_last_of( '.' );
122  return pos == npos ? "" : fname.substr( pos + 1 );
123  }
124 
125 private:
126 
130  static std::string & pathPrefix()
131  {
132  static std::string s_pathPrefix = "";
133  return s_pathPrefix;
134  }
135 
136 };
137 
145 
152 inline bool isAbsolutePath( std::string const & path )
153 {
154  return !path.empty() && path[ 0 ] == '/';
155 }
156 
163 std::istream & operator>>( std::istream & is, Path & p );
164 
170 inline std::string trimPath( std::string const & path )
171 {
172  return ( !path.empty() && path.back() == '/' ) ? path.substr( 0, path.size() - 1 ) : path;
173 }
174 
180 std::pair< std::string, std::string > splitPath( std::string const & path );
181 
188 template< typename ... ARGS >
189 std::string joinPath( ARGS const & ... args )
190 {
191  size_t constexpr numParts = sizeof...(args);
192  static_assert( numParts > 0, "Must provide arguments" );
193  std::string parts[numParts] { trimPath( args ) ... };
194  std::ostringstream oss;
195  for( size_t i = 0; i < numParts - 1; ++i )
196  {
197  if( !parts[i].empty() )
198  {
199  oss << parts[i] << '/';
200  }
201  }
202  oss << parts[numParts - 1];
203  return oss.str();
204 }
205 
211 std::vector< std::string > readDirectory( std::string const & path );
212 
217 void makeDirectory( std::string const & path );
218 
228 void makeDirsForPath( std::string const & path );
229 
230 } /* end namespace geos */
231 
232 
233 #endif
Class describing a file Path.
Definition: Path.hpp:33
Path & operator=(Path &&rhs) noexcept
Move assignment.
Definition: Path.hpp:74
std::string filename() const
Definition: Path.hpp:109
Path(Path &&rhs) noexcept
Move constructor.
Definition: Path.hpp:55
Path()
Default constructor.
Definition: Path.hpp:41
Path & operator=(Path const &rhs)
Copy assignment.
Definition: Path.hpp:63
static void setPathPrefix(std::string_view p)
Set the path prefix of the file.
Definition: Path.hpp:91
std::string extension() const
Definition: Path.hpp:118
Path(Path const &rhs)
Copy constructor.
Definition: Path.hpp:48
~Path()
Destructor.
Definition: Path.hpp:83
static std::string_view getPathPrefix()
Get the path prefix of the file.
Definition: Path.hpp:101
std::istream & operator>>(std::istream &is, Path &p)
Operator use with the class Path while parsing the XML file.
void makeDirectory(std::string const &path)
Create a directory path, where parent directories must already exist.
std::string string
String type.
Definition: DataTypes.hpp:91
void makeDirsForPath(std::string const &path)
Make directories for path.
std::string getAbsolutePath(std::string const &path)
Gets the absolute path of a file.
std::string trimPath(std::string const &path)
Remove the trailing slash is if present.
Definition: Path.hpp:170
std::pair< std::string, std::string > splitPath(std::string const &path)
Split the path in two parts: directory name and file name.
std::vector< std::string > readDirectory(std::string const &path)
List all the files of one directory.
std::string joinPath(ARGS const &... args)
Join parts of a path.
Definition: Path.hpp:189
bool isAbsolutePath(std::string const &path)
Tells whether the path is absolute of not.
Definition: Path.hpp:152
std::string_view string_view
String type.
Definition: DataTypes.hpp:94