当前位置: 首页 > article >正文

跟着 Lua 5.1 官方参考文档学习 Lua (12)

文章目录

    • 5.7 – Input and Output Facilities
      • 补充内容
      • `io.input ([file])`
      • `io.read (···)`
      • `io.write (···)`
      • `io.output ([file])`
      • `io.lines ([filename])`
      • `io.flush ()`
      • `io.close ([file])`
      • `io.open (filename [, mode])`
      • `io.popen (prog [, mode])`
      • `io.tmpfile ()`
      • `io.type (ob)`
      • `file:read (···)`
      • `file:lines ()`
      • `file:write (···)`
      • `file:flush ()`
      • `file:seek ([whence] [, offset])`
      • `file:setvbuf (mode [, size])`
      • `file:close ()`
    • 5.8 – Operating System Facilities
      • `os.clock ()`
      • `os.date ([format [, time]])`
      • `os.difftime (t2, t1)`
      • `os.execute ([command])`
      • `os.exit ([code])`
      • `os.getenv (varname)`
      • `os.remove (filename)`
      • `os.rename (oldname, newname)`
      • `os.setlocale (locale [, category])`
      • `os.time ([table])`
      • `os.tmpname ()`
    • 5.9 – The Debug Library
  • 6 – Lua Stand-alone
  • 7 – Incompatibilities with the Previous Version
  • 8 – The Complete Syntax of Lua

5.7 – Input and Output Facilities

The I/O library provides two different styles for file manipulation. The first one uses implicit file descriptors; that is, there are operations to set a default input file and a default output file, and all input/output operations are over these default files. The second style uses explicit file descriptors.

When using implicit file descriptors, all operations are supplied by table io. When using explicit file descriptors, the operation io.open returns a file descriptor and then all operations are supplied as methods of the file descriptor.

The table io also provides three predefined file descriptors with their usual meanings from C: io.stdin, io.stdout, and io.stderr. The I/O library never closes these files.

Unless otherwise stated, all I/O functions return nil on failure (plus an error message as a second result and a system-dependent error code as a third result) and some value different from nil on success.

补充内容

21.1 The Simple I/O Model

The simple model does all of its operations on two current files. The library initializes the current input file as the process standard input (stdin) and the current output file as the process standard output (stdout). Therefore, when we execute something like io.read(), we read a line from the standard input. We can change these current files with the io.input and io.output functions. A call like io.input(filename) opens the given file in read mode and sets it as the current input file. From this point on, all input will come from this file, until another call to io.input; io.output does a similar job for output. In case of error, both functions raise the error. If you want to handle errors directly, you must use io.open, from the complete model.

As write is simpler than read, we will look at it first. The io.write function simply gets an arbitrary number of string arguments and writes them to the current output file. Numbers are converted to strings following the usual conversion rules; for full control over this conversion, you should use the
string.format function:

> io.write("sin (3) = ", math.sin(3), "\n")
--> sin (3) = 0.14112000805987
> io.write(string.format("sin (3) = %.4f\n", math.sin(3)))
--> sin (3) = 0.1411

Avoid code like io.write(a…b…c); the call io.write(a,b,c) accomplishes the same effect with fewer resources, as it avoids the concatenations.
As a rule, you should use print for quick-and-dirty programs, or for debugging, and write when you need full control over your output:

> print("hello", "Lua"); print("Hi")
--> hello Lua
--> Hi
> io.write("hello", "Lua"); io.write("Hi", "\n")
--> helloLuaHi

Unlike print, write adds no extra characters to the output, such as tabs or newlines. Moreover, write uses the current output file, whereas print always uses the standard output. Finally, print automatically applies tostring to its arguments, so it can also show tables, functions, and nil.

The io.read function reads strings from the current input file. Its arguments control what is read:

“*all” reads the whole file
“*line” reads the next line
“*number” reads a number
num reads a string with up to num characters

The call *io.read("all") reads the whole current input file, starting at its current position. If we are at the end of the file, or if the file is empty, the call returns an empty string

