paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | File objects. |
paul@6 | 5 | |
paul@173 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@347 | 22 | from __builtins__.types import check_int, check_string |
paul@436 | 23 | from native import isinstance as _isinstance, fclose, fflush, fopen, fread, fwrite |
paul@347 | 24 | |
paul@347 | 25 | class filestream: |
paul@347 | 26 | |
paul@347 | 27 | "Generic file-oriented stream functionality." |
paul@347 | 28 | |
paul@392 | 29 | def __init__(self, encoding=None, bufsize=1024): |
paul@351 | 30 | |
paul@392 | 31 | "Initialise the stream with the given 'encoding' and 'bufsize'." |
paul@351 | 32 | |
paul@392 | 33 | self.encoding = encoding |
paul@347 | 34 | self.bufsize = bufsize |
paul@435 | 35 | |
paul@435 | 36 | # Internal stream details. |
paul@435 | 37 | |
paul@351 | 38 | self.__data__ = None |
paul@347 | 39 | |
paul@435 | 40 | def _convert(self, bytes): |
paul@435 | 41 | |
paul@435 | 42 | "Convert 'bytes' to text if necessary." |
paul@435 | 43 | |
paul@435 | 44 | if self.encoding: |
paul@435 | 45 | return unicode(bytes, self.encoding) |
paul@435 | 46 | else: |
paul@435 | 47 | return bytes |
paul@435 | 48 | |
paul@436 | 49 | def flush(self): |
paul@436 | 50 | |
paul@436 | 51 | "Flush the stream." |
paul@436 | 52 | |
paul@436 | 53 | fflush(self.__data__) |
paul@436 | 54 | |
paul@347 | 55 | def read(self, n=0): |
paul@347 | 56 | |
paul@347 | 57 | "Read 'n' bytes from the stream." |
paul@347 | 58 | |
paul@347 | 59 | check_int(n) |
paul@347 | 60 | |
paul@347 | 61 | # Read any indicated number of bytes. |
paul@347 | 62 | |
paul@347 | 63 | if n > 0: |
paul@392 | 64 | s = fread(self.__data__, n) |
paul@347 | 65 | |
paul@347 | 66 | # Read all remaining bytes. |
paul@347 | 67 | |
paul@347 | 68 | else: |
paul@347 | 69 | l = [] |
paul@347 | 70 | |
paul@347 | 71 | # Read until end-of-file. |
paul@347 | 72 | |
paul@347 | 73 | try: |
paul@347 | 74 | while True: |
paul@436 | 75 | self._read_data(l) |
paul@347 | 76 | |
paul@347 | 77 | # Handle end-of-file reads. |
paul@347 | 78 | |
paul@347 | 79 | except EOFError: |
paul@347 | 80 | pass |
paul@347 | 81 | |
paul@392 | 82 | s = "".join(l) |
paul@392 | 83 | |
paul@435 | 84 | return self._convert(s) |
paul@435 | 85 | |
paul@435 | 86 | def readline(self, n=0): |
paul@435 | 87 | |
paul@435 | 88 | """ |
paul@435 | 89 | Read until an end-of-line indicator is encountered or at most 'n' bytes, |
paul@435 | 90 | if indicated. |
paul@435 | 91 | """ |
paul@435 | 92 | |
paul@435 | 93 | check_int(n) |
paul@435 | 94 | |
paul@435 | 95 | # Read any indicated number of bytes. |
paul@435 | 96 | |
paul@435 | 97 | if n > 0: |
paul@435 | 98 | s = fread(self.__data__, n) |
paul@435 | 99 | |
paul@435 | 100 | # Read until an end-of-line indicator. |
paul@435 | 101 | |
paul@435 | 102 | else: |
paul@435 | 103 | l = [] |
paul@392 | 104 | |
paul@435 | 105 | # Read until end-of-line or end-of-file. |
paul@435 | 106 | |
paul@435 | 107 | try: |
paul@436 | 108 | while not self._read_until_newline(l): |
paul@436 | 109 | pass |
paul@435 | 110 | |
paul@435 | 111 | # Handle end-of-file reads. |
paul@435 | 112 | |
paul@435 | 113 | except EOFError: |
paul@435 | 114 | pass |
paul@435 | 115 | |
paul@435 | 116 | s = "".join(l) |
paul@435 | 117 | |
paul@435 | 118 | return self._convert(s) |
paul@435 | 119 | |
paul@436 | 120 | def _read_data(self, l): |
paul@436 | 121 | |
paul@436 | 122 | "Read data into 'l'." |
paul@436 | 123 | |
paul@436 | 124 | l.append(fread(self.__data__, self.bufsize)) |
paul@436 | 125 | |
paul@436 | 126 | def _read_until_newline(self, l): |
paul@436 | 127 | |
paul@436 | 128 | "Read data into 'l', returning whether a newline has been read." |
paul@436 | 129 | |
paul@436 | 130 | # NOTE: Only POSIX newlines are supported currently. |
paul@436 | 131 | |
paul@436 | 132 | s = fread(self.__data__, 1) |
paul@436 | 133 | l.append(s) |
paul@436 | 134 | return s == "\n" |
paul@436 | 135 | |
paul@435 | 136 | def readlines(self, n=None): pass |
paul@347 | 137 | |
paul@347 | 138 | def write(self, s): |
paul@347 | 139 | |
paul@347 | 140 | "Write string 's' to the stream." |
paul@347 | 141 | |
paul@347 | 142 | check_string(s) |
paul@392 | 143 | |
paul@401 | 144 | # Encode text as bytes if necessary. When the encoding is not set, any |
paul@401 | 145 | # original encoding of the text will be applied. |
paul@392 | 146 | |
paul@401 | 147 | if _isinstance(s, utf8string): |
paul@392 | 148 | s = s.encode(self.encoding) |
paul@392 | 149 | |
paul@356 | 150 | fwrite(self.__data__, s) |
paul@347 | 151 | |
paul@352 | 152 | def close(self): |
paul@352 | 153 | |
paul@352 | 154 | "Close the stream." |
paul@352 | 155 | |
paul@356 | 156 | fclose(self.__data__) |
paul@347 | 157 | |
paul@347 | 158 | class file(filestream): |
paul@173 | 159 | |
paul@173 | 160 | "A file abstraction." |
paul@173 | 161 | |
paul@392 | 162 | def __init__(self, filename, mode="r", encoding=None, bufsize=1024): |
paul@347 | 163 | |
paul@392 | 164 | """ |
paul@392 | 165 | Open the file with the given 'filename' using the given access 'mode', |
paul@392 | 166 | any specified 'encoding', and the given 'bufsize'. |
paul@392 | 167 | """ |
paul@347 | 168 | |
paul@392 | 169 | get_using(filestream.__init__, self)(encoding, bufsize) |
paul@356 | 170 | self.__data__ = fopen(filename, mode) |
paul@436 | 171 | self.buffered = "" |
paul@436 | 172 | |
paul@436 | 173 | def _get_data(self): |
paul@436 | 174 | |
paul@436 | 175 | "Get data from the file." |
paul@436 | 176 | |
paul@436 | 177 | if self.buffered: |
paul@436 | 178 | s = self.buffered |
paul@436 | 179 | self.buffered = "" |
paul@436 | 180 | else: |
paul@436 | 181 | s = fread(self.__data__, self.bufsize) |
paul@436 | 182 | |
paul@436 | 183 | return s |
paul@436 | 184 | |
paul@436 | 185 | def _read_data(self, l): |
paul@436 | 186 | |
paul@436 | 187 | "Read data into 'l'." |
paul@436 | 188 | |
paul@436 | 189 | s = self._get_data() |
paul@436 | 190 | l.append(s) |
paul@436 | 191 | |
paul@436 | 192 | def _read_until_newline(self, l): |
paul@436 | 193 | |
paul@436 | 194 | "Read data into 'l', returning whether a newline has been read." |
paul@436 | 195 | |
paul@436 | 196 | s = self._get_data() |
paul@436 | 197 | |
paul@436 | 198 | # NOTE: Only POSIX newlines are supported currently. |
paul@436 | 199 | |
paul@436 | 200 | i = s.find("\n") |
paul@436 | 201 | |
paul@436 | 202 | if i != -1: |
paul@436 | 203 | l.append(s[:i+1]) |
paul@436 | 204 | self.buffered = s[i+1:] |
paul@436 | 205 | return True |
paul@436 | 206 | |
paul@436 | 207 | l.append(s) |
paul@436 | 208 | return False |
paul@347 | 209 | |
paul@6 | 210 | # vim: tabstop=4 expandtab shiftwidth=4 |