diff --git a/src/Tools/cpp_out/src/lib.rs b/src/Tools/cpp_out/src/lib.rs index 357404aa..f4109f32 100644 --- a/src/Tools/cpp_out/src/lib.rs +++ b/src/Tools/cpp_out/src/lib.rs @@ -1,4 +1,5 @@ use tool_helper::{Input, Output, Result}; +use std::io::Read; pub use tool_helper::Error; pub enum LineFeed { @@ -34,11 +35,41 @@ const CPP_VARIABLES: FileVariables = FileVariables{include: "#include fn write_source_file(variables: &FileVariables, input: Input, output: &mut Output, line_feed: &str) -> Result<()> { Error::try_or_new(write!(output, "{}{}{}", variables.include, line_feed, line_feed), None)?; Error::try_or_new(write!(output, "const {} data[] = {{{}", variables.var_type, line_feed), None)?; - Error::try_or_new(write!(output, "}};"), None)?; - + + let mut byte_line_count = 1; + let mut bytes = input.bytes(); + + if let Some(byte) = bytes.next() { + Error::try_or_new(write!(output, "\t{:#04X}", Error::try_or_new(byte, None)?), None)?; + + for byte in bytes { + Error::try_or_new(write!(output, ", {:#04X}", Error::try_or_new(byte, None)?), None)?; + byte_line_count += 1; + + if byte_line_count >= 16 { + Error::try_or_new(write!(output, "{}\t", line_feed), None)?; + byte_line_count = 0; + } + } + } + Error::try_or_new(write!(output, "{}}};", line_feed), None)?; Ok(()) } -pub fn convert(_cfg: Configuration, _input: Input, mut output: Output) -> Result<()> { - write_source_file(&CPP_VARIABLES, _input, &mut output, WINDOWS_LINEFEED) +pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<()> { + let line_feed = { + match cfg.line_feed { + LineFeed::Windows => WINDOWS_LINEFEED, + LineFeed::Unix => UNIX_LINEFEED + } + }; + let source_variables = { + match cfg.file_type { + FileType::CSource => &C_VARIABLES, + FileType::CPPSource => &CPP_VARIABLES, + _ => return Err(Error::new("Not implemented yet".to_owned(), None)) + } + }; + + write_source_file(source_variables, input, &mut output, line_feed) } \ No newline at end of file