@@ -13,8 +13,9 @@ extern crate plib;
1313use clap:: Parser ;
1414use gettextrs:: { bind_textdomain_codeset, textdomain} ;
1515use plib:: PROJECT_NAME ;
16- use std:: fs ;
16+ use std:: ffi :: OsStr ;
1717use 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
4344struct 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
0 commit comments