use std::{ffi::c_char, ptr::null_mut};
#[repr(C)]
pub struct SPDict {
pub name: *const c_char,
pub value: *const c_char,
pub next: *mut SPDict,
pub prev: *mut SPDict,
}
#[no_mangle]
pub extern "C" fn spdict_add(
head: *mut *mut SPDict,
name: *const c_char,
value: *const c_char,
) {
unsafe {
if (*head).is_null() {
let new_node = Box::into_raw(Box::new(SPDict {
name,
value,
prev: null_mut(),
next: null_mut(),
}));
*head = new_node;
return;
}
let mut current = *(head);
while !(*current).next.is_null() {
current = (*current).next;
}
let new_node = Box::into_raw(Box::new(SPDict {
name,
value,
prev: current,
next: null_mut(),
}));
(*current).next = new_node;
}
}
#[no_mangle]
pub extern "C" fn spdict_del(head: *mut SPDict) {
unsafe {
if !(*head).prev.is_null() {
let mut tmp_prev = (*head).prev;
while !(*tmp_prev).prev.is_null() {
let tmp = tmp_prev;
tmp_prev = (*tmp).prev;
let _ = Box::from_raw(tmp);
}
let _ = Box::from_raw(tmp_prev);
}
let mut tmp_next = head;
while !(*tmp_next).next.is_null() {
let tmp = tmp_next;
tmp_next = (*tmp).next;
let _ = Box::from_raw(tmp);
}
let _ = Box::from_raw(tmp_next);
}
}