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