@@ -19,11 +19,17 @@ file:setvbuf(mode [, size])
19
19
]]
20
20
21
21
local luzer = require (" luzer" )
22
+ local test_lib = require (" lib" )
23
+
22
24
-- The maximum file size is 1Mb (1000 * 1000).
23
25
local MAX_N = 1e3
24
26
25
27
local function io_seek (self )
26
- local SEEK_MODE = { " set" , " cur" , " end" }
28
+ local SEEK_MODE = {
29
+ " set" , -- Base is position 0 (beginning of the file).
30
+ " cur" , -- Base is current position.
31
+ " end" , -- Base is end of file.
32
+ }
27
33
local mode = self .fdp :oneof (SEEK_MODE )
28
34
local offset = self .fdp :consume_integer (0 , self .MAX_N )
29
35
self .fh :seek (mode , offset )
@@ -34,7 +40,11 @@ local function io_flush(self)
34
40
end
35
41
36
42
local function io_setvbuf (self )
37
- local VBUF_MODE = { " no" , " full" , " line" }
43
+ local VBUF_MODE = {
44
+ " no" , -- no buffering.
45
+ " full" , -- full buffering.
46
+ " line" , -- line buffering.
47
+ }
38
48
local mode = self .fdp :oneof (VBUF_MODE )
39
49
local size = self .fdp :consume_integer (0 , self .MAX_N )
40
50
self .fh :setvbuf (mode , size )
@@ -46,13 +56,39 @@ local function io_write(self)
46
56
end
47
57
48
58
local function io_read (self )
49
- -- As a special case, `io.read(0)` works as a test for end of
50
- -- file: It returns an empty string if there is more to be
51
- -- read or `nil` otherwise.
52
- local size = self .fdp :consume_integer (0 , self .MAX_N )
53
- local READ_MODE = { " *number" , " *all" , " *line" , size }
54
- local mode = self .fdp :oneof (READ_MODE )
55
- local _ = self .fh :read (mode )
59
+ local n_formats = self .fdp :consume_integer (1 , self .MAX_N )
60
+ local READ_FORMAT = {
61
+ " *n" , -- Reads a number.
62
+ " *a" , -- Reads the whole file, starting at the current
63
+ -- position.
64
+ " *l" , -- Reads the next line (skipping the end of line),
65
+ -- returning nil on end of file.
66
+ }
67
+ if test_lib .lua_current_version_ge_than (5 , 2 ) then
68
+ -- "*L" reads the next line keeping the end of line
69
+ -- (if present), returning nil on end of file.
70
+ table.insert (READ_FORMAT , " *L" )
71
+ end
72
+ -- Build a table with formats, which specify what to read. For
73
+ -- each format, the function returns a string (or a number)
74
+ -- with the characters read, or `nil` if it cannot read data
75
+ -- with the specified format. When called without formats, it
76
+ -- uses a default format that reads the entire next line.
77
+ local read_formats = {}
78
+ for _ = 1 , n_formats do
79
+ local format_is_size = self .fdp :consume_boolean ()
80
+ if format_is_size then
81
+ -- As a special case, `io.read(0)` works as a test for
82
+ -- end of file: It returns an empty string if there is
83
+ -- more to be read or `nil` otherwise.
84
+ local size = self .fdp :consume_integer (0 , test_lib .MAX_INT64 )
85
+ table.insert (read_formats , size )
86
+ else
87
+ local format = self .fdp :oneof (READ_FORMAT )
88
+ -- table.insert(read_formats, format)
89
+ end
90
+ end
91
+ local _ = self .fh :read (unpack (read_formats ))
56
92
end
57
93
58
94
local function io_close (self )
0 commit comments