1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Numeric reader from binary files.

use std::fs::File;
use std::io::{BufRead, BufReader};
use std::marker::PhantomData;
use std::path::Path;

use anyhow;
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use num_complex::Complex;

#[cfg(test)]
#[path = "numeric_tests.rs"]
mod numeric_tests;

/// Iterable structure for reading numeric binary files.
pub(crate) struct NumericReader<R: BufRead, B: ByteOrder, T> {
    /// The inner file reader.
    inner: R,

    /// The byte order of the numeric values to be read.
    byte_order: PhantomData<B>,

    /// The type of the numeric values to be read.
    numeric_type: PhantomData<T>,
}

impl<R: BufRead, B: ByteOrder, T> NumericReader<R, B, T> {
    /// Constructs a new numeric binary reader wrapping around a standard file reader.
    ///
    /// # Arguments
    ///
    /// * `inner` - The underlying file reader.
    fn new(inner: R) -> Self {
        Self {
            inner,
            byte_order: PhantomData,
            numeric_type: PhantomData,
        }
    }
}

impl<B: ByteOrder, T> NumericReader<BufReader<File>, B, T> {
    /// Constructs a new numeric binary reader wrapping around a buffered file reader from a
    /// filename.
    ///
    /// # Arguments
    ///
    /// * `filename` - The path to the file to be read.
    pub(crate) fn from_file<P: AsRef<Path>>(filename: P) -> Result<Self, anyhow::Error> {
        let f = File::open(&filename)?;
        Ok(Self::new(BufReader::new(f)))
    }
}

macro_rules! impl_iterator_numeric_reader {
    ($($t:ty),+) => {$(
        impl<R: BufRead> Iterator for NumericReader<R, LittleEndian, $t> {
            type Item = $t;

            fn next(&mut self) -> Option<Self::Item> {
                let mut buff: [u8; std::mem::size_of::<$t>()] = [0_u8; std::mem::size_of::<$t>()];
                self.inner.read_exact(&mut buff).ok()?;
                Some(<$t>::from_le_bytes(buff))
            }
        }

        impl<R: BufRead> Iterator for NumericReader<R, BigEndian, $t> {
            type Item = $t;

            fn next(&mut self) -> Option<Self::Item> {
                let mut buff: [u8; std::mem::size_of::<$t>()] = [0_u8; std::mem::size_of::<$t>()];
                self.inner.read_exact(&mut buff).ok()?;
                Some(<$t>::from_be_bytes(buff))
            }
        }
    )+}
}

macro_rules! impl_iterator_numeric_reader_complex {
    ($($t:ty),+) => {$(
        impl<R: BufRead> Iterator for NumericReader<R, LittleEndian, Complex<$t>> {
            type Item = Complex<$t>;

            fn next(&mut self) -> Option<Self::Item> {
                let mut buff: [u8; std::mem::size_of::<$t>()] = [0_u8; std::mem::size_of::<$t>()];
                self.inner.read_exact(&mut buff).ok()?;
                let re = <$t>::from_le_bytes(buff);
                self.inner.read_exact(&mut buff).ok()?;
                let im = <$t>::from_le_bytes(buff);
                Some(Complex::<$t> { re, im })
            }
        }

        impl<R: BufRead> Iterator for NumericReader<R, BigEndian, Complex<$t>> {
            type Item = Complex<$t>;

            fn next(&mut self) -> Option<Self::Item> {
                let mut buff: [u8; std::mem::size_of::<$t>()] = [0_u8; std::mem::size_of::<$t>()];
                self.inner.read_exact(&mut buff).ok()?;
                let re = <$t>::from_be_bytes(buff);
                self.inner.read_exact(&mut buff).ok()?;
                let im = <$t>::from_be_bytes(buff);
                Some(Complex::<$t> { re, im })
            }
        }
    )+}
}

impl_iterator_numeric_reader!(
    u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64
);

impl_iterator_numeric_reader_complex!(
    u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64
);