anyscript_compiler/setup/
lynch.rs

1use std::ffi::CString;
2use std::path::{Path, PathBuf};
3use std::fs::{File, write};
4use std::io::{BufReader, BufWriter, Read};
5
6pub struct LynchReader {
7    filepath: String,
8    content: String,
9    coded: String,
10    encoded: String,
11}
12
13impl LynchReader {
14
15    pub fn new(filepath: String) -> Self {
16        LynchReader {
17            filepath,
18            content: String::new(),
19            coded: String::new(),
20            encoded: String::new(),
21        }
22    }
23
24    pub fn from_cstring(filepath: CString) -> Self {
25        let _string = filepath.to_str().unwrap().to_string();
26        Self {
27            filepath: _string.clone(),
28            content: String::new(),
29            coded: String::new(),
30            encoded: String::new(),
31        }
32    }
33
34    pub fn content_to_string(&mut self) -> String {
35        let _file = File::open(&self.filepath).expect("Could not open file.");
36        let mut _content = String::new();
37        let mut reader = BufReader::new(_file);
38        reader.read_to_string(&mut _content).expect("Could not read file.");
39        _content
40    }
41
42    pub fn content_to_cstring(&mut self) -> CString {
43        let _content = self.content_to_string();
44        CString::new(_content).expect("Could not convert string to cstring.")
45    }
46
47    pub fn read_content(&mut self) {
48        let _content = self.content_to_string();
49        self.content = _content;
50    }
51
52    pub fn decode_lynch(&mut self) {
53        let _content = self.content_to_string();
54        let mut _result = String::new();
55        // Wir schreiben den Code um und codieren es in Zahlen um
56        // Das sieht so aus, dass jede Zahl die Stelle des Alphabets entspricht mit dem Splitter 0
57        // 0 steht für die Trennung zwischen den verschiedenen Zeilen
58        // 1 steht für a
59        // 2 steht für b
60        // usw...
61        // Sonderzeichen bleiben wo sie sind, das Resultat ist ein Code-String mit dem wir arbeiten
62        // Es gibt 26, 1 bis 26 sind Buchstaben,
63        // vor und nach Zahlen kommen 00, 0 steht für nächster Buchstabe, aber 00 steht nur dafür, dass es eine Zahl und kein Buchstabe ist
64        // Alle Zahlen nach 27 sind ungültig, heißt dann ist der String Invalid, der String dient dazu, den String zu codieren, also besser für das Programm hier
65        // Jetzt müssen wir den Content so splitten und speichern, dass es in einem Vektor ist, wo immer exakt ein Zeichen drinnen ist.
66        let mut _liste = Vec::new();
67        for _line in _content.split('\n') {
68            for _char in _line.chars() {
69                // Wir haben nun das Wort, jetzt müssen wir es codieren
70                let mut _raw = String::new();
71                let code = match _char {
72                    'a'..='z' => (_char as u8 - b'a' + 1).to_string(),
73                    'A'..='Z' => (_char as u8 - b'A' + 1).to_string(),
74                    '0'..='9' => format!("00{}", _char),
75                    _ => _char.to_string(),
76                };
77                _raw.push_str(&code);
78                _liste.push(_raw);
79            }
80            _liste.push("0".to_string()); // Zeilentrenner hinzufügen
81        }
82        _result = _liste.join(" ");
83        self.coded = _result;
84    }
85
86    pub fn encode_lynch(&mut self) {
87        let mut _result = String::new();
88        let mut _encoded_list = Vec::new();
89        let coded_content = &self.coded;
90
91        for code in coded_content.split_whitespace() {
92            let encoded_char = match code {
93                "1" => 'a',
94                "2" => 'b',
95                "3" => 'c',
96                "4" => 'd',
97                "5" => 'e',
98                "6" => 'f',
99                "7" => 'g',
100                "8" => 'h',
101                "9" => 'i',
102                "10" => 'j',
103                "11" => 'k',
104                "12" => 'l',
105                "13" => 'm',
106                "14" => 'n',
107                "15" => 'o',
108                "16" => 'p',
109                "17" => 'q',
110                "18" => 'r',
111                "19" => 's',
112                "20" => 't',
113                "21" => 'u',
114                "22" => 'v',
115                "23" => 'w',
116                "24" => 'x',
117                "25" => 'y',
118                "26" => 'z',
119                "00" => '0',
120                "001" => '1',
121                "002" => '2',
122                "003" => '3',
123                "004" => '4',
124                "005" => '5',
125                "006" => '6',
126                "007" => '7',
127                "008" => '8',
128                "009" => '9',
129                "0" => '\n', // Zeilentrenner
130                _ => '?', // Unbekanntes Zeichen
131            };
132            _encoded_list.push(encoded_char);
133        }
134
135        _result = _encoded_list.iter().collect::<String>();
136        self.encoded = _result;
137    }
138
139    pub fn coded_to_string(&mut self) -> String {
140        self.decode_lynch();
141        
142        self.encoded.clone()
143    }
144
145    pub fn coded_to_cstring(&mut self) -> CString {
146        let _result = self.coded_to_string();
147        CString::new(_result).expect("Could not convert string to cstring.")
148    }
149
150    pub fn encoded_to_string(&mut self) -> String {
151        self.encode_lynch();
152        
153        self.encoded.clone()
154    }
155
156    pub fn encoded_to_cstring(&mut self) -> CString {
157        let _result = self.encoded_to_string();
158        CString::new(_result).expect("Could not convert string to cstring.")
159    }
160}