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