GEOSX
Path.hpp
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
15 #ifndef GEOS_COMMON_PATH_HPP
16 #define GEOS_COMMON_PATH_HPP
17 
18 // System includes
19 #include <string>
20 #include <sstream>
21 #include <vector>
22 
23 namespace geos
24 {
31 class Path : public std::string
32 {
33 public:
34 
35  using std::string::string;
36 
40  Path()
41  {}
42 
47  Path( Path const & rhs ): std::string( rhs )
48  {}
49 
54  Path( Path && rhs ) noexcept: std::string( std::move( rhs ) )
55  {}
56 
62  Path & operator=( Path const & rhs )
63  {
64  std::string::operator=( rhs );
65  return *this;
66  }
67 
73  Path & operator=( Path && rhs ) noexcept
74  {
75  std::string::operator=( std::move( rhs ) );
76  return *this;
77  }
78 
83  {}
84 
91  {
92  pathPrefix() = p;
93  }
94 
101  {
102  return pathPrefix();
103  }
104 
109  {
110  size_type const pos = find_last_of( '/' );
111  return pos == npos ? static_cast< std::string >( *this ) : substr( pos + 1 );
112  }
113 
118  {
119  std::string const fname = filename();
120  size_type const pos = fname.find_last_of( '.' );
121  return pos == npos ? "" : fname.substr( pos + 1 );
122  }
123 
124 private:
125 
129  static std::string & pathPrefix()
130  {
131  static std::string s_pathPrefix = "";
132  return s_pathPrefix;
133  }
134 
135 };
136 
144 
151 inline bool isAbsolutePath( std::string const & path )
152 {
153  return !path.empty() && path[ 0 ] == '/';
154 }
155 
162 std::istream & operator>>( std::istream & is, Path & p );
163 
169 inline std::string trimPath( std::string const & path )
170 {
171  return ( !path.empty() && path.back() == '/' ) ? path.substr( 0, path.size() - 1 ) : path;
172 }
173 
179 std::pair< std::string, std::string > splitPath( std::string const & path );
180 
187 template< typename ... ARGS >
188 std::string joinPath( ARGS const & ... args )
189 {
190  size_t constexpr numParts = sizeof...(args);
191  static_assert( numParts > 0, "Must provide arguments" );
192  std::string parts[numParts] { trimPath( args ) ... };
193  std::ostringstream oss;
194  for( size_t i = 0; i < numParts - 1; ++i )
195  {
196  if( !parts[i].empty() )
197  {
198  oss << parts[i] << '/';
199  }
200  }
201  oss << parts[numParts - 1];
202  return oss.str();
203 }
204 
210 std::vector< std::string > readDirectory( std::string const & path );
211 
216 void makeDirectory( std::string const & path );
217 
227 void makeDirsForPath( std::string const & path );
228 
229 } /* end namespace geos */
230 
231 
232 #endif
Class describing a file Path.
Definition: Path.hpp:32
Path & operator=(Path &&rhs) noexcept
Move assignment.
Definition: Path.hpp:73
std::string filename() const
Definition: Path.hpp:108
Path(Path &&rhs) noexcept
Move constructor.
Definition: Path.hpp:54
Path()
Default constructor.
Definition: Path.hpp:40
Path & operator=(Path const &rhs)
Copy assignment.
Definition: Path.hpp:62
static void setPathPrefix(std::string_view p)
Set the path prefix of the file.
Definition: Path.hpp:90
std::string extension() const
Definition: Path.hpp:117
Path(Path const &rhs)
Copy constructor.
Definition: Path.hpp:47
~Path()
Destructor.
Definition: Path.hpp:82
static std::string_view getPathPrefix()
Get the path prefix of the file.
Definition: Path.hpp:100
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:131
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:169
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:188
bool isAbsolutePath(std::string const &path)
Tells whether the path is absolute of not.
Definition: Path.hpp:151
std::string_view string_view
String type.
Definition: DataTypes.hpp:134