Mercurial > lbo > hg > myi3stat
changeset 10:ab47007c73a9
Add helper module
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 14 Feb 2016 12:54:20 +0100 |
parents | dc4eb2d24d06 |
children | c2dc7a85a0e3 |
files | src/helper.rs src/main.rs |
diffstat | 2 files changed, 37 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/helper.rs Sun Feb 14 12:54:20 2016 +0100 @@ -0,0 +1,36 @@ +use std::fs; +use std::io::Read; + +/// Reads and returns a full procfs file. +/// Example: +/// +/// read_procfs_file("/net/dev") +pub fn read_procfs_file(path: String) -> Option<String> { + let mut file; + let size; + + let mut fullpath = String::from("/proc/"); + fullpath.push_str(&path); + + match fs::OpenOptions::new().read(true).open(path) { + Err(_) => return None, + Ok(f) => file = f, + } + + match file.metadata() { + Err(_) => return None, + Ok(md) => size = md.len() as usize, + } + + let mut buf = String::with_capacity(size); + file.read_to_string(&mut buf); + + Some(buf) +} + +pub fn get_procfs_file_lines(path: String) -> Option<Vec<String>> { + match read_procfs_file(path) { + None => None, + Some(s) => Some(s.lines().map(|s| String::from(s)).collect()), + } +}