mastrust_ls_lib/cdef/mastrust/raw/
include.rs

1//! # This is the Raw Client to the Bindings generated from my C code
2//! This is the First Wrapper Class, all Functions define we with the 'RAW' suffix, and we use only C Types
3//! 
4//! First after this, in the 'optim' folder we found functions, to make the integration between Rust and C easier
5//! 
6//! For all Enduser, generelly you will never see this RawClasses in Your Master Project, but this is the First Step to integrate Mastrust in your Project
7include!(concat!(env!("OUT_DIR"), "/mastrust_bindings.rs"));
8use std::ffi::CString;
9use std::ptr;
10
11pub struct TokenRaw {
12    pub typ: *mut ::std::os::raw::c_char,
13    pub value: *mut u8,
14    pub string: *mut ::std::os::raw::c_char,
15    pub length: u8,
16    token: CToken,
17}
18
19impl TokenRaw {
20
21    pub fn new(_string: CString) -> Self {
22        let cstring = _string.to_str().unwrap();
23        // Erstelle eine CString aus dem gegebenen &str
24        let c_char_string = std::ffi::CString::new(cstring).expect("Failed to create CString.");
25        let c_char_ptr: *mut ::std::os::raw::c_char = c_char_string.into_raw();
26
27        let _token = CToken {
28            type_: c_char_ptr,
29            value: ptr::null_mut(),
30            string: c_char_ptr,
31            length: ptr::null_mut(), // Annahme: length ist ein u8, nicht ein Zeiger
32        };
33
34        let mut token_raw = TokenRaw {
35            typ: c_char_ptr,
36            value: ptr::null_mut(),
37            string: c_char_ptr,
38            length: 0,
39            token: _token,
40        };
41
42        unsafe {
43            ctoken_init(&mut token_raw.token, c_char_ptr);
44        }
45
46        token_raw
47    }
48
49    pub fn tokenize(&mut self) {
50        let _token = &mut self.token;
51        unsafe {
52            ctoken_tokenize(_token);
53            self.value = (*_token).value;
54            self.length = (*_token).length as u8; // Konvertierung zu u8
55        }
56    }
57
58    pub fn get_token(&self) -> Vec<u8> {
59        unsafe {
60            if self.value.is_null() {
61                return Vec::new();
62            }
63            Vec::from_raw_parts(self.value, self.length as usize, self.length as usize)
64        }
65    }
66}