1use std::io;
2use std::fs::File;
3use std::io::{BufRead, BufReader};
4#[allow(unused_macros)]
5macro_rules! delete_line_macro {
6 ($path:expr,$($xline:expr),+) => {
7 {
8 use std::io::{BufRead};
9 use std::io::Write;
10 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",$path)).expect("Unable to create file buffer");
11 let mut file = std::io::BufReader::new(std::fs::File::open(format!("{}",$path)).expect("Unable to open"));
12 let mut buf = String::with_capacity(100);
13 let mut i = 1;
14 while file.read_line(&mut buf).expect("Unable to read file") != 0 {
15 match i {
16 $($xline =>{})*
17 _=>{file_ganti.write(buf.as_bytes()).expect("");}
18 }
19 buf.clear();
20 i = i + 1;
21 }
22 }
23 std::fs::rename(format!("{}._pengganti",$path), format!("{}",$path)).expect("Unable to read");
24 };
25 ($path:expr,$line:expr => $xline:expr )=>{
26 {
27 use std::io::{BufRead};
28 use std::io::Write;
29 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",$path)).expect("Unable to create file buffer");
30 let mut file = std::io::BufReader::new(std::fs::File::open(format!("{}",$path)).expect("Unable to open"));
31 let mut buf = String::with_capacity(100);
32 let mut i = 1;
33 while file.read_line(&mut buf).expect("Unable to read file") != 0 {
34 match i {
35 $line..=$xline =>{}
36 _=>{file_ganti.write(buf.as_bytes()).expect("Unable to write file");}
37 }
38 buf.clear();
39 i = i + 1;
40 }
41 }
42 std::fs::rename(format!("{}._pengganti",$path), format!("{}",$path)).expect("Unable to read");
43 }
44}
45#[allow(unused_macros)]
46macro_rules! read_line_index_macro {
47 ($path:expr,$($xline:expr),+) => {
48 {
49 let mut file = std::io::BufReader::new(std::fs::File::open(format!("{}",$path)).expect("Unable to open file"));
50 let mut buf = String::with_capacity(100);
51 let mut buf2 = String::with_capacity(100);
52 let mut i = 1;
53 use std::io::{BufRead};
54 while file.read_line(&mut buf).expect("Unable to read file") != 0
55 {
56 $(if i == $xline {buf2.push_str(buf.as_str())})*
57 buf.clear();
58 i = i + 1
59 }
60 buf2
61 }
62 };
63 ($path:expr,$line:expr => $xline:expr )=>{
64 {
65 let mut file = std::io::BufReader::new(std::fs::File::open(format!("{}",$path)).expect("Unable to open file"));
66 let mut buf = String::with_capacity(100);
67 let mut buf2 = String::with_capacity(100);
68 let mut i = 1;
69 use std::io::{BufRead};
70 while file.read_line(&mut buf).expect("Unable to read") != 0
71 {
72 match i {
73 $line..=$xline =>{buf2.push_str(buf.as_str())}
74 _=>{}
75 }
76 buf.clear();
77 i = i + 1
78 }
79 buf2
80 }
81 };
82}
83#[allow(unused_macros)]
84macro_rules! replase_line_macro{
85 ($path:expr,$($xline:expr,$line:expr),+) => {
86 {
87 use std::io::Write;
88 use std::io::{BufRead};
89 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",$path)).expect("Unable to create buffer file");
90 let mut file = std::io::BufReader::new(std::fs::File::open(format!("{}",$path)).expect("Unable to open file"));
91 let mut buf = String::with_capacity(100);
92 let mut i = 1;
93 while file.read_line(&mut buf).expect("") != 0 {
94 match i {
95 $(n if n == $line =>{file_ganti.write($xline.as_bytes()).expect("Unable to write file");})*
96 _=>{file_ganti.write(buf.as_bytes()).expect("Unable to write file");}
97 }
98 buf.clear();
99 i = i + 1;
100 }
101 }
102 std::fs::rename(format!("{}._pengganti",$path), format!("{}",$path)).expect("Unable to rename ._pengganti");
103 };
104}
105#[allow(unused_imports)]
106pub(crate) use delete_line_macro;
107#[allow(unused_imports)]
108pub(crate) use read_line_index_macro;
109#[allow(unused_imports)]
110pub(crate) use replase_line_macro;
111#[allow(dead_code)]
112pub fn delete_line(path:&str,line:usize)->io::Result<()>{
113 {
114 use std::io::Write;
115 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",path))?;
116 let mut file = std::io::BufReader::new(std::fs::File::open(format!("{}",path))?);
117 let mut buf = String::with_capacity(100);
118 let mut i = 1;
119 while file.read_line(&mut buf)? != 0 {
120 if i != line { file_ganti.write(buf.as_bytes())?;}
121 buf.clear();
122 i = i + 1;
123 }
124 }
125 std::fs::rename(format!("{}._pengganti",path), format!("{}",path))?;
126 Ok(())
127}
128#[allow(dead_code)]
129pub fn delete_line_with_capacity(path:&str,line:usize,capacity:usize)->io::Result<()>{
130 {
131 use std::io::Write;
132 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",path))?;
133 let mut file = std::io::BufReader::with_capacity(capacity,std::fs::File::open(format!("{}",path))?);
134 let mut buf = String::with_capacity(capacity);
135 let mut i = 1;
136 while file.read_line(&mut buf)? != 0 {
137 if i != line { file_ganti.write(buf.as_bytes())?;}
138 buf.clear();
139 i = i + 1;
140 }
141 }
142 std::fs::rename(format!("{}._pengganti",path), format!("{}",path))?;
143 Ok(())
144}
145#[allow(dead_code)]
146pub fn read_line_index(path:&str,line:usize)-> io::Result<String>{
147 use std::io::{Error, ErrorKind};
148 let mut file = BufReader::new(File::open(format!("{}",path))?);
149 let mut buf = String::with_capacity(100);
150 let mut i = 1;
151 while file.read_line(&mut buf)? != 0
152 {
153 if i == line {
154 return Ok(buf)
155 }
156 buf.clear();
157 i = i + 1
158 }
159 Err(Error::new(ErrorKind::Other, "line don't exist"))
160}
161#[allow(dead_code)]
162pub fn read_line_index_with_capacity(path:&str,line:usize,capacity: usize)-> io::Result<String>{
163 use std::io::{Error, ErrorKind};
164 let mut file = BufReader::with_capacity(capacity,File::open(format!("{}",path))?);
165 let mut buf = String::with_capacity(capacity);
166 let mut i = 1;
167 while file.read_line(&mut buf)? != 0
168 {
169 if i == line {
170 return Ok(buf)
171 }
172 buf.clear();
173 i = i + 1
174 }
175 Err(Error::new(ErrorKind::Other, "line don't exist"))
176}
177#[allow(dead_code)]
178pub fn replase_line(path:&str,replase:&str,line:usize)->io::Result<()>{
179 {
180 use std::io::Write;
181 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",path))?;
182 let mut file = std::io::BufReader::new(std::fs::File::open(format!("{}",path))?);
183 let mut buf = String::with_capacity(100);
184 let mut i = 1;
185 while file.read_line(&mut buf)? != 0 {
186 match i {
187 n if n == line =>{file_ganti.write(replase.as_bytes())?;}
188 _=>{file_ganti.write(buf.as_bytes())?;}
189 }
190 buf.clear();
191 i = i + 1;
192 }
193 }
194 std::fs::rename(format!("{}._pengganti",path), format!("{}",path))?;
195 Ok(())
196}
197#[allow(dead_code)]
198pub fn replase_line_with_capacity(path:&str,replase:&str,line:usize,capacity: usize)->io::Result<()>{
199 {
200 use std::io::Write;
201 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",path))?;
202 let mut file = std::io::BufReader::with_capacity(capacity,std::fs::File::open(format!("{}",path))?);
203 let mut buf = String::with_capacity(capacity);
204 let mut i = 1;
205 while file.read_line(&mut buf)? != 0 {
206 match i {
207 n if n == line =>{file_ganti.write(replase.as_bytes())?;}
208 _=>{file_ganti.write(buf.as_bytes())?;}
209 }
210 buf.clear();
211 i = i + 1;
212 }
213 }
214 std::fs::rename(format!("{}._pengganti",path), format!("{}",path))?;
215 Ok(())
216}
217#[allow(dead_code)]
218pub fn rotate(path:&str,lop:usize) ->io::Result<()> {
219 use std::io::Write;
220 for _ in 0..lop {
221 let mut buf2 = String::with_capacity(100);
222 {
223 let mut file = BufReader::new(File::open(format!("{}",path))?);
224 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",path))?;
225 if file.read_line(&mut buf2)? != 0 {
226 let mut buf = String::with_capacity(100);
227 while file.read_line(&mut buf)? != 0 {
228 file_ganti.write(buf.as_bytes())?;
229 buf.clear()
230 }
231 }
232 file_ganti.write(buf2.as_bytes())?;
233 }
234 std::fs::rename(format!("{}._pengganti",path), format!("{}",path))?;
235 }
236 Ok(())
237}
238#[allow(dead_code)]
239pub fn rotate_with_capacity(path:&str,lop:usize,capacity: usize) ->io::Result<()> {
240 use std::io::Write;
241 for _ in 0..lop {
242 let mut buf2 = String::with_capacity(100);
243 {
244 let mut file = BufReader::with_capacity(capacity,File::open(format!("{}",path))?);
245 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",path))?;
246 if file.read_line(&mut buf2)? != 0 {
247 let mut buf = String::with_capacity(capacity);
248 while file.read_line(&mut buf)? != 0 {
249 file_ganti.write(buf.as_bytes())?;
250 buf.clear()
251 }
252 }
253 file_ganti.write(buf2.as_bytes())?;
254 }
255 std::fs::rename(format!("{}._pengganti",path), format!("{}",path))?;
256 }
257 Ok(())
258}
259#[allow(dead_code)]
260pub fn len(path:&str) ->io::Result<usize> {
261 let mut i :usize = 0;
262 for _ in BufReader::new(File::open(path)?).lines() {
263 i = i + 1
264 }
265 Ok(i)
266}
267#[allow(dead_code)]
268pub fn pop(path:&str) ->io::Result<()> {
269 delete_line(path, len(path)?)?;
270 Ok(())
271}
272#[allow(dead_code)]
273pub fn pust(path:&str,_str:&str) ->io::Result<()> {
274 {
275 use std::io::Write;
276 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",path))?;
277 let mut file = BufReader::new(File::open(format!("{}",path))?);
278 let mut buf = String::with_capacity(100);
279 while file.read_line(&mut buf)? != 0 {
280 file_ganti.write(buf.as_bytes())?;
281 buf.clear()
282 }
283 file_ganti.write(_str.as_bytes())?;
284 }
285 std::fs::rename(format!("{}._pengganti",path), format!("{}",path))?;
286 Ok(())
287}
288#[allow(dead_code)]
289pub fn pust_with_capacity(path:&str,_str:&str,capacity: usize) ->io::Result<()> {
290 {
291 use std::io::Write;
292 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",path))?;
293 let mut file = BufReader::with_capacity(capacity,File::open(format!("{}",path))?);
294 let mut buf = String::with_capacity(capacity);
295 while file.read_line(&mut buf)? != 0 {
296 file_ganti.write(buf.as_bytes())?;
297 buf.clear()
298 }
299 file_ganti.write(_str.as_bytes())?;
300 }
301 std::fs::rename(format!("{}._pengganti",path), format!("{}",path))?;
302 Ok(())
303}
304#[allow(dead_code)]
305pub struct FileIndexing{
306 pub file:String,
307}
308#[allow(dead_code)]
309impl FileIndexing {
310 pub fn len(&self) ->io::Result<usize>{
311 len(&self.file)
312 }
313 pub fn index(&self,indx:usize)->io::Result<String>{
314 read_line_index(&self.file,indx)
315 }
316 pub fn pop(&self)->io::Result<()>{
317 pop(&self.file)
318 }
319 pub fn rotate(&self,lop: usize) ->io::Result<()>{
320 rotate(&self.file, lop)
321 }
322 pub fn pust_str(&self,_str: &str)->io::Result<()>{
323 pust(&self.file, _str)
324 }
325 pub fn replase_line(&self,replase:&str,line:usize)->io::Result<()>{
326 replase_line(&self.file, replase, line)
327 }
328 pub fn readto_string(&self)->io::Result<String>{
329 std::fs::read_to_string(&self.file)
330 }
331 pub fn delete(self)->io::Result<()>{
332 std::fs::remove_file(self.file)
333 }
334 pub fn is_empty(&self)->bool{
335 std::fs::read_to_string(&self.file).unwrap().is_empty()
336 }
337}
338#[allow(non_camel_case_types)]
339#[allow(dead_code)]
340pub struct FileIndexing_with_capacity{
341 pub file:String,
342 pub capacity:usize,
343 pub buf:String
344}
345#[allow(dead_code)]
346impl FileIndexing_with_capacity {
347 pub fn len(&self) ->io::Result<usize>{
348 let mut i :usize = 0;
349 for _ in BufReader::with_capacity(self.capacity,File::open(&self.file)?).lines() {
350 i = i + 1
351 }
352 Ok(i)
353 }
354 pub fn index(&mut self,indx:usize)->io::Result<String>{
355 use std::io::{Error, ErrorKind};
356 let mut file = BufReader::with_capacity(self.capacity,File::open(format!("{}",self.file))?);
357
358 let mut i = 1;
359 while file.read_line(&mut self.buf)? != 0
360 {
361 if i == indx {
362 self.buf.clear();
363 return Ok(self.buf.clone())
364 }
365 self.buf.clear();
366 i = i + 1
367 }
368 self.buf.clear();
369 Err(Error::new(ErrorKind::Other, "line don't exist"))
370 }
371 pub fn pop(&self)->io::Result<()>{
372 delete_line_with_capacity(&self.file, self.len()?,self.capacity)?;
373 Ok(())
374 }
375 pub fn rotate(&mut self,lop: usize) ->io::Result<()>{
376 use std::io::Write;
377 for _ in 0..lop {
378 let mut buf2 = String::with_capacity(100);
379 {
380 let mut file = BufReader::with_capacity(self.capacity,File::open(format!("{}",self.file))?);
381 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",self.file))?;
382 if file.read_line(&mut buf2)? != 0 {
383
384 while file.read_line(&mut self.buf)? != 0 {
385 file_ganti.write(self.buf.as_bytes())?;
386 self.buf.clear()
387 }
388 self.buf.clear()
389 }
390 file_ganti.write(buf2.as_bytes())?;
391 }
392 std::fs::rename(format!("{}._pengganti",self.file), format!("{}",self.file))?;
393 }
394 Ok(())
395 }
396 pub fn pust_str(&mut self,_str: &str)->io::Result<()>{
397 {
398 use std::io::Write;
399 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",self.file))?;
400 let mut file = BufReader::with_capacity(self.capacity,File::open(format!("{}",self.file))?);
401 while file.read_line(&mut self.buf)? != 0 {
402 file_ganti.write(self.buf.as_bytes())?;
403 self.buf.clear()
404 }
405 self.buf.clear();
406 file_ganti.write(_str.as_bytes())?;
407 }
408 std::fs::rename(format!("{}._pengganti",self.file), format!("{}",self.file))?;
409 Ok(())
410 }
411 pub fn replase_line(&mut self,replase:&str,line:usize)->io::Result<()>{
412 {
413 use std::io::Write;
414 let mut file_ganti = std::fs::File::create(format!("{}._pengganti",self.file))?;
415 let mut file = std::io::BufReader::with_capacity(self.capacity,std::fs::File::open(format!("{}",self.file))?);
416
417 let mut i = 1;
418 while file.read_line(&mut self.buf)? != 0 {
419 match i {
420 n if n == line =>{file_ganti.write(replase.as_bytes())?;}
421 _=>{file_ganti.write(self.buf.as_bytes())?;}
422 }
423 self.buf.clear();
424 i = i + 1;
425 }
426 }
427 self.buf.clear();
428 std::fs::rename(format!("{}._pengganti",self.file), format!("{}",self.file))?;
429 Ok(())
430 }
431 pub fn readto_string(&self)->io::Result<String>{
432 std::fs::read_to_string(&self.file)
433 }
434 pub fn delete(self)->io::Result<()>{
435 std::fs::remove_file(self.file)
436 }
437 pub fn is_empty(&self)->bool{
438 std::fs::read_to_string(&self.file).unwrap().is_empty()
439 }
440}