1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
pub mod depends {
    pub use dexalt_lib_sys::file::*;
}

use std::ffi::{CStr, CString};
use std::ptr;

pub struct DexaltFile {
    pub filepath: CString,
    pub filemode: CString,
    pub file: *mut depends::FILE,
    pub instance: *mut depends::DexaltFile,
}

impl DexaltFile {

    pub fn new(filepath: CString, filemode: CString) -> Self {
        let mut instance = DexaltFile {
            filepath: filepath.clone(),
            filemode: filemode.clone(),
            file: ptr::null_mut(),
            instance: ptr::null_mut(),
        };

        unsafe {
            depends::DexaltFile_init(
                &mut instance as *mut _ as *mut depends::DexaltFile,
                filepath.as_ptr(),
                filemode.as_ptr(),
            );
        }

        instance
    }

    pub fn open(&mut self) {
        unsafe {
            depends::DexaltFile_open(self.instance);
        }
    }

    pub fn close(&mut self) {
        unsafe {
            depends::DexaltFile_close(self.instance);
        }
    }

    pub fn write(&mut self, data: CString) {
        let c_data = data.as_ptr();
        unsafe {
            depends::DexaltFile_write(self.instance, c_data as *const _);
        }
    }

    pub fn read(&mut self) -> CString {
        unsafe {
            let data_ptr = depends::DexaltFile_read(self.instance);
            let c_str = CStr::from_ptr(data_ptr as *const _).to_string_lossy().into_owned();
            let c_string = CString::new(c_str).unwrap();
            c_string
        }
    }

    pub fn get_filepath(&self) -> CString {
        unsafe {
            let c_str = CStr::from_ptr(depends::DexaltFile_get_filepath(self.instance))
                .to_string_lossy()
                .into_owned();
            let c_string = CString::new(c_str).unwrap();
            c_string
        }
    }

    pub fn get_filemode(&self) -> CString {
        unsafe {
            let c_str = CStr::from_ptr(depends::DexaltFile_get_filemode(self.instance))
                .to_string_lossy()
                .into_owned();
            let c_string = CString::new(c_str).unwrap();
            c_string
        }
    }

    pub fn get_filesize(&self) -> f32 {
        unsafe { depends::DexaltFile_get_filesize(self.instance) }
    }
}

impl Drop for DexaltFile {
    fn drop(&mut self) {
        unsafe {
            depends::DexaltFile_destroy(self.instance)
        }
    }
}