1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

Combine 4 duplicate seek/skip parsing functions into one

This commit is contained in:
chordtoll 2022-03-17 18:34:36 -07:00 committed by Sylvestre Ledru
parent 72ce815d87
commit a84202ee00
3 changed files with 29 additions and 79 deletions

View file

@ -60,8 +60,9 @@ impl Input<io::Stdin> {
let print_level = parseargs::parse_status_level(matches)?;
let cflags = parseargs::parse_conv_flag_input(matches)?;
let iflags = parseargs::parse_iflags(matches)?;
let skip = parseargs::parse_skip_amt(&ibs, &iflags, matches)?;
let iseek = parseargs::parse_iseek_amt(&ibs, &iflags, matches)?;
let skip = parseargs::parse_seek_skip_amt(&ibs, iflags.skip_bytes, matches, options::SKIP)?;
let iseek =
parseargs::parse_seek_skip_amt(&ibs, iflags.skip_bytes, matches, options::ISEEK)?;
let count = parseargs::parse_count(&iflags, matches)?;
let mut i = Self {
@ -134,8 +135,9 @@ impl Input<File> {
let print_level = parseargs::parse_status_level(matches)?;
let cflags = parseargs::parse_conv_flag_input(matches)?;
let iflags = parseargs::parse_iflags(matches)?;
let skip = parseargs::parse_skip_amt(&ibs, &iflags, matches)?;
let iseek = parseargs::parse_iseek_amt(&ibs, &iflags, matches)?;
let skip = parseargs::parse_seek_skip_amt(&ibs, iflags.skip_bytes, matches, options::SKIP)?;
let iseek =
parseargs::parse_seek_skip_amt(&ibs, iflags.skip_bytes, matches, options::ISEEK)?;
let count = parseargs::parse_count(&iflags, matches)?;
if let Some(fname) = matches.value_of(options::INFILE) {
@ -298,8 +300,9 @@ impl OutputTrait for Output<io::Stdout> {
let obs = parseargs::parse_obs(matches)?;
let cflags = parseargs::parse_conv_flag_output(matches)?;
let oflags = parseargs::parse_oflags(matches)?;
let seek = parseargs::parse_seek_amt(&obs, &oflags, matches)?;
let oseek = parseargs::parse_oseek_amt(&obs, &oflags, matches)?;
let seek = parseargs::parse_seek_skip_amt(&obs, oflags.seek_bytes, matches, options::SEEK)?;
let oseek =
parseargs::parse_seek_skip_amt(&obs, oflags.seek_bytes, matches, options::OSEEK)?;
let mut dst = io::stdout();
@ -517,8 +520,9 @@ impl OutputTrait for Output<File> {
let obs = parseargs::parse_obs(matches)?;
let cflags = parseargs::parse_conv_flag_output(matches)?;
let oflags = parseargs::parse_oflags(matches)?;
let seek = parseargs::parse_seek_amt(&obs, &oflags, matches)?;
let oseek = parseargs::parse_oseek_amt(&obs, &oflags, matches)?;
let seek = parseargs::parse_seek_skip_amt(&obs, oflags.seek_bytes, matches, options::SEEK)?;
let oseek =
parseargs::parse_seek_skip_amt(&obs, oflags.seek_bytes, matches, options::OSEEK)?;
if let Some(fname) = matches.value_of(options::OUTFILE) {
let mut dst = open_dst(Path::new(&fname), &cflags, &oflags)

View file

@ -740,15 +740,15 @@ pub fn parse_oflags(matches: &Matches) -> Result<OFlags, ParseError> {
Ok(oflags)
}
/// Parse the amount of the input file to skip.
pub fn parse_skip_amt(
pub fn parse_seek_skip_amt(
ibs: &usize,
iflags: &IFlags,
bytes: bool,
matches: &Matches,
option: &str,
) -> Result<Option<u64>, ParseError> {
if let Some(amt) = matches.value_of(options::SKIP) {
if let Some(amt) = matches.value_of(option) {
let n = parse_bytes_with_opt_multiplier(amt)?;
if iflags.skip_bytes {
if bytes {
Ok(Some(n))
} else {
Ok(Some(*ibs as u64 * n))
@ -758,60 +758,6 @@ pub fn parse_skip_amt(
}
}
/// Parse the amount of the output file to seek.
pub fn parse_seek_amt(
obs: &usize,
oflags: &OFlags,
matches: &Matches,
) -> Result<Option<u64>, ParseError> {
if let Some(amt) = matches.value_of(options::SEEK) {
let n = parse_bytes_with_opt_multiplier(amt)?;
if oflags.seek_bytes {
Ok(Some(n))
} else {
Ok(Some(*obs as u64 * n))
}
} else {
Ok(None)
}
}
/// Parse the amount of the input file to seek.
pub fn parse_iseek_amt(
ibs: &usize,
iflags: &IFlags,
matches: &Matches,
) -> Result<Option<u64>, ParseError> {
if let Some(amt) = matches.value_of(options::ISEEK) {
let n = parse_bytes_with_opt_multiplier(amt)?;
if iflags.skip_bytes {
Ok(Some(n))
} else {
Ok(Some(*ibs as u64 * n))
}
} else {
Ok(None)
}
}
/// Parse the amount of the input file to seek.
pub fn parse_oseek_amt(
obs: &usize,
oflags: &OFlags,
matches: &Matches,
) -> Result<Option<u64>, ParseError> {
if let Some(amt) = matches.value_of(options::OSEEK) {
let n = parse_bytes_with_opt_multiplier(amt)?;
if oflags.seek_bytes {
Ok(Some(n))
} else {
Ok(Some(*obs as u64 * n))
}
} else {
Ok(None)
}
}
/// Parse the value of count=N and the type of N implied by iflags
pub fn parse_count(iflags: &IFlags, matches: &Matches) -> Result<Option<CountType>, ParseError> {
if let Some(amt) = matches.value_of(options::COUNT) {

View file

@ -142,25 +142,25 @@ fn test_all_top_level_args_no_leading_dashes() {
);
assert_eq!(
200,
parse_skip_amt(&100, &IFlags::default(), &matches)
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::SKIP)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_amt(&100, &OFlags::default(), &matches)
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::SEEK)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_iseek_amt(&100, &IFlags::default(), &matches)
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::ISEEK)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_oseek_amt(&100, &OFlags::default(), &matches)
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::OSEEK)
.unwrap()
.unwrap()
);
@ -241,25 +241,25 @@ fn test_all_top_level_args_with_leading_dashes() {
);
assert_eq!(
200,
parse_skip_amt(&100, &IFlags::default(), &matches)
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::SKIP)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_seek_amt(&100, &OFlags::default(), &matches)
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::SEEK)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_iseek_amt(&100, &IFlags::default(), &matches)
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::ISEEK)
.unwrap()
.unwrap()
);
assert_eq!(
200,
parse_oseek_amt(&100, &OFlags::default(), &matches)
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::OSEEK)
.unwrap()
.unwrap()
);
@ -413,7 +413,7 @@ fn test_override_multiple_options() {
// skip
assert_eq!(
200,
parse_skip_amt(&100, &IFlags::default(), &matches)
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::SKIP)
.unwrap()
.unwrap()
);
@ -421,7 +421,7 @@ fn test_override_multiple_options() {
// seek
assert_eq!(
200,
parse_seek_amt(&100, &OFlags::default(), &matches)
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::SEEK)
.unwrap()
.unwrap()
);
@ -429,7 +429,7 @@ fn test_override_multiple_options() {
// iseek
assert_eq!(
200,
parse_iseek_amt(&100, &IFlags::default(), &matches)
parse_seek_skip_amt(&100, IFlags::default().skip_bytes, &matches, options::ISEEK)
.unwrap()
.unwrap()
);
@ -437,7 +437,7 @@ fn test_override_multiple_options() {
// oseek
assert_eq!(
200,
parse_oseek_amt(&100, &OFlags::default(), &matches)
parse_seek_skip_amt(&100, OFlags::default().seek_bytes, &matches, options::OSEEK)
.unwrap()
.unwrap()
);