Marvin's Blog【程式人生】

Ability will never catch up with the demand for it

24 Feb 2021

The Scheme Programming Language读书笔记【七】

The Scheme Programming Language 读书笔记,chapter 7.

Chapter 7. Input and Output

所有的IO操作都是通过ports来完成的。Port是一个指向数据流的指针。一个Port可以是只为输入服务,或者只为输出服务的,或者同时为输入和输出服务的。

Port也是第一等级的对象。port没有像字串或者数值那样的可打印形式。初始情况下有三个port:current input port, current output port, standard error port。

Port也可以是二进制的,或者是文本格式的。二进制port允许按8位无符号字节读写数据流;文本格式的port允许按字符读写数据流。文本流常常涉及到格式转化,即transcoder。三种标准的转码格式:latin-1, utf-8, utf-16。

转码器封装了eol样式用于区分不同过的换行。如果eol样式为none,那么不会识别换行。下面是其他可选的eol样式:

  • lf, line-feed
  • cr, carriage-return
  • nel, Unicode next-line
  • ls; Unicode line-separator
  • crlf, carriage return连着line-feed
  • crnel, carriage return连着next-line

对于输入流,none以外的eol样式会将识别的字符们转化为单个line-feed。对于输出流,none以外的eol样式会将line-feed转化为对应的行尾。另外对于输入流来说,除none以外的所有的eol样式都是相等的;对于输出流来说,none和lf是等效的。

除了codec和eol以外,转码器还对应着另一个信息:错误处理模式,用于在编解码出错的时候给出具体的错误处理行为。如果错误处理模式为ignore,那么错误会被忽略;如果是raise,那么会抛出i/o-decoding或者i/o-encoding异常;如果是replace,那么会将错误的字符替换成U+FFFD,如果是输出方向,还可以将替换字符设成对应编码条件下的?

port可以设置不同的缓冲类型,比如none, line以及block。

Section 7.1. Transcoders

相关执行诀:

  • (make-transcoder codec)
  • (make-transcoder codec eol-style)
  • (make-transcoder codec eol-style error-handling-mode)
  • (transcoder-codec transcoder)
  • (transcoder-eol-style transcoder)
  • (transcoder-error-handling-mode transcoder)
  • (native-transcoder)
  • (latin-1-codec)
  • (utf-8-codec)
  • (utf-16-codec)
  • (eol-style symbol), symbol must be one of the symbols lf, cr, nel, ls, crlf, crnel, or none
  • (native-eol-style)
  • (error-handling-mode symbol), symbol must be one of the symbols ignore, raise, or replace

Section 7.2. Opening Files

介绍如何打开文件。可以设置参数options, b-mode或者?transcoder。options的取值范围必须在(file-options)枚举合集之内;b-mode指的是缓冲格式;?transcoder必须设置成一个有效的trancoder,或者设置成#f表示不适用transcoder。

语法形式 (file-options symbol ...)可以获取打开文件的选项,默认选项情况下下(也就是不带任何symbol)会以输出方式打开一个文件,如果文件不存在的话会创建该文件。如果指定了no-fail选项,那么即便文件存在,也会将其打开,并将内容清空。如果制定了no-create选项,那么文件不存在的情况下不会自动创建文件。no-truncate只能搭配no-fail使用,即文件存在的时候依然打开文件,但不情况其内容。

默认选项可以看成是create, fail-if-exists,以及truncate的结合。no-create选项移除create;no-fail选项移除fail-if-exists;no-truncate选项移除truncate。

chez支持其他选项,比如是否压缩文件,是否排他访问,以及设置具体的文件访问权限。

相关语法:

  • (buffer-mode symbol),symbol必须是block, line以及none之一。
  • (buffer-mode? obj)

相关执行诀:

  • (open-file-input-port path)
  • (open-file-input-port path options)
  • (open-file-input-port path options b-mode)
  • (open-file-input-port path options b-mode ?transcoder)
  • (open-file-output-port path options)
  • (open-file-output-port path options b-mode)
  • (open-file-output-port path options b-mode ?transcoder)
  • (open-file-input/output-port path)
  • (open-file-input/output-port path options)
  • (open-file-input/output-port path options b-mode)
  • (open-file-input/output-port path options b-mode ?transcoder)

Section 7.3. Standard Ports

进程的标准输入,输出以及错误输出。

文本port相关的执行诀:

  • (current-input-port)
  • (current-output-port)
  • (current-error-port)

