dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
resources.hpp File Reference

Finding a library's data: compiled-in files, or a resources/ directory found on disk. More...

#include "dataset.hpp"
#include "tsv.hpp"
#include <cstddef>
#include <filesystem>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>
#include <vector>

Go to the source code of this file.

Classes

struct  dasmig::core::embedded_file
 A data file compiled into the program. More...
 

Functions

constexpr std::string_view dasmig::core::tier_name (dataset tier) noexcept
 The directory name of a data tier.
 
constexpr const embedded_file * dasmig::core::find_embedded (std::span< const embedded_file > files, std::string_view path) noexcept
 Find a compiled-in file by path.
 
std::vector< std::filesystem::path > dasmig::core::resource_candidates (std::string_view library, const std::filesystem::path &base=std::filesystem::current_path())
 The directories searched for a library's resources/, in order.
 
std::optional< std::filesystem::path > dasmig::core::find_resources (std::string_view library, std::string_view marker, const std::filesystem::path &base=std::filesystem::current_path())
 The first candidate directory that contains a marker file.
 
std::optional< tsv_document > dasmig::core::load_resource (std::string_view path, std::span< const embedded_file > embedded, const std::optional< std::filesystem::path > &root, std::vector< std::string > columns={})
 Load a data file, compiled-in if available, otherwise from disk.
 

Detailed Description

Finding a library's data: compiled-in files, or a resources/ directory found on disk.

Author
Diego Dasso Migotto (diegomigotto at hotmail dot com)

Definition in file resources.hpp.

Function Documentation

◆ find_embedded()

constexpr const embedded_file * dasmig::core::find_embedded ( std::span< const embedded_file >  files,
std::string_view  path 
)
constexprnoexcept

Find a compiled-in file by path.

Parameters
filesThe embedded files (empty when none are compiled in).
pathPath below resources/, with / separators.
Returns
The file, or nullptr.

Definition at line 64 of file resources.hpp.

◆ find_resources()

std::optional< std::filesystem::path > dasmig::core::find_resources ( std::string_view  library,
std::string_view  marker,
const std::filesystem::path &  base = std::filesystem::current_path() 
)
inline

The first candidate directory that contains a marker file.

The marker tells a library's resources/ apart from another library's (for example lite/biodata.tsv).

Parameters
libraryRepository name, e.g. biodata-generator.
markerA file path below resources/ that the library ships.
baseDirectory to search from (default: the working directory).
Returns
The resources/ directory, or empty when none is found.

Definition at line 103 of file resources.hpp.

◆ load_resource()

std::optional< tsv_document > dasmig::core::load_resource ( std::string_view  path,
std::span< const embedded_file >  embedded,
const std::optional< std::filesystem::path > &  root,
std::vector< std::string >  columns = {} 
)
inline

Load a data file, compiled-in if available, otherwise from disk.

Embedded files win, so a build with compiled-in data never depends on the working directory. The two give the same tsv_document::content_hash(), so seeds replay the same either way.

Parameters
pathPath below resources/, e.g. lite/biodata.tsv.
embeddedCompiled-in files (empty when none).
rootA resources/ directory (from find_resources()), or empty.
columnsColumn names for a headerless file; empty when the first line names them.
Returns
The document, or empty when the file is neither embedded nor present under root.
Exceptions
tsv_errorIf the file exists but cannot be parsed.

Definition at line 129 of file resources.hpp.

◆ resource_candidates()

std::vector< std::filesystem::path > dasmig::core::resource_candidates ( std::string_view  library,
const std::filesystem::path &  base = std::filesystem::current_path() 
)
inline

The directories searched for a library's resources/, in order.

Relative to base: resources, ../resources, <library>/resources and ../<library>/resources. These cover running from the repository, from its build directory, and from a parent directory holding several libraries side by side.

Parameters
libraryRepository name, e.g. biodata-generator.
baseDirectory to search from (default: the working directory).
Returns
The candidate directories.

Definition at line 84 of file resources.hpp.

◆ tier_name()

constexpr std::string_view dasmig::core::tier_name ( dataset  tier)
constexprnoexcept

The directory name of a data tier.

Parameters
tierThe tier.
Returns
"lite" or "full".

Definition at line 31 of file resources.hpp.