c2pa-cpp
C++ API for the C2PA SDK
Loading...
Searching...
No Matches
c2pa::Reader Class Reference

Reader class for reading a manifest. More...

#include <c2pa.hpp>

Public Member Functions

 Reader (IContextProvider &context, const std::string &format, std::istream &stream)
 Create a Reader from a context and stream.
 
 Reader (IContextProvider &context, const std::filesystem::path &source_path)
 Create a Reader from a context and file path.
 
 Reader (std::shared_ptr< IContextProvider > context, const std::string &format, std::istream &stream)
 Create a Reader from a shared context and stream.
 
 Reader (std::shared_ptr< IContextProvider > context, const std::filesystem::path &source_path)
 Create a Reader from a shared context and file path.
 
 Reader (const std::string &format, std::istream &stream)
 Create a Reader from a stream (will use global settings if any loaded).
 
 Reader (const std::filesystem::path &source_path)
 Create a Reader from a file path (will use global settings if any loaded).
 
 Reader (const Reader &)=delete
 
Readeroperator= (const Reader &)=delete
 
 Reader (Reader &&other) noexcept
 
Readeroperator= (Reader &&other) noexcept
 
 ~Reader ()
 
bool is_embedded () const
 Check if the reader was created from an embedded manifest.
 
std::optional< std::string > remote_url () const
 Returns the remote url of the manifest if this Reader obtained the manifest remotely.
 
std::string json () const
 Get the manifest as a JSON string.
 
int64_t get_resource (const std::string &uri, const std::filesystem::path &path)
 Get a resource from the reader and write it to a file.
 
int64_t get_resource (const std::string &uri, std::ostream &stream)
 Get a resource from the reader and write it to an output stream.
 
C2paReader * get_api_internal_raw_reader () const
 Get the raw C2paReader pointer.
 

Static Public Member Functions

static std::optional< Readerfrom_asset (IContextProvider &context, const std::filesystem::path &source_path)
 Try to open a Reader from a context and file path when the asset may lack C2PA data.
 
static std::optional< Readerfrom_asset (IContextProvider &context, const std::string &format, std::istream &stream)
 Try to create a Reader from a context and stream when the asset may lack C2PA data.
 
static std::optional< Readerfrom_asset (std::shared_ptr< IContextProvider > context, const std::filesystem::path &source_path)
 Try to open a Reader from a shared context and file path when the asset may lack C2PA data.
 
static std::optional< Readerfrom_asset (std::shared_ptr< IContextProvider > context, const std::string &format, std::istream &stream)
 Try to create a Reader from a shared context and stream when the asset may lack C2PA data.
 
static std::vector< std::string > supported_mime_types ()
 Get a list of mime types that the SDK can read manifests from.
 

Detailed Description

Reader class for reading a manifest.

This class is used to read and validate a manifest from a stream or file. Resources are managed using RAII; member order ensures cpp_stream (which holds a C stream pointing at the ifstream) is destroyed before owned_stream.

Constructor & Destructor Documentation

◆ Reader() [1/8]

c2pa::Reader::Reader ( IContextProvider context,
const std::string &  format,
std::istream &  stream 
)

Create a Reader from a context and stream.

Parameters
contextContext provider; used at construction to configure settings.
formatThe mime format of the stream.
streamThe input stream to read from.
Exceptions
C2paExceptionif context.is_valid() returns false, or for other errors encountered by the C2PA library.
Deprecated:
Use Reader(std::shared_ptr<IContextProvider>, format, stream) instead. The reference overload does not extend the lifetime of the context, which can be problematic when progress callbacks fire after the context is destroyed.

◆ Reader() [2/8]

c2pa::Reader::Reader ( IContextProvider context,
const std::filesystem::path &  source_path 
)

Create a Reader from a context and file path.

Parameters
contextContext provider; used at construction only to configure settings.
source_pathThe path to the file to read.
Exceptions
C2paExceptionif context.is_valid() returns false, or for other errors encountered by the C2PA library.
Note
Prefer using the streaming APIs if possible.
Deprecated:
Use Reader(std::shared_ptr<IContextProvider>, source_path) instead. The reference overload does not extend the lifetime of the context, which can be problematic when progress callbacks fire after the context is destroyed.

◆ Reader() [3/8]

c2pa::Reader::Reader ( std::shared_ptr< IContextProvider context,
const std::string &  format,
std::istream &  stream 
)

Create a Reader from a shared context and stream.

The Reader retains a shared reference to the context, keeping it alive for the lifetime of the Reader.

Parameters
contextShared context provider.
formatThe mime format of the stream.
streamThe input stream to read from.
Exceptions
C2paExceptionif context is null or context->is_valid() returns false.

◆ Reader() [4/8]

c2pa::Reader::Reader ( std::shared_ptr< IContextProvider context,
const std::filesystem::path &  source_path 
)

Create a Reader from a shared context and file path.

The Reader retains a shared reference to the context, keeping it alive for the lifetime of the Reader.

Parameters
contextShared context provider.
source_pathThe path to the file to read.
Exceptions
C2paExceptionif context is null or context->is_valid() returns false.

◆ Reader() [5/8]

c2pa::Reader::Reader ( const std::string &  format,
std::istream &  stream 
)

Create a Reader from a stream (will use global settings if any loaded).

The validation_status field in the JSON contains validation results.

