Read params from a file on Android

This commit is contained in:
Connor Imes 2015-10-29 15:16:21 -05:00
parent f85f22c04f
commit 5a2459748b
2 changed files with 43 additions and 4 deletions

View file

@ -23,6 +23,8 @@ extern crate android_glue;
// The window backed by glutin
extern crate glutin_app as app;
extern crate env_logger;
#[macro_use]
extern crate log;
// The Servo engine
extern crate servo;
extern crate time;
@ -128,11 +130,39 @@ fn setup_logging() {
}
#[cfg(target_os = "android")]
/// Attempt to read parameters from a file since they are not passed to us in Android environments.
/// The first line should be the "servo" argument and the last should be the URL to load.
/// Blank lines and those beginning with a '#' are ignored.
/// Each line should be a separate parameter as would be parsed by the shell.
/// For example, "servo -p 10 http://en.wikipedia.org/wiki/Rust" would take 4 lines.
fn args() -> Vec<String> {
vec![
"servo".to_owned(),
"http://en.wikipedia.org/wiki/Rust".to_owned()
]
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};
const PARAMS_FILE: &'static str = "/sdcard/servo/android_params";
match File::open(PARAMS_FILE) {
Ok(f) => {
let mut vec = Vec::new();
let file = BufReader::new(&f);
for line in file.lines() {
let l = line.unwrap().trim().to_owned();
// ignore blank lines and those that start with a '#'
match l.is_empty() || l.as_bytes()[0] == b'#' {
true => (),
false => vec.push(l),
}
}
vec
},
Err(e) => {
debug!("Failed to open params file '{}': {}", PARAMS_FILE, Error::description(&e));
vec![
"servo".to_owned(),
"http://en.wikipedia.org/wiki/Rust".to_owned()
]
},
}
}
#[cfg(not(target_os = "android"))]

9
resources/android_params Normal file
View file

@ -0,0 +1,9 @@
# The first line here should be the "servo" argument (without quotes) and the
# last should be the URL to load.
# Blank lines and those beginning with a '#' are ignored.
# Each line should be a separate parameter as would be parsed by the shell.
# For example, "servo -p 10 http://en.wikipedia.org/wiki/Rust" would take 4
# lines (the "-p" and "10" are separate even though they are related).
servo
http://en.wikipedia.org/wiki/Rust