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@392 | 23 | from native import isinstance as _isinstance, fclose, 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@351 | 35 | self.__data__ = None |
paul@347 | 36 | |
paul@347 | 37 | def read(self, n=0): |
paul@347 | 38 | |
paul@347 | 39 | "Read 'n' bytes from the stream." |
paul@347 | 40 | |
paul@347 | 41 | check_int(n) |
paul@347 | 42 | |
paul@347 | 43 | # Read any indicated number of bytes. |
paul@347 | 44 | |
paul@347 | 45 | if n > 0: |
paul@392 | 46 | s = fread(self.__data__, n) |
paul@347 | 47 | |
paul@347 | 48 | # Read all remaining bytes. |
paul@347 | 49 | |
paul@347 | 50 | else: |
paul@347 | 51 | l = [] |
paul@347 | 52 | |
paul@347 | 53 | # Read until end-of-file. |
paul@347 | 54 | |
paul@347 | 55 | try: |
paul@347 | 56 | while True: |
paul@356 | 57 | l.append(fread(self.__data__, self.bufsize)) |
paul@347 | 58 | |
paul@347 | 59 | # Handle end-of-file reads. |
paul@347 | 60 | |
paul@347 | 61 | except EOFError: |
paul@347 | 62 | pass |
paul@347 | 63 | |
paul@392 | 64 | s = "".join(l) |
paul@392 | 65 | |
paul@392 | 66 | # Convert bytes to text if necessary. |
paul@392 | 67 | |
paul@392 | 68 | if self.encoding: |
paul@392 | 69 | return unicode(s, self.encoding) |
paul@392 | 70 | else: |
paul@392 | 71 | return s |
paul@347 | 72 | |
paul@347 | 73 | def write(self, s): |
paul@347 | 74 | |
paul@347 | 75 | "Write string 's' to the stream." |
paul@347 | 76 | |
paul@347 | 77 | check_string(s) |
paul@392 | 78 | |
paul@401 | 79 | # Encode text as bytes if necessary. When the encoding is not set, any |
paul@401 | 80 | # original encoding of the text will be applied. |
paul@392 | 81 | |
paul@401 | 82 | if _isinstance(s, utf8string): |
paul@392 | 83 | s = s.encode(self.encoding) |
paul@392 | 84 | |
paul@356 | 85 | fwrite(self.__data__, s) |
paul@347 | 86 | |
paul@352 | 87 | def close(self): |
paul@352 | 88 | |
paul@352 | 89 | "Close the stream." |
paul@352 | 90 | |
paul@356 | 91 | fclose(self.__data__) |
paul@347 | 92 | |
paul@347 | 93 | class file(filestream): |
paul@173 | 94 | |
paul@173 | 95 | "A file abstraction." |
paul@173 | 96 | |
paul@392 | 97 | def __init__(self, filename, mode="r", encoding=None, bufsize=1024): |
paul@347 | 98 | |
paul@392 | 99 | """ |
paul@392 | 100 | Open the file with the given 'filename' using the given access 'mode', |
paul@392 | 101 | any specified 'encoding', and the given 'bufsize'. |
paul@392 | 102 | """ |
paul@347 | 103 | |
paul@392 | 104 | get_using(filestream.__init__, self)(encoding, bufsize) |
paul@356 | 105 | self.__data__ = fopen(filename, mode) |
paul@347 | 106 | |
paul@6 | 107 | def readline(self, size=None): pass |
paul@6 | 108 | def readlines(self, size=None): pass |
paul@6 | 109 | |
paul@6 | 110 | # vim: tabstop=4 expandtab shiftwidth=4 |