@@ -13,8 +13,9 @@ extern crate plib;
13
13
use clap:: Parser ;
14
14
use gettextrs:: { bind_textdomain_codeset, textdomain} ;
15
15
use plib:: PROJECT_NAME ;
16
- use std:: fs ;
16
+ use std:: ffi :: OsStr ;
17
17
use std:: io:: { self , BufRead , Read } ;
18
+ use std:: path:: PathBuf ;
18
19
19
20
/// wc - word, line, and byte or character count
20
21
#[ derive( Parser , Debug ) ]
@@ -37,7 +38,7 @@ struct Args {
37
38
words : bool ,
38
39
39
40
/// Files to read as input.
40
- files : Vec < String > ,
41
+ files : Vec < PathBuf > ,
41
42
}
42
43
43
44
struct CountInfo {
@@ -62,7 +63,7 @@ impl CountInfo {
62
63
}
63
64
}
64
65
65
- fn build_display_str ( args : & Args , count : & CountInfo , filename : & str ) -> String {
66
+ fn build_display_str ( args : & Args , count : & CountInfo , filename : & OsStr ) -> String {
66
67
let mut output = String :: with_capacity ( filename. len ( ) + ( 3 * 10 ) ) ;
67
68
68
69
let multi_file = args. files . len ( ) > 1 ;
@@ -104,20 +105,15 @@ fn build_display_str(args: &Args, count: &CountInfo, filename: &str) -> String {
104
105
if filename == "" {
105
106
output. push_str ( "(stdin)" ) ;
106
107
} else {
107
- output. push_str ( filename) ;
108
+ output. push_str ( filename. to_string_lossy ( ) . as_ref ( ) ) ;
108
109
}
109
110
}
110
111
111
112
output
112
113
}
113
114
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 ) ?;
121
117
122
118
let mut buffer = [ 0 ; plib:: BUFSZ ] ;
123
119
let mut in_word = false ;
@@ -161,15 +157,8 @@ fn wc_file_bytes(count: &mut CountInfo, filename: &str) -> io::Result<()> {
161
157
Ok ( ( ) )
162
158
}
163
159
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 ) ?;
173
162
174
163
loop {
175
164
let mut buffer = String :: new ( ) ;
@@ -205,14 +194,19 @@ fn wc_file_chars(args: &Args, count: &mut CountInfo, filename: &str) -> io::Resu
205
194
Ok ( ( ) )
206
195
}
207
196
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 < ( ) > {
209
203
if chars_mode {
210
- wc_file_chars ( args, count, filename ) ?;
204
+ wc_file_chars ( args, count, pathname ) ?;
211
205
} else {
212
- wc_file_bytes ( count, filename ) ?;
206
+ wc_file_bytes ( count, pathname ) ?;
213
207
}
214
208
215
- let output = build_display_str ( & args, count, filename ) ;
209
+ let output = build_display_str ( & args, count, pathname . as_os_str ( ) ) ;
216
210
217
211
println ! ( "{}" , output) ;
218
212
@@ -245,7 +239,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
245
239
if args. files . is_empty ( ) {
246
240
let mut count = CountInfo :: new ( ) ;
247
241
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) {
249
243
exit_code = 1 ;
250
244
eprintln ! ( "stdin: {}" , e) ;
251
245
}
@@ -257,15 +251,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
257
251
258
252
if let Err ( e) = wc_file ( & args, chars_mode, filename, & mut count) {
259
253
exit_code = 1 ;
260
- eprintln ! ( "{}: {}" , filename, e) ;
254
+ eprintln ! ( "{}: {}" , filename. display ( ) , e) ;
261
255
}
262
256
263
257
totals. accum ( & count) ;
264
258
}
265
259
}
266
260
267
261
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" ) ) ;
269
263
println ! ( "{}" , output) ;
270
264
}
271
265
0 commit comments