You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 5, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: Manual/IO/Files.lean
+175-9Lines changed: 175 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -18,27 +18,57 @@ set_option pp.rawOnError true
18
18
19
19
set_option linter.unusedVariables false
20
20
21
+
/-
21
22
#doc (Manual) "Files, File Handles, and Streams" =>
23
+
-/
24
+
#doc (Manual) "ファイル・ファイルハンドラ・ストリーム(Files, File Handles, and Streams)" =>
22
25
26
+
:::comment
23
27
Lean provides a consistent filesystem API on all supported platforms.
24
28
These are the key concepts:
25
29
30
+
:::
31
+
32
+
Lean はサポート対象のすべてのプラットフォームに対して一貫したファイルシステム API を提供します。以下がカギとなるコンセプトです:
33
+
34
+
:::comment
26
35
: {deftech}[Files]
27
36
28
37
Files are an abstraction provided by operating systems that provide random access to persistently-stored data, organized hierarchically into directories.
File handles ({name IO.FS.Handle}`Handle`) are abstract references to files that have been opened for reading and/or writing.
38
61
A file handle maintains a mode that determines whether reading and/or writing are allowed, along with a cursor that points at a specific location in the file.
39
62
Reading from or writing to a file handle advances the cursor.
40
63
File handles may be {deftech}[buffered], which means that reading from a file handle may not return the current contents of the persistent data, and writing to a file handle may not modify them immediately.
Streams are a higher-level abstraction over files, both providing additional functionality and hiding some details of files.
60
101
While {tech}[file handles] are essentially a thin wrapper around the operating system's representation, streams are implemented in Lean as a structurecalled {lean}`IO.FS.Stream`.
61
102
Because streams are implemented in Lean, user code can create additional streams, which can be used seamlessly together with those provided in the standard library.
@@ -104,15 +161,23 @@ There is no explicit way to close a file handle other than by ensuring that ther
104
161
{docstring IO.FS.Handle.unlock}
105
162
106
163
107
-
::::example"One File, Multiple Handles"
164
+
:::comment
165
+
::example"One File, Multiple Handles"
166
+
:::
167
+
:::::example"1つのファイルと複数のハンドラ"
168
+
:::comment
108
169
This program has two handles to the same file.
109
170
Because file I/O may be buffered independently for each handle, {name IO.FS.Handle.flush}`Handle.flush` should be called when the buffers need to be synchronized with the file's actual contents.
110
171
Here, the two handles proceed in lock-step through the file, with one of them a single byte ahead of the other.
111
172
The first handle is used to count the number of occurrences of `'A'`, while the second is used to replace each `'A'`with`'!'`.
112
173
The second handle is opened in {name IO.FS.Mode.readWrite}`readWrite` mode rather than {name IO.FS.Mode.write}`write` mode because opening an existing file in {name IO.FS.Mode.write}`write` mode replaces it with an empty file.
113
174
In this case, the buffers don't need to be flushed during execution because modifications occur only to parts of the file that will not be read again, but the write handle should be flushed after the loop has completed.
@@ -179,20 +264,40 @@ Afterwards, the file contains:
179
264
180
265
{docstring IO.FS.Stream.Buffer.pos}
181
266
267
+
:::comment
182
268
# Paths
269
+
:::
270
+
271
+
# パス(Paths)
183
272
273
+
274
+
:::comment
184
275
Paths are represented by strings.
185
276
Different platforms have different conventions for paths: some use slashes (`/`) as directory separators, others use backslashes (`\`).
186
277
Some are case-sensitive, others are not.
187
278
Different Unicode encodings and normal forms may be used to represent filenames, and some platforms consider filenames to be byte sequences rather than strings.
188
279
A string that represents an {tech}[absolute path] on one system may not even be a valid path on another system.
To write Lean code that is as compatible as possible with multiple systems, it can be helpful to use Lean's path manipulation primitives instead of raw string manipulation.
191
287
Helpers such as {name}`System.FilePath.join` take platform-specific rules for absolute paths into account, {name}`System.FilePath.pathSeparator` contains the appropriate path separator for the current platform, and {name}`System.FilePath.exeExtension` contains any necessary extension for executable files.
@@ -229,10 +334,20 @@ There is an instance of the {lean}`Div` type class for {name System.FilePath}`Fi
229
334
230
335
{docstring System.FilePath.exeExtension}
231
336
337
+
:::comment
232
338
# Interacting with the Filesystem
339
+
:::
233
340
341
+
# ファイルシステムの対話(Interacting with the Filesystem)
342
+
343
+
344
+
:::comment
234
345
Some operations on paths consult the filesystem.
235
346
347
+
:::
348
+
349
+
パスに対するいくつかの操作はファイルシステムを参照します。
350
+
236
351
{docstring IO.FS.Metadata}
237
352
238
353
{docstring System.FilePath.metadata}
@@ -285,27 +400,50 @@ Some operations on paths consult the filesystem.
285
400
286
401
{docstring IO.FS.createDir}
287
402
403
+
:::comment
288
404
# Standard I/O
405
+
:::
406
+
407
+
# 標準 I/O(Standard I/O)
408
+
289
409
%%%
290
410
tag := "stdio"
291
411
%%%
292
412
413
+
:::comment
293
414
On operating systems that are derived from or inspired by Unix, {deftech}_standard input_, {deftech}_standard output_, and {deftech}_standard error_ are the names of three streams that are available in each process.
294
415
Generally, programs are expected to read from standard input, write ordinary output to the standard output, and error messages to the standard error.
295
416
By default, standard input receives input from the console, while standard output and standard error output to the console, but all three are often redirected to or from pipes or files.
0 commit comments