二进制port相关的执行诀:

  • (standard-input-port)
  • (standard-output-port)
  • (standard-error-port)

Section 7.4. String and Bytevector Ports

本章的执行诀允许使用bytevector以及string作为输入输出流。

  • (open-bytevector-input-port bytevector)
  • (open-bytevector-input-port bytevector ?transcoder)
  • (open-string-input-port string)
  • (open-bytevector-output-port)
  • (open-bytevector-output-port ?transcoder)
  • (open-string-output-port)
  • (call-with-bytevector-output-port procedure)
  • (call-with-bytevector-output-port procedure ?transcoder)
  • (call-with-string-output-port procedure)

Section 7.5. Opening Custom Ports

  • (make-custom-binary-input-port id r! gp sp! close)
  • (make-custom-binary-output-port id w! gp sp! close)
  • (make-custom-binary-input/output-port id r! w! gp sp! close)

id是一个字串用来给新开的port命名;r!表示port支持输入;w!表示port支持输出gp跟获取port-position相关;sp!跟set-port-position!相关;close用来关掉port。

  • (make-custom-textual-input-port id r! gp sp! close)
  • (make-custom-textual-output-port id w! gp sp! close)
  • (make-custom-textual-input/output-port id r! w! gp sp! close)

Section 7.6. Port Operations

相关的执行诀:

  • (port? obj)
  • (input-port? obj)
  • (output-port? obj)
  • (binary-port? obj)
  • (textual-port? obj)
  • (close-port port)
  • (transcoded-port binary-port transcoder)
  • (port-transcoder port)
  • (port-position port)
  • (port-has-port-position? port)
  • (set-port-position! port pos)
  • (port-has-set-port-position!? port)
  • (call-with-port port procedure)
  • (output-port-buffer-mode port)

Section 7.7. Input Operations

相关的执行诀:

  • (eof-object? obj)
  • (eof-object)
  • (get-u8 binary-input-port)
  • (lookahead-u8 binary-input-port)
  • (get-bytevector-n binary-input-port n)
  • (get-bytevector-n! binary-input-port bytevector start n)
  • (get-bytevector-some binary-input-port)
  • (get-bytevector-all binary-input-port)
  • (get-char textual-input-port)
  • (lookahead-char textual-input-port)
  • (get-string-n textual-input-port n)
  • (get-string-n! textual-input-port string start n)
  • (get-string-all textual-input-port)
  • (get-line textual-input-port)
  • (get-datum textual-input-port)
  • (port-eof? input-port)

Section 7.8. Output Operations

  • (put-u8 binary-output-port octet)
  • (put-bytevector binary-output-port bytevector)
  • (put-bytevector binary-output-port bytevector start)
  • (put-bytevector binary-output-port bytevector start n)
  • (put-char textual-output-port char)
  • (put-string textual-output-port string)
  • (put-string textual-output-port string start)
  • (put-string textual-output-port string start n)
  • (put-datum textual-output-port obj)
  • (flush-output-port output-port)

Section 7.9. Convenience I/O

  • (open-input-file path)
  • (open-output-file path)
  • (call-with-input-file path procedure)
  • (call-with-output-file path procedure)
  • (with-input-from-file path thunk)
  • (with-output-to-file path thunk)
  • (read)
  • (read textual-input-port)
  • (read-char)
  • (read-char textual-input-port)
  • (peek-char)
  • (peek-char textual-input-port)
  • (write obj)
  • (write obj textual-output-port)
  • (display obj)
  • (display obj textual-output-port)
  • (write-char char)
  • (write-char char textual-output-port)
  • (newline)
  • (newline textual-output-port)
  • (close-input-port input-port)
  • (close-output-port output-port)

Section 7.10. Filesystem Operations

  • (file-exists? path)
  • (delete-file path)

Section 7.11. Bytevector/String Conversions

  • (bytevector->string bytevector transcoder)
  • (string->bytevector string transcoder)
  • (string->utf8 string)
  • (string->utf16 string)
  • (string->utf16 string endianness)
  • (string->utf32 string)
  • (string->utf32 string endianness)
  • (utf8->string bytevector)
  • (utf16->string bytevector endianness)
  • (utf16->string bytevector endianness endianness-mandatory?)
  • (utf32->string bytevector endianness)
  • (utf32->string bytevector endianness endianness-mandatory?)

(本篇完)

Categories

Tags