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 #include "common/StdContainerWrappers.hpp"
20 
21 // System includes
22 #include <string>
23 #include <sstream>
24 #include <vector>
25 
26 namespace geos
27 {
34 class Path : public std::string
35 {
36 public:
37 
38  using std::string::string;
39 
43  Path()
44  {}
45 
50  Path( Path const & rhs ): std::string( rhs )
51  {}
52 
57  Path( Path && rhs ) noexcept: std::string( std::move( rhs ) )
58  {}
59 
65  Path & operator=( Path const & rhs )
66  {
67  std::string::operator=( rhs );
68  return *this;
69  }
70 
76  Path & operator=( Path && rhs ) noexcept
77  {
78  std::string::operator=( std::move( rhs ) );
79  return *this;
80  }
81 
86  {}
87 
94  { pathPrefix() = p; }
95 
102  { return pathPrefix(); }
103 
108 
113 
118 
119 private:
120 
124  static std::string & pathPrefix();
125 
126 };
127 
135 
142 inline bool isAbsolutePath( std::string const & path )
143 {
144  return !path.empty() && path[ 0 ] == '/';
145 }
146 
153 std::istream & operator>>( std::istream & is, Path & p );
154 
160 inline std::string trimPath( std::string const & path )
161 {
162  return ( !path.empty() && path.back() == '/' ) ? path.substr( 0, path.size() - 1 ) : path;
163 }
164 
170 std::pair< std::string, std::string > splitPath( std::string const & path );
171 
178 template< typename ... ARGS >
179 std::string joinPath( ARGS const & ... args )
180 {
181  size_t constexpr numParts = sizeof...(args);
182  static_assert( numParts > 0, "Must provide arguments" );
183  std::string parts[numParts] { trimPath( args ) ... };
184  std::ostringstream oss;
185  for( size_t i = 0; i < numParts - 1; ++i )
186  {
187  if( !parts[i].empty() )
188  {
189  oss << parts[i] << '/';
190  }
191  }
192  oss << parts[numParts - 1];
193  return oss.str();
194 }
195 
202 
207 void makeDirectory( std::string const & path );
208 
218 void makeDirsForPath( std::string const & path );
219 
220 } /* end namespace geos */
221 
222 
223 #endif
Class describing a file Path.
Definition: Path.hpp:35
Path & operator=(Path &&rhs) noexcept
Move assignment.
Definition: Path.hpp:76
std::string filename() const
Path(Path &&rhs) noexcept
Move constructor.
Definition: Path.hpp:57
Path()
Default constructor.
Definition: Path.hpp:43
std::string relativeFilePath() const
Path & operator=(Path const &rhs)
Copy assignment.
Definition: Path.hpp:65
static void setPathPrefix(std::string_view p)
Set the path prefix of the file.
Definition: Path.hpp:93
std::string extension() const
Path(Path const &rhs)
Copy constructor.
Definition: Path.hpp:50
~Path()
Destructor.
Definition: Path.hpp:85
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.
stdVector< std::string > readDirectory(std::string const &path)
List all the files of one directory.
void makeDirectory(std::string const &path)
Create a directory path, where parent directories must already exist.
std::string string
String type.
Definition: DataTypes.hpp:90
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:160
std::pair< std::string, std::string > splitPath(std::string const &path)
Split the path in two parts: directory name and file name.
std::string joinPath(ARGS const &... args)
Join parts of a path.
Definition: Path.hpp:179
std::vector< T, Allocator > stdVector
bool isAbsolutePath(std::string const &path)
Tells whether the path is absolute of not.
Definition: Path.hpp:142
std::string_view string_view
String type.
Definition: DataTypes.hpp:93