summaryrefslogtreecommitdiffstats
path: root/src/c/stdio.rs
blob: aa824ed2fd89b8bce53f8412a7ef96541d7692dd (plain)
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
use core::fmt;

use super::posix;
use super::string;

pub struct OStream {
	file: *mut libc::FILE
}

pub fn stdout() -> OStream {
	OStream { file: unsafe { posix::stdout } }
}

impl OStream {
	pub fn write(&mut self, b: &[u8]) {
		unsafe {
			libc::fwrite(
				b.as_ptr() as *const libc::c_void,
				1,
				b.len(),
				self.file,
			);
		}
	}

	pub fn puts(&mut self, s: &string::CStr) {
		unsafe {
			libc::fputs(s.as_ptr(), self.file);
		}
	}
}

impl fmt::Write for OStream {
	fn write_str(&mut self, s: &str) -> fmt::Result {
		self.write(s.as_bytes());
		Ok(())
	}
}