anyscript_compiler/setup/
lynch.rs1use 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 let mut _liste = Vec::new();
67 for _line in _content.split('\n') {
68 for _char in _line.chars() {
69 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()); }
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', _ => '?', };
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}