feat: add dos2unix and unix2dos
This commit is contained in:
parent
9b73d54905
commit
b1351ae2b4
4 changed files with 125 additions and 1 deletions
76
coreutils/src/commands/dos2unix.rs
Normal file
76
coreutils/src/commands/dos2unix.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
use boxutils::args::ArgParser;
|
||||
use boxutils::commands::Command;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufReader, Read, Write};
|
||||
use std::process::exit;
|
||||
|
||||
pub fn convert(arguments: &ArgParser, d2u: bool) -> Vec<u8> {
|
||||
let mut vecbuf = Vec::new();
|
||||
|
||||
if arguments.get_normal_args().len() == 0 {
|
||||
let _ = io::stdin().read_to_end(&mut vecbuf);
|
||||
}
|
||||
|
||||
for file in arguments.get_normal_args().iter() {
|
||||
let mut tmpbuf = Vec::new();
|
||||
let mut read = BufReader::new(File::open(file).unwrap());
|
||||
let _ = read.read_to_end(&mut tmpbuf);
|
||||
let _ = vecbuf.append(&mut tmpbuf);
|
||||
}
|
||||
|
||||
if d2u {
|
||||
vecbuf.retain(
|
||||
|x|
|
||||
*x != b'\r'
|
||||
);
|
||||
} else {
|
||||
let mut tmpbuf = Vec::new();
|
||||
vecbuf.iter().enumerate().for_each(|(i, &b)| {
|
||||
if b == b'\n' && i > 0 && vecbuf[i - 1] != b'\r' {
|
||||
tmpbuf.push(b'\r');
|
||||
}
|
||||
tmpbuf.push(b);
|
||||
});
|
||||
vecbuf = tmpbuf;
|
||||
}
|
||||
|
||||
vecbuf
|
||||
}
|
||||
|
||||
pub struct Dos2Unix;
|
||||
|
||||
impl Command for Dos2Unix {
|
||||
fn execute(&self) {
|
||||
let args = ArgParser::builder()
|
||||
.add_flag("-u")
|
||||
.add_flag("-d")
|
||||
.add_flag("--help")
|
||||
.parse_args("dos2unix");
|
||||
|
||||
let mut dos2unix = true;
|
||||
|
||||
if args.get_flag("-u") {
|
||||
dos2unix = true;
|
||||
}
|
||||
|
||||
if args.get_flag("-d") {
|
||||
dos2unix = false;
|
||||
}
|
||||
|
||||
if args.get_flag("--help") {
|
||||
println!("Usage: dos2unix [-d] [-u] [FILE]");
|
||||
print!("\n");
|
||||
println!("-d: unix2dos");
|
||||
println!("-u: dos2unix (default)");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
let result = convert(&args, dos2unix);
|
||||
|
||||
if args.get_normal_args().len() < 1 {
|
||||
let _ = io::stdout().write_all(&result);
|
||||
} else {
|
||||
let _ = std::fs::write(args.get_normal_args()[0].clone(), &result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -25,4 +25,6 @@ command!(base32::Base32);
|
|||
command!(seq::Seq);
|
||||
command!(env::Env);
|
||||
command!(ln::Ln);
|
||||
command!(dirname::Dirname);
|
||||
command!(dirname::Dirname);
|
||||
command!(dos2unix::Dos2Unix);
|
||||
command!(unix2dos::Unix2Dos);
|
||||
|
|
|
|||
44
coreutils/src/commands/unix2dos.rs
Normal file
44
coreutils/src/commands/unix2dos.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use crate::commands::dos2unix::convert;
|
||||
use boxutils::args::ArgParser;
|
||||
use boxutils::commands::Command;
|
||||
use std::io::{self, Write};
|
||||
use std::process::exit;
|
||||
|
||||
pub struct Unix2Dos;
|
||||
|
||||
impl Command for Unix2Dos {
|
||||
fn execute(&self) {
|
||||
let args = ArgParser::builder()
|
||||
.add_flag("-u")
|
||||
.add_flag("-d")
|
||||
.add_flag("--help")
|
||||
.parse_args("unix2dos");
|
||||
|
||||
let mut dos2unix = false;
|
||||
|
||||
if args.get_flag("-u") {
|
||||
dos2unix = true;
|
||||
}
|
||||
|
||||
if args.get_flag("-d") {
|
||||
dos2unix = false;
|
||||
}
|
||||
|
||||
if args.get_flag("--help") {
|
||||
println!("Usage: unix2dos [-d] [-u] [FILE]");
|
||||
println!("\n");
|
||||
println!("-d: unix2dos (default)");
|
||||
println!("-u: dos2unix");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
let result = convert(&args, dos2unix);
|
||||
|
||||
if args.get_normal_args().len() < 1 {
|
||||
let _ = io::stdout().write_all(&result);
|
||||
} else {
|
||||
let _ = std::fs::write(args.get_normal_args()[0].clone(), &result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -36,6 +36,8 @@ pub fn get_registry() -> CommandRegistry {
|
|||
"env" => coreutils::commands::Env,
|
||||
"ln" => coreutils::commands::Ln,
|
||||
"dirname" => coreutils::commands::Dirname,
|
||||
"dos2unix" => coreutils::commands::Dos2Unix,
|
||||
"unix2dos" => coreutils::commands::Unix2Dos,
|
||||
"box" => Boxcmd
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue