1.1 --- a/lib/__builtins__/file.py Thu Feb 02 22:38:26 2017 +0100
1.2 +++ b/lib/__builtins__/file.py Thu Feb 02 22:41:07 2017 +0100
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 File objects.
1.6
1.7 -Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -19,141 +19,8 @@
1.13 this program. If not, see <http://www.gnu.org/licenses/>.
1.14 """
1.15
1.16 -from __builtins__.types import check_int, check_string
1.17 -from native import isinstance as _isinstance, fclose, fflush, fopen, fread, fwrite
1.18 -
1.19 -class filestream:
1.20 -
1.21 - "Generic file-oriented stream functionality."
1.22 -
1.23 - def __init__(self, encoding=None, bufsize=1024):
1.24 -
1.25 - "Initialise the stream with the given 'encoding' and 'bufsize'."
1.26 -
1.27 - self.encoding = encoding
1.28 - self.bufsize = bufsize
1.29 -
1.30 - # Internal stream details.
1.31 -
1.32 - self.__data__ = None
1.33 -
1.34 - def _convert(self, bytes):
1.35 -
1.36 - "Convert 'bytes' to text if necessary."
1.37 -
1.38 - if self.encoding:
1.39 - return unicode(bytes, self.encoding)
1.40 - else:
1.41 - return bytes
1.42 -
1.43 - def flush(self):
1.44 -
1.45 - "Flush the stream."
1.46 -
1.47 - fflush(self.__data__)
1.48 -
1.49 - def read(self, n=0):
1.50 -
1.51 - "Read 'n' bytes from the stream."
1.52 -
1.53 - check_int(n)
1.54 -
1.55 - # Read any indicated number of bytes.
1.56 -
1.57 - if n > 0:
1.58 - s = fread(self.__data__, n)
1.59 -
1.60 - # Read all remaining bytes.
1.61 -
1.62 - else:
1.63 - l = []
1.64 -
1.65 - # Read until end-of-file.
1.66 -
1.67 - try:
1.68 - while True:
1.69 - self._read_data(l)
1.70 -
1.71 - # Handle end-of-file reads.
1.72 -
1.73 - except EOFError:
1.74 - pass
1.75 -
1.76 - s = "".join(l)
1.77 -
1.78 - return self._convert(s)
1.79 -
1.80 - def readline(self, n=0):
1.81 -
1.82 - """
1.83 - Read until an end-of-line indicator is encountered or at most 'n' bytes,
1.84 - if indicated.
1.85 - """
1.86 -
1.87 - check_int(n)
1.88 -
1.89 - # Read any indicated number of bytes.
1.90 -
1.91 - if n > 0:
1.92 - s = fread(self.__data__, n)
1.93 -
1.94 - # Read until an end-of-line indicator.
1.95 -
1.96 - else:
1.97 - l = []
1.98 -
1.99 - # Read until end-of-line or end-of-file.
1.100 -
1.101 - try:
1.102 - while not self._read_until_newline(l):
1.103 - pass
1.104 -
1.105 - # Handle end-of-file reads.
1.106 -
1.107 - except EOFError:
1.108 - pass
1.109 -
1.110 - s = "".join(l)
1.111 -
1.112 - return self._convert(s)
1.113 -
1.114 - def _read_data(self, l):
1.115 -
1.116 - "Read data into 'l'."
1.117 -
1.118 - l.append(fread(self.__data__, self.bufsize))
1.119 -
1.120 - def _read_until_newline(self, l):
1.121 -
1.122 - "Read data into 'l', returning whether a newline has been read."
1.123 -
1.124 - # NOTE: Only POSIX newlines are supported currently.
1.125 -
1.126 - s = fread(self.__data__, 1)
1.127 - l.append(s)
1.128 - return s == "\n"
1.129 -
1.130 - def readlines(self, n=None): pass
1.131 -
1.132 - def write(self, s):
1.133 -
1.134 - "Write string 's' to the stream."
1.135 -
1.136 - check_string(s)
1.137 -
1.138 - # Encode text as bytes if necessary. When the encoding is not set, any
1.139 - # original encoding of the text will be applied.
1.140 -
1.141 - if _isinstance(s, utf8string):
1.142 - s = s.encode(self.encoding)
1.143 -
1.144 - fwrite(self.__data__, s)
1.145 -
1.146 - def close(self):
1.147 -
1.148 - "Close the stream."
1.149 -
1.150 - fclose(self.__data__)
1.151 +from __builtins__.stream import filestream
1.152 +from native import fopen, fread
1.153
1.154 class file(filestream):
1.155