Skip to content

PathBuf conversions #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions text/src/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use gettextrs::{bind_textdomain_codeset, textdomain};
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, BufRead, Write};
use std::path::PathBuf;

const NO1: u32 = 1 << 0;
const NO2: u32 = 1 << 1;
Expand All @@ -37,10 +38,10 @@ struct Args {
no_dup: bool,

/// Comparison file1
file1: String,
file1: PathBuf,

/// Comparison file2
file2: String,
file2: PathBuf,
}

fn line_out(lead_dup: &'static str, outmask: u32, curtype: u32, s: &str) -> io::Result<()> {
Expand Down Expand Up @@ -75,15 +76,15 @@ fn line_out(lead_dup: &'static str, outmask: u32, curtype: u32, s: &str) -> io::
Ok(())
}

fn open_file(filename: &str) -> io::Result<io::BufReader<fs::File>> {
Ok(io::BufReader::new(fs::File::open(filename)?))
fn open_file(pathname: &PathBuf) -> io::Result<io::BufReader<fs::File>> {
Ok(io::BufReader::new(fs::File::open(pathname)?))
}

fn comm_file(
mask: u32,
lead_dup: &'static str,
file1name: &str,
file2name: &str,
file1name: &PathBuf,
file2name: &PathBuf,
) -> io::Result<()> {
// open files, or stdin
let mut rdr1 = open_file(file1name)?;
Expand Down
21 changes: 11 additions & 10 deletions text/src/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use plib::PROJECT_NAME;
use regex::Regex;
use std::fs::{self, File, OpenOptions};
use std::io::{self, BufRead, Error, ErrorKind, Read, Write};
use std::path::PathBuf;

/// csplit - split files based on context
#[derive(Parser, Debug)]
Expand All @@ -41,7 +42,7 @@ struct Args {
suppress: bool,

/// File to read as input.
filename: String,
filename: PathBuf,

/// Operands defining context on which to split.
operands: Vec<String>,
Expand Down Expand Up @@ -189,7 +190,7 @@ fn csplit_file(args: &Args, ctx: SplitOps, new_files: &mut Vec<String>) -> io::R
let mut split_options = ctx.ops;
// open file, or stdin
let file: Box<dyn Read> = {
if args.filename == "-" {
if args.filename == PathBuf::from("-") {
Box::new(io::stdin().lock())
} else {
Box::new(fs::File::open(&args.filename)?)
Expand Down Expand Up @@ -605,7 +606,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut new_files = vec![];
if let Err(err) = csplit_file(&args, ctx, &mut new_files) {
exit_code = 1;
eprintln!("{}: {}", args.filename, err);
eprintln!("{}: {}", args.filename.display(), err);
if !args.keep {
for file_name in new_files.iter() {
fs::remove_file(file_name).unwrap();
Expand Down Expand Up @@ -803,7 +804,7 @@ mod tests {
keep: false,
num: 2,
suppress: false,
filename: String::from("test.txt"),
filename: PathBuf::from("test.txt"),
operands: vec![
String::from("/pattern/+1"),
String::from("%skip%10"),
Expand Down Expand Up @@ -850,7 +851,7 @@ mod tests {
keep: false,
num: 2,
suppress: false,
filename: String::from("tests/assets/test_file.txt"),
filename: PathBuf::from("tests/assets/test_file.txt"),
operands: vec![String::from("5"), String::from("{3}")],
};

Expand Down Expand Up @@ -900,7 +901,7 @@ mod tests {
keep: false,
num: 2,
suppress: false,
filename: String::from("tests/assets/test_file_c"),
filename: PathBuf::from("tests/assets/test_file_c"),
operands: vec![
String::from(r"%main\(%"),
String::from("/^}/+1"),
Expand Down Expand Up @@ -957,7 +958,7 @@ mod tests {
keep: false,
num: 2,
suppress: false,
filename: String::from("tests/assets/test_file_c"),
filename: PathBuf::from("tests/assets/test_file_c"),
operands: vec![
String::from(r"%main\(%+1"),
String::from("/^}/+1"),
Expand Down Expand Up @@ -1013,7 +1014,7 @@ mod tests {
keep: false,
num: 2,
suppress: false,
filename: String::from("tests/assets/test_file_c"),
filename: PathBuf::from("tests/assets/test_file_c"),
operands: vec![
String::from(r"%main\(%-1"),
String::from("/^}/+1"),
Expand Down Expand Up @@ -1070,7 +1071,7 @@ mod tests {
keep: false,
num: 2,
suppress: false,
filename: String::from("tests/assets/test_file_c"),
filename: PathBuf::from("tests/assets/test_file_c"),
operands: vec![
String::from(r"%main\(%"),
String::from("/^}/"),
Expand Down Expand Up @@ -1127,7 +1128,7 @@ mod tests {
keep: false,
num: 2,
suppress: false,
filename: String::from("tests/assets/test_file_c"),
filename: PathBuf::from("tests/assets/test_file_c"),
operands: vec![
String::from(r"%main\(%"),
String::from("/^}/-1"),
Expand Down
25 changes: 13 additions & 12 deletions text/src/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ extern crate clap;
extern crate plib;
use std::io::{self, BufRead, Error, ErrorKind, Read};

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, textdomain};
use plib::PROJECT_NAME;

use clap::Parser;
use std::path::PathBuf;

/// Cut - cut out selected fields of each line of a file
#[derive(Parser, Debug, Clone)]
Expand Down Expand Up @@ -45,7 +45,7 @@ struct Args {
no_split: bool,

/// Input files
filenames: Vec<String>,
filenames: Vec<PathBuf>,
}

fn validate_args(args: &Args) -> Result<(), String> {
Expand Down Expand Up @@ -292,15 +292,16 @@ fn cut_files(args: Args) -> Result<(), Box<dyn std::error::Error>> {

// open files, or stdin

let readers: Vec<Box<dyn Read>> = if args.filenames.len() == 1 && args.filenames[0] == "-" {
vec![Box::new(io::stdin().lock())]
} else {
let mut bufs: Vec<Box<dyn Read>> = vec![];
for file in &args.filenames {
bufs.push(Box::new(std::fs::File::open(file)?))
}
bufs
};
let readers: Vec<Box<dyn Read>> =
if args.filenames.len() == 1 && args.filenames[0] == PathBuf::from("-") {
vec![Box::new(io::stdin().lock())]
} else {
let mut bufs: Vec<Box<dyn Read>> = vec![];
for file in &args.filenames {
bufs.push(Box::new(std::fs::File::open(file)?))
}
bufs
};

// Process each file
for file in readers {
Expand Down