Просмотр исходного кода

Support getting the filename from the cmdline

Lucas Stadler лет назад: 10
Родитель
Сommit
ddca15b9cd
1 измененных файлов с 8 добавлено и 6 удалено
  1. 8 6
      rust/coffi/src/main.rs

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

16
    flags: libc::uint32_t,
16
    flags: libc::uint32_t,
17
    colormap_entries: libc::uint32_t,
17
    colormap_entries: libc::uint32_t,
18
    warning_or_error:  libc::uint32_t,
18
    warning_or_error:  libc::uint32_t,
19
    message: [u8; 64],
19
    message: [libc::c_char; 64],
20
}
20
}
21
21
22
impl png_image {
22
impl png_image {
26
        return img
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
        unsafe { png_image_begin_read_from_file(self, file_name) as u32 }
30
        unsafe { png_image_begin_read_from_file(self, file_name) as u32 }
31
    }
31
    }
32
}
32
}
33
33
34
impl std::fmt::Display for png_image {
34
impl std::fmt::Display for png_image {
35
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
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
            let mut vec = Vec::new();
37
            let mut vec = Vec::new();
38
            for i in 0..64 {
38
            for i in 0..64 {
39
                vec.push(msg[i]);
39
                vec.push(msg[i] as u8);
40
            }
40
            }
41
            String::from_utf8(vec).unwrap()
41
            String::from_utf8(vec).unwrap()
42
        }
42
        }
49
49
50
#[link(name = "png")]
50
#[link(name = "png")]
51
extern {
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
fn main() {
55
fn main() {
58
58
59
    let mut img = png_image::new();
59
    let mut img = png_image::new();
60
    println!("{}", img);
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
    println!("read_from_file: {}", res);
64
    println!("read_from_file: {}", res);
63
    println!("{}", img);
65
    println!("{}", img);
64
}
66
}