paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Generic file access. |
paul@44 | 5 | |
paul@69 | 6 | Copyright (C) 2009, 2010 Paul Boddie <paul@boddie.org.uk> |
paul@44 | 7 | |
paul@44 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@44 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@44 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@44 | 11 | version. |
paul@44 | 12 | |
paul@44 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@44 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@44 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@44 | 16 | |
paul@44 | 17 | You should have received a copy of the GNU General Public License along |
paul@44 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@44 | 19 | """ |
paul@44 | 20 | |
paul@44 | 21 | from iixr.data import vint |
paul@67 | 22 | from array import array |
paul@50 | 23 | import zlib |
paul@44 | 24 | |
paul@44 | 25 | # Constants. |
paul@44 | 26 | |
paul@44 | 27 | class File: |
paul@44 | 28 | |
paul@44 | 29 | "A basic file abstraction." |
paul@44 | 30 | |
paul@44 | 31 | def __init__(self, f): |
paul@44 | 32 | self.f = f |
paul@44 | 33 | self.reset() |
paul@44 | 34 | |
paul@44 | 35 | def reset(self): |
paul@44 | 36 | |
paul@44 | 37 | "To be used to reset the state of the reader or writer between records." |
paul@44 | 38 | |
paul@44 | 39 | pass |
paul@44 | 40 | |
paul@69 | 41 | def seek(self, offset): |
paul@69 | 42 | self.f.seek(offset) |
paul@69 | 43 | self.reset() |
paul@69 | 44 | |
paul@44 | 45 | def rewind(self): |
paul@52 | 46 | self.f.seek(0) |
paul@44 | 47 | self.reset() |
paul@44 | 48 | |
paul@44 | 49 | def close(self): |
paul@44 | 50 | if self.f is not None: |
paul@44 | 51 | self.f.close() |
paul@44 | 52 | self.f = None |
paul@44 | 53 | |
paul@44 | 54 | class FileWriter(File): |
paul@44 | 55 | |
paul@44 | 56 | "Writing basic data types to files." |
paul@44 | 57 | |
paul@44 | 58 | def __init__(self, f): |
paul@44 | 59 | File.__init__(self, f) |
paul@44 | 60 | |
paul@44 | 61 | def write_number(self, number): |
paul@44 | 62 | |
paul@44 | 63 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 64 | |
paul@52 | 65 | self.f.write(vint(number)) |
paul@44 | 66 | |
paul@44 | 67 | def write_string(self, s, compress=0): |
paul@44 | 68 | |
paul@44 | 69 | """ |
paul@44 | 70 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 71 | if 'compress' is set to a true value. |
paul@44 | 72 | """ |
paul@44 | 73 | |
paul@44 | 74 | # Convert Unicode objects to strings. |
paul@44 | 75 | |
paul@44 | 76 | if isinstance(s, unicode): |
paul@44 | 77 | s = s.encode("utf-8") |
paul@44 | 78 | |
paul@44 | 79 | # Compress the string if requested. |
paul@44 | 80 | |
paul@44 | 81 | if compress: |
paul@50 | 82 | cs = zlib.compress(s) |
paul@50 | 83 | |
paul@50 | 84 | # Take any shorter than the original. |
paul@44 | 85 | |
paul@50 | 86 | if len(cs) < len(s): |
paul@50 | 87 | flag = "z" |
paul@50 | 88 | s = cs |
paul@44 | 89 | else: |
paul@44 | 90 | flag = "-" |
paul@44 | 91 | |
paul@44 | 92 | else: |
paul@44 | 93 | flag = "" |
paul@44 | 94 | |
paul@44 | 95 | # Write the length of the data before the data itself. |
paul@44 | 96 | |
paul@44 | 97 | length = len(s) |
paul@52 | 98 | self.f.write("".join([flag, vint(length), s])) |
paul@44 | 99 | |
paul@44 | 100 | class FileReader(File): |
paul@44 | 101 | |
paul@44 | 102 | "Reading basic data types from files." |
paul@44 | 103 | |
paul@44 | 104 | def __init__(self, f): |
paul@44 | 105 | File.__init__(self, f) |
paul@44 | 106 | |
paul@44 | 107 | def read_number(self): |
paul@44 | 108 | |
paul@44 | 109 | "Read a number from the file." |
paul@44 | 110 | |
paul@44 | 111 | # Read each byte, adding it to the number. |
paul@44 | 112 | |
paul@67 | 113 | a = array('B') |
paul@67 | 114 | fromfile = a.fromfile |
paul@67 | 115 | f = self.f |
paul@44 | 116 | |
paul@67 | 117 | fromfile(f, 1) |
paul@67 | 118 | csd = a[-1] |
paul@67 | 119 | if csd < 128: |
paul@67 | 120 | return csd |
paul@55 | 121 | else: |
paul@67 | 122 | while csd & 128: |
paul@67 | 123 | fromfile(f, 1) |
paul@67 | 124 | csd = a[-1] |
paul@67 | 125 | return sum([((csd & 127) << (number * 7)) for (number, csd) in enumerate(a)]) |
paul@44 | 126 | |
paul@44 | 127 | def read_string(self, decompress=0): |
paul@44 | 128 | |
paul@44 | 129 | """ |
paul@44 | 130 | Read a string from the file, decompressing the stored data if |
paul@44 | 131 | 'decompress' is set to a true value. |
paul@44 | 132 | """ |
paul@44 | 133 | |
paul@52 | 134 | read = self.f.read |
paul@52 | 135 | |
paul@44 | 136 | # Decompress the data if requested. |
paul@44 | 137 | |
paul@44 | 138 | if decompress: |
paul@52 | 139 | flag = read(1) |
paul@44 | 140 | else: |
paul@44 | 141 | flag = "-" |
paul@44 | 142 | |
paul@44 | 143 | length = self.read_number() |
paul@52 | 144 | s = read(length) |
paul@44 | 145 | |
paul@44 | 146 | # Perform decompression if applicable. |
paul@44 | 147 | |
paul@50 | 148 | if flag == "z": |
paul@50 | 149 | s = zlib.decompress(s) |
paul@44 | 150 | |
paul@44 | 151 | # Convert strings to Unicode objects. |
paul@44 | 152 | |
paul@44 | 153 | return unicode(s, "utf-8") |
paul@44 | 154 | |
paul@44 | 155 | # vim: tabstop=4 expandtab shiftwidth=4 |