hex-editor/src/source.h

65 lines
2.1 KiB
C

// source: A data source to read and edit
//
// This header provides the main definitions for data sources, which back the
// data that is shown and edited in the application. Data sources implement a
// base interface for reading and writing chunks, with some optional features
// being defined by capabilities.
#pragma once
#include <stdbool.h>
#include <sys/types.h>
#include "buffer.h"
// Data source capabilities //
/* The data source is writable and can be modified by API. */
#define SOURCE_WRITE 0x01
/* The data source can expand, adding new bytes at the end. */
#define SOURCE_EXPAND 0x02
/* The data source can shrink, removing bytes at the end. */
#define SOURCE_SHRINK 0x03
// Interface to data sources //
typedef struct {
/* Source capabilities */
int cap;
/* Load a chunk from the data source into a buffer. */
bool (*read)(void *_cookie, void *buf, off_t offset, size_t size);
/* Write from a buffer into a segment of the data source. If the interface
has the SOURCE_EXPAND capabilitiy, the buffer might be larger than the
segment. Similarly, if the interfadce has the SOURCE_SHRINK capability,
the buffer might be smaller than the segment. */
bool (*write)(void *_cookie, void *buf, size_t buf_size, off_t offset,
size_t segment_size);
/* Save the data to its original medium. */
bool (*save)(void *_cookie);
/* Close the data source. */
void (*close)(void *_cookie);
} source_intf_t;
// An open data source //
typedef struct {
/* Interface identifier, and opaque interface data */
source_intf_t *intf;
void *cookie;
/* Front buffer for edition */
buffer_t *buf;
/* Address of the front buffer in the source */
off_t buf_offset;
} source_t;
/* Open a data source; this is a low-level function used by source-specific
opening functions. */
source_t *source_open(source_intf_t *intf, void *cookie);
/* Replace the source's buffer's contents with data read from the source. */
bool source_load(source_t *s, off_t offset, size_t segment_size);
// Supported data sources //
#include "source-loaded-file.h"
#include "source-lazy-file.h"
#include "source-memory.h"