use std::ffi::c_int;
use crate::bindings::{
unformat_init_string, unformat_input_t, uword, vlib_helper_unformat_free,
vlib_helper_unformat_get_input,
};
const UNFORMAT_END_OF_INPUT: uword = !0;
pub unsafe fn raw_unformat_line_input_to_string(i: *mut unformat_input_t) -> String {
unsafe {
let mut line = vec![];
loop {
let b = vlib_helper_unformat_get_input(i);
if b == b'\n'.into() || b == UNFORMAT_END_OF_INPUT {
break;
}
line.push(b as u8);
}
String::from_utf8_lossy(&line).to_string()
}
}
pub struct UnformatInput(unformat_input_t);
impl UnformatInput {
pub fn as_ptr(&mut self) -> *mut unformat_input_t {
&mut self.0
}
}
impl From<&str> for UnformatInput {
fn from(value: &str) -> Self {
let mut me = Self(Default::default());
unsafe {
unformat_init_string(&mut me.0, value.as_ptr().cast(), value.len() as c_int);
}
me
}
}
impl From<String> for UnformatInput {
fn from(value: String) -> Self {
value.as_str().into()
}
}
impl Drop for UnformatInput {
fn drop(&mut self) {
unsafe {
vlib_helper_unformat_free(self.as_ptr());
}
}
}