From 3963965d7640c2bfbcf6a4cd3a1d4699f4c20919 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 17 Sep 2022 14:44:13 +0200 Subject: [PATCH] Finish cpp_out --- src/Tools/.gitignore | 2 +- src/Tools/Tests/Test.mk | 1 + src/Tools/cpp_out/Cargo.toml | 2 +- src/Tools/cpp_out/src/lib.rs | 53 ++++++++++++++++++++++++-------- src/Tools/tool_helper/Cargo.toml | 2 +- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/Tools/.gitignore b/src/Tools/.gitignore index bb4248c4..cc8e0b0d 100644 --- a/src/Tools/.gitignore +++ b/src/Tools/.gitignore @@ -3,4 +3,4 @@ Input/** Output/** *.lock -**/Tests/Test_*.cpp \ No newline at end of file +**/Tests/Test_*.* \ No newline at end of file diff --git a/src/Tools/Tests/Test.mk b/src/Tools/Tests/Test.mk index 59eafe2e..f3cfb7f7 100644 --- a/src/Tools/Tests/Test.mk +++ b/src/Tools/Tests/Test.mk @@ -3,5 +3,6 @@ export PATH := $(HOME)/.cargo/bin/:$(PATH) test_cpp_out: always @cargo build --manifest-path ../cpp_out/Cargo.toml --release @echo "Planschbecken" | ./../cpp_out/target/release/cpp_out --name Dino -o "Test_Planschbecken.cpp" + @echo "Planschbecken" | ./../cpp_out/target/release/cpp_out --name Dino -o "Test_Planschbecken.hpp" always: ; \ No newline at end of file diff --git a/src/Tools/cpp_out/Cargo.toml b/src/Tools/cpp_out/Cargo.toml index 8377e2c3..f2c8c94b 100644 --- a/src/Tools/cpp_out/Cargo.toml +++ b/src/Tools/cpp_out/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cpp_out" -version = "0.1.0" +version = "1.0.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/Tools/cpp_out/src/lib.rs b/src/Tools/cpp_out/src/lib.rs index dd09b9af..81e4cfa2 100644 --- a/src/Tools/cpp_out/src/lib.rs +++ b/src/Tools/cpp_out/src/lib.rs @@ -21,7 +21,7 @@ pub struct Configuration { pub file_type: FileType, } -struct FileVariables<'a> { +struct FileDeclarations<'a> { include: &'a str, var_type: &'a str, } @@ -29,13 +29,10 @@ struct FileVariables<'a> { const UNIX_LINEFEED: &'static str = "\n"; const WINDOWS_LINEFEED: &'static str = "\r\n"; -const C_VARIABLES: FileVariables = FileVariables{include: "", var_type: "char"}; -const CPP_VARIABLES: FileVariables = FileVariables{include: "#include ", var_type: "uint8_t"}; - -fn write_source_file(input: Input, output: &mut Output, variables: &FileVariables, data_name: String, line_feed: &str) -> Result<(), std::io::Error> { - write!(output, "{}{}{}", variables.include, line_feed, line_feed)?; - write!(output, "const {} {}[] = {{{}", variables.var_type, data_name, line_feed)?; +const C_DECLARATIONS: FileDeclarations = FileDeclarations{include: "", var_type: "char"}; +const CPP_DECLARATIONS: FileDeclarations = FileDeclarations{include: "#include ", var_type: "uint8_t"}; +fn output_bytes(input: Input, output: &mut Output, line_feed: &str) -> Result<(), std::io::Error> { let mut byte_line_count = 1; let mut bytes = input.bytes(); @@ -52,10 +49,33 @@ fn write_source_file(input: Input, output: &mut Output, variables: &FileVariable } } } - write!(output, "{}}};", line_feed)?; + Ok(()) } +fn write_header_file(input: Input, output: &mut Output, file_name: String, declarations: &FileDeclarations, data_name: String, line_feed: &str) -> Result<(), std::io::Error> { + let file_name = file_name.to_uppercase().replace('.', "_"); + + write!(output, "#ifndef __{}__{}", file_name, line_feed)?; + write!(output, "#define __{}__{}", file_name, line_feed)?; + write!(output, "{}{}{}", declarations.include, line_feed, line_feed)?; + write!(output, "static const {} {}[] = {{{}", declarations.var_type, data_name, line_feed)?; + + output_bytes(input, output, line_feed)?; + + write!(output, "{}}};{}", line_feed, line_feed)?; + write!(output, "#endif // !__{}__{}", file_name, line_feed) +} + +fn write_source_file(input: Input, output: &mut Output, declarations: &FileDeclarations, data_name: String, line_feed: &str) -> Result<(), std::io::Error> { + write!(output, "{}{}{}", declarations.include, line_feed, line_feed)?; + write!(output, "const {} {}[] = {{{}", declarations.var_type, data_name, line_feed)?; + + output_bytes(input, output, line_feed)?; + + write!(output, "{}}};", line_feed) +} + pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<(), Error> { let line_feed = { match cfg.line_feed { @@ -63,13 +83,20 @@ pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<( LineFeed::Unix => UNIX_LINEFEED } }; - let source_variables = { + let (declarations, is_source_file) = { match cfg.file_type { - FileType::CSource => &C_VARIABLES, - FileType::CPPSource => &CPP_VARIABLES, - _ => return Err(Error::new("Not implemented yet".to_owned(), None)) + FileType::CHeader => (&C_DECLARATIONS, false), + FileType::CSource => (&C_DECLARATIONS, true), + FileType::CPPHeader => (&CPP_DECLARATIONS, false), + FileType::CPPSource => (&CPP_DECLARATIONS, true), } }; - Error::try_or_new(write_source_file(input, &mut output, source_variables, cfg.data_name, line_feed), None) + if is_source_file { + Error::try_or_new(write_source_file(input, &mut output, declarations, cfg.data_name, line_feed), None) + } + + else { + Error::try_or_new(write_header_file(input, &mut output, cfg.file_name, declarations, cfg.data_name, line_feed), None) + } } \ No newline at end of file diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml index cd579ff2..6b487e97 100644 --- a/src/Tools/tool_helper/Cargo.toml +++ b/src/Tools/tool_helper/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tool_helper" -version = "0.1.0" +version = "0.5.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html