diff --git a/generator-native/file.h b/generator-native/file.h new file mode 100644 index 0000000..024af87 --- /dev/null +++ b/generator-native/file.h @@ -0,0 +1,13 @@ +constexpr const int DATA_COUNT = 1; +constexpr const char* const DATA_EXEC_AFTER = nullptr; +static constexpr auto DATA_EXEC_AFTER_HASH = "unbound"_sha256; +constexpr const unsigned char DATA[] = { + 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x70, 0x6c, + 0x61, 0x6e, 0x65, 0x74, 0x21, 0x0a, +}; +constexpr const long DATA_LENGTHS[DATA_COUNT] = { + 14ll, +}; +constexpr const char* const DATA_NAMES[DATA_COUNT] = { + "test.txt", +}; diff --git a/generator-native/src/main.rs b/generator-native/src/main.rs index be1a6a4..4c8d665 100644 --- a/generator-native/src/main.rs +++ b/generator-native/src/main.rs @@ -8,6 +8,7 @@ use std::{ }, io::{self, Write,Read,}, error::Error, + path::{Path,}, }; mod iter; @@ -40,20 +41,36 @@ where From: Read, { let mut count =0; for buf in from.into_iter(8) - .map(|byte| { - count += 1; - byte - }) .map(|byte| format!("0x{:02x},", byte)) .group_at(8) + .map(|bytes| { + count += bytes.len(); + bytes + }) .map(|strs| format!("\t{}", strs.join(" "))) { - println!("{}", buf); + writeln!(to, "{}", buf)?; } Ok(count) } +fn attempt_get_name<'a, P>(path: &'a P) -> Result<&'a str, &'static str> + where P: AsRef + ?Sized +{ + let path = path.as_ref(); + if let Some(path) = path.file_name() { + if let Some(file_name) = path.to_str() { + Ok(file_name) + } + else { + Err("Invalid unicode in filename") + } + } else { + Err("No filename, are you trying to add a directory?") + } +} + fn main() -> Result<(), Box>{ /*let file = OpenOptions::new() @@ -87,6 +104,7 @@ fn main() -> Result<(), Box>{ let mut fp = OpenOptions::new() .write(true) .truncate(true) + .create(true) .open(output)?; if silent { @@ -108,13 +126,58 @@ fn main() -> Result<(), Box>{ writeln!(fp, "constexpr const unsigned char DATA[] = {{")?; for file in files.iter() { - let rfp = OpenOptions::new() + print!(" + {}", file); + let file = OpenOptions::new() .read(true) .open(file)?; - print!(" + {}", file); - sizes.push(write_file(rfp, &mut fp)?); + sizes.push(match write_file(file, &mut fp) { + Ok(size) => { + println!(" OK"); + size + }, + Err(error) => { + println!(" FAILED: {}",error); + return Err("state corrupted: cannot continue after mid-failed write.")?; + }, + }); + } + writeln!(fp, "}};")?; + println!("Adding lengths..."); + + writeln!(fp, "constexpr const long DATA_LENGTHS[DATA_COUNT] = {{")?; + for size in sizes.into_iter() { + write!(fp, "\t{}ll,", size)?; + + } + writeln!(fp, "\n}};")?; + + println!("Adding names..."); + + writeln!(fp, "constexpr const char* const DATA_NAMES[DATA_COUNT] = {{")?; + for file in files.into_iter() { + let file = Path::new(&file); + print!(" - {:?}", file); + let file = match attempt_get_name(&file) { + Ok(file) => file, + Err(error) => { + println!(" FAILED: pathspec: {}", error); + + return Err("name write failed, aborting.")?; + } + }; + + match write!(fp, "\t\"{}\",", translate::c_escape(file)) { + Err(error) => { + println!(" FAILED: write: {}", error); + + return Err("name write failed, aborting.")?; + }, + _ => (), + }; + println!(" OK"); } + writeln!(fp, "\n}};")?; }, arg::OperationMode::Help => { usage();