Skip to content
Simple file-path module for C++
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
include
.gitignore
CMakeLists.txt
LICENSE
README.md
cpppath.pc.in
cpppathConfig.cmake.in

README.md

cpppath

Simple, header only, file-path module for C++ similar to os in Python. This module is nothing fancy and currently only suitable for Linux and macOS. But is might be helpful to accomplish some simple tasks.

Contents

Usage

This library is header only, so one just has to

#include <cpppath.h>

Then compile using

c++ -I/path/to/cpppath

The -I/path/to/cpppath can be simplified or often even omitted by 'installing' cpppath

Overview

The following functions are available:

cpppath::dirname

std::string dirname(const std::string &path);

Return the directory from the path. Depending on the path, an empty string may be returned.

cpppath::filename

std::string filename(const std::string &path);

Return the filename from the path. Depending on the path, an empty string may be returned.

cpppath::filebase

std::string filebase(const std::string &path);

Return the filename without extension from the path. Depending on the path, an empty string may be returned.

cpppath::split

std::vector<std::string> split(const std::string &path);
std::vector<std::string> split(const std::string &path, int start, int end);

Split sub-paths using the separator. The output is a list of path components. Optionally a subset of the list can be selected using the start and end indices. Negative indices may be used to that count from the right (instead of from the left).

cpppath::join

std::string join(const std::vector<std::string> &paths);
std::string join(const std::string &a, const std::string &b);

Join path components using separator.

cpppath::select

std::string select(const std::string &path, int start, int end);

Selection of sub-paths (see split). Negative indices may be used to that count from the right (instead of from the left).

cpppath::normpath

std::string normpath(const std::string &path);

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::common_prefix

std::string common_prefix(const std::vector<std::string> &paths);

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::common_prefix(paths) << std::endl;

returns "/path/to/id=00".

cpppath::common_dirname

std::string common_dirname(const std::vector<std::string> &paths);

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::common_dirname(paths) << std::endl;

returns "/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::common_dirname(paths)+"/")[0] << std::endl;

returns "id=000/file.txt".

cpppath::exists

bool exists(const std::string &path);

Return true is the path exists.

cpppath::curdir

std::string curdir();

The current working directory.

Installation

To install:

cd /path/to/temp_dir
cmake /path/to/cpppath
make install

Thereafter one usually does not have to specify any include path. If needed, one can use

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

or use CMake, by adding the following to your CMakeLists.txt:

find_package(PkgConfig)

pkg_check_modules(CPPPATH REQUIRED cpppath)
include_directories(${CPPPATH_INCLUDE_DIRS})

To install in a custom location (e.g. ~/opt) use

cmake .. -DCMAKE_INSTALL_PREFIX:PATH=~/opt
You can’t perform that action at this time.