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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use std::any::Any;
use std::str::{self, FromStr};

use byteorder::ByteOrder;
use nom::{digit, IResult, Needed};
use num_traits::Zero;

use crate::model::IOBuffer;

/// This enum indicates if bulk data is saved in binary.
/// NOTE: VTK files are saved in ASCII format with bulk data optionally saved in
/// Binary among ASCII type keywords.  Binary data must be placed into the file
/// immediately after the "newline" (`\n`) character from the previous ASCII
/// keyword and parameter sequence. For example point positions and cell indices
/// and types can be saved in Binary in VTK files.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum FileType {
    Binary,
    ASCII,
}

/// Parse floating point numbers. This macro works for differnt buffer types.
macro_rules! impl_real_parser {
    ($name:ident ($buf_type:ty)) => {
        pub fn $name<T>(input: &$buf_type) -> IResult<&$buf_type, T>
        where
            T: FromStr,
        {
            flat_map!(
                input,
                recognize!(tuple!(
                    opt!(alt!(tag!("+") | tag!("-"))),
                    alt_complete!(
                        delimited!(digit, tag!("."), opt!(digit))
                            | delimited!(opt!(digit), tag!("."), digit)
                            | digit
                    ),
                    opt!(complete!(tuple!(
                        alt!(tag!("e") | tag!("E")),
                        opt!(alt!(tag!("+") | tag!("-"))),
                        digit
                    )))
                )),
                parse_to!(T)
            )
        }
    };
}

/*
 * Parsing routines
 */

// Consume a spaces and tabs.
named!(pub whitespace, eat_separator!(" \t"));

/// Whitespace separator `sp`. Like `ws` but excludes new-lines.
macro_rules! sp (
    ($i:expr, $($args:tt)*) => ( {
            sep!($i, whitespace, $($args)*)
        })
    );

// Parse a floating point number from a byte array.
// This extends `nom`'s implementation by allowing floats without a decimal point (e.g. `3e3`).
impl_real_parser!(real([u8]));

/// Parse a number in binary form from a byte array.
pub trait FromBinary
where
    Self: Sized,
{
    fn from_binary<T: ByteOrder>(input: &[u8]) -> IResult<&[u8], Self>;
}

macro_rules! impl_from_binary {
    ($type:ty) => {
        impl FromBinary for $type {
            fn from_binary<T: ByteOrder>(input: &[u8]) -> IResult<&[u8], $type> {
                debug_assert_eq!(::std::mem::size_of::<$type>(), 1);
                if input.len() < 1 {
                    IResult::Incomplete(Needed::Size(1))
                } else {
                    IResult::Done(&input[1..], input[0] as $type)
                }
            }
        }
    };
    ($type:ty, $read_fn:ident) => {
        impl FromBinary for $type {
            fn from_binary<T: ByteOrder>(input: &[u8]) -> IResult<&[u8], $type> {
                let size = ::std::mem::size_of::<$type>();
                if input.len() < size {
                    IResult::Incomplete(Needed::Size(size))
                } else {
                    let res = T::$read_fn(input);
                    IResult::Done(&input[size..], res)
                }
            }
        }
    };
}
impl_from_binary!(u8);
impl_from_binary!(i8);
impl_from_binary!(u16, read_u16);
impl_from_binary!(i16, read_i16);
impl_from_binary!(u32, read_u32);
impl_from_binary!(i32, read_i32);
impl_from_binary!(u64, read_u64);
impl_from_binary!(i64, read_i64);
impl_from_binary!(f32, read_f32);
impl_from_binary!(f64, read_f64);

pub trait FromAscii
where
    Self: Sized,
{
    fn from_ascii(input: &[u8]) -> IResult<&[u8], Self>;
}

