paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Input/output-related functions. |
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@173 | 22 | from native import _read, _write |
paul@173 | 23 | |
paul@173 | 24 | class sysfile: |
paul@173 | 25 | |
paul@173 | 26 | "A system-level file object." |
paul@173 | 27 | |
paul@173 | 28 | def __init__(self, fd): |
paul@173 | 29 | |
paul@173 | 30 | "Initialise the file with the given 'fd'." |
paul@173 | 31 | |
paul@173 | 32 | self.fd = fd |
paul@173 | 33 | |
paul@173 | 34 | def read(self, n): |
paul@173 | 35 | |
paul@173 | 36 | "Read 'n' bytes from the file." |
paul@173 | 37 | |
paul@173 | 38 | return _read(self.fd, n) |
paul@173 | 39 | |
paul@173 | 40 | def write(self, s): |
paul@173 | 41 | |
paul@173 | 42 | "Write 's' to the file." |
paul@173 | 43 | |
paul@173 | 44 | _write(self.fd, str(s)) |
paul@173 | 45 | |
paul@173 | 46 | def open(name, mode=None, buffering=None): |
paul@173 | 47 | |
paul@173 | 48 | """ |
paul@173 | 49 | Open the file with the given 'name', using the given 'mode' and applying the |
paul@173 | 50 | indicated 'buffering'. |
paul@173 | 51 | """ |
paul@173 | 52 | |
paul@173 | 53 | return file(name, mode, buffering) |
paul@173 | 54 | |
paul@6 | 55 | def raw_input(prompt=None): pass |
paul@6 | 56 | |
paul@173 | 57 | def print_(dest, args, nl): |
paul@173 | 58 | |
paul@173 | 59 | """ |
paul@173 | 60 | Write to 'dest' the string representation of 'args', adding a newline if |
paul@173 | 61 | 'nl' is given as a true value. |
paul@173 | 62 | """ |
paul@173 | 63 | |
paul@186 | 64 | # If imported at the module level, the sys module must be set up first, |
paul@186 | 65 | # which should be ensured by the module ordering activity, and a module |
paul@186 | 66 | # attribute will be employed to hold a reference to sys.stdout. |
paul@184 | 67 | |
paul@184 | 68 | from sys import stdout |
paul@184 | 69 | |
paul@173 | 70 | # Write to standard output if dest is not specified. |
paul@173 | 71 | |
paul@173 | 72 | dest = dest or stdout |
paul@173 | 73 | |
paul@173 | 74 | first = True |
paul@173 | 75 | |
paul@173 | 76 | for arg in args: |
paul@173 | 77 | |
paul@173 | 78 | # Insert spaces between arguments. |
paul@173 | 79 | |
paul@173 | 80 | if first: |
paul@173 | 81 | first = False |
paul@173 | 82 | else: |
paul@173 | 83 | dest.write(" ") |
paul@173 | 84 | |
paul@173 | 85 | dest.write(str(arg)) |
paul@173 | 86 | |
paul@173 | 87 | # Add a newline if specified. |
paul@173 | 88 | |
paul@173 | 89 | if nl: |
paul@173 | 90 | dest.write("\n") |
paul@173 | 91 | |
paul@6 | 92 | # vim: tabstop=4 expandtab shiftwidth=4 |