Skip to content

Commit 47f6124

Browse files
authored
Merge pull request #55 from rustcoreutils/hacking
PathBuf conversions
2 parents 1973378 + 7513699 commit 47f6124

File tree

8 files changed

+78
-103
lines changed

8 files changed

+78
-103
lines changed

plib/src/io.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ pub fn input_stream(pathname: &PathBuf, dashed_stdin: bool) -> io::Result<Box<dy
2626
Ok(file)
2727
}
2828

29+
pub fn input_stream_opt(pathname: &Option<PathBuf>) -> io::Result<Box<dyn Read>> {
30+
match pathname {
31+
Some(path) => input_stream(&path, false),
32+
None => input_stream(&PathBuf::new(), false),
33+
}
34+
}
35+
2936
pub fn input_reader(
3037
pathname: &PathBuf,
3138
dashed_stdin: bool,

text/src/asa.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ extern crate plib;
1717
use clap::Parser;
1818
use gettextrs::{bind_textdomain_codeset, gettext, textdomain};
1919
use plib::PROJECT_NAME;
20-
use std::fs;
21-
use std::io::{self, BufRead, Read};
20+
use std::io::{self, BufRead};
21+
use std::path::PathBuf;
2222

2323
/// asa - interpret carriage-control characters
2424
#[derive(Parser, Debug)]
2525
#[command(author, version, about, long_about)]
2626
struct Args {
2727
/// Files to read as input.
28-
files: Vec<String>,
28+
files: Vec<PathBuf>,
2929
}
3030

3131
struct AsaState {
@@ -69,14 +69,8 @@ impl AsaState {
6969
}
7070
}
7171

72-
fn asa_file(filename: &str) -> io::Result<()> {
73-
let file: Box<dyn Read>;
74-
if filename == "" {
75-
file = Box::new(io::stdin().lock());
76-
} else {
77-
file = Box::new(fs::File::open(filename)?);
78-
}
79-
let mut reader = io::BufReader::new(file);
72+
fn asa_file(pathname: &PathBuf) -> io::Result<()> {
73+
let mut reader = plib::io::input_reader(pathname, false)?;
8074
let mut line_no: usize = 0;
8175
let mut state = AsaState::new();
8276

@@ -142,15 +136,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
142136

143137
// if no files, read from stdin
144138
if args.files.is_empty() {
145-
args.files.push(String::new());
139+
args.files.push(PathBuf::new());
146140
}
147141

148142
let mut exit_code = 0;
149143

150144
for filename in &args.files {
151145
if let Err(e) = asa_file(filename) {
152146
exit_code = 1;
153-
eprintln!("{}: {}", filename, e);
147+
eprintln!("{}: {}", filename.display(), e);
154148
}
155149
}
156150

text/src/expand.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ extern crate plib;
1313
use clap::Parser;
1414
use gettextrs::{bind_textdomain_codeset, textdomain};
1515
use plib::PROJECT_NAME;
16-
use std::fs;
1716
use std::io::{self, BufWriter, Read, Write};
17+
use std::path::PathBuf;
1818

1919
/// expand - convert tabs to spaces
2020
#[derive(Parser, Debug)]
@@ -25,7 +25,7 @@ struct Args {
2525
tablist: Option<String>,
2626

2727
/// Files to read as input.
28-
files: Vec<String>,
28+
files: Vec<PathBuf>,
2929
}
3030

3131
enum TabList {
@@ -67,14 +67,9 @@ fn space_out(column: &mut usize, writer: &mut BufWriter<dyn Write>) -> io::Resul
6767
Ok(())
6868
}
6969

70-
fn expand_file(tablist: &TabList, filename: &str) -> io::Result<()> {
70+
fn expand_file(tablist: &TabList, pathname: &PathBuf) -> io::Result<()> {
7171
// open file, or stdin
72-
let mut file: Box<dyn Read>;
73-
if filename == "" {
74-
file = Box::new(io::stdin().lock());
75-
} else {
76-
file = Box::new(fs::File::open(filename)?);
77-
}
72+
let mut file = plib::io::input_stream(pathname, false)?;
7873

7974
let mut raw_buffer = [0; plib::BUFSZ];
8075
let mut writer = BufWriter::new(io::stdout());
@@ -160,15 +155,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
160155

161156
// if no files, read from stdin
162157
if args.files.is_empty() {
163-
args.files.push(String::new());
158+
args.files.push(PathBuf::new());
164159
}
165160

166161
let mut exit_code = 0;
167162

168163
for filename in &args.files {
169164
if let Err(e) = expand_file(&tablist, filename) {
170165
exit_code = 1;
171-
eprintln!("{}: {}", filename, e);
166+
eprintln!("{}: {}", filename.display(), e);
172167
}
173168
}
174169

text/src/fold.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ extern crate plib;
1717
use clap::Parser;
1818
use gettextrs::{bind_textdomain_codeset, textdomain};
1919
use plib::PROJECT_NAME;
20-
use std::fs;
2120
use std::io::{self, Read, Write};
21+
use std::path::PathBuf;
2222

2323
const TABSTOP: usize = 8;
2424

@@ -39,7 +39,7 @@ struct Args {
3939
width: u64,
4040

4141
/// Files to read as input.
42-
files: Vec<String>,
42+
files: Vec<PathBuf>,
4343
}
4444

4545
struct OutputState {
@@ -105,14 +105,9 @@ fn find_last_blank(v: &Vec<u8>) -> Option<usize> {
105105
return None;
106106
}
107107

108-
fn fold_file(args: &Args, filename: &str) -> io::Result<()> {
108+
fn fold_file(args: &Args, pathname: &PathBuf) -> io::Result<()> {
109109
// open file, or stdin
110-
let mut file: Box<dyn Read>;
111-
if filename == "" {
112-
file = Box::new(io::stdin().lock());
113-
} else {
114-
file = Box::new(fs::File::open(filename)?);
115-
}
110+
let mut file = plib::io::input_stream(pathname, false)?;
116111

117112
let mut raw_buffer = [0; plib::BUFSZ];
118113
let mut state = OutputState::new(args);
@@ -185,15 +180,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
185180

186181
// if no files, read from stdin
187182
if args.files.is_empty() {
188-
args.files.push(String::new());
183+
args.files.push(PathBuf::new());
189184
}
190185

191186
let mut exit_code = 0;
192187

193188
for filename in &args.files {
194189
if let Err(e) = fold_file(&args, filename) {
195190
exit_code = 1;
196-
eprintln!("{}: {}", filename, e);
191+
eprintln!("{}: {}", filename.display(), e);
197192
}
198193
}
199194

text/src/tsort.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,22 @@ extern crate plib;
1313
use clap::Parser;
1414
use gettextrs::{bind_textdomain_codeset, textdomain};
1515
use plib::PROJECT_NAME;
16-
use std::fs;
17-
use std::io::{self, BufRead, Read};
16+
use std::io::{self, BufRead};
17+
use std::path::PathBuf;
1818
use topological_sort::TopologicalSort;
1919

2020
/// tsort - topological sort
2121
#[derive(Parser, Debug)]
2222
#[command(author, version, about, long_about)]
2323
struct Args {
2424
/// File to read as input.
25-
file: Option<String>,
25+
file: Option<PathBuf>,
2626
}
2727

28-
fn tsort_file(filename: &str) -> io::Result<()> {
29-
let file: Box<dyn Read>;
30-
if filename == "" {
31-
file = Box::new(io::stdin().lock());
32-
} else {
33-
file = Box::new(fs::File::open(filename)?);
34-
}
35-
28+
fn tsort_file(pathname: &Option<PathBuf>) -> io::Result<()> {
29+
let file = plib::io::input_stream_opt(pathname)?;
3630
let mut reader = io::BufReader::new(file);
31+
3732
let mut ts = TopologicalSort::<String>::new();
3833
let mut sv: Vec<String> = Vec::new();
3934

@@ -65,6 +60,13 @@ fn tsort_file(filename: &str) -> io::Result<()> {
6560
Ok(())
6661
}
6762

63+
fn pathname_display(path: &Option<PathBuf>) -> String {
64+
match path {
65+
None => String::from("stdin"),
66+
Some(p) => p.display().to_string(),
67+
}
68+
}
69+
6870
fn main() -> Result<(), Box<dyn std::error::Error>> {
6971
// parse command line arguments
7072
let args = Args::parse();
@@ -74,14 +76,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
7476

7577
let mut exit_code = 0;
7678

77-
let filename = match &args.file {
78-
None => String::new(),
79-
Some(name) => String::from(name),
80-
};
81-
82-
if let Err(e) = tsort_file(&filename) {
79+
if let Err(e) = tsort_file(&args.file) {
8380
exit_code = 1;
84-
eprintln!("{}: {}", filename, e);
81+
eprintln!("{}: {}", pathname_display(&args.file), e);
8582
}
8683

8784
std::process::exit(exit_code)

xform/src/cksum.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,19 @@ mod crc32;
2323
use clap::Parser;
2424
use gettextrs::{bind_textdomain_codeset, textdomain};
2525
use plib::PROJECT_NAME;
26-
use std::fs;
2726
use std::io::{self, Read};
27+
use std::path::PathBuf;
2828

2929
/// cksum - write file checksums and sizes
3030
#[derive(Parser, Debug)]
3131
#[command(author, version, about, long_about)]
3232
struct Args {
3333
/// Files to read as input. Use "-" or no-args for stdin.
34-
files: Vec<String>,
34+
files: Vec<PathBuf>,
3535
}
3636

37-
fn cksum_file(filename: &str) -> io::Result<()> {
38-
let mut file: Box<dyn Read>;
39-
if filename == "" {
40-
file = Box::new(io::stdin().lock());
41-
} else {
42-
file = Box::new(fs::File::open(filename)?);
43-
}
37+
fn cksum_file(filename: &PathBuf) -> io::Result<()> {
38+
let mut file = plib::io::input_stream(filename, false)?;
4439

4540
let mut buffer = [0; plib::BUFSZ];
4641
let mut n_bytes: u64 = 0;
@@ -57,7 +52,7 @@ fn cksum_file(filename: &str) -> io::Result<()> {
5752
}
5853

5954
let filename_prefix = {
60-
if filename == "" {
55+
if filename.as_os_str() == "" {
6156
""
6257
} else {
6358
" "
@@ -68,7 +63,7 @@ fn cksum_file(filename: &str) -> io::Result<()> {
6863
crc32::finalize(crc, n_bytes as usize),
6964
n_bytes,
7065
filename_prefix,
71-
filename
66+
filename.display()
7267
);
7368

7469
Ok(())
@@ -83,15 +78,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8378

8479
// if no file args, read from stdin
8580
if args.files.is_empty() {
86-
args.files.push(String::new());
81+
args.files.push(PathBuf::new());
8782
}
8883

8984
let mut exit_code = 0;
9085

9186
for filename in &args.files {
9287
if let Err(e) = cksum_file(filename) {
9388
exit_code = 1;
94-
eprintln!("{}: {}", filename, e);
89+
eprintln!("{}: {}", filename.display(), e);
9590
}
9691
}
9792

xform/src/uncompress.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use clap::Parser;
2121
use gettextrs::{bind_textdomain_codeset, textdomain};
2222
use lzw::UnixLZWReader;
2323
use plib::PROJECT_NAME;
24-
use std::fs;
25-
use std::io::{self, Read, Write};
24+
use std::io::{self, Write};
25+
use std::path::PathBuf;
2626

2727
/// uncompress - expand compressed data
2828
#[derive(Parser, Debug)]
@@ -41,17 +41,11 @@ struct Args {
4141
verbose: bool,
4242

4343
/// Files to read as input. Use "-" or no-args for stdin.
44-
files: Vec<String>,
44+
files: Vec<PathBuf>,
4545
}
4646

47-
fn uncompress_file(filename: &str) -> io::Result<()> {
48-
let file: Box<dyn Read>;
49-
if filename == "" {
50-
file = Box::new(io::stdin().lock());
51-
} else {
52-
file = Box::new(fs::File::open(filename)?);
53-
}
54-
47+
fn uncompress_file(pathname: &PathBuf) -> io::Result<()> {
48+
let file = plib::io::input_stream(pathname, false)?;
5549
let mut decoder = UnixLZWReader::new(file);
5650

5751
loop {
@@ -80,7 +74,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8074

8175
// if no file args, read from stdin
8276
if args.files.is_empty() {
83-
args.files.push(String::new());
77+
args.files.push(PathBuf::new());
8478
}
8579

8680
// zcat is a special case: always write to stdout
@@ -93,7 +87,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
9387
for filename in &args.files {
9488
if let Err(e) = uncompress_file(filename) {
9589
exit_code = 1;
96-
eprintln!("{}: {}", filename, e);
90+
eprintln!("{}: {}", filename.display(), e);
9791
}
9892
}
9993

0 commit comments

Comments
 (0)