/cpppath

Simple file-path module for C++

Primary LanguageC++MIT LicenseMIT

cpppath

CI Conda Version

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.

Contents

Disclaimer

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

Getting cpppath

Using conda

conda install -c conda-forge cpppath

From source

# Download cpppath
git checkout https://github.com/tdegeus/cpppath.git
cd cpppath

# For CMake or pkg-config use
cmake .
make install

Usage

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.

Compiling

Using CMake

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
    

Using pkg-config

Presuming that the compiler is c++, compile using (Unix):

c++ `pkg-config --cflags cpppath` ...

By hand

Presuming that the compiler is c++, compile using (Unix):

c++ -I/path/to/cpppath/include ...

Overview

cpppath::sep

Get OS's separator.

  • Unix: "/"
  • Windows: "\\"

cpppath::dirname

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"

cpppath::filename

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"

cpppath::filebase

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"

cpppath::splitext

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", ""}.

cpppath::ext

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"

cpppath::split

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).

cpppath::join

Join path components using separator. Provides option to prepend the output string with the separator.

cpppath::select

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"

cpppath::normpath

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.

cpppath::commonprefix

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"

cpppath::commondirname

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"

cpppath::exists

Returns true is the path exists.

cpppath::curdir

The current working directory.