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