std.stdio: add glossary, fileno_t (#8772)

This commit is contained in:
Walter Bright 2023-06-22 07:41:08 -07:00 committed by GitHub
parent b3e664e174
commit 33f63dbd5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,9 +34,17 @@ $(TR $(TD Misc) $(TD
))
))
Standard I/O functions that extend $(B core.stdc.stdio). $(B core.stdc.stdio)
Standard I/O functions that extend $(LINK2 https://dlang.org/phobos/core_stdc_stdio.html, core.stdc.stdio). $(B core.stdc.stdio)
is $(D_PARAM public)ally imported when importing $(B std.stdio).
There are three layers of I/O:
$(OL
$(LI The lowest layer is the operating system layer. The two main schemes are Windows and Posix.)
$(LI C's $(TT stdio.h) which unifies the two operating system schemes.)
$(LI $(TT std.stdio), this module, unifies the various $(TT stdio.h) implementations into
a high level package for D programs.)
)
Source: $(PHOBOSSRC std/stdio.d)
Copyright: Copyright The D Language Foundation 2007-.
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
@ -48,6 +56,55 @@ CSTDIO=$(HTTP cplusplus.com/reference/cstdio/$1/, $1)
*/
module std.stdio;
/*
# Glossary
The three layers have many terms for their data structures and types.
Here we try to bring some sanity to them for the intrepid code spelunker.
## Windows
Handle
A Windows handle is an opaque object of type HANDLE.
The `HANDLE` for standard devices can be retrieved with
Windows `GetStdHandle()`.
## Posix
file descriptor, aka fileno, aka fildes
An int from 0..`FOPEN_MAX`, which is an index into some internal data
structure.
0 is for `stdin`, 1 for `stdout`, 2 for `stderr`.
Negative values usually indicate an error.
## stdio.h
`FILE`
A struct that encapsulates the C library's view of the operating system
files. A `FILE` should only be referred to via a pointer.
`fileno`
A field of `FILE` which is the Posix file descriptor for Posix systems, and
and an index into an array of file `HANDLE`s for Windows.
This array is how Posix behavior is emulated on Windows.
For Digital Mars C, that array is `__osfhnd[]`, and is initialized
at program start by the C runtime library.
In this module, they are typed as `fileno_t`.
`stdin`, `stdout`, `stderr`
Global pointers to `FILE` representing standard input, output, and error streams.
Being global means there are synchronization issues when multiple threads
are doing I/O on the same streams.
## std.stdio
*/
import core.stdc.stddef : wchar_t;
public import core.stdc.stdio;
import std.algorithm.mutation : copy;
@ -144,6 +201,7 @@ else
private alias FSChar = char;
}
private alias fileno_t = int; // file descriptor, fildes, fileno
version (Windows)
{
@ -207,7 +265,7 @@ version (DIGITAL_MARS_STDIO)
// @@@DEPRECATED_2.107@@@
deprecated("internal function _fileno was unintentionally available from "
~ "std.stdio and will be removed afer 2.107")
int _fileno(FILE* f) { return f._file; }
fileno_t _fileno(FILE* f) { return f._file; }
}
else version (MICROSOFT_STDIO)
{
@ -1144,7 +1202,7 @@ Throws: `ErrnoException` if the file is not opened or the call to `fread` fails.
enforce(isOpen, "Attempting to read from an unopened file");
version (Windows)
{
immutable fd = .fileno(_p.handle);
immutable fileno_t fd = .fileno(_p.handle);
immutable mode = .__setmode(fd, _O_BINARY);
scope(exit) .__setmode(fd, mode);
version (DIGITAL_MARS_STDIO)
@ -1234,7 +1292,7 @@ Throws: `ErrnoException` if the file is not opened or if the call to `fwrite` fa
version (Windows)
{
immutable fd = .fileno(_p.handle);
immutable fileno_t fd = .fileno(_p.handle);
immutable oldMode = .__setmode(fd, _O_BINARY);
if (oldMode != _O_BINARY)
@ -2291,7 +2349,7 @@ Returns the `FILE*` corresponding to this object.
/**
Returns the file number corresponding to this object.
*/
@property int fileno() const @trusted
@property fileno_t fileno() const @trusted
{
import std.exception : enforce;
@ -3343,7 +3401,8 @@ is empty, throws an `Exception`. In case of an I/O error throws
version (Windows)
{
int fd, oldMode;
fileno_t fd;
int oldMode;
version (DIGITAL_MARS_STDIO)
ubyte oldInfo;
}