Vanilla.PDF  1.6.0
Cross-platform toolkit for creating and modifying PDF documents
write_custom.c

Example use of custom file writer callbacks

#include "tools.h"
void print_write_custom_help() {
printf("Usage: write_custom -s [source file] -d [destination file]");
}
// Custom callbacks
typedef struct {
int data;
} ObserverData;
error_type on_initializing(void* user_data, InputOutputStreamHandle* input_output_stream) {
ObserverData* observer_data = (ObserverData*) user_data;
offset_type current_offset = 0;
long long current_offset_converted = 0;
OutputStreamHandle* output_stream = NULL;
UNUSED(observer_data);
// Do something with the output stream
RETURN_ERROR_IF_NOT_SUCCESS(InputOutputStream_ToOutputStream(input_output_stream, &output_stream));
RETURN_ERROR_IF_NOT_SUCCESS(OutputStream_GetOutputPosition(output_stream, &current_offset));
RETURN_ERROR_IF_NOT_SUCCESS(OutputStream_Release(output_stream));
// Convert to long long for printf
current_offset_converted = current_offset;
printf("File writer is initializing\n");
printf("Current output position: %lld\n", current_offset_converted);
}
error_type on_finalizing(void* user_data, InputOutputStreamHandle* input_output_stream) {
ObserverData* observer_data = (ObserverData*) user_data;
offset_type current_offset = 0;
long long current_offset_converted = 0;
OutputStreamHandle* output_stream = NULL;
UNUSED(observer_data);
// Do something with the output stream
RETURN_ERROR_IF_NOT_SUCCESS(InputOutputStream_ToOutputStream(input_output_stream, &output_stream));
RETURN_ERROR_IF_NOT_SUCCESS(OutputStream_GetOutputPosition(output_stream, &current_offset));
RETURN_ERROR_IF_NOT_SUCCESS(OutputStream_Release(output_stream));
// Convert to long long for printf
current_offset_converted = current_offset;
printf("File writer is finalizing\n");
printf("Current output position: %lld\n", current_offset_converted);
}
int process_write_custom(int argc, char *argv[]) {
string_type source_file_path = NULL;
string_type destination_file_path = NULL;
FileWriterHandle* file_writer = NULL;
FileHandle* source_file = NULL;
FileHandle* destination_file = NULL;
ObserverData observer_data = { 0 };
FileWriterObserverHandle* file_writer_observer = NULL;
integer_type i = 0;
for (i = 0; i < argc; ++i) {
// source document path
if (strcmp(argv[i], "-s") == 0 && (i + 1 < argc)) {
source_file_path = argv[i + 1];
i++;
// destination file path
} else if (strcmp(argv[i], "-d") == 0 && (i + 1 < argc)) {
destination_file_path = argv[i + 1];
i++;
} else {
print_write_custom_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
}
if (source_file_path == NULL || destination_file_path == NULL) {
print_write_custom_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
RETURN_ERROR_IF_NOT_SUCCESS(File_Open(source_file_path, &source_file));
RETURN_ERROR_IF_NOT_SUCCESS(File_Create(destination_file_path, &destination_file));
RETURN_ERROR_IF_NOT_SUCCESS(File_Initialize(source_file));
// Unused callbacks can be set to NULL
RETURN_ERROR_IF_NOT_SUCCESS(FileWriterObserver_CreateCustom(on_initializing, on_finalizing, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &observer_data, &file_writer_observer));
RETURN_ERROR_IF_NOT_SUCCESS(FileWriter_Create(&file_writer));
RETURN_ERROR_IF_NOT_SUCCESS(FileWriter_Subscribe(file_writer, file_writer_observer));
RETURN_ERROR_IF_NOT_SUCCESS(FileWriter_Write(file_writer, source_file, destination_file));
RETURN_ERROR_IF_NOT_SUCCESS(FileWriter_Release(file_writer));
RETURN_ERROR_IF_NOT_SUCCESS(FileWriterObserver_Release(file_writer_observer));
RETURN_ERROR_IF_NOT_SUCCESS(File_Release(destination_file));
RETURN_ERROR_IF_NOT_SUCCESS(File_Release(source_file));
return VANILLAPDF_TOOLS_ERROR_SUCCESS;
}
Represents low-level file access handle.
error_type CALLING_CONVENTION File_Create(string_type filename, FileHandle **result)
Creates a file for writing.
error_type CALLING_CONVENTION File_Release(FileHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION File_Open(string_type filename, FileHandle **result)
Opens a file for reading.
error_type CALLING_CONVENTION File_Initialize(FileHandle *handle)
Perform basic intialization.
Implements serialization of Files to destination stream.
error_type CALLING_CONVENTION FileWriter_Write(FileWriterHandle *handle, FileHandle *source, FileHandle *destination)
Save file state into new destination.
error_type CALLING_CONVENTION FileWriter_Create(FileWriterHandle **result)
Create new FileWriter instance.
error_type CALLING_CONVENTION FileWriter_Release(FileWriterHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION FileWriter_Subscribe(FileWriterHandle *handle, FileWriterObserverHandle *observer)
Subscribe a new observer to the file writer events.
Allows hooking on FileWriterHandle events.
error_type CALLING_CONVENTION FileWriterObserver_Release(FileWriterObserverHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION FileWriterObserver_CreateCustom(FileWriterObserver_OnInitializing_Function on_initializing, FileWriterObserver_OnFinalizing_Function on_finalizing, FileWriterObserver_OnBeforeObjectWrite_Function on_before_object_write, FileWriterObserver_OnAfterObjectWrite_Function on_after_object_write, FileWriterObserver_OnBeforeObjectOffsetRecalculation_Function on_before_object_offset_recalculation, FileWriterObserver_OnAfterObjectOffsetRecalculation_Function on_after_object_offset_recalculation, FileWriterObserver_OnBeforeEntryOffsetRecalculation_Function on_before_entry_offset_recalculation, FileWriterObserver_OnAfterEntryOffsetRecalculation_Function on_after_entry_offset_recalculation, FileWriterObserver_OnBeforeOutputFlush_Function on_before_output_flush, FileWriterObserver_OnAfterOutputFlush_Function on_after_output_flush, void *user_data, FileWriterObserverHandle **result)
Create a new FileWriterObserverHandle instance with custom callbacks.
Input stream can read and interpret input from sequences of characters.
error_type CALLING_CONVENTION InputOutputStream_ToOutputStream(InputOutputStreamHandle *handle, OutputStreamHandle **result)
Reinterpret current object as OutputStreamHandle.
Output stream can write sequences of characters and represent other kinds of data.
error_type CALLING_CONVENTION OutputStream_Release(OutputStreamHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION OutputStream_GetOutputPosition(OutputStreamHandle *handle, offset_type *result)
Gets current offset in the output stream.
const error_type VANILLAPDF_ERROR_SUCCESS
Indicates that the operation completed successfully.
uint32_t error_type
This is return value type of all API functions.
Definition: c_types.h:25
int64_t offset_type
Offset type compatible with standard IO.
Definition: c_types.h:56
int32_t integer_type
32-bit signed integer
Definition: c_types.h:51
const char * string_type
C-Style string.
Definition: c_types.h:82