Skip to content

Commit

Permalink
Support file IO as well as pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkema committed Apr 28, 2019
1 parent d023506 commit 4f898c2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 30 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -31,10 +31,12 @@ then copy wherever you need.

## Usage

Right now `x12pp` doesn't deal with files directly; it works only with pipelines
through STDIN and STDOUT.
```
$ x12pp < FILE > NEWFILE
$ x12pp FILE -o NEWFILE
```

`$ x12pp < FILE > NEWFILE`
See manpage or `--help` for more.

## Benchmarks

Expand Down
25 changes: 17 additions & 8 deletions man/x12pp.1
Expand Up @@ -3,19 +3,28 @@
x12pp \- pretty printer for X12 EDI files
.SH SYNOPSIS
.B x12pp
< INPUT > OUTPUT
[\fIOPTIONS\fP]... [INPUT] -o OUTPUT
.P
cat [...] |
.B x12pp
| head
.SH DESCRIPTION
.I x12pp
is a filter that pretty-prints X12 files passed through it.

`Pretty printing' in this case means automatically detecting the segment
.P
"Pretty printing" in this case means automatically detecting the segment
terminator from the ISA header and then splitting the input data into
one line per segment.
.SH BUGS
Right now
.I x12pp
only reads from STDIN and outputs to STDOUT. There is no support for
processing files directly.
.TP
.B -h, --help
print a brief usage message
.TP
.B -V, --version
print version information
.TP
.B -o, --output
specify the output file; STDOUT if omitted
.
.SH AUTHOR
Mike Clarke <mike@lambdafunctions.com>
.SH HOME
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
@@ -0,0 +1 @@
max_width = 80
61 changes: 49 additions & 12 deletions src/main.rs
Expand Up @@ -3,9 +3,10 @@ use byteorder::WriteBytesExt; // for write_u8;
extern crate clap;
use memchr::memchr;
use std::{
env,
fs::File,
io,
io::{stdout, BufReader, BufWriter, Read, Write},
io::{BufReader, BufWriter, Read, Write},
os::unix::io::FromRawFd,
process,
};
Expand All @@ -14,11 +15,35 @@ const BUF_SIZE: usize = 16384;
const NL: u8 = 10;
const CR: u8 = 13;

fn run() -> io::Result<()> {
let stdin = unsafe { File::from_raw_fd(0) };
let mut reader = BufReader::with_capacity(BUF_SIZE, stdin);
let stdout = stdout();
let mut writer = BufWriter::with_capacity(BUF_SIZE, stdout.lock());
fn run(input_path: &str, output_path: &str) -> io::Result<()> {
let mut reader = if input_path == "-" {
let stdin = unsafe { File::from_raw_fd(0) };
BufReader::with_capacity(BUF_SIZE, stdin)
} else {
BufReader::with_capacity(
BUF_SIZE,
File::open(input_path).map_err(|e| {
io::Error::new(
e.kind(),
format!("Failed to open '{}': {}", input_path, e),
)
})?,
)
};
let mut writer = if output_path == "-" {
let stdout = unsafe { File::from_raw_fd(1) };
BufWriter::with_capacity(BUF_SIZE, stdout)
} else {
BufWriter::with_capacity(
BUF_SIZE,
File::create(output_path).map_err(|e| {
io::Error::new(
e.kind(),
format!("Failed to create '{}': {}", output_path, e),
)
})?,
)
};

let mut buf = vec![0u8; 106];

Expand Down Expand Up @@ -94,13 +119,25 @@ fn run() -> io::Result<()> {
}

fn main() {
let _matches = clap_app!(x12pp =>
(version: "0.1.0")
(author: "Mike Clarke <mike@lambdafunctions.com>")
(about: "X12 pretty-printer")
).get_matches();
let mut input_path = "-";
let mut output_path = "-";
let matches;

match run() {
if env::args().len() > 1 {
matches = clap_app!(x12pp =>
(version: "0.1.0")
(author: "Mike Clarke <mike@lambdafunctions.com>")
(about: "X12 pretty-printer")
(@arg INPUT: "Input file. Omit or use '-' for STDIN")
(@arg output: -o --output +takes_value "Output file.")
)
.get_matches();

input_path = matches.value_of("INPUT").unwrap_or("-");
output_path = matches.value_of("output").unwrap_or("-");
}

match run(input_path, output_path) {
Ok(_) => {}
Err(ref e) if e.kind() == std::io::ErrorKind::BrokenPipe => {}
Err(err) => {
Expand Down

0 comments on commit 4f898c2

Please sign in to comment.