Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
String Conversion Functions

Free functions for converting Magic types to human-readable strings. More...

Collaboration diagram for String Conversion Functions:

Functions

std::string Recognition::ToString (const Magic::FileTypeEntryT &file_type_entry, const std::string &type_separator=" -> ")
 Convert a file type entry to a string.
std::string Recognition::ToString (const Magic::FileTypeMapT &file_type_map, const std::string &type_separator=" -> ", const std::string &file_separator="\n")
 Convert a file type map to a string.
std::string Recognition::ToString (const Magic::ExpectedFileTypeT &expected_file_type)
 Convert an expected file type result to a string.
std::string Recognition::ToString (const Magic::ExpectedFileTypeEntryT &expected_file_type_entry, const std::string &type_separator=" -> ")
 Convert an expected file type entry to a string.
std::string Recognition::ToString (const Magic::ExpectedFileTypeMapT &expected_file_type_map, const std::string &type_separator=" -> ", const std::string &file_separator="\n")
 Convert an expected file type map to a string.
std::string Recognition::ToString (Magic::Flags flag)
 Convert a Magic flag to its string name.
std::string Recognition::ToString (const Magic::FlagsContainerT &flags, const std::string &separator=", ")
 Convert a container of flags to a string.
std::string Recognition::ToString (Magic::Parameters parameter)
 Convert a Magic parameter to its string name.
std::string Recognition::ToString (const Magic::ParameterValueT &parameter_value, const std::string &value_separator=": ")
 Convert a parameter-value pair to a string.
std::string Recognition::ToString (const Magic::ParameterValueMapT &parameters, const std::string &value_separator=": ", const std::string &parameter_separator=", ")
 Convert a parameter-value map to a string.

Detailed Description

Free functions for converting Magic types to human-readable strings.

These functions provide convenient string representations of Magic's result types for logging, debugging, and display purposes.

Function Documentation

◆ ToString() [1/10]

std::string Recognition::ToString ( const Magic::ExpectedFileTypeEntryT & expected_file_type_entry,
const std::string & type_separator = " -> " )
nodiscard

Convert an expected file type entry to a string.

Formats a file path and its expected result (type or error) as a string.

Parameters
[in]expected_file_type_entryThe file path and expected result pair.
[in]type_separatorSeparator between path and result (default: " -> ").
Returns
Formatted string: "path -> type" or "path -> [error message]".
See also
Magic::ExpectedFileTypeEntryT
Since
10.0.0

◆ ToString() [2/10]

std::string Recognition::ToString ( const Magic::ExpectedFileTypeMapT & expected_file_type_map,
const std::string & type_separator = " -> ",
const std::string & file_separator = "\n" )
nodiscard

Convert an expected file type map to a string.

Formats all file paths and their expected results as a multi-line string.

Parameters
[in]expected_file_type_mapMap of file paths to expected results.
[in]type_separatorSeparator between path and result (default: " -> ").
[in]file_separatorSeparator between entries (default: "\\n").
Returns
Formatted string with all entries.
See also
Magic::ExpectedFileTypeMapT
Since
10.0.0

◆ ToString() [3/10]

std::string Recognition::ToString ( const Magic::ExpectedFileTypeT & expected_file_type)
nodiscard

Convert an expected file type result to a string.

Returns the file type if successful, or the error message if failed.

Parameters
[in]expected_file_typeThe expected result from noexcept identification.
Returns
The file type string on success, or error message on failure.
auto result = magic.IdentifyFile("/path/to/file", std::nothrow);
std::println("{}", ToString(result));
std::string ToString(const Magic::FileTypeEntryT &file_type_entry, const std::string &type_separator=" -> ")
Convert a file type entry to a string.
Definition magic.cpp:2293
See also
Magic::ExpectedFileTypeT
Since
10.0.0

◆ ToString() [4/10]

std::string Recognition::ToString ( const Magic::FileTypeEntryT & file_type_entry,
const std::string & type_separator = " -> " )
nodiscard

Convert a file type entry to a string.

Formats a single file path and its identified type as a string.

Parameters
[in]file_type_entryThe file type entry (path -> type pair).
[in]type_separatorSeparator between path and type (default: " -> ").
Returns
Formatted string: "path -> type".
Magic::FileTypeEntryT entry = {"/path/to/file.txt", "text/plain"};
std::println("{}", ToString(entry));
// Output: /path/to/file.txt -> text/plain
FileTypeMapT::value_type FileTypeEntryT
Key-value pair representing a single file and its detected type.
Definition magic.hpp:258
See also
Magic::FileTypeEntryT
Since
10.0.0
Examples
magic_examples.cpp.

◆ ToString() [5/10]

std::string Recognition::ToString ( const Magic::FileTypeMapT & file_type_map,
const std::string & type_separator = " -> ",
const std::string & file_separator = "\n" )
nodiscard

Convert a file type map to a string.

Formats all file paths and their identified types as a multi-line string.

Parameters
[in]file_type_mapMap of file paths to their types.
[in]type_separatorSeparator between path and type (default: " -> ").
[in]file_separatorSeparator between entries (default: "\\n").
Returns
Formatted string with all entries.
auto results = magic.IdentifyFiles(files);
std::println("{}", ToString(results));
// Output:
// /path/to/file1.txt -> text/plain
// /path/to/image.png -> image/png
See also
Magic::FileTypeMapT
Since
10.0.0

◆ ToString() [6/10]

std::string Recognition::ToString ( const Magic::FlagsContainerT & flags,
const std::string & separator = ", " )
nodiscard

Convert a container of flags to a string.

Formats multiple flags as a comma-separated list.

Parameters
[in]flagsContainer of flags to convert.
[in]separatorSeparator between flags (default: ", ").
Returns
Formatted string of flag names.
std::println("{}", ToString(flags));
// Output: Mime, Compress
std::vector< Flags > FlagsContainerT
Container type holding a collection of Magic::Flags.
Definition magic.hpp:401
@ Mime
Definition magic.hpp:328
@ Compress
Definition magic.hpp:319
See also
Magic::FlagsContainerT
Since
10.0.0

◆ ToString() [7/10]

std::string Recognition::ToString ( const Magic::ParameterValueMapT & parameters,
const std::string & value_separator = ": ",
const std::string & parameter_separator = ", " )
nodiscard

Convert a parameter-value map to a string.

Formats all parameters and their values as a formatted list.

Parameters
[in]parametersMap of parameters to values.
[in]value_separatorSeparator between name and value (default: ": ").
[in]parameter_separatorSeparator between entries (default: ", ").
Returns
Formatted string of all parameter-value pairs.
auto params = magic.GetParameters();
std::println("{}", ToString(params));
// Output: BytesMax: 1048576, RegexMax: 8192, ...
See also
Magic::ParameterValueMapT
Magic::GetParameters()
Since
10.0.0

◆ ToString() [8/10]

std::string Recognition::ToString ( const Magic::ParameterValueT & parameter_value,
const std::string & value_separator = ": " )
nodiscard

Convert a parameter-value pair to a string.

Formats a parameter and its value as "name: value".

Parameters
[in]parameter_valueThe parameter and its value.
[in]value_separatorSeparator between name and value (default: ": ").
Returns
Formatted string: "ParameterName: value".
std::println("{}", ToString(pv));
// Output: BytesMax: 1048576
ParameterValueMapT::value_type ParameterValueT
Key-value pair representing a single parameter and its value.
Definition magic.hpp:415
@ BytesMax
Definition magic.hpp:389
See also
Magic::ParameterValueT
Since
10.0.0

◆ ToString() [9/10]

std::string Recognition::ToString ( Magic::Flags flag)
nodiscard

Convert a Magic flag to its string name.

Friend declaration for ToString(Flags) free function.

Returns the symbolic name of a single flag value.

Parameters
[in]flagThe flag to convert.
Returns
String name of the flag (e.g., "Mime", "MimeType").
std::println("{}", ToString(Magic::Flags::Mime));
// Output: Mime
See also
Magic::Flags
Since
10.0.0

◆ ToString() [10/10]

std::string Recognition::ToString ( Magic::Parameters parameter)
nodiscard

Convert a Magic parameter to its string name.

Friend declaration for ToString(Parameters) free function.

Returns the symbolic name of a single parameter value.

Parameters
[in]parameterThe parameter to convert.
Returns
String name of the parameter (e.g., "BytesMax", "RegexMax").
// Output: BytesMax
See also
Magic::Parameters
Since
10.0.0