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@347 | 23 | import native |
paul@347 | 24 | |
paul@347 | 25 | class filestream: |
paul@347 | 26 | |
paul@347 | 27 | "Generic file-oriented stream functionality." |
paul@347 | 28 | |
paul@347 | 29 | def __init__(self, bufsize=1024): |
paul@347 | 30 | self.bufsize = bufsize |
paul@347 | 31 | |
paul@347 | 32 | def read(self, n=0): |
paul@347 | 33 | |
paul@347 | 34 | "Read 'n' bytes from the stream." |
paul@347 | 35 | |
paul@347 | 36 | check_int(n) |
paul@347 | 37 | |
paul@347 | 38 | # Read any indicated number of bytes. |
paul@347 | 39 | |
paul@347 | 40 | if n > 0: |
paul@347 | 41 | return native._fread(self.__data__, n) |
paul@347 | 42 | |
paul@347 | 43 | # Read all remaining bytes. |
paul@347 | 44 | |
paul@347 | 45 | else: |
paul@347 | 46 | l = [] |
paul@347 | 47 | |
paul@347 | 48 | # Read until end-of-file. |
paul@347 | 49 | |
paul@347 | 50 | try: |
paul@347 | 51 | while True: |
paul@347 | 52 | l.append(native._fread(self.__data__, self.bufsize)) |
paul@347 | 53 | |
paul@347 | 54 | # Handle end-of-file reads. |
paul@347 | 55 | |
paul@347 | 56 | except EOFError: |
paul@347 | 57 | pass |
paul@347 | 58 | |
paul@347 | 59 | return "".join(l) |
paul@347 | 60 | |
paul@347 | 61 | def write(self, s): |
paul@347 | 62 | |
paul@347 | 63 | "Write string 's' to the stream." |
paul@347 | 64 | |
paul@347 | 65 | check_string(s) |
paul@347 | 66 | native._fwrite(self.__data__, s) |
paul@347 | 67 | |
paul@347 | 68 | def close(self): pass |
paul@347 | 69 | |
paul@347 | 70 | class file(filestream): |
paul@173 | 71 | |
paul@173 | 72 | "A file abstraction." |
paul@173 | 73 | |
paul@347 | 74 | def __init__(self, filename, mode="r", bufsize=1024): |
paul@347 | 75 | |
paul@347 | 76 | "Open the file with the given 'filename' using the given access 'mode'." |
paul@347 | 77 | |
paul@347 | 78 | get_using(filestream.__init__, self)(bufsize) |
paul@347 | 79 | self.__data__ = native._fopen(filename, mode) |
paul@347 | 80 | |
paul@6 | 81 | def readline(self, size=None): pass |
paul@6 | 82 | def readlines(self, size=None): pass |
paul@6 | 83 | |
paul@6 | 84 | # vim: tabstop=4 expandtab shiftwidth=4 |