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
/*
    rust-dos2unix
    Copyright 2016 Sam Saint-Pettersen.

    Released under the MIT license;
    see LICENSE.
*/

//! Rust library to convert DOS to Unix line endings.

use std::fs::File;
use std::io::prelude::*;

pub struct Dos2Unix;

impl Dos2Unix {
    fn is_ascii(contents: String) -> bool {
        let mut ascii = true;
        for c in contents.chars() {
            let code = c as i32;
            if code > 127 {
                ascii = false;
                break;
            }
        }
        ascii
    }

    fn is_dos_eol(contents: String) -> bool {
        let mut dos_eol = false;
        for c in contents.chars() {
            if c == '\r' {
                dos_eol = true;
                break;
            }
        }
        dos_eol
    }

    fn to_unix_line_endings(contents: String) -> Vec<String> {
        let mut ucontents = Vec::new();
        for c in contents.chars() {
            if c != '\r' {
                ucontents.push(format!("{}", c));
            }
        }
        ucontents
    }

    ///
    /// Convert a file to have Unix line endings.
    ///
    /// * `filename` - Filename to open and change line endings for.
    /// * `feedback` - Display feedback when file already has Unix line endings or is binary.
    pub fn convert(filename: &str, feedback: bool) -> bool {
        let mut input = File::open(filename).unwrap();
        let mut contents = String::new();
        let _ = input.read_to_string(&mut contents);
        let ascii = Dos2Unix::is_ascii(contents.clone());
        let dos_eol = Dos2Unix::is_dos_eol(contents.clone());

        let message = "dos2unix: File already has Unix line endings or is binary.";
        let mut success = false;

        if ascii && dos_eol {
            let converted = Dos2Unix::to_unix_line_endings(contents.clone());
            let mut w = File::create(filename).unwrap();
            let _ = w.write_all(converted.join("").as_bytes());
            success = true;
        } else if feedback {
            println!("{}", message);
        }
        success
    }
}

#[cfg(test)]
#[test]
fn convert() {
    Dos2Unix::convert("README.md", true);
    let mut input = File::open("README.md").unwrap();
    let mut contents = String::new();
    let _ = input.read_to_string(&mut contents);
    let mut has_dos_eol = false;
    for c in contents.chars() {
        if c == '\r' {
            has_dos_eol = true;
            break;
        }
    }
    assert_eq!(has_dos_eol, false);
}