Skip to main content

print/
print.rs

1#![feature(c_variadic)]
2#![allow(unsafe_op_in_unsafe_fn)]
3use lwprintf_rs::{
4    CustomOutPut, LwprintfObj, lwprintf_init, lwprintf_init_ex, lwprintf_printf,
5    lwprintf_printf_ex, lwprintf_sprintf, lwprintf_sprintf_ex, lwprintf_vprintf_ex,
6    lwprintf_vprintf_ex_rust, lwprintf_vsnprintf_ex, lwprintf_vsnprintf_ex_rust,
7};
8
9struct StdOut;
10
11impl CustomOutPut for StdOut {
12    fn putch(ch: i32) -> i32 {
13        print!("{}", ch as u8 as char);
14        ch
15    }
16}
17
18/// Demonstrate various lwprintf functions with a custom output function.
19unsafe fn lwprintf_ex() {
20    println!("--- lwprintf_ex demo ---");
21    let mut lwobj = LwprintfObj::<StdOut>::new();
22    // initialize lwprintf object with custom output function
23    lwprintf_init_ex(&mut lwobj);
24
25    lwprintf_printf_ex(
26        lwobj.as_mut_ptr(),
27        b"Hello, printf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
28        1 as i32,
29        b"printf_ex\0".as_ptr(),
30    );
31
32    let mut buf = [0u8; 100];
33    let l = lwprintf_rs::lwprintf_snprintf_ex(
34        lwobj.as_mut_ptr(),
35        buf.as_mut_ptr() as *mut i8,
36        buf.len(),
37        b"Hello, snprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
38        2 as i32,
39        b"snprintf_ex\0".as_ptr(),
40    );
41    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
42    print!("{}", s);
43
44    let l = lwprintf_sprintf_ex!(
45        lwobj.as_mut_ptr(),
46        buf.as_mut_ptr() as *mut i8,
47        b"Hello, sprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
48        3 as i32,
49        b"sprintf_ex macro\0".as_ptr()
50    );
51    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
52    print!("{}", s);
53
54    unsafe extern "C" fn call_lwprintf_vprintf_ex(
55        lwobj: &mut LwprintfObj<StdOut>,
56        format: *const i8,
57        args: ...
58    ) {
59        // let args = args.as_va_list();
60        unsafe {
61            lwprintf_vprintf_ex(lwobj.as_mut_ptr(), format, args);
62        }
63    }
64
65    call_lwprintf_vprintf_ex(
66        &mut lwobj,
67        b"Hello, vprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
68        4 as i32,
69        b"vprintf_ex\0".as_ptr(),
70    );
71
72    unsafe extern "C" fn call_lwprintf_vsnprintf_ex(
73        lwobj: &mut LwprintfObj<StdOut>,
74        buf: *mut u8,
75        n: usize,
76        format: *const i8,
77        args: ...
78    ) -> i32 {
79        // let args = args.as_va_list();
80        unsafe { lwprintf_vsnprintf_ex(lwobj.as_mut_ptr(), buf as *mut i8, n, format, args) }
81    }
82
83    let l = call_lwprintf_vsnprintf_ex(
84        &mut lwobj,
85        buf.as_mut_ptr(),
86        buf.len(),
87        b"Hello, vsnprintf_ex Number: %d, String: %s\n\0".as_ptr() as *const i8,
88        6 as i32,
89        b"vsnprintf_ex\0".as_ptr(),
90    );
91    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
92    print!("{}", s);
93
94    lwprintf_vprintf_ex_rust(
95        lwobj.as_mut_ptr(),
96        b"Hello, vprintf_ex_rust Number: %d, String: %s\n\0".as_ptr() as *const i8,
97        5 as i32,
98        b"vprintf_ex_rust\0".as_ptr(),
99    );
100
101    let l = lwprintf_vsnprintf_ex_rust(
102        lwobj.as_mut_ptr(),
103        buf.as_mut_ptr() as *mut i8,
104        buf.len(),
105        b"Hello, vsnprintf_ex_rust Number: %d, String: %s\n\0".as_ptr() as *const i8,
106        6 as i32,
107        b"vsnprintf_ex_rust\0".as_ptr(),
108    );
109    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
110    print!("{}", s);
111}
112
113/// Print formatted data to the output with default LwPRINTF instance.
114fn lwprintf() {
115    println!("--- lwprintf demo ---");
116    // initialize default lwprintf instance
117    lwprintf_init::<StdOut>();
118    lwprintf_printf!(
119        b"Hello, printf Number: %d, String: %s\n\0".as_ptr() as *const i8,
120        200 as i32,
121        b"printf\0".as_ptr()
122    );
123
124    let mut buf = [0u8; 100];
125    let l = lwprintf_sprintf!(
126        buf.as_mut_ptr() as *mut i8,
127        b"Hello, sprintf Number: %d, String: %s\n\0".as_ptr() as *const i8,
128        300 as i32,
129        b"sprintf macro\0".as_ptr()
130    );
131    let s = core::str::from_utf8(&buf[..l as usize]).unwrap();
132    print!("{}", s);
133    // other macros can be used similarly
134}
135
136fn other_test() {
137    println!("--- other tests ---");
138    // float has been disabled by default.
139    // lwprintf_printf!("float test: %.5f\n\0".as_ptr() as *const i8, 3.14159f64);
140    lwprintf_printf!("hex test: 0x%08X\n\0".as_ptr() as *const i8, 0xDEADBEEFu32);
141}
142
143fn main() {
144    unsafe {
145        lwprintf_ex();
146    }
147
148    lwprintf();
149
150    other_test();
151}