mirror of
https://github.com/132nd-vWing/tacview-splitter.git
synced 2026-08-26 11:28:37 +02:00
works now
This commit is contained in:
+34
-24
@@ -4,6 +4,7 @@ pub mod lib {
|
||||
use zip;
|
||||
|
||||
const ERR_CANNOT_WRITE_DATA: &str = "Could not write data";
|
||||
const ERR_CANNOT_OPEN_OUTPUT: &str = "Could not open output file";
|
||||
|
||||
pub struct IDs<'a> {
|
||||
pub blue: Vec<&'a str>,
|
||||
@@ -12,10 +13,10 @@ pub mod lib {
|
||||
pub unknown: Vec<&'a str>,
|
||||
}
|
||||
|
||||
pub struct DescriptorsZip {
|
||||
blue: zip::ZipWriter<fs::File>,
|
||||
red: zip::ZipWriter<fs::File>,
|
||||
violet: zip::ZipWriter<fs::File>,
|
||||
pub struct Descriptors<T: Write> {
|
||||
blue: T,
|
||||
red: T,
|
||||
violet: T,
|
||||
}
|
||||
|
||||
pub struct DescriptorsTxt {
|
||||
@@ -41,11 +42,11 @@ pub mod lib {
|
||||
pub violet: Vec<&'a str>,
|
||||
}
|
||||
|
||||
pub trait WriteData {
|
||||
pub trait Handling {
|
||||
fn write(&mut self, header: Vec<String>, bodies_by_coalition: BodiesByCoalition);
|
||||
}
|
||||
|
||||
impl WriteData for DescriptorsTxt {
|
||||
impl<T: Write> Handling for Descriptors<T> {
|
||||
fn write(&mut self, header: Vec<String>, bodies_by_coalition: BodiesByCoalition) {
|
||||
for line in &header {
|
||||
write!(self.blue, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
@@ -64,25 +65,34 @@ pub mod lib {
|
||||
}
|
||||
}
|
||||
|
||||
impl WriteData for DescriptorsZip {
|
||||
fn write(&mut self, header: Vec<String>, bodies_by_coalition: BodiesByCoalition) {
|
||||
for line in &header {
|
||||
write!(self.blue, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
write!(self.red, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
write!(self.violet, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
}
|
||||
for line in &bodies_by_coalition.blue {
|
||||
write!(self.blue, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
}
|
||||
for line in &bodies_by_coalition.red {
|
||||
write!(self.red, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
}
|
||||
for line in &bodies_by_coalition.violet {
|
||||
write!(self.violet, "{}\n", line).expect(ERR_CANNOT_WRITE_DATA);
|
||||
}
|
||||
|
||||
impl Descriptors<fs::File> {
|
||||
pub fn new_for_file(filenames: OutputFilenames) -> Descriptors<fs::File> {
|
||||
let blue = fs::File::create(&filenames.txt.blue).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let red = fs::File::create(&filenames.txt.red).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let violet = fs::File::create(&filenames.txt.violet).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let descriptors = Descriptors { blue, red, violet };
|
||||
return descriptors
|
||||
}
|
||||
}
|
||||
|
||||
impl Descriptors<zip::ZipWriter<fs::File>> {
|
||||
pub fn new_for_zip(filenames: OutputFilenames) -> Descriptors<zip::ZipWriter<fs::File>> {
|
||||
let options = zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
||||
|
||||
let file = fs::File::create(&filenames.zip.blue).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut blue = zip::ZipWriter::new(file);
|
||||
blue.start_file(&filenames.txt.blue, options).unwrap();
|
||||
|
||||
let file = fs::File::create(&filenames.zip.red).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut red = zip::ZipWriter::new(file);
|
||||
red.start_file(&filenames.txt.red, options).unwrap();
|
||||
|
||||
let file = fs::File::create(&filenames.zip.violet).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut violet = zip::ZipWriter::new(file);
|
||||
violet.start_file(&filenames.txt.violet, options).unwrap();
|
||||
|
||||
let descriptors = Descriptors{blue, red, violet};
|
||||
descriptors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-37
@@ -3,7 +3,7 @@ use std::{fs, str};
|
||||
use std::io::{BufRead, BufReader};
|
||||
use zip;
|
||||
use tacview_splitter::lib;
|
||||
use tacview_splitter::lib::WriteData;
|
||||
use tacview_splitter::lib::Handling;
|
||||
|
||||
const COMMENT: char = '#';
|
||||
const MINUS: char = '-';
|
||||
@@ -15,9 +15,6 @@ const TIMESTAMP: u8 = 1;
|
||||
const DESTRUCTION: u8 = 2;
|
||||
const TELEMETRY: u8 = 3;
|
||||
|
||||
const ERR_CANNOT_OPEN_OUTPUT: &str = "Could not open output file";
|
||||
|
||||
|
||||
fn main() {
|
||||
let (input_filename, is_zip) = find_input_file();
|
||||
println!("Processing {}", input_filename);
|
||||
@@ -27,11 +24,11 @@ fn main() {
|
||||
|
||||
let output_filenames = get_output_filenames(&input_filename);
|
||||
if is_zip {
|
||||
let mut descriptors = get_descriptors_zip(output_filenames);
|
||||
descriptors.write(header, bodies_by_coalition)
|
||||
let mut descriptors = lib::Descriptors::new_for_zip(output_filenames);
|
||||
descriptors.write(header, bodies_by_coalition);
|
||||
} else {
|
||||
let mut descriptors = get_descriptors_txt(output_filenames);
|
||||
descriptors.write(header, bodies_by_coalition)
|
||||
let mut descriptors = lib::Descriptors::new_for_file(output_filenames);
|
||||
descriptors.write(header, bodies_by_coalition);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +43,7 @@ fn split_into_header_and_body(lines: Vec<String>) -> (Vec<String>, Vec<String>)
|
||||
return (lines[..i].to_vec(), lines[i..].to_vec());
|
||||
}
|
||||
|
||||
fn divide_body_by_coalition(body: &'static Vec<String>) -> lib::BodiesByCoalition<'static> {
|
||||
fn divide_body_by_coalition(body: &Vec<String>) -> lib::BodiesByCoalition {
|
||||
let mut bbc = lib::BodiesByCoalition{blue: Vec::new(), red: Vec::new(), violet: Vec::new()};
|
||||
let mut continued = false;
|
||||
let mut line_type: u8 = UNKNOWN;
|
||||
@@ -166,31 +163,3 @@ fn read_data(filename: &String, is_zip: bool) -> Vec<String> {
|
||||
lines
|
||||
}
|
||||
}
|
||||
|
||||
fn get_descriptors_zip(filenames: lib::OutputFilenames) -> lib::DescriptorsZip {
|
||||
let options = zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
||||
|
||||
let file = fs::File::create(&filenames.zip.blue).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut blue = zip::ZipWriter::new(file);
|
||||
blue.start_file(&filenames.txt.blue, options).unwrap();
|
||||
|
||||
let file = fs::File::create(&filenames.zip.red).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut red = zip::ZipWriter::new(file);
|
||||
red.start_file(&filenames.txt.red, options).unwrap();
|
||||
|
||||
let file = fs::File::create(&filenames.zip.violet).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let mut violet = zip::ZipWriter::new(file);
|
||||
violet.start_file(&filenames.txt.violet, options).unwrap();
|
||||
|
||||
let descriptors = lib::DescriptorsZip{blue, red, violet};
|
||||
descriptors
|
||||
}
|
||||
|
||||
fn get_descriptors_txt(filenames: lib::OutputFilenames) -> lib::DescriptorsTxt {
|
||||
let blue = fs::File::create(&filenames.txt.blue).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let red = fs::File::create(&filenames.txt.red).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let violet = fs::File::create(&filenames.txt.violet).expect(ERR_CANNOT_OPEN_OUTPUT);
|
||||
let descriptors = lib::DescriptorsTxt{blue, red, violet};
|
||||
descriptors
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user