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
// MIT License
//
// Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
// Distributed under the terms of the MIT License.
//
//

use {
    std::io::BufRead,

    crate::{
        logger::{
            EliteLogType,
            elite_logger
        }
    }
};

pub struct EliteFileData {
    pub raw_data: String,
    pub unparsed: Vec<String>
}

pub mod elite_file_contents {
    // to_lowercase(filename) supported.
    // filename: EliTeFilE, eliteFILE, EliteFile etc.
    pub const GENERIC_FILENAME: &str = "elitefile";

    pub fn create_empty_string() -> String {
        return "".to_string();
    }
}

impl EliteFileData {
    pub fn check_is_elite(&self, file: &String) -> bool {
        return if file.to_ascii_lowercase() == elite_file_contents::GENERIC_FILENAME {
            true
        } else {
            false
        };
    }

    pub fn read_raw_file(&mut self, file: &String) {
        if !self.check_is_elite(file) {
            elite_logger::log(EliteLogType::Info,
                              "filename",
                              &format!("to_lowercase({}) ignored", file));
        }

        let mut raw_data = String::new();

        if let Ok(lines) = self.read_lines(file) {
            for line in lines {
                if let Ok(ip) = line {
                    if crate::tokenizer::elite_tokenizer::is_comment(&ip.as_str()) {
                        continue;
                    }

                    raw_data.push(' '); raw_data.push_str(&ip); raw_data.push('\n');
                }
            }
        }

        self.raw_data = raw_data;
    }

    fn read_lines<P>(&self, file: &P) -> std::io::Result<
        std::io::Lines<std::io::BufReader<std::fs::File>>
    > where P: AsRef<std::path::Path>, {
        Ok(std::io::BufReader::new(
            std::fs::File::open(file)?).lines())
    }
}