Finish cpp_out

This commit is contained in:
jaby 2022-09-17 14:44:13 +02:00
parent db897e0f3d
commit 7c5c8ae50a
5 changed files with 44 additions and 16 deletions

View File

@ -3,4 +3,4 @@ Input/**
Output/**
*.lock
**/Tests/Test_*.cpp
**/Tests/Test_*.*

View File

@ -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: ;

View File

@ -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

View File

@ -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 <stdint.h>", 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 <stdint.h>", 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)
}
}

View File

@ -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