1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/libc/io.py Thu Feb 02 22:41:07 2017 +0100
1.3 @@ -0,0 +1,129 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +C library input/output.
1.8 +
1.9 +Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from __builtins__.stream import filestream
1.26 +from __builtins__.types import check_int, check_string
1.27 +
1.28 +import locale
1.29 +
1.30 +from native import (
1.31 + close as _close,
1.32 + fdopen as _fdopen,
1.33 + read as _read,
1.34 + write as _write
1.35 + )
1.36 +
1.37 +# Abstractions for system-level files and streams.
1.38 +
1.39 +class sysfile:
1.40 +
1.41 + "A system-level file object."
1.42 +
1.43 + def __init__(self, fd):
1.44 +
1.45 + "Initialise the file with the given 'fd'."
1.46 +
1.47 + self.fd = fd
1.48 +
1.49 + def read(self, n):
1.50 +
1.51 + "Read 'n' bytes from the file, returning a string."
1.52 +
1.53 + return read(self.fd, n)
1.54 +
1.55 + def write(self, s):
1.56 +
1.57 + "Write string 's' to the file."
1.58 +
1.59 + return write(self.fd, s)
1.60 +
1.61 + def close(self):
1.62 +
1.63 + "Close the file."
1.64 +
1.65 + close(self.fd)
1.66 +
1.67 +class sysstream(filestream):
1.68 +
1.69 + "A system-level stream object."
1.70 +
1.71 + def __init__(self, fd, mode="r", encoding=None, bufsize=1024):
1.72 +
1.73 + """
1.74 + Initialise the stream with the given 'fd', 'mode', 'encoding' and
1.75 + 'bufsize'.
1.76 + """
1.77 +
1.78 + check_int(fd)
1.79 + check_string(mode)
1.80 +
1.81 + get_using(filestream.__init__, self)(encoding, bufsize)
1.82 + self.__data__ = _fdopen(fd, mode)
1.83 +
1.84 +# Standard streams.
1.85 +
1.86 +stdin = sysstream(0)
1.87 +stdout = sysstream(1, "w")
1.88 +stderr = sysstream(2, "w")
1.89 +
1.90 +# Localised streams.
1.91 +# Perform locale initialisation explicitly to ensure that the locale module
1.92 +# and various function defaults have been initialised.
1.93 +
1.94 +locale.initlocale()
1.95 +lstdin = sysstream(0, "r", locale.getpreferredencoding())
1.96 +
1.97 +# Input/output functions.
1.98 +
1.99 +def close(fd):
1.100 +
1.101 + "Close the file descriptor 'fd'."
1.102 +
1.103 + _close(fd)
1.104 +
1.105 +def fdopen(fd, mode="r"):
1.106 +
1.107 + """
1.108 + Open a stream for the given file descriptor 'fd', operating in the given
1.109 + 'mode'.
1.110 + """
1.111 +
1.112 + return sysstream(fd, mode)
1.113 +
1.114 +def read(fd, n):
1.115 +
1.116 + """
1.117 + Read using the low-level file descriptor 'fd' the given number of bytes 'n'.
1.118 + """
1.119 +
1.120 + check_int(fd)
1.121 + check_int(n)
1.122 + return _read(fd, n)
1.123 +
1.124 +def write(fd, s):
1.125 +
1.126 + "Write using the low-level file descriptor 'fd' the given string 's'."
1.127 +
1.128 + check_int(fd)
1.129 + check_string(s)
1.130 + return _write(fd, s)
1.131 +
1.132 +# vim: tabstop=4 expandtab shiftwidth=4