Implement LZ4 strip and make tools write errors to err instead of out
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cpp_out"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
@@ -2,6 +2,8 @@ use tool_helper::{Input, Output};
|
||||
use std::io::Read;
|
||||
pub use tool_helper::{Error, format_if_error};
|
||||
|
||||
const EMPTY_CONTENT_ERROR_STRING:&'static str = "Content is not allowed to be empty";
|
||||
|
||||
pub enum LineFeed {
|
||||
Unix,
|
||||
Windows
|
||||
@@ -32,8 +34,9 @@ const WINDOWS_LINEFEED: &'static str = "\r\n";
|
||||
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> {
|
||||
fn output_bytes(input: Input, output: &mut Output, line_feed: &str) -> Result<usize, std::io::Error> {
|
||||
let mut byte_line_count = 0;
|
||||
let mut bytes_written = 0;
|
||||
|
||||
write!(output, "\t")?;
|
||||
for byte in input.bytes() {
|
||||
@@ -43,6 +46,7 @@ fn output_bytes(input: Input, output: &mut Output, line_feed: &str) -> Result<()
|
||||
|
||||
write!(output, "{:#04X}", byte?)?;
|
||||
byte_line_count += 1;
|
||||
bytes_written += 1;
|
||||
|
||||
if byte_line_count >= 16 {
|
||||
write!(output, ",{}\t", line_feed)?;
|
||||
@@ -50,10 +54,10 @@ fn output_bytes(input: Input, output: &mut Output, line_feed: &str) -> Result<()
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(bytes_written)
|
||||
}
|
||||
|
||||
fn write_header_file(input: Input, output: &mut Output, file_name: String, declarations: &FileDeclarations, data_name: String, line_feed: &str) -> Result<(), std::io::Error> {
|
||||
fn write_header_file(input: Input, output: &mut Output, file_name: String, declarations: &FileDeclarations, data_name: String, line_feed: &str) -> Result<(), Error> {
|
||||
let file_name = file_name.to_uppercase().replace('.', "_");
|
||||
|
||||
write!(output, "#ifndef __{}__{}", file_name, line_feed)?;
|
||||
@@ -61,19 +65,25 @@ fn write_header_file(input: Input, output: &mut Output, file_name: String, decla
|
||||
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)?;
|
||||
if output_bytes(input, output, line_feed)? == 0 {
|
||||
return Err(Error::from_str(EMPTY_CONTENT_ERROR_STRING));
|
||||
}
|
||||
|
||||
write!(output, "{}}};{}", line_feed, line_feed)?;
|
||||
write!(output, "#endif // !__{}__{}", file_name, line_feed)
|
||||
write!(output, "#endif // !__{}__{}", file_name, line_feed)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_source_file(input: Input, output: &mut Output, declarations: &FileDeclarations, data_name: String, line_feed: &str) -> Result<(), std::io::Error> {
|
||||
fn write_source_file(input: Input, output: &mut Output, declarations: &FileDeclarations, data_name: String, line_feed: &str) -> Result<(), 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)?;
|
||||
if output_bytes(input, output, line_feed)? == 0 {
|
||||
return Err(Error::from_str(EMPTY_CONTENT_ERROR_STRING));
|
||||
}
|
||||
|
||||
write!(output, "{}}};", line_feed)
|
||||
write!(output, "{}}};", line_feed)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn convert(cfg: Configuration, input: Input, mut output: Output) -> Result<(), Error> {
|
||||
|
Reference in New Issue
Block a user