kplayer_rust_wrap/kplayer/util/
string.rs

1extern "C" {
2    fn NewString() -> i32;
3    fn DeleteString(i: i32) -> i32;
4    fn AppendChar(p: i32, i: i32) -> i32;
5    fn GetString(p: i32, i: i32) -> i32;
6}
7
8pub struct DynamicString {
9    index: i32,
10}
11
12impl DynamicString {
13    pub fn get_index(&self) -> i32 {
14        self.index
15    }
16
17    pub fn receive(index: i32) -> Result<String, &'static str> {
18        let mut collection: Vec<u8> = Vec::new();
19
20        unsafe {
21            let mut c: i32 = 0;
22            loop {
23                let char_u8 = GetString(index, c) as u8;
24                if char_u8 == 0 {
25                    break;
26                }
27                collection.push(char_u8);
28
29                c = c + 1;
30            }
31            DeleteString(index);
32        }
33
34        let str = String::from_utf8(collection).unwrap();
35        Ok(str)
36    }
37
38    fn append(&mut self, d: &[u8]) {
39        unsafe {
40            for i in d {
41                self.index = AppendChar(self.index, (*i) as i32)
42            }
43        }
44    }
45}
46
47impl Drop for DynamicString {
48    fn drop(&mut self) {
49        unsafe {
50            DeleteString(self.index);
51        }
52    }
53}
54
55impl From<&[u8]> for DynamicString {
56    fn from(d: &[u8]) -> DynamicString {
57        let mut str = DynamicString {
58            index: unsafe { NewString() },
59        };
60        str.append(d);
61
62        str
63    }
64}