Quellcode durchsuchen

Support getting the filename from the cmdline

Lucas Stadler vor 10 Jahren
Ursprung
Commit
ddca15b9cd
1 geänderte Dateien mit 8 neuen und 6 gelöschten Zeilen
  1. 8 6
      rust/coffi/src/main.rs

+ 8 - 6
rust/coffi/src/main.rs

@ -16,7 +16,7 @@ struct png_image {
16 16
    flags: libc::uint32_t,
17 17
    colormap_entries: libc::uint32_t,
18 18
    warning_or_error:  libc::uint32_t,
19
    message: [u8; 64],
19
    message: [libc::c_char; 64],
20 20
}
21 21
22 22
impl png_image {
@ -26,17 +26,17 @@ impl png_image {
26 26
        return img
27 27
    }
28 28
29
    fn begin_read_from_file(&mut self, file_name: *const u8) -> u32 {
29
    fn begin_read_from_file(&mut self, file_name: *const libc::c_char) -> u32 {
30 30
        unsafe { png_image_begin_read_from_file(self, file_name) as u32 }
31 31
    }
32 32
}
33 33
34 34
impl std::fmt::Display for png_image {
35 35
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
36
        fn get_message(msg: [u8; 64]) -> String {
36
        fn get_message(msg: [libc::c_char; 64]) -> String {
37 37
            let mut vec = Vec::new();
38 38
            for i in 0..64 {
39
                vec.push(msg[i]);
39
                vec.push(msg[i] as u8);
40 40
            }
41 41
            String::from_utf8(vec).unwrap()
42 42
        }
@ -49,7 +49,7 @@ impl std::fmt::Display for png_image {
49 49
50 50
#[link(name = "png")]
51 51
extern {
52
    fn png_image_begin_read_from_file(img: *mut png_image, file_name: *const u8) -> libc::c_int;
52
    fn png_image_begin_read_from_file(img: *mut png_image, file_name: *const libc::c_char) -> libc::c_int;
53 53
}
54 54
55 55
fn main() {
@ -58,7 +58,9 @@ fn main() {
58 58
59 59
    let mut img = png_image::new();
60 60
    println!("{}", img);
61
    let res = img.begin_read_from_file("mei.png\0".as_ptr());
61
    let file_name = std::env::args().nth(1).unwrap_or(String::from("mei.png"));
62
    let c_name = std::ffi::CString::new(file_name).unwrap();
63
    let res = img.begin_read_from_file(c_name.as_ptr());
62 64
    println!("read_from_file: {}", res);
63 65
    println!("{}", img);
64 66
}