From b1351ae2b47386914602081f52471bcebc787635 Mon Sep 17 00:00:00 2001 From: user0-07161 Date: Sat, 12 Apr 2025 09:15:04 +0200 Subject: [PATCH] feat: add dos2unix and unix2dos --- coreutils/src/commands/dos2unix.rs | 76 ++++++++++++++++++++++++++++++ coreutils/src/commands/mod.rs | 4 +- coreutils/src/commands/unix2dos.rs | 44 +++++++++++++++++ src/registry.rs | 2 + 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 coreutils/src/commands/dos2unix.rs create mode 100644 coreutils/src/commands/unix2dos.rs diff --git a/coreutils/src/commands/dos2unix.rs b/coreutils/src/commands/dos2unix.rs new file mode 100644 index 0000000..2a73912 --- /dev/null +++ b/coreutils/src/commands/dos2unix.rs @@ -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 { + 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); + } + } +} diff --git a/coreutils/src/commands/mod.rs b/coreutils/src/commands/mod.rs index b8d13c2..4d16f9e 100644 --- a/coreutils/src/commands/mod.rs +++ b/coreutils/src/commands/mod.rs @@ -25,4 +25,6 @@ command!(base32::Base32); command!(seq::Seq); command!(env::Env); command!(ln::Ln); -command!(dirname::Dirname); \ No newline at end of file +command!(dirname::Dirname); +command!(dos2unix::Dos2Unix); +command!(unix2dos::Unix2Dos); diff --git a/coreutils/src/commands/unix2dos.rs b/coreutils/src/commands/unix2dos.rs new file mode 100644 index 0000000..cccbef6 --- /dev/null +++ b/coreutils/src/commands/unix2dos.rs @@ -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); + } + + } +} diff --git a/src/registry.rs b/src/registry.rs index a504308..4a8c550 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -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 });