Skip to content

Commit c41ef16

Browse files
authored
Merge pull request #56 from rustcoreutils/hacking
PathBuf conversions
2 parents 47f6124 + 444055f commit c41ef16

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed

text/src/wc.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ extern crate plib;
1313
use clap::Parser;
1414
use gettextrs::{bind_textdomain_codeset, textdomain};
1515
use plib::PROJECT_NAME;
16-
use std::fs;
16+
use std::ffi::OsStr;
1717
use std::io::{self, BufRead, Read};
18+
use std::path::PathBuf;
1819

1920
/// wc - word, line, and byte or character count
2021
#[derive(Parser, Debug)]
@@ -37,7 +38,7 @@ struct Args {
3738
words: bool,
3839

3940
/// Files to read as input.
40-
files: Vec<String>,
41+
files: Vec<PathBuf>,
4142
}
4243

4344
struct CountInfo {
@@ -62,7 +63,7 @@ impl CountInfo {
6263
}
6364
}
6465

65-
fn build_display_str(args: &Args, count: &CountInfo, filename: &str) -> String {
66+
fn build_display_str(args: &Args, count: &CountInfo, filename: &OsStr) -> String {
6667
let mut output = String::with_capacity(filename.len() + (3 * 10));
6768

6869
let multi_file = args.files.len() > 1;
@@ -104,20 +105,15 @@ fn build_display_str(args: &Args, count: &CountInfo, filename: &str) -> String {
104105
if filename == "" {
105106
output.push_str("(stdin)");
106107
} else {
107-
output.push_str(filename);
108+
output.push_str(filename.to_string_lossy().as_ref());
108109
}
109110
}
110111

111112
output
112113
}
113114

114-
fn wc_file_bytes(count: &mut CountInfo, filename: &str) -> io::Result<()> {
115-
let mut file: Box<dyn Read>;
116-
if filename == "" {
117-
file = Box::new(io::stdin().lock());
118-
} else {
119-
file = Box::new(fs::File::open(filename)?);
120-
}
115+
fn wc_file_bytes(count: &mut CountInfo, pathname: &PathBuf) -> io::Result<()> {
116+
let mut file = plib::io::input_stream(pathname, false)?;
121117

122118
let mut buffer = [0; plib::BUFSZ];
123119
let mut in_word = false;
@@ -161,15 +157,8 @@ fn wc_file_bytes(count: &mut CountInfo, filename: &str) -> io::Result<()> {
161157
Ok(())
162158
}
163159

164-
fn wc_file_chars(args: &Args, count: &mut CountInfo, filename: &str) -> io::Result<()> {
165-
let file: Box<dyn Read>;
166-
if filename == "" {
167-
file = Box::new(io::stdin().lock());
168-
} else {
169-
file = Box::new(fs::File::open(filename)?);
170-
}
171-
172-
let mut reader = io::BufReader::new(file);
160+
fn wc_file_chars(args: &Args, count: &mut CountInfo, pathname: &PathBuf) -> io::Result<()> {
161+
let mut reader = plib::io::input_reader(pathname, false)?;
173162

174163
loop {
175164
let mut buffer = String::new();
@@ -205,14 +194,19 @@ fn wc_file_chars(args: &Args, count: &mut CountInfo, filename: &str) -> io::Resu
205194
Ok(())
206195
}
207196

208-
fn wc_file(args: &Args, chars_mode: bool, filename: &str, count: &mut CountInfo) -> io::Result<()> {
197+
fn wc_file(
198+
args: &Args,
199+
chars_mode: bool,
200+
pathname: &PathBuf,
201+
count: &mut CountInfo,
202+
) -> io::Result<()> {
209203
if chars_mode {
210-
wc_file_chars(args, count, filename)?;
204+
wc_file_chars(args, count, pathname)?;
211205
} else {
212-
wc_file_bytes(count, filename)?;
206+
wc_file_bytes(count, pathname)?;
213207
}
214208

215-
let output = build_display_str(&args, count, filename);
209+
let output = build_display_str(&args, count, pathname.as_os_str());
216210

217211
println!("{}", output);
218212

@@ -245,7 +239,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
245239
if args.files.is_empty() {
246240
let mut count = CountInfo::new();
247241

248-
if let Err(e) = wc_file(&args, chars_mode, "", &mut count) {
242+
if let Err(e) = wc_file(&args, chars_mode, &PathBuf::new(), &mut count) {
249243
exit_code = 1;
250244
eprintln!("stdin: {}", e);
251245
}
@@ -257,15 +251,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
257251

258252
if let Err(e) = wc_file(&args, chars_mode, filename, &mut count) {
259253
exit_code = 1;
260-
eprintln!("{}: {}", filename, e);
254+
eprintln!("{}: {}", filename.display(), e);
261255
}
262256

263257
totals.accum(&count);
264258
}
265259
}
266260

267261
if args.files.len() > 1 {
268-
let output = build_display_str(&args, &totals, "total");
262+
let output = build_display_str(&args, &totals, &OsStr::new("total"));
269263
println!("{}", output);
270264
}
271265

xform/src/uudecode.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,41 @@ use base64::prelude::*;
1515
use clap::Parser;
1616
use gettextrs::{bind_textdomain_codeset, textdomain};
1717
use plib::PROJECT_NAME;
18-
use std::fs::{self, OpenOptions};
18+
use std::fs::OpenOptions;
1919
use std::io::{self, Error, ErrorKind, Read, Write};
20+
use std::path::PathBuf;
2021

2122
/// uudecode - decode a binary file
2223
#[derive(Parser, Debug)]
2324
#[command(author, version, about, long_about)]
2425
struct Args {
2526
/// A pathname of a file that shall be used instead of any pathname contained in the input data.
2627
#[arg(short, long)]
27-
outfile: Option<String>,
28+
outfile: Option<PathBuf>,
2829

29-
/// The pathname of a file containing the output of uuencode.
30-
file: Option<String>,
30+
/// The pathname of a file containing uuencoded data.
31+
file: Option<PathBuf>,
3132
}
3233

33-
fn write_file(filename: &str, bindata: &[u8]) -> io::Result<()> {
34+
fn write_file(pathname: &PathBuf, bindata: &[u8]) -> io::Result<()> {
3435
let f_res = OpenOptions::new()
3536
.read(false)
3637
.write(true)
3738
.create(true)
3839
.truncate(true)
39-
.open(filename);
40+
.open(pathname);
4041

4142
match f_res {
4243
Err(e) => {
43-
eprintln!("{}: {}", filename, e);
44+
eprintln!("{}: {}", pathname.display(), e);
4445
return Err(e);
4546
}
4647
Ok(mut file) => file.write_all(bindata),
4748
}
4849
}
4950

5051
fn decode_file(args: &Args) -> io::Result<()> {
51-
let mut file: Box<dyn Read>;
52-
if let Some(filename) = &args.file {
53-
file = Box::new(fs::File::open(filename)?);
54-
} else {
55-
file = Box::new(io::stdin().lock());
56-
}
52+
let mut file = plib::io::input_stream_opt(&args.file)?;
5753

5854
// read entire file into memory.
5955
// ugly but necessary due to uudecode crate implementation.
@@ -68,7 +64,7 @@ fn decode_file(args: &Args) -> io::Result<()> {
6864

6965
// decode succeeded. exit here.
7066
Ok(bindata) => match &args.outfile {
71-
None => return write_file("bindata.out", &bindata[..]),
67+
None => return write_file(&PathBuf::from("bindata.out"), &bindata[..]),
7268
Some(outfn) => return write_file(outfn, &bindata[..]),
7369
},
7470
}
@@ -77,12 +73,19 @@ fn decode_file(args: &Args) -> io::Result<()> {
7773
match uuencode::uudecode(&buffer) {
7874
None => return Err(Error::new(ErrorKind::Other, "invalid input data")),
7975
Some((bindata, filename)) => match &args.outfile {
80-
None => write_file(&filename, &bindata[..]),
76+
None => write_file(&PathBuf::from(filename), &bindata[..]),
8177
Some(outfn) => write_file(outfn, &bindata[..]),
8278
},
8379
}
8480
}
8581

82+
fn pathname_display(path: &Option<PathBuf>) -> String {
83+
match path {
84+
None => "stdin".to_string(),
85+
Some(p) => p.display().to_string(),
86+
}
87+
}
88+
8689
fn main() -> Result<(), Box<dyn std::error::Error>> {
8790
// parse command line arguments
8891
let args = Args::parse();
@@ -94,7 +97,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
9497

9598
if let Err(e) = decode_file(&args) {
9699
exit_code = 1;
97-
eprintln!("{:?}: {}", args.file, e);
100+
eprintln!("{:?}: {}", pathname_display(&args.file), e);
98101
}
99102

100103
std::process::exit(exit_code)

0 commit comments

Comments
 (0)