macro_rules! impl_from_ascii {
    ($type:ty, $fn:ident) => {
        impl FromAscii for $type {
            fn from_ascii(input: &[u8]) -> IResult<&[u8], $type> {
                $fn(input)
            }
        }
    };
}
impl_from_ascii!(u8, unsigned);
impl_from_ascii!(i8, integer);
impl_from_ascii!(u16, unsigned);
impl_from_ascii!(i16, integer);
impl_from_ascii!(u32, unsigned);
impl_from_ascii!(i32, integer);
impl_from_ascii!(u64, unsigned);
impl_from_ascii!(i64, integer);
impl_from_ascii!(f32, real);
impl_from_ascii!(f64, real);

/// Parse a formatted unsigned integer.
pub fn unsigned<T>(input: &[u8]) -> IResult<&[u8], T>
where
    T: FromStr,
{
    map_res!(input, map_res!(digit, str::from_utf8), FromStr::from_str)
}

/// Parse a formatted signed integer.
pub fn integer<T>(input: &[u8]) -> IResult<&[u8], T>
where
    T: FromStr,
{
    flat_map!(
        input,
        recognize!(tuple!(opt!(alt!(tag!("+") | tag!("-"))), digit)),
        parse_to!(T)
    )
}

// A trait identifying all scalar types supported by VTK.
pub trait Scalar: FromStr + FromAscii + FromBinary {}
macro_rules! impl_scalar {
    ($($type:ty),* $(,)*) => {
        $(
            impl Scalar for $type {}
        )*
    }
}

impl_scalar!(u8, i8, u16, i16, u32, i32, u64, i64, f32, f64);

/// Parse a set of typed numbers into an `IOBuffer`.
pub fn parse_data_buffer<T, BO>(input: &[u8], n: usize, ft: FileType) -> IResult<&[u8], IOBuffer>
where
    T: Scalar + Any + Clone + Zero + ::std::fmt::Debug,
    BO: ByteOrder,
    IOBuffer: From<Vec<T>>,
{
    parse_data_vec::<T, BO>(input, n, ft).map(IOBuffer::from)
}

/// Parse a set of unsigned bytes into an `IOBuffer`.
pub fn parse_data_buffer_u8(input: &[u8], n: usize, ft: FileType) -> IResult<&[u8], IOBuffer> {
    parse_data_vec_u8(input, n, ft).map(IOBuffer::from)
}

/// Parse a set of signed bytes into an `IOBuffer`.
pub fn parse_data_buffer_i8(input: &[u8], n: usize, ft: FileType) -> IResult<&[u8], IOBuffer> {
    parse_data_vec_i8(input, n, ft).map(IOBuffer::from)
}

/// Parse a set of bits into an `IOBuffer`.
pub fn parse_data_bit_buffer(input: &[u8], n: usize, ft: FileType) -> IResult<&[u8], IOBuffer> {
    parse_data_bit_vec(input, n, ft).map(IOBuffer::from)
}

/// Parse a set of typed numbers into a `Vec`.
pub fn parse_data_vec<T, BO>(input: &[u8], n: usize, ft: FileType) -> IResult<&[u8], Vec<T>>
where
    T: Scalar,
    BO: ByteOrder,
{
    match ft {
        FileType::ASCII => many_m_n!(input, n, n, ws!(T::from_ascii)),
        FileType::Binary => many_m_n!(input, n, n, T::from_binary::<BO>),
    }
}

/// Parse a set of unsigned bytes into a `Vec`.
pub fn parse_data_vec_u8(input: &[u8], n: usize, ft: FileType) -> IResult<&[u8], Vec<u8>> {
    match ft {
        FileType::ASCII => many_m_n!(input, n, n, ws!(u8::from_ascii)),
        FileType::Binary => {
            // If expecting bytes, byte order doesn't matter, just return the entire block.
            if input.len() < n {
                IResult::Incomplete(Needed::Size(n))
            } else {
                IResult::Done(&input[n..], input[0..n].to_vec())
            }
        }
    }
}

