diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-09-21 21:29:12 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-09-21 21:29:12 +0200 |
| commit | 8ef674a027509b5f7d4c331ade7c0677e9b2eaca (patch) | |
| tree | 60b6464c8c45f3c4f135aafa9229fe8d70313890 | |
| parent | bc1138364ac3bd812ae16207bd68c669003e4671 (diff) | |
| download | tools.nes-8ef674a027509b5f7d4c331ade7c0677e9b2eaca.tar.gz tools.nes-8ef674a027509b5f7d4c331ade7c0677e9b2eaca.zip | |
nasm: combine the -o option with --split-segments
Allow these to options to be combined, so each segment file is stored
inside the directory specified in the -o option. Note that this can only
be done if the -o option does not refer to an existing file.
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
| -rw-r--r-- | crates/nasm/src/main.rs | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/crates/nasm/src/main.rs b/crates/nasm/src/main.rs index a5c446f..eea135e 100644 --- a/crates/nasm/src/main.rs +++ b/crates/nasm/src/main.rs @@ -328,7 +328,7 @@ fn main() { } else if args.split { (BufWriter::new(Box::new(io::stdout())), "<segments>") } else { - let name = args.out.unwrap_or(String::from("out.nes")); + let name = args.out.clone().unwrap_or(String::from("out.nes")); match File::create(&name) { Ok(f) => (BufWriter::new(Box::new(f)), args.file.as_str()), Err(_) => die(format!("could not create file '{name}'")), @@ -371,16 +371,32 @@ fn main() { // case we have to deliver the segments into different files, or with // the regular output in which everything will be dumped into 'output'. if args.split { + // Check if the files are to be saved into a particular directory. + let dir_name = args.out.unwrap_or(String::from(".")); + let dir = Path::new(&dir_name); + if dir.exists() { + if !dir.is_dir() { + die(format!("given path '{}' is not a directory", dir_name)); + } + } else { + if let Err(e) = std::fs::create_dir_all(dir) { + die(format!( + "could not create output directory '{}': {}", + dir_name, e + )); + } + } + for mapping in &res.mappings { for segment in &mapping.segments { let name = &segment.name; - let path = format!("{name}.out"); + let path = dir.join(format!("{name}.out")); // Open the file and truncate it if it already exists. let file = match File::create(&path) { Ok(f) => f, Err(e) => { - eprintln!("error: could not create '{path}': {e}"); + eprintln!("error: could not create '{name}.out': {e}"); std::process::exit(1); } }; @@ -390,12 +406,12 @@ fn main() { let mut writer = BufWriter::new(file); for b in &segment.bundles { if let Err(e) = writer.write_all(&b.bytes[..b.size as usize]) { - eprintln!("error: could not write to '{path}': {e}"); + eprintln!("error: could not write to '{name}.out': {e}"); std::process::exit(1); } } if let Err(e) = writer.flush() { - eprintln!("error: could not write to '{path}': {e}"); + eprintln!("error: could not write to '{name}.out': {e}"); std::process::exit(1); } } |
