-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
630 lines (528 loc) · 19.1 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
A library for iterating through the lines in a series of
files. Very similar to [the Python module of the same
name](http://docs.python.org/3.3/library/fileinput.html).
It allows the programmer to automatically take filenames from the
command line arguments (via `input` and `input_state`), as well as
specify them as a vector directly (`input_vec` and
`input_vec_state`). The files are opened as necessary, so any files
that can't be opened only cause an error when reached in the
iteration.
On the command line, `stdin` is represented by a filename of `-` (a
single hyphen) and in the functions that take a vector directly
(e.g. `input_vec`) it is represented by `None`. Note `stdin` is *not*
reset once it has been finished, so attempting to iterate on `[None,
None]` will only take input once unless `io::stdin().seek(0, SeekSet)`
is called between.
The `make_path_option_vec` function handles converting a list of file paths as
strings to the appropriate format, including the (optional) conversion
of `"-"` to `stdin`.
# Basic
In many cases, one can use the `input_*` functions without having
to handle any `FileInput` structs. E.g. a simple `cat` program
do input |line| {
io::println(line)
}
or a program that numbers lines after concatenating two files
do input_vec_state(make_path_option_vec([~"a.txt", ~"b.txt"])) |line, state| {
io::println(format!("{}: %s", state.line_num,
line));
}
The two `input_vec*` functions take a vec of file names (where empty
means read from `stdin`), the other two functions use the command line
arguments.
# Advanced
For more complicated uses (e.g. if one needs to pause iteration and
resume it later), a `FileInput` instance can be constructed via the
`from_vec`, `from_vec_raw` and `from_args` functions.
Once created, the `each_line` (from the `std::io::ReaderUtil` trait)
and `each_line_state` methods allow one to iterate on the lines; the
latter provides more information about the position within the
iteration to the caller.
It is possible (and safe) to skip lines and files using the
`read_line` and `next_file` methods. Also, `FileInput` implements
`std::io::Reader`, and the state will be updated correctly while
using any of those methods.
E.g. the following program reads until an empty line, pauses for user
input, skips the current file and then numbers the remaining lines
(where the numbers are from the start of each file, rather than the
total line count).
let input = FileInput::from_vec(
make_path_option_vec([~"a.txt", ~"b.txt", ~"c.txt"], true)
);
do input.each_line |line| {
if line.is_empty() {
break
}
io::println(line);
}
io::println("Continue?");
if io::stdin().read_line() == ~"yes" {
input.next_file(); // skip!
do input.each_line_state |line, state| {
io::println(format!("{}: %s", state.line_num_file,
line))
}
}
*/
#[allow(missing_doc)];
use std::io::ReaderUtil;
use std::io;
use std::os;
/**
A summary of the internal state of a `FileInput` object. `line_num`
and `line_num_file` represent the number of lines read in total and in
the current file respectively. `current_path` is `None` if the current
file is `stdin`.
*/
#[deriving(Clone)]
pub struct FileInputState {
current_path: Option<Path>,
line_num: uint,
line_num_file: uint
}
impl FileInputState {
fn is_stdin(&self) -> bool {
self.current_path.is_none()
}
fn is_first_line(&self) -> bool {
self.line_num_file == 1
}
}
struct FileInput_ {
/**
`Some(path)` is the file represented by `path`, `None` is
`stdin`. Consumed as the files are read.
*/
files: ~[Option<Path>],
/**
The current file: `Some(r)` for an open file, `None` before
starting and after reading everything.
*/
current_reader: Option<@io::Reader>,
state: FileInputState,
/**
Used to keep track of whether we need to insert the newline at the
end of a file that is missing it, which is needed to separate the
last and first lines.
*/
previous_was_newline: bool
}
// FIXME #5723: remove this when Reader has &mut self.
// Removing it would mean giving read_byte in the Reader impl for
// FileInput &mut self, which in turn means giving most of the
// io::Reader trait methods &mut self. That can't be done right now
// because of io::with_bytes_reader and #5723.
// Should be removable via
// "self.fi" -> "self." and renaming FileInput_. Documentation above
// will likely have to be updated to use `let mut in = ...`.
pub struct FileInput {
fi: @mut FileInput_
}
impl FileInput {
/**
Create a `FileInput` object from a vec of files. An empty
vec means lines are read from `stdin` (use `from_vec_raw` to stop
this behaviour). Any occurrence of `None` represents `stdin`.
*/
pub fn from_vec(files: ~[Option<Path>]) -> FileInput {
FileInput::from_vec_raw(
if files.is_empty() {
~[None]
} else {
files
})
}
/**
Identical to `from_vec`, but an empty `files` vec stays
empty. (`None` is `stdin`.)
*/
pub fn from_vec_raw(files: ~[Option<Path>])
-> FileInput {
FileInput{
fi: @mut FileInput_ {
files: files,
current_reader: None,
state: FileInputState {
current_path: None,
line_num: 0,
line_num_file: 0
},
// there was no previous unended line
previous_was_newline: true
}
}
}
/**
Create a `FileInput` object from the command line
arguments. `"-"` represents `stdin`.
*/
pub fn from_args() -> FileInput {
let args = os::args();
let pathed = make_path_option_vec(args.tail(), true);
FileInput::from_vec(pathed)
}
fn current_file_eof(&self) -> bool {
match self.fi.current_reader {
None => false,
Some(r) => r.eof()
}
}
/**
Skip to the next file in the queue. Can `fail` when opening
a file.
Returns `false` if there is no more files, and `true` when it
successfully opens the next file.
*/
pub fn next_file(&self) -> bool {
// No more files
if self.fi.files.is_empty() {
self.fi.current_reader = None;
return false;
}
let path_option = self.fi.files.shift();
let file = match path_option {
None => io::stdin(),
Some(ref path) => io::file_reader(path).unwrap()
};
self.fi.current_reader = Some(file);
self.fi.state.current_path = path_option;
self.fi.state.line_num_file = 0;
true
}
/**
Attempt to open the next file if there is none currently open,
or if the current one is EOF'd.
Returns `true` if it had to move to the next file and did
so successfully.
*/
fn next_file_if_eof(&self) -> bool {
match self.fi.current_reader {
None => self.next_file(),
Some(r) => {
if r.eof() {
self.next_file()
} else {
false
}
}
}
}
/**
Apply `f` to each line successively, along with some state
(line numbers and file names, see documentation for
`FileInputState`). Otherwise identical to `lines_each`.
*/
pub fn each_line_state(&self,
f: &fn(&str, FileInputState) -> bool) -> bool {
self.each_line(|line| f(line, self.fi.state.clone()))
}
/**
Retrieve the current `FileInputState` information.
*/
pub fn state(&self) -> FileInputState {
self.fi.state.clone()
}
}
impl io::Reader for FileInput {
fn read_byte(&self) -> int {
loop {
let stepped = self.next_file_if_eof();
// if we moved to the next file, and the previous
// character wasn't \n, then there is an unfinished line
// from the previous file. This library models
// line-by-line processing and the trailing line of the
// previous file and the leading of the current file
// should be considered different, so we need to insert a
// fake line separator
if stepped && !self.fi.previous_was_newline {
self.fi.state.line_num += 1;
self.fi.state.line_num_file += 1;
self.fi.previous_was_newline = true;
return '\n' as int;
}
match self.fi.current_reader {
None => return -1,
Some(r) => {
let b = r.read_byte();
if b < 0 {
continue;
}
if b == '\n' as int {
self.fi.state.line_num += 1;
self.fi.state.line_num_file += 1;
self.fi.previous_was_newline = true;
} else {
self.fi.previous_was_newline = false;
}
return b;
}
}
}
}
fn read(&self, buf: &mut [u8], len: uint) -> uint {
let mut count = 0;
while count < len {
let b = self.read_byte();
if b < 0 { break }
buf[count] = b as u8;
count += 1;
}
count
}
fn eof(&self) -> bool {
// we've run out of files, and current_reader is either None or eof.
self.fi.files.is_empty() &&
match self.fi.current_reader { None => true, Some(r) => r.eof() }
}
fn seek(&self, offset: int, whence: io::SeekStyle) {
match self.fi.current_reader {
None => {},
Some(r) => r.seek(offset, whence)
}
}
fn tell(&self) -> uint {
match self.fi.current_reader {
None => 0,
Some(r) => r.tell()
}
}
}
/**
Convert a list of strings to an appropriate form for a `FileInput`
instance. `stdin_hyphen` controls whether `-` represents `stdin` or
a literal `-`.
*/
pub fn make_path_option_vec(vec: &[~str], stdin_hyphen : bool) -> ~[Option<Path>] {
vec.iter().map(|s| {
if stdin_hyphen && "-" == *s {
None
} else {
Some(Path::new(s.as_slice()))
}
}).collect()
}
/**
Iterate directly over the command line arguments (no arguments implies
reading from `stdin`).
Fails when attempting to read from a file that can't be opened.
*/
pub fn input(f: &fn(&str) -> bool) -> bool {
let i = FileInput::from_args();
i.each_line(f)
}
/**
Iterate directly over the command line arguments (no arguments
implies reading from `stdin`) with the current state of the iteration
provided at each call.
Fails when attempting to read from a file that can't be opened.
*/
pub fn input_state(f: &fn(&str, FileInputState) -> bool) -> bool {
let i = FileInput::from_args();
i.each_line_state(f)
}
/**
Iterate over a vector of files (an empty vector implies just `stdin`).
Fails when attempting to read from a file that can't be opened.
*/
pub fn input_vec(files: ~[Option<Path>], f: &fn(&str) -> bool) -> bool {
let i = FileInput::from_vec(files);
i.each_line(f)
}
/**
Iterate over a vector of files (an empty vector implies just `stdin`)
with the current state of the iteration provided at each call.
Fails when attempting to read from a file that can't be opened.
*/
pub fn input_vec_state(files: ~[Option<Path>],
f: &fn(&str, FileInputState) -> bool) -> bool {
let i = FileInput::from_vec(files);
i.each_line_state(f)
}
#[cfg(test)]
mod test {
use super::{FileInput, make_path_option_vec, input_vec, input_vec_state};
use std::rt::io;
use std::rt::io::Writer;
use std::rt::io::file;
use std::vec;
fn make_file(path : &Path, contents: &[~str]) {
let mut file = file::open(path, io::CreateOrTruncate, io::Write).unwrap();
for str in contents.iter() {
file.write(str.as_bytes());
file.write(['\n' as u8]);
}
}
#[test]
fn test_make_path_option_vec() {
let strs = [~"some/path",
~"some/other/path"];
let paths = ~[Some(Path::new("some/path")),
Some(Path::new("some/other/path"))];
assert_eq!(make_path_option_vec(strs, true), paths.clone());
assert_eq!(make_path_option_vec(strs, false), paths);
assert_eq!(make_path_option_vec([~"-"], true), ~[None]);
assert_eq!(make_path_option_vec([~"-"], false), ~[Some(Path::new("-"))]);
}
#[test]
fn test_fileinput_read_byte() {
let filenames = make_path_option_vec(vec::from_fn(
3,
|i| format!("tmp/lib-fileinput-test-fileinput-read-byte-{}.tmp", i)), true);
// 3 files containing 0\n, 1\n, and 2\n respectively
for (i, filename) in filenames.iter().enumerate() {
make_file(filename.get_ref(), [format!("{}", i)]);
}
let fi = FileInput::from_vec(filenames.clone());
for (line, c) in "012".iter().enumerate() {
assert_eq!(fi.read_byte(), c as int);
assert_eq!(fi.state().line_num, line);
assert_eq!(fi.state().line_num_file, 0);
assert_eq!(fi.read_byte(), '\n' as int);
assert_eq!(fi.state().line_num, line + 1);
assert_eq!(fi.state().line_num_file, 1);
assert_eq!(fi.state().current_path.clone(), filenames[line].clone());
}
assert_eq!(fi.read_byte(), -1);
assert!(fi.eof());
assert_eq!(fi.state().line_num, 3)
}
#[test]
fn test_fileinput_read() {
let filenames = make_path_option_vec(vec::from_fn(
3,
|i| format!("tmp/lib-fileinput-test-fileinput-read-{}.tmp", i)), true);
// 3 files containing 1\n, 2\n, and 3\n respectively
for (i, filename) in filenames.iter().enumerate() {
make_file(filename.get_ref(), [format!("{}", i)]);
}
let fi = FileInput::from_vec(filenames);
let mut buf : ~[u8] = vec::from_elem(6, 0u8);
let count = fi.read(buf, 10);
assert_eq!(count, 6);
assert_eq!(buf, "0\n1\n2\n".as_bytes().to_owned());
assert!(fi.eof())
assert_eq!(fi.state().line_num, 3);
}
#[test]
fn test_input_vec() {
let mut all_lines = ~[];
let filenames = make_path_option_vec(vec::from_fn(
3,
|i| format!("tmp/lib-fileinput-test-input-vec-{}.tmp", i)), true);
for (i, filename) in filenames.iter().enumerate() {
let contents =
vec::from_fn(3, |j| format!("{} {}", i, j));
make_file(filename.get_ref(), contents);
debug2!("contents={:?}", contents);
all_lines.push_all(contents);
}
let mut read_lines = ~[];
do input_vec(filenames) |line| {
read_lines.push(line.to_owned());
true
};
assert_eq!(read_lines, all_lines);
}
#[test]
fn test_input_vec_state() {
let filenames = make_path_option_vec(vec::from_fn(
3,
|i| format!("tmp/lib-fileinput-test-input-vec-state-{}.tmp", i)),true);
for (i, filename) in filenames.iter().enumerate() {
let contents =
vec::from_fn(3, |j| format!("{} {}", i, j + 1));
make_file(filename.get_ref(), contents);
}
do input_vec_state(filenames) |line, state| {
let nums: ~[&str] = line.split_iter(' ').collect();
let file_num = from_str::<uint>(nums[0]).unwrap();
let line_num = from_str::<uint>(nums[1]).unwrap();
assert_eq!(line_num, state.line_num_file);
assert_eq!(file_num * 3 + line_num, state.line_num);
true
};
}
#[test]
fn test_empty_files() {
let filenames = make_path_option_vec(vec::from_fn(
3,
|i| format!("tmp/lib-fileinput-test-empty-files-{}.tmp", i)),true);
make_file(filenames[0].get_ref(), [~"1", ~"2"]);
make_file(filenames[1].get_ref(), []);
make_file(filenames[2].get_ref(), [~"3", ~"4"]);
let mut count = 0;
do input_vec_state(filenames.clone()) |line, state| {
let expected_path = match line {
"1" | "2" => filenames[0].clone(),
"3" | "4" => filenames[2].clone(),
_ => fail2!("unexpected line")
};
assert_eq!(state.current_path.clone(), expected_path);
count += 1;
true
};
assert_eq!(count, 4);
}
#[test]
fn test_no_trailing_newline() {
let f1 =
Some(Path::new("tmp/lib-fileinput-test-no-trailing-newline-1.tmp"));
let f2 =
Some(Path::new("tmp/lib-fileinput-test-no-trailing-newline-2.tmp"));
{
let mut wr = file::open(f1.get_ref(), io::CreateOrTruncate,
io::Write).unwrap();
wr.write("1\n2".as_bytes());
let mut wr = file::open(f2.get_ref(), io::CreateOrTruncate,
io::Write).unwrap();
wr.write("3\n4".as_bytes());
}
let mut lines = ~[];
do input_vec(~[f1, f2]) |line| {
lines.push(line.to_owned());
true
};
assert_eq!(lines, ~[~"1", ~"2", ~"3", ~"4"]);
}
#[test]
fn test_next_file() {
let filenames = make_path_option_vec(vec::from_fn(
3,
|i| format!("tmp/lib-fileinput-test-next-file-{}.tmp", i)),true);
for (i, filename) in filenames.iter().enumerate() {
let contents = vec::from_fn(3, |j| format!("{} {}", i, j + 1));
make_file(filename.get_ref(), contents);
}
let input = FileInput::from_vec(filenames);
// read once from 0
assert_eq!(input.read_line(), ~"0 1");
input.next_file(); // skip the rest of 1
// read all lines from 1 (but don't read any from 2),
for i in range(1u, 4) {
assert_eq!(input.read_line(), format!("1 {}", i));
}
// 1 is finished, but 2 hasn't been started yet, so this will
// just "skip" to the beginning of 2 (Python's fileinput does
// the same)
input.next_file();
assert_eq!(input.read_line(), ~"2 1");
}
#[test]
#[should_fail]
fn test_input_vec_missing_file() {
do input_vec(make_path_option_vec([~"this/file/doesnt/exist"], true)) |line| {
println(line);
true
};
}
}