1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/__builtins__/stream.py Thu Feb 02 22:41:07 2017 +0100
1.3 @@ -0,0 +1,158 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Stream objects.
1.8 +
1.9 +Copyright (C) 2015, 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__.types import check_int, check_string
1.26 +from native import isinstance as _isinstance, fclose, fflush, fread, fwrite
1.27 +
1.28 +class filestream:
1.29 +
1.30 + "Generic file-oriented stream functionality."
1.31 +
1.32 + def __init__(self, encoding=None, bufsize=1024):
1.33 +
1.34 + "Initialise the stream with the given 'encoding' and 'bufsize'."
1.35 +
1.36 + self.encoding = encoding
1.37 + self.bufsize = bufsize
1.38 +
1.39 + # Internal stream details.
1.40 +
1.41 + self.__data__ = None
1.42 +
1.43 + def _convert(self, bytes):
1.44 +
1.45 + "Convert 'bytes' to text if necessary."
1.46 +
1.47 + if self.encoding:
1.48 + return unicode(bytes, self.encoding)
1.49 + else:
1.50 + return bytes
1.51 +
1.52 + def flush(self):
1.53 +
1.54 + "Flush the stream."
1.55 +
1.56 + fflush(self.__data__)
1.57 +
1.58 + def read(self, n=0):
1.59 +
1.60 + "Read 'n' bytes from the stream."
1.61 +
1.62 + check_int(n)
1.63 +
1.64 + # Read any indicated number of bytes.
1.65 +
1.66 + if n > 0:
1.67 + s = fread(self.__data__, n)
1.68 +
1.69 + # Read all remaining bytes.
1.70 +
1.71 + else:
1.72 + l = []
1.73 +
1.74 + # Read until end-of-file.
1.75 +
1.76 + try:
1.77 + while True:
1.78 + self._read_data(l)
1.79 +
1.80 + # Handle end-of-file reads.
1.81 +
1.82 + except EOFError:
1.83 + pass
1.84 +
1.85 + s = "".join(l)
1.86 +
1.87 + return self._convert(s)
1.88 +
1.89 + def readline(self, n=0):
1.90 +
1.91 + """
1.92 + Read until an end-of-line indicator is encountered or at most 'n' bytes,
1.93 + if indicated.
1.94 + """
1.95 +
1.96 + check_int(n)
1.97 +
1.98 + # Read any indicated number of bytes.
1.99 +
1.100 + if n > 0:
1.101 + s = fread(self.__data__, n)
1.102 +
1.103 + # Read until an end-of-line indicator.
1.104 +
1.105 + else:
1.106 + l = []
1.107 +
1.108 + # Read until end-of-line or end-of-file.
1.109 +
1.110 + try:
1.111 + while not self._read_until_newline(l):
1.112 + pass
1.113 +
1.114 + # Handle end-of-file reads.
1.115 +
1.116 + except EOFError:
1.117 + pass
1.118 +
1.119 + s = "".join(l)
1.120 +
1.121 + return self._convert(s)
1.122 +
1.123 + def _read_data(self, l):
1.124 +
1.125 + "Read data into 'l'."
1.126 +
1.127 + l.append(fread(self.__data__, self.bufsize))
1.128 +
1.129 + def _read_until_newline(self, l):
1.130 +
1.131 + "Read data into 'l', returning whether a newline has been read."
1.132 +
1.133 + # NOTE: Only POSIX newlines are supported currently.
1.134 +
1.135 + s = fread(self.__data__, 1)
1.136 + l.append(s)
1.137 + return s == "\n"
1.138 +
1.139 + def readlines(self, n=None): pass
1.140 +
1.141 + def write(self, s):
1.142 +
1.143 + "Write string 's' to the stream."
1.144 +
1.145 + check_string(s)
1.146 +
1.147 + # Encode text as bytes if necessary. When the encoding is not set, any
1.148 + # original encoding of the text will be applied.
1.149 +
1.150 + if _isinstance(s, utf8string):
1.151 + s = s.encode(self.encoding)
1.152 +
1.153 + fwrite(self.__data__, s)
1.154 +
1.155 + def close(self):
1.156 +
1.157 + "Close the stream."
1.158 +
1.159 + fclose(self.__data__)
1.160 +
1.161 +# vim: tabstop=4 expandtab shiftwidth=4