Skip to content

Commit

Permalink
Read params from a file on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
connorimes committed Oct 30, 2015
1 parent f85f22c commit 5a24597
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
38 changes: 34 additions & 4 deletions components/servo/main.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))]
Expand Down
9 changes: 9 additions & 0 deletions resources/android_params
@@ -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

0 comments on commit 5a24597

Please sign in to comment.