2024-10-13 18:56:29 +02:00
|
|
|
//! C functions for interacting with [SPCp437Grid]s
|
2024-05-28 22:23:21 +02:00
|
|
|
//!
|
2024-06-23 15:42:15 +02:00
|
|
|
//! prefix `sp_cp437_grid_`
|
2024-05-28 22:23:21 +02:00
|
|
|
|
2024-09-07 12:23:32 +02:00
|
|
|
use crate::SPByteSlice;
|
|
|
|
use servicepoint::{DataRef, Grid};
|
2024-05-26 13:15:11 +02:00
|
|
|
|
2024-06-23 15:42:15 +02:00
|
|
|
/// A C-wrapper for grid containing codepage 437 characters.
|
|
|
|
///
|
|
|
|
/// The encoding is currently not enforced.
|
2024-09-05 21:15:53 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```C
|
|
|
|
/// Cp437Grid grid = sp_cp437_grid_new(4, 3);
|
|
|
|
/// sp_cp437_grid_fill(grid, '?');
|
|
|
|
/// sp_cp437_grid_set(grid, 0, 0, '!');
|
2024-09-07 14:11:15 +02:00
|
|
|
/// sp_cp437_grid_free(grid);
|
2024-09-05 21:15:53 +02:00
|
|
|
/// ```
|
2024-09-07 15:18:51 +02:00
|
|
|
pub struct SPCp437Grid(pub(crate) servicepoint::Cp437Grid);
|
2024-08-29 22:21:21 +02:00
|
|
|
|
2024-09-05 21:15:53 +02:00
|
|
|
impl Clone for SPCp437Grid {
|
2024-08-29 22:21:21 +02:00
|
|
|
fn clone(&self) -> Self {
|
2024-09-07 15:18:51 +02:00
|
|
|
SPCp437Grid(self.0.clone())
|
2024-08-29 22:21:21 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-23 15:12:03 +02:00
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Creates a new [SPCp437Grid] with the specified dimensions.
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// returns: [SPCp437Grid] initialized to 0. Will never return NULL.
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
2024-09-07 14:11:15 +02:00
|
|
|
/// by explicitly calling `sp_cp437_grid_free`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-23 15:42:15 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_new(
|
2024-05-26 13:15:11 +02:00
|
|
|
width: usize,
|
|
|
|
height: usize,
|
2024-09-05 21:15:53 +02:00
|
|
|
) -> *mut SPCp437Grid {
|
2024-10-15 21:33:32 +02:00
|
|
|
let result = Box::into_raw(Box::new(SPCp437Grid(
|
|
|
|
servicepoint::Cp437Grid::new(width, height),
|
|
|
|
)));
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!result.is_null());
|
|
|
|
result
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Loads a [SPCp437Grid] with the specified dimensions from the provided data.
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
2024-09-07 14:35:16 +02:00
|
|
|
/// Will never return NULL.
|
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// - when `data` is NULL
|
|
|
|
/// - when the provided `data_length` does not match `height` and `width`
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `data` points to a valid memory location of at least `data_length`
|
|
|
|
/// bytes in size.
|
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
2024-09-07 14:11:15 +02:00
|
|
|
/// by explicitly calling `sp_cp437_grid_free`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-23 15:42:15 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_load(
|
2024-05-26 13:15:11 +02:00
|
|
|
width: usize,
|
|
|
|
height: usize,
|
|
|
|
data: *const u8,
|
|
|
|
data_length: usize,
|
2024-09-05 21:15:53 +02:00
|
|
|
) -> *mut SPCp437Grid {
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(data.is_null());
|
2024-05-26 13:15:11 +02:00
|
|
|
let data = std::slice::from_raw_parts(data, data_length);
|
2024-10-15 21:33:32 +02:00
|
|
|
let result = Box::into_raw(Box::new(SPCp437Grid(
|
|
|
|
servicepoint::Cp437Grid::load(width, height, data),
|
|
|
|
)));
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!result.is_null());
|
|
|
|
result
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Clones a [SPCp437Grid].
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
2024-09-07 14:35:16 +02:00
|
|
|
/// Will never return NULL.
|
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when `cp437_grid` is NULL
|
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-10-13 18:56:29 +02:00
|
|
|
/// - `cp437_grid` points to a valid [SPCp437Grid]
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid` is not written to concurrently
|
2024-05-28 20:37:55 +02:00
|
|
|
/// - the returned instance is freed in some way, either by using a consuming function or
|
2024-09-07 14:11:15 +02:00
|
|
|
/// by explicitly calling `sp_cp437_grid_free`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-23 15:42:15 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_clone(
|
2024-09-07 15:06:11 +02:00
|
|
|
cp437_grid: *const SPCp437Grid,
|
2024-09-05 21:15:53 +02:00
|
|
|
) -> *mut SPCp437Grid {
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!cp437_grid.is_null());
|
|
|
|
let result = Box::into_raw(Box::new((*cp437_grid).clone()));
|
|
|
|
assert!(!result.is_null());
|
|
|
|
result
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Deallocates a [SPCp437Grid].
|
2024-05-26 13:15:11 +02:00
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when `cp437_grid` is NULL
|
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-10-13 18:56:29 +02:00
|
|
|
/// - `cp437_grid` points to a valid [SPCp437Grid]
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid` is not used concurrently or after cp437_grid call
|
2024-10-13 18:12:55 +02:00
|
|
|
/// - `cp437_grid` was not passed to another consuming function, e.g. to create a [SPCommand]
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-07 15:06:11 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: *mut SPCp437Grid) {
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!cp437_grid.is_null());
|
2024-09-07 15:06:11 +02:00
|
|
|
_ = Box::from_raw(cp437_grid);
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 20:37:55 +02:00
|
|
|
/// Gets the current value at the specified position.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid`: instance to read from
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `x` and `y`: position of the cell to read
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// - when `cp437_grid` is NULL
|
|
|
|
/// - when accessing `x` or `y` out of bounds
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-10-13 18:56:29 +02:00
|
|
|
/// - `cp437_grid` points to a valid [SPCp437Grid]
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid` is not written to concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-23 15:42:15 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_get(
|
2024-09-07 15:06:11 +02:00
|
|
|
cp437_grid: *const SPCp437Grid,
|
2024-05-26 13:15:11 +02:00
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
) -> u8 {
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!cp437_grid.is_null());
|
2024-09-07 15:18:51 +02:00
|
|
|
(*cp437_grid).0.get(x, y)
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Sets the value of the specified position in the [SPCp437Grid].
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid`: instance to write to
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `x` and `y`: position of the cell
|
|
|
|
/// - `value`: the value to write to the cell
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// returns: old value of the cell
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// - when `cp437_grid` is NULL
|
|
|
|
/// - when accessing `x` or `y` out of bounds
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-10-13 18:12:55 +02:00
|
|
|
/// - `cp437_grid` points to a valid [SPBitVec]
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid` is not written to or read from concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-23 15:42:15 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_set(
|
2024-09-07 15:06:11 +02:00
|
|
|
cp437_grid: *mut SPCp437Grid,
|
2024-05-26 13:15:11 +02:00
|
|
|
x: usize,
|
|
|
|
y: usize,
|
|
|
|
value: u8,
|
|
|
|
) {
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!cp437_grid.is_null());
|
2024-09-07 15:18:51 +02:00
|
|
|
(*cp437_grid).0.set(x, y, value);
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Sets the value of all cells in the [SPCp437Grid].
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid`: instance to write to
|
2024-08-29 19:39:18 +02:00
|
|
|
/// - `value`: the value to set all cells to
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when `cp437_grid` is NULL
|
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-10-13 18:56:29 +02:00
|
|
|
/// - `cp437_grid` points to a valid [SPCp437Grid]
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid` is not written to or read from concurrently
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-07 15:06:11 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_fill(
|
|
|
|
cp437_grid: *mut SPCp437Grid,
|
|
|
|
value: u8,
|
|
|
|
) {
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!cp437_grid.is_null());
|
2024-09-07 15:18:51 +02:00
|
|
|
(*cp437_grid).0.fill(value);
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Gets the width of the [SPCp437Grid] instance.
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid`: instance to read from
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when `cp437_grid` is NULL
|
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-10-13 18:56:29 +02:00
|
|
|
/// - `cp437_grid` points to a valid [SPCp437Grid]
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-05 21:15:53 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_width(
|
2024-09-07 15:06:11 +02:00
|
|
|
cp437_grid: *const SPCp437Grid,
|
2024-09-05 21:15:53 +02:00
|
|
|
) -> usize {
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!cp437_grid.is_null());
|
2024-09-07 15:18:51 +02:00
|
|
|
(*cp437_grid).0.width()
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Gets the height of the [SPCp437Grid] instance.
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2024-09-07 15:06:11 +02:00
|
|
|
/// - `cp437_grid`: instance to read from
|
2024-05-28 20:37:55 +02:00
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when `cp437_grid` is NULL
|
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-10-13 18:56:29 +02:00
|
|
|
/// - `cp437_grid` points to a valid [SPCp437Grid]
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-23 16:01:11 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_height(
|
2024-09-07 15:06:11 +02:00
|
|
|
cp437_grid: *const SPCp437Grid,
|
2024-06-23 16:01:11 +02:00
|
|
|
) -> usize {
|
2024-10-14 22:01:45 +02:00
|
|
|
assert!(!cp437_grid.is_null());
|
2024-09-07 15:18:51 +02:00
|
|
|
(*cp437_grid).0.height()
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-13 18:56:29 +02:00
|
|
|
/// Gets an unsafe reference to the data of the [SPCp437Grid] instance.
|
2024-05-26 13:15:11 +02:00
|
|
|
///
|
2024-09-07 14:35:16 +02:00
|
|
|
/// Will never return NULL.
|
|
|
|
///
|
2024-10-14 22:01:45 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - when `cp437_grid` is NULL
|
|
|
|
///
|
2024-05-26 13:15:11 +02:00
|
|
|
/// ## Safety
|
|
|
|
///
|
2024-05-28 20:37:55 +02:00
|
|
|
/// The caller has to make sure that:
|
2024-05-26 13:15:11 +02:00
|
|
|
///
|
2024-10-13 18:56:29 +02:00
|
|
|
/// - `cp437_grid` points to a valid [SPCp437Grid]
|
|
|
|
/// - the returned memory range is never accessed after the passed [SPCp437Grid] has been freed
|
|
|
|
/// - the returned memory range is never accessed concurrently, either via the [SPCp437Grid] or directly
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-06-23 15:42:15 +02:00
|
|
|
pub unsafe extern "C" fn sp_cp437_grid_unsafe_data_ref(
|
2024-09-07 15:06:11 +02:00
|
|
|
cp437_grid: *mut SPCp437Grid,
|
2024-09-05 21:15:53 +02:00
|
|
|
) -> SPByteSlice {
|
2024-09-07 15:18:51 +02:00
|
|
|
let data = (*cp437_grid).0.data_ref_mut();
|
2024-09-05 21:15:53 +02:00
|
|
|
SPByteSlice {
|
2024-05-26 13:15:11 +02:00
|
|
|
start: data.as_mut_ptr_range().start,
|
|
|
|
length: data.len(),
|
|
|
|
}
|
|
|
|
}
|