/// Parse a set of signed bytes into a `Vec`.
pub fn parse_data_vec_i8(input: &[u8], n: usize, ft: FileType) -> IResult<&[u8], Vec<i8>> {
    match ft {
        FileType::ASCII => many_m_n!(input, n, n, ws!(i8::from_ascii)),
        FileType::Binary => {
            // If expecting bytes, byte order doesn't matter, just return the entire block.
            // Unsafety is used here to avoid having to iterate.
            if input.len() < n {
                IResult::Incomplete(Needed::Size(n))
            } else {
                // SAFETY: All u8 are representable as i8 and both are 8 bits.
                IResult::Done(
                    &input[n..],
                    unsafe { std::slice::from_raw_parts(input[0..n].as_ptr() as *const i8, n) }
                        .to_vec(),
                )
            }
        }
    }
}

pub fn parse_data_bit_vec(input: &[u8], n: usize, ft: FileType) -> IResult<&[u8], Vec<u8>> {
    match ft {
        FileType::ASCII => many_m_n!(input, n, n, ws!(u8::from_ascii)),
        FileType::Binary => {
            let nbytes = n / 8 + if n % 8 == 0 { 0 } else { 1 };
            if input.len() < nbytes {
                IResult::Incomplete(Needed::Size(nbytes))
            } else {
                IResult::Done(&input[nbytes..], input[0..nbytes].to_vec())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use byteorder::BigEndian;
    use nom::IResult;

    #[test]
    fn can_parse_float() {
        assert_eq!(real::<f32>(&b"-0.00005"[..]).unwrap().1, -0.00005);
        assert_eq!(real::<f32>(&b"4."[..]).unwrap().1, 4.0);
        assert_eq!(real::<f32>(&b"3"[..]).unwrap().1, 3.0);
        assert_eq!(real::<f32>(&b"-.3"[..]).unwrap().1, -0.3);
        assert_eq!(real::<f32>(&b"3e3"[..]).unwrap().1, 3000.0);
        assert_eq!(real::<f32>(&b"-3.2e2"[..]).unwrap().1, -320.0);
    }
    #[test]
    fn can_parse_int() {
        assert_eq!(integer::<i32>(&b"-1"[..]).unwrap().1, -1);
        assert_eq!(integer::<i32>(&b"1"[..]).unwrap().1, 1);
        assert_eq!(integer::<i32>(&b"43242"[..]).unwrap().1, 43242);
        assert_eq!(integer::<u8>(&b"255"[..]).unwrap().1, 255);
    }
    #[test]
    fn can_parse_binary_float() {
        assert_eq!(
            f32::from_binary::<BigEndian>(&[0u8, 0, 0, 0]).unwrap().1,
            0.0_f32
        );
        assert_eq!(
            f32::from_binary::<BigEndian>(&[62u8, 32, 0, 0]).unwrap().1,
            0.15625_f32
        );
    }
    #[test]
    fn data_test() {
        let f = parse_data_buffer::<f32, BigEndian>("".as_bytes(), 0, FileType::ASCII);
        assert_eq!(
            f,
            IResult::Done("".as_bytes(), IOBuffer::from(Vec::<f32>::new()))
        );
        let f = parse_data_buffer::<f32, BigEndian>("3".as_bytes(), 1, FileType::ASCII);
        assert_eq!(
            f,
            IResult::Done("".as_bytes(), IOBuffer::from(vec![3.0f32]))
        );
        let f = parse_data_buffer::<f32, BigEndian>("3 32".as_bytes(), 2, FileType::ASCII);
        assert_eq!(
            f,
            IResult::Done("".as_bytes(), IOBuffer::from(vec![3.0f32, 32.0]))
        );
        let f = parse_data_buffer::<f32, BigEndian>("3 32 32.0 4e3".as_bytes(), 4, FileType::ASCII);
        assert_eq!(
            f,
            IResult::Done(
                "".as_bytes(),
                IOBuffer::from(vec![3.0f32, 32.0, 32.0, 4.0e3])
            )
        );
        let f = parse_data_buffer::<f64, BigEndian>("3 32 32.0 4e3".as_bytes(), 4, FileType::ASCII);
        assert_eq!(
            f,
            IResult::Done(
                "".as_bytes(),
                IOBuffer::from(vec![3.0f64, 32.0, 32.0, 4.0e3])
            )
        );
    }
}