Parameters
formatThe mime format of the stream.
streamThe input stream to read from.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.
Deprecated:
Use Reader(IContextProvider& context, format, stream) instead.

◆ Reader() [6/8]

c2pa::Reader::Reader ( const std::filesystem::path &  source_path)

Create a Reader from a file path (will use global settings if any loaded).

Parameters
source_pathThe path to the file to read.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.
Deprecated:
Use Reader(IContextProvider& context, source_path) instead.
Note
Prefer using the streaming APIs if possible.

◆ Reader() [7/8]

c2pa::Reader::Reader ( const Reader )
delete

◆ Reader() [8/8]

c2pa::Reader::Reader ( Reader &&  other)
inlinenoexcept
889 : c2pa_reader(std::exchange(other.c2pa_reader, nullptr)),
890 owned_stream(std::move(other.owned_stream)),
891 cpp_stream(std::move(other.cpp_stream)),
892 context_ref(std::move(other.context_ref)) {
893 }

◆ ~Reader()

c2pa::Reader::~Reader ( )

Member Function Documentation

◆ from_asset() [1/4]

static std::optional< Reader > c2pa::Reader::from_asset ( IContextProvider context,
const std::filesystem::path &  source_path 
)
static

Try to open a Reader from a context and file path when the asset may lack C2PA data.

Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest (e.g. invalid asset).
std::system_errorif the file cannot be opened.
Deprecated:
Use from_asset(std::shared_ptr<IContextProvider>, source_path) instead. The reference overload does not extend the lifetime of the context, which can cause a use-after-free crash when progress callbacks fire after the context is destroyed.

◆ from_asset() [2/4]

static std::optional< Reader > c2pa::Reader::from_asset ( IContextProvider context,
const std::string &  format,
std::istream &  stream 
)
static

Try to create a Reader from a context and stream when the asset may lack C2PA data.

Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest.
Deprecated:
Use from_asset(std::shared_ptr<IContextProvider>, format, stream) instead. The reference overload does not extend the lifetime of the context, which can cause a use-after-free crash when progress callbacks fire after the context is destroyed.

◆ from_asset() [3/4]

static std::optional< Reader > c2pa::Reader::from_asset ( std::shared_ptr< IContextProvider context,
const std::filesystem::path &  source_path 
)
static

Try to open a Reader from a shared context and file path when the asset may lack C2PA data.

The Reader retains a shared reference to the context if C2PA data is found.

Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest.

◆ from_asset() [4/4]

static std::optional< Reader > c2pa::Reader::from_asset ( std::shared_ptr< IContextProvider context,
const std::string &  format,
std::istream &  stream 
)
static

Try to create a Reader from a shared context and stream when the asset may lack C2PA data.

The Reader retains a shared reference to the context if C2PA data is found.

Returns
A Reader if JUMBF (c2pa/manifest) data is present; std::nullopt if none.
Exceptions
C2paExceptionfor errors other than a missing manifest.

◆ get_api_internal_raw_reader()

C2paReader * c2pa::Reader::get_api_internal_raw_reader ( ) const
inline

Get the raw C2paReader pointer.

Returns
The raw C2paReader pointer.
Note
This is intended for internal API use and compatibility with C APIs.
944{ return c2pa_reader; }

◆ get_resource() [1/2]

int64_t c2pa::Reader::get_resource ( const std::string &  uri,
const std::filesystem::path &  path 
)

Get a resource from the reader and write it to a file.

Parameters
uriThe URI of the resource.
pathThe file path to write the resource to.
Returns
The number of bytes written.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.
Note
Prefer using the streaming APIs if possible.

◆ get_resource() [2/2]

int64_t c2pa::Reader::get_resource ( const std::string &  uri,
std::ostream &  stream 
)

Get a resource from the reader and write it to an output stream.

Parameters
uriThe URI of the resource.
streamThe output stream to write the resource to.
Returns
The number of bytes written.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.

◆ is_embedded()

bool c2pa::Reader::is_embedded ( ) const
inline

Check if the reader was created from an embedded manifest.

Returns
true if the manifest was embedded in the asset, false if external.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.
911 {
912 return c2pa_reader_is_embedded(c2pa_reader);
913 }

◆ json()

std::string c2pa::Reader::json ( ) const

Get the manifest as a JSON string.

Returns
The manifest as a JSON string.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.

◆ operator=() [1/2]

Reader & c2pa::Reader::operator= ( const Reader )
delete

◆ operator=() [2/2]

Reader & c2pa::Reader::operator= ( Reader &&  other)
inlinenoexcept
895 {
896 if (this != &other) {
897 c2pa_free(c2pa_reader);
898 c2pa_reader = std::exchange(other.c2pa_reader, nullptr);
899 owned_stream = std::move(other.owned_stream);
900 cpp_stream = std::move(other.cpp_stream);
901 context_ref = std::move(other.context_ref);
902 }
903 return *this;
904 }

◆ remote_url()

std::optional< std::string > c2pa::Reader::remote_url ( ) const

Returns the remote url of the manifest if this Reader obtained the manifest remotely.

Returns
Optional string containing the remote URL, or std::nullopt if manifest was embedded.
Exceptions
C2paExceptionfor errors encountered by the C2PA library.

◆ supported_mime_types()

static std::vector< std::string > c2pa::Reader::supported_mime_types ( )
static

Get a list of mime types that the SDK can read manifests from.

Returns
Vector of supported MIME type strings.

The documentation for this class was generated from the following file: