Simple, header only, file-path module for C++ similar to os
in Python. This module is nothing fancy, but it might be helpful to accomplish some simple tasks.
This library is free to use under the MIT license. Any additions are very much appreciated, in terms of suggested functionality, code, documentation, testimonials, word-of-mouth advertisement, etc. Bug reports or feature requests can be filed on GitHub. As always, the code comes with no guarantee. None of the developers can be held responsible for possible mistakes.
Download: .zip file | .tar.gz file.
(c - MIT) T.W.J. de Geus (Tom) | tom@geus.me | www.geus.me | github.com/tdegeus/cpppath
conda install -c conda-forge cpppath
# Download cpppath
git checkout https://github.com/tdegeus/cpppath.git
cd cpppath
# For CMake or pkg-config use
cmake .
make install
This library is header only, so one just has to
#include <cpppath.h>
and make sure that the header is in the include path of the compiler. See below.
Consider this micro-example:
#include <cpppath.h>
int main()
{
std::cout << cpppath::join({"path", "to", "foo", "bar.txt"}) << std::endl;
return 0;
}
which will print
- Unix:
"path/to/foo/bar.txt"
- Windows:
"path\to\foo\bar.txt"
All functions take the file-path separator as argument to allow customisation. To overwrite the library-wide default
/
on Unix and\
use:#define CPPPATH_SEP "/"before including cpppath for the first time.
The CMakeLists.txt
can be as follows
cmake_minimum_required(VERSION 3.1)
project(example)
find_package(cpppath REQUIRED)
add_executable(example example.cpp)
target_link_libraries(example PRIVATE cpppath)
Compilation can then proceed using
-
Unix:
cmake . make
-
Windows:
cmake -G"NMake Makefiles" . nmake
Presuming that the compiler is c++
, compile using (Unix):
c++ `pkg-config --cflags cpppath` ...
Presuming that the compiler is c++
, compile using (Unix):
c++ -I/path/to/cpppath/include ...
Get OS's separator.
- Unix:
"/"
- Windows:
"\\"
Get dirname part of a path. Depending on the path, an empty string may be returned.
Example:
std::cout << cpppath::dirname("/path/to/foo/bar.txt") << std::endl;
outputs:
"/path/to/foo"
Get filename part of a path. Depending on the path, an empty string may be returned.
Example:
std::cout << cpppath::filename("/path/to/foo/bar.txt") << std::endl;
outputs:
"bar.txt"
Get filename part of a path, without extension. Depending on the path, an empty string may be returned.
Example:
std::cout << cpppath::filebase("/path/to/foo/bar.txt") << std::endl;
outputs:
"bar"
Split the pathname path into a pair (root, ext)
such that root + ext == path
,
and ext
is empty or begins with a period and contains at most one period.
Leading periods on the basename are ignored; cpppath::splitext(".cshrc")
returns {".cshrc", ""}
.
Get the extension of a path. Depending on the path, an empty string may be returned.
Example:
std::cout << cpppath::ext("/path/to/foo/bar.txt") << std::endl;
outputs:
"txt"
Split sub-paths using the separator. The output is a list of path components.
Optionally the list can be sliced as cpppath::split(path, begin, end)
(Python equivalent: os.split(path)[begin: end]
). Negative indices may be used to that count from the right (instead of from the left).
Join path components using separator. Provides option to prepend the output string with the separator.
Selection of sub-paths (see split
). Negative indices may be used to that count from the right (instead of from the left).
Example 1:
std::cout << cpppath::select("/path/to/foo/bar.txt", 2) << std::endl;
outputs
"foo/bar.txt"
Example 2:
std::cout << select("/path/to/foo/bar.txt", 2, 3) << std::endl;
outputs
"foo"
Normalize a path by collapsing redundant separators and up-level references so that A//B
, A/B/
, A/./B
and A/foo/../B
all become A/B
. This string manipulation may change the meaning of a path that contains symbolic links.
Select the common part of a list of strings. For example:
std::vector<std::string> paths = {"/path/to/id=000/file.txt", "/path/to/id=001/file.txt"};
std::cout << cpppath::commonprefix(paths) << std::endl;
outputs
"/path/to/id=00"
Select the common path of a list of paths. For example:
std::vector<std::string> paths = {"/path/to/id=000/file.txt", "/path/to/id=001/file.txt"};
std::cout << cpppath::commondirname(paths) << std::endl;
outputs
"/path/to"
This can also be used to select the part of the paths that in unique to each string. For example:
std::vector<std::string> paths = {"/path/to/id=000/file.txt", "/path/to/id=001/file.txt"};
std::cout << cpppath::split(paths[0], cpppath::commondirname(paths)+"/")[0] << std::endl;
outputs
"id=000/file.txt"
Returns true
is the path
exists.
The current working directory.