From f9777b124b0260093b86c4ff47082e18bbe39688 Mon Sep 17 00:00:00 2001 From: Misha Vicha Date: Mon, 13 Oct 2025 08:20:42 +0200 Subject: [PATCH] Hashmap for file:signals pairs, signal_data file --- src/headparse.rs | 12 +++++-- src/main.rs | 89 ++++++++++++++++++++++------------------------ src/signal_data.rs | 48 +++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 49 deletions(-) create mode 100644 src/signal_data.rs diff --git a/src/headparse.rs b/src/headparse.rs index 12ce99e..e162603 100644 --- a/src/headparse.rs +++ b/src/headparse.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, u64, vec}; +use std::{collections::HashMap, hash::Hash, u64, vec}; use crate::SignalFormat; @@ -153,7 +153,7 @@ enum AdcBlockKeys { /// /// By minimum, only carries the `filename`, `format`, and `adc_zero` attributes #[derive(Debug, Clone)] -struct SignalSpec { +pub struct SignalSpec { filename: String, format: SignalFormat, samples_frame: Option, @@ -425,6 +425,14 @@ impl Header { None => true } } + + pub fn signals_per_file(&self) -> HashMap { + let mut map: HashMap = HashMap::new(); + for signal in &self.signal_specs { + map.entry(signal.filename.clone()).and_modify(|val: &mut u64| *val += 1 ).or_insert(1); + } + return map; + } } /// Attempts to parse the header file. diff --git a/src/main.rs b/src/main.rs index 91ce3b4..ace1a41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ -use std::{env::{self}, io::Error, path::Path}; +use std::{env::{self}, path::Path}; use std::fs; -use crate::headparse::Header; +pub mod headparse; // The HEAder parsing +pub mod signal_data; -pub mod headparse; // The HEAder parsing +use crate::{headparse::Header, signal_data::SignalData}; /// Use for handling possible formats of the WFDB data #[derive(Debug, Clone, Copy)] @@ -13,7 +14,7 @@ enum SignalFormat { Unimpl = 0 } -fn main() -> Result<(), Error>{ +fn main() -> Result<(), String>{ let args: Vec = env::args().collect(); if args.len() <= 1 || args.contains(&"help".to_string()) { @@ -30,18 +31,41 @@ fn main() -> Result<(), Error>{ println!("File provided is not a .hea file"); return Ok(()); } - + + // Parse the header information + let header: Header; { let hea_file_result = fs::read_to_string(filepath); match hea_file_result { Ok(file_data) => { - let header = headparse::parse_header(file_data.as_str()); - - dbg!(header); + match headparse::parse_header(file_data.as_str()) { + Ok(h) => {header = h} + Err(e) => {return Err(e.to_string())} + } } - Err(e) => return Err(e) + Err(e) => {return Err("error: Provided file cannot be opened".to_string())} } } + + let path_parent: String; + { + // This is cursed lol + match filepath.parent() { + Some(p) => { + match p.to_str() { + Some(str) => { + path_parent = str.to_string(); + } + None => {path_parent = "".to_string()} + } + + } + None => {path_parent = "".to_string()} + } + } + + let parsed_signals = get_all_data(header, path_parent); + println!("Hello, world!"); Ok(()) } @@ -51,43 +75,14 @@ fn help() { println!("\nUse in the format \"wfdb_corrosion (.hea filename)\"") } -/// Holds fully processed signal data already parsed from the .dat file, ready -/// to be outputted in a practical way -#[derive(Debug, Clone)] -struct SignalData { - name: String, - values: Vec, - unit: String -} - -impl SignalData { - /// Creates a [`SignalData`](crate::SignalData) with a name and an empty - /// `values` vector and `unit` string. - /// - /// The `values` vector is initialized with no capacity, which causes this - /// struct to be quite a bit slower. This method is thus not suggested, use - /// [`SignalData::with_name_and_capacity`](crate::SignalData::with_name_and_capacity) - /// when you know how much you want to allocate - /// - /// # Arguments - /// * `name` - The name of the Signal - pub fn with_name(name: String) -> SignalData { - SignalData { name: name, values: vec![], unit: String::new() } - } - - /// Creates a [`SignalData`](crate::SignalData) with a name and a - /// `values` vector of a certain capacity and an empty `unit` string. - /// - /// # Arguments - /// - /// * `name` - The name of the Signal - /// * `capacity` - The capacity with which the `values` vector will be - /// initialized - pub fn with_name_and_capacity(name: String, capacity: usize) -> SignalData { - SignalData { name: name, values: Vec::with_capacity(capacity), unit: String::new() } - } -} - -fn get_signal_data() -> SignalData { +fn get_signal_data(spec: headparse::SignalSpec, offset: u64, data_length: usize) -> SignalData { todo!() } + +fn get_all_data(header: Header, header_root: String) -> Vec { + let signal_counts = header.signals_per_file(); + for (filename, sigcount) in signal_counts { + + } + todo!() +} \ No newline at end of file diff --git a/src/signal_data.rs b/src/signal_data.rs new file mode 100644 index 0000000..9ca1a86 --- /dev/null +++ b/src/signal_data.rs @@ -0,0 +1,48 @@ +/// Holds fully processed signal data already parsed from the .dat file, ready +/// to be outputted in a practical way +#[derive(Debug, Clone)] +pub struct SignalData { + name: String, + values: Vec, + unit: String +} + +impl SignalData { + /// Creates a [`SignalData`](crate::SignalData) with a name and an empty + /// `values` vector and `unit` string. + /// + /// The `values` vector is initialized with no capacity, which causes this + /// struct to be quite a bit slower. This method is thus not suggested, use + /// [`SignalData::with_name_and_capacity`](crate::SignalData::with_name_and_capacity) + /// when you know how much you want to allocate + /// + /// # Arguments + /// * `name` - The name of the Signal + pub fn with_name(name: String) -> SignalData { + SignalData { name: name, values: vec![], unit: String::new() } + } + + /// Creates a [`SignalData`](crate::SignalData) with a name and a + /// `values` vector of a certain capacity and an empty `unit` string. + /// + /// # Arguments + /// + /// * `name` - The name of the Signal + /// * `capacity` - The capacity with which the `values` vector will be + /// initialized + pub fn with_name_and_capacity(name: String, capacity: usize) -> SignalData { + SignalData { name: name, values: Vec::with_capacity(capacity), unit: String::new() } + } + + /// Appends a value to `values` inside the struct + pub fn push(&mut self, value: f64) { + self.values.push(value); + } + + /// Appends data copied from the supplied `data` argument to the end of the + /// `values` vector inside the struct + pub fn append_copy(&mut self, data: Vec) { + let mut to_add: Vec = data; + to_add.append(&mut self.values); + } +} \ No newline at end of file