Because Lua handles long strings efficiently, a simple technique for writing filters in Lua is to read the whole file into a string, do the processing to the string (typically with gsub), and then write the string to the output:

t = io.read("*all") -- read the whole file
t = string.gsub(t, ...) -- do the job
io.write(t) -- write the file  

The call io.read(“*line”) returns the next line from the current input file, without the newline character. When we reach the end of file, the call returns nil (as there is no next line to return). This pattern is the default for read.

Usually, I use this pattern only when the algorithm naturally handles the file line by line; otherwise, I favor reading the whole file at once, with *all, or in blocks, as we will see later.

As a simple example of the use of this pattern, the following program copies
its current input to the current output, numbering each line:

for count = 1, math.huge do
    local line = io.read()
    if line == nil then break end
    io.write(string.format("%6d ", count), line, "\n")
end

However, to iterate on a whole file line by line, we do better to use the io.lines iterator. For instance, we can write a complete program to sort the lines of a file as follows:

local lines = {}
-- read the lines in table ’lines’
for line in io.lines() do lines[#lines + 1] = line end
-- sort
table.sort(lines)
-- write all the lines
for _, l in ipairs(lines) do io.write(l, "\n") end

Besides the basic read patterns, you can call read with a number n as an argument: in this case, read tries to read n characters from the input file. If it cannot read any character (end of file), read returns nil; otherwise, it returns a string with at most n characters.

As an example of this read pattern, the following program is an efficient way (in Lua, of course) to copy a file from stdin to stdout:

while true do
    local block = io.read(2 ^ 13) -- buffer size is 8K
    if not block then break end
    io.write(block)
end

As a special case, io.read(0) works as a test for end of file: it returns an empty string if there is more to be read or nil otherwise.

21.2 The Complete I/O Model

For more control over I/O, you can use the complete model. A central concept in this model is the file handle, which is equivalent to streams (FILE*) in C: itrepresents an open file with a current position.

To open a file, you use the io.open function, which mimics the fopen function in C. It takes as arguments the name of the file to open plus a mode string. This mode string may contain an ‘r’ for reading, a ‘w’ for writing (which also erases any previous content of the file), or an ‘a’ for appending, plus an optional ‘b’ to open binary files. The open function returns a new handle for the file. In case of error, open returns nil, plus an error message and an error number:

print(io.open("non-existent-file", "r"))
--> nil non-existent-file: No such file or directory 2
print(io.open("/etc/passwd", "w"))
--> nil /etc/passwd: Permission denied 13

The interpretation of the error numbers is system dependent.

A typical idiom to check for errors is

local f = assert(io.open(filename, mode))

If the open fails, the error message goes as the second argument to assert, which then shows the message. After you open a file, you can read from it or write to it with the methods read/write. They are similar to the read/write functions, but you call them as methods on the file handle, using the colon syntax. For instance, to open a file and read it all, you can use a chunk like this:

local f = assert(io.open(filename, "r"))
local t = f:read("*all")
f:close()

The I/O library offers handles for the three predefined C streams: io.stdin, io.stdout, and io.stderr. So, you can send a message directly to the error stream with a code like this:

io.stderr:write(message)

We can mix the complete model with the simple model. We get the current input file handle by calling io.input(), without arguments. We set this handle with the call io.input(handle). (Similar calls are also valid for io.output.) For instance, if you want to change the current input file temporarily, you can write something like this:

local temp = io.input() -- save current file
io.input("newinput") -- open a new current file
-- <do something with new input>
io.input():close() -- close current file
io.input(temp) -- restore previous current file

A small performance trick

Usually, in Lua, it is faster to read a file as a whole than to read it line by line. However, sometimes we must face a big file (say, tens or hundreds megabytes) for which it is not reasonable to read it all at once. If you want to handle such big files with maximum performance, the fastest way is to read them in reasonably large chunks (e.g., 8 Kbytes each). To avoid the problem of breaking lines in the
middle, you simply ask to read a chunk plus a line:

local lines, rest = f:read(BUFSIZE, "*line")

The variable rest will get the rest of any line broken by the chunk. We then concatenate the chunk and this rest of line. This way, the resulting chunk will always break at line boundaries.

The example in Listing 21.1 uses this technique to implement wc, a program that counts the number of characters, words, and lines in a file.

local BUFSIZE = 2 ^ 13     -- 8K
local f = io.input(arg[1]) -- open input file
local cc, lc, wc = 0, 0, 0 -- char, line, and word counts
while true do
    local lines, rest = f:read(BUFSIZE, "*line")
    if not lines then break end
    if rest then lines = lines .. rest .. "\n" end
    cc = cc + #lines
    -- count words in the chunk
    local _, t = string.gsub(lines, "%S+", "")
    wc = wc + t
    -- count newlines in the chunk
    _, t = string.gsub(lines, "\n", "\n")
    lc = lc + t
end
print(lc, wc, cc)

Binary files

The simple-model functions io.input and io.output always open a file in text mode (the default). In Unix, there is no difference between binary files and text files. But in some systems, notably Windows, binary files must be opened with a special flag. To handle such binary files, you must use io.open, with the letter ‘b’ in the mode string.

Binary data in Lua are handled similarly to text. A string in Lua may contain any bytes, and almost all functions in the libraries can handle arbitrary bytes. You can even do pattern matching over binary data, as long as the pattern does not contain a zero byte. If you want to match this byte in the subject, you can use the class %z instead.

Typically, you read binary data either with the *all pattern, that reads the whole file, or with the pattern n, that reads n bytes. As a simple example, the following program converts a text file from DOS format to Unix format (that is, it translates sequences of carriage return–newlines to newlines). It does not use the standard I/O files (stdin–stdout), because these files are open in text mode. Instead, it assumes that the names of the input file and the output file are given as arguments to the program:

local inp = assert(io.open(arg[1], "rb"))
local out = assert(io.open(arg[2], "wb"))
local data = inp:read("*all")
data = string.gsub(data, "\r\n", "\n")
out:write(data)
assert(out:close())

As another example, the following program prints all strings found in a binary file:

local f = assert(io.open(arg[1], "rb"))
local data = f:read("*all")
local validchars = "[%w%p%s]"
local pattern = string.rep(validchars, 6) .. "+%z"
for w in string.gmatch(data, pattern) do
    print(w)
end

The program assumes that a string is any zero-terminated sequence of six or more valid characters, where a valid character is any character accepted by the pattern validchars. In our example, this pattern comprises the alphanumeric, the punctuation, and the space characters. We use string.rep and concatenation to create a pattern that captures all sequences of six or more validchars. The %z at the end of the pattern matches the byte zero at the end of a string.

As a last example, the following program makes a dump of a binary file:

vlocal f = assert(io.open(arg[1], "rb"))
local block = 16
while true do
    local bytes = f:read(block)
    if not bytes then break end
    for _, b in pairs { string.byte(bytes, 1, -1) } do
        io.write(string.format("%02X ", b))
    end
    io.write(string.rep(" ", block - string.len(bytes)))
    io.write(" ", string.gsub(bytes, "%c", "."), "\n")
end

Again, the first program argument is the input file name; the output goes to the standard output. The program reads the file in chunks of 16 bytes. For each chunk, it writes the hexadecimal representation of each byte, and then it writes the chunk as text, changing control characters to dots. (Note the use of the idiom {string.byte(bytes,1,-1)} to create a table with all bytes of the string bytes.)

21.3 Other Operations on Files

The tmpfile function returns a handle for a temporary file, open in read/write mode. This file is automatically removed (deleted) when your program ends.

The flush function executes all pending writes to a file. Like the write function, you can call it as a function, io.flush(), to flush the current output file; or as a method, f:flush(), to flush a particular file f.

The seek function can both get and set the current position of a file. Its general form is f:seek(whence,offset). The whence parameter is a string that specifies how to interpret the offset. Its valid values are “set”, when offsets are interpreted from the beginning of the file; “cur”, when offsets are interpreted from the current position of the file; and “end”, when offsets are interpreted from the end of the file. Independently of the value of whence, the call returns the final current position of the file, measured in bytes from the beginning of the file.

The default value for whence is “cur” and for offset is zero. Therefore, the call file:seek() returns the current file position, without changing it; the call file:seek(“set”) resets the position to the beginning of the file (and returns zero); and the call file:seek(“end”) sets the position to the end of the file and returns its size. The following function gets the file size without changing its current position:

function fsize(file)
    local current = file:seek()   -- get current position
    local size = file:seek("end") -- get file size
    file:seek("set", current)     -- restore position
    return size
end

All these functions return nil plus an error message in case of error.


io.input ([file])

When called with a file name, it opens the named file (in text mode), and sets its handle as the default input file. When called with a file handle, it simply sets this file handle as the default input file. When called without parameters, it returns the current default input file.

In case of errors this function raises the error, instead of returning an error code.

io.read (···)

Equivalent to io.input():read.

io.write (···)

Equivalent to io.output():write.

io.output ([file])

Similar to io.input, but operates over the default output file.

io.lines ([filename])

Opens the given file name in read mode and returns an iterator function that, each time it is called, returns a new line from the file. Therefore, the construction

     for line in io.lines(filename) do body end

will iterate over all lines of the file. When the iterator function detects the end of file, it returns nil (to finish the loop) and automatically closes the file.

The call io.lines() (with no file name) is equivalent to io.input():lines(); that is, it iterates over the lines of the default input file. In this case it does not close the file when the loop ends.

io.flush ()

Equivalent to file:flush over the default output file.

io.close ([file])

Equivalent to file:close(). Without a file, closes the default output file.

io.open (filename [, mode])

This function opens a file, in the mode specified in the string mode. It returns a new file handle, or, in case of errors, nil plus an error message.

The mode string can be any of the following:

  • “r”: read mode (the default);
  • “w”: write mode;
  • “a”: append mode;
  • “r+”: update mode, all previous data is preserved;
  • “w+”: update mode, all previous data is erased;
  • “a+”: append update mode, previous data is preserved, writing is only allowed at the end of file.

The mode string can also have a ‘b’ at the end, which is needed in some systems to open the file in binary mode. This string is exactly what is used in the standard C function fopen.

io.popen (prog [, mode])

Starts program prog in a separated process and returns a file handle that you can use to read data from this program (if mode is "r", the default) or to write data to this program (if mode is "w").

This function is system dependent and is not available on all platforms.

io.tmpfile ()

Returns a handle for a temporary file. This file is opened in update mode and it is automatically removed when the program ends.

io.type (ob)

Checks whether obj is a valid file handle. Returns the string "file" if obj is an open file handle, "closed file" if obj is a closed file handle, or nil if obj is not a file handle.

file:read (···)

Reads the file file, according to the given formats, which specify what to read. For each format, the function returns a string (or a number) with the characters read, or nil if it cannot read data with the specified format. When called without formats, it uses a default format that reads the entire next line (see below).

The available formats are

  • “*n”: reads a number; this is the only format that returns a number instead of a string.
  • “*a”: reads the whole file, starting at the current position. On end of file, it returns the empty string.
  • “*l”: reads the next line (skipping the end of line), returning nil on end of file. This is the default format.
  • *number*: reads a string with up to this number of characters, returning nil on end of file. If number is zero, it reads nothing and returns an empty string, or nil on end of file.

file:lines ()

Returns an iterator function that, each time it is called, returns a new line from the file. Therefore, the construction

     for line in file:lines() do body end

will iterate over all lines of the file. (Unlike io.lines, this function does not close the file when the loop ends.)

file:write (···)

Writes the value of each of its arguments to the file. The arguments must be strings or numbers. To write other values, use tostring or string.format before write.

file:flush ()

Saves any written data to file.

file:seek ([whence] [, offset])

Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence, as follows:

  • “set”: base is position 0 (beginning of the file);
  • “cur”: base is current position;
  • “end”: base is end of file;

In case of success, function seek returns the final file position, measured in bytes from the beginning of the file. If this function fails, it returns nil, plus a string describing the error.

The default value for whence is "cur", and for offset is 0. Therefore, the call file:seek() returns the current file position, without changing it; the call file:seek("set") sets the position to the beginning of the file (and returns 0); and the call file:seek("end") sets the position to the end of the file, and returns its size.

file:setvbuf (mode [, size])

Sets the buffering mode for an output file. There are three available modes:

  • “no”: no buffering; the result of any output operation appears immediately.
  • “full”: full buffering; output operation is performed only when the buffer is full (or when you explicitly flush the file (see io.flush)).
  • “line”: line buffering; output is buffered until a newline is output or there is any input from some special files (such as a terminal device).

For the last two cases, size specifies the size of the buffer, in bytes. The default is an appropriate size.

file:close ()

Closes file. Note that files are automatically closed when their handles are garbage collected, but that takes an unpredictable amount of time to happen.

5.8 – Operating System Facilities

This library is implemented through table os.

os.clock ()

Returns an approximation of the amount in seconds of CPU time used by the program.

os.date ([format [, time]])

Returns a string or a table containing date and time, formatted according to the given string format.

If the time argument is present, this is the time to be formatted (see the os.time function for a description of this value). Otherwise, date formats the current time.

If format starts with ‘!’, then the date is formatted in Coordinated Universal Time. After this optional character, if format is the string “*t”, then date returns a table with the following fields: year (four digits), month (1–12), day (1–31), hour (0–23), min (0–59), sec (0–61), wday (weekday, Sunday is 1), yday (day of the year), and isdst (daylight saving flag, a boolean).

If format is not “*t”, then date returns the date as a string, formatted according to the same rules as the C function strftime.

When called without arguments, date returns a reasonable date and time representation that depends on the host system and on the current locale (that is, os.date() is equivalent to os.date("%c")).

os.difftime (t2, t1)

Returns the number of seconds from time t1 to time t2. In POSIX, Windows, and some other systems, this value is exactly t2-t1.

os.execute ([command])

This function is equivalent to the C function system. It passes command to be executed by an operating system shell. It returns a status code, which is system-dependent. If command is absent, then it returns nonzero if a shell is available and zero otherwise.

os.exit ([code])

Calls the C function exit, with an optional code, to terminate the host program. The default value for code is the success code.

os.getenv (varname)

Returns the value of the process environment variable varname, or nil if the variable is not defined.

os.remove (filename)

Deletes the file or directory with the given name. Directories must be empty to be removed. If this function fails, it returns nil, plus a string describing the error.

os.rename (oldname, newname)

Renames file or directory named oldname to newname. If this function fails, it returns nil, plus a string describing the error.

os.setlocale (locale [, category])

Sets the current locale of the program. locale is a string specifying a locale; category is an optional string describing which category to change: "all", "collate", "ctype", "monetary", "numeric", or "time"; the default category is "all". The function returns the name of the new locale, or nil if the request cannot be honored.

If locale is the empty string, the current locale is set to an implementation-defined native locale. If locale is the string “C”, the current locale is set to the standard C locale.

When called with nil as the first argument, this function only returns the name of the current locale for the given category.

os.time ([table])

Returns the current time when called without arguments, or a time representing the date and time specified by the given table. This table must have fields year, month, and day, and may have fields hour, min, sec, and isdst (for a description of these fields, see the os.date function).

The returned value is a number, whose meaning depends on your system. In POSIX, Windows, and some other systems, this number counts the number of seconds since some given start time (the “epoch”). In other systems, the meaning is not specified, and the number returned by time can be used only as an argument to date and difftime.

os.tmpname ()

Returns a string with a file name that can be used for a temporary file. The file must be explicitly opened before its use and explicitly removed when no longer needed.

On some systems (POSIX), this function also creates a file with that name, to avoid security risks. (Someone else might create the file with wrong permissions in the time between getting the name and creating the file.) You still have to open the file to use it and to remove it (even if you do not use it).

When possible, you may prefer to use io.tmpfile, which automatically removes the file when the program ends.

5.9 – The Debug Library

This library provides the functionality of the debug interface to Lua programs. You should exert care when using this library. The functions provided here should be used exclusively for debugging and similar tasks, such as profiling. Please resist the temptation to use them as a usual programming tool: they can be very slow. Moreover, several of these functions violate some assumptions about Lua code (e.g., that variables local to a function cannot be accessed from outside or that userdata metatables cannot be changed by Lua code) and therefore can compromise otherwise secure code.

All functions in this library are provided inside the debug table. All functions that operate over a thread have an optional first argument which is the thread to operate over. The default is always the current thread.

6 – Lua Stand-alone

7 – Incompatibilities with the Previous Version

8 – The Complete Syntax of Lua

Here is the complete syntax of Lua in extended BNF. (It does not describe operator precedences.)

	chunk ::= {stat [`;´]} [laststat [`;´]]

	block ::= chunk

	stat ::=  varlist `=´ explist | 
		 functioncall | 
		 do block end | 
		 while exp do block end | 
		 repeat block until exp | 
		 if exp then block {elseif exp then block} [else block] end | 
		 for Name `=´ exp `,´ exp [`,´ exp] do block end | 
		 for namelist in explist do block end | 
		 function funcname funcbody | 
		 local function Name funcbody | 
		 local namelist [`=´ explist] 

	laststat ::= return [explist] | break

	funcname ::= Name {`.´ Name} [`:´ Name]

	varlist ::= var {`,´ var}

	var ::=  Name | prefixexp `[´ exp `]´ | prefixexp `.´ Name 

	namelist ::= Name {`,´ Name}

	explist ::= {exp `,´} exp

	exp ::=  nil | false | true | Number | String | `...´ | function | 
		 prefixexp | tableconstructor | exp binop exp | unop exp 

	prefixexp ::= var | functioncall | `(´ exp `)´

	functioncall ::=  prefixexp args | prefixexp `:´ Name args 

	args ::=  `(´ [explist] `)´ | tableconstructor | String 

	function ::= function funcbody

	funcbody ::= `(´ [parlist] `)´ block end

	parlist ::= namelist [`,´ `...´] | `...´

	tableconstructor ::= `{´ [fieldlist] `}´

	fieldlist ::= field {fieldsep field} [fieldsep]

	field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp

	fieldsep ::= `,´ | `;´

	binop ::= `+´ | `-´ | `*´ | `/´ | `^´ | `%´ | `..´ | 
		 `<´ | `<=´ | `>´ | `>=´ | `==´ | `~=´ | 
		 and | or

	unop ::= `-´ | not | `#´

http://www.kler.cn/a/578833.html

相关文章:

  • 浅谈流媒体协议以及视频编解码
  • C#中异步窗体的调用方法
  • sqlserver中的锁模式 | SQL SERVER如何开启MVCC(使用row-versioning)【启用行版本控制减少锁争用】
  • 如何基于LLM及NL2SQL打造对话式智能BI助手
  • Go JSON数据处理(Gin+Gorm)
  • 摩托车PKE感应一键启动智能安全双防护
  • 2025/03/06(嵌入式学习开始第二天)
  • C++ Qt创建计时器
  • godot在_process()函数实现非阻塞延时触发逻辑
  • 基于模糊PID控制器的混合动力汽车EMS能量管理控制系统simulink建模与仿真
  • 深度学习PyTorch之13种模型精度评估公式及调用方法
  • 3.3.2 Proteus第一个仿真图
  • 基于DeepSeek与搜索引擎构建智能搜索摘要工具
  • ThinkPhp 5 安装阿里云内容安全(绿化)
  • STM32-I2C通信外设
  • 解决JDK 序列化导致的 Redis Key 非预期编码问题
  • npm install 报错ERESOLVE
  • Django工程获取请求参数的几种方式
  • Scala 中的访问修饰符
  • WebGPT: 基于浏览器辅助的问答系统,结合人类反馈优化答案质量