From caff5ada741a937bbb1c95a5f2160fce338b0f44 Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Fri, 5 Oct 2018 20:18:45 +0200 Subject: [PATCH] window files are now relative to metadata.dat --- src/io.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/io.rs b/src/io.rs index 3eb9031..a819b9f 100644 --- a/src/io.rs +++ b/src/io.rs @@ -7,6 +7,16 @@ use std::io::BufReader; use k_B; use std::process; use std::option::Option; +use std::path::Path; + +// Returns the path to path2 relative to path1 +// path1: "path/to/file.dat" +// path2: "another_file.dat" +// => result = path/to/another_file.dat +fn get_relative_path(path1: &str, path2: &str) -> String { + let path1 = Path::new(path1); + path1.parent().unwrap().join(path2).to_str().unwrap().to_string() +} // Read input data into a histogram set by iterating over input files // given in the metadata file @@ -15,7 +25,7 @@ pub fn read_data(cfg: &Config) -> Option { let mut bias_fc: Vec = Vec::new(); let mut histograms: Vec = Vec::new(); let kT = cfg.temperature * k_B; - let f = File::open(&cfg.metadata_file).unwrap_or_else(|x| { + let f = File::open(&cfg.metadata_file).unwrap_or_else(|x| { eprintln!("Failed to read metadata from {}. {}", &cfg.metadata_file, x); process::exit(1) }); @@ -29,7 +39,7 @@ pub fn read_data(cfg: &Config) -> Option { } let (path, x0, k) = scan_fmt!(&line, "{} {} {}", String, f32, f32); - let path = path.unwrap(); + let path = get_relative_path(&cfg.metadata_file, &path.unwrap()); match read_window_file(&path, cfg) { Some(h) => { histograms.push(h); @@ -166,4 +176,15 @@ mod tests { assert_eq!(cfg.temperature * k_B, hs.kT); assert_eq!(2, hs.histograms.len()) } + + #[test] + fn get_relative_path() { + let path1 = "path/to/some_file.dat"; + let path2 = "another_file.dat"; + let path3 = "subfolder/another_file.dat"; + let relative2 = super::get_relative_path(&path1, &path2); + assert_eq!("path/to/another_file.dat" ,relative2); + let relative3 = super::get_relative_path(&path1, &path3); + assert_eq!("path/to/subfolder/another_file.dat" ,relative3); + } } \ No newline at end of file