Skip to content
Snippets Groups Projects
file_system.h 3.36 KiB
#pragma once
/*
* Copyright Moravec Vojtech 2019.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
*/

#include "boost/filesystem.hpp"
#include "boost/algorithm/string.hpp"

namespace fs_wrapper
{

namespace fs = boost::filesystem;

// CZI file extension.
constexpr char CziExtUpperCase[] = ".CZI";

struct SmallFileInfo
{
    // Name of the file.
    std::string name;
    // Whole path to file.
    std::string path;
};

// Check if given path points to file.
bool is_file(const std::string &pathToCheck)
{
    auto file = fs::path(pathToCheck);
    return (fs::exists(file) && fs::is_regular_file(file));
}

// Check if given path points to directory.
bool is_directory(const std::string &pathToCheck)
{
    return fs::is_directory(fs::path(pathToCheck));
}

// Create all missing directories in path.
bool create_directory_path(const std::string &desiredPath)
{
    return fs::create_directories(fs::path(desiredPath));
}

// Get name of the file, specified by its path.
std::string get_filename(const std::string &selectedPath)
{
    return fs::path(selectedPath).filename().string();
}

// Get name, without extension, of the file, specified by its path.
std::string get_filename_without_extension(const std::string &selectedPath)
{
    fs::path file(selectedPath);
    auto name = file.filename().string();
    auto ext = file.extension().string();
    return name.substr(0, name.length() - ext.length());
}

// Get files, whose names begin with prefix.
std::vector<SmallFileInfo> get_files_with_same_prefix(const std::vector<SmallFileInfo> &files, const std::string &prefix)
{
    std::vector<SmallFileInfo> result;

    for (const SmallFileInfo &file : files)
    {
        if (boost::algorithm::starts_with(file.name, prefix))
        {
            result.push_back(file);