paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Generic file access. |
paul@44 | 5 | |
paul@86 | 6 | Copyright (C) 2009, 2010, 2011 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@89 | 21 | from iixr.data import * |
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@89 | 33 | self.data = array('B') # master buffer |
paul@89 | 34 | self.record = array('B') # record buffer |
paul@44 | 35 | self.reset() |
paul@44 | 36 | |
paul@44 | 37 | def reset(self): |
paul@44 | 38 | |
paul@44 | 39 | "To be used to reset the state of the reader or writer between records." |
paul@44 | 40 | |
paul@44 | 41 | pass |
paul@44 | 42 | |
paul@69 | 43 | def seek(self, offset): |
paul@69 | 44 | self.f.seek(offset) |
paul@69 | 45 | self.reset() |
paul@69 | 46 | |
paul@44 | 47 | def rewind(self): |
paul@52 | 48 | self.f.seek(0) |
paul@44 | 49 | self.reset() |
paul@44 | 50 | |
paul@44 | 51 | def close(self): |
paul@44 | 52 | if self.f is not None: |
paul@44 | 53 | self.f.close() |
paul@44 | 54 | self.f = None |
paul@44 | 55 | |
paul@44 | 56 | class FileWriter(File): |
paul@44 | 57 | |
paul@44 | 58 | "Writing basic data types to files." |
paul@44 | 59 | |
paul@88 | 60 | def tell(self): |
paul@88 | 61 | return self.f.tell() + len(self.data) |
paul@44 | 62 | |
paul@89 | 63 | def begin_record(self): |
paul@89 | 64 | pass |
paul@89 | 65 | |
paul@89 | 66 | def end_record(self): |
paul@89 | 67 | vint_to_array(len(self.record), self.data) |
paul@89 | 68 | self.data += self.record |
paul@89 | 69 | self.record = array('B') |
paul@89 | 70 | |
paul@44 | 71 | def write_number(self, number): |
paul@44 | 72 | |
paul@44 | 73 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 74 | |
paul@89 | 75 | vint_to_array(number, self.record) |
paul@44 | 76 | |
paul@86 | 77 | def write_numbers(self, numbers): |
paul@86 | 78 | |
paul@86 | 79 | "Write 'numbers' to the file using a variable length encoding." |
paul@86 | 80 | |
paul@86 | 81 | for number in numbers: |
paul@89 | 82 | vint_to_array(number, self.record) |
paul@86 | 83 | |
paul@44 | 84 | def write_string(self, s, compress=0): |
paul@44 | 85 | |
paul@44 | 86 | """ |
paul@44 | 87 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 88 | if 'compress' is set to a true value. |
paul@44 | 89 | """ |
paul@44 | 90 | |
paul@44 | 91 | # Convert Unicode objects to strings. |
paul@44 | 92 | |
paul@44 | 93 | if isinstance(s, unicode): |
paul@44 | 94 | s = s.encode("utf-8") |
paul@44 | 95 | |
paul@44 | 96 | # Compress the string if requested. |
paul@44 | 97 | |
paul@44 | 98 | if compress: |
paul@50 | 99 | cs = zlib.compress(s) |
paul@50 | 100 | |
paul@50 | 101 | # Take any shorter than the original. |
paul@44 | 102 | |
paul@50 | 103 | if len(cs) < len(s): |
paul@50 | 104 | flag = "z" |
paul@50 | 105 | s = cs |
paul@44 | 106 | else: |
paul@44 | 107 | flag = "-" |
paul@44 | 108 | |
paul@44 | 109 | else: |
paul@44 | 110 | flag = "" |
paul@44 | 111 | |
paul@44 | 112 | # Write the length of the data before the data itself. |
paul@44 | 113 | |
paul@44 | 114 | length = len(s) |
paul@89 | 115 | self.record.fromstring("".join([flag, vint(length), s])) |
paul@89 | 116 | |
paul@89 | 117 | def write_sequence_value(self, value): |
paul@89 | 118 | sequence_to_array(value, self.record) |
paul@89 | 119 | |
paul@89 | 120 | def write_sequence_values(self, values): |
paul@89 | 121 | vint_to_array(len(values), self.record) |
paul@89 | 122 | for value in values: |
paul@89 | 123 | self.write_sequence_value(value) |
paul@44 | 124 | |
paul@89 | 125 | def write_delta_sequence(self, values): |
paul@89 | 126 | convert_sequence(values, get_subtractor(values[0])) |
paul@89 | 127 | self.write_sequence_values(values) |
paul@89 | 128 | |
paul@89 | 129 | def write_monotonic_sequence(self, values): |
paul@89 | 130 | convert_sequence(values, get_monotonic_subtractor(values[0])) |
paul@89 | 131 | self.write_sequence_values(values) |
paul@74 | 132 | |
paul@89 | 133 | def flush(self): |
paul@89 | 134 | if self.f is not None: |
paul@89 | 135 | self.data.tofile(self.f) |
paul@89 | 136 | self.data = array('B') |
paul@89 | 137 | |
paul@89 | 138 | def close(self): |
paul@89 | 139 | self.flush() |
paul@89 | 140 | File.close(self) |
paul@74 | 141 | |
paul@44 | 142 | class FileReader(File): |
paul@44 | 143 | |
paul@44 | 144 | "Reading basic data types from files." |
paul@44 | 145 | |
paul@89 | 146 | def begin_record(self): |
paul@89 | 147 | size = self.read_number_from_file() |
paul@89 | 148 | self.record.fromfile(self.f, size) |
paul@89 | 149 | self.start = 0 |
paul@89 | 150 | |
paul@89 | 151 | def end_record(self): |
paul@89 | 152 | self.record = array('B') |
paul@89 | 153 | |
paul@89 | 154 | def read_number_from_file(self): |
paul@44 | 155 | |
paul@44 | 156 | "Read a number from the file." |
paul@44 | 157 | |
paul@44 | 158 | # Read each byte, adding it to the number. |
paul@44 | 159 | |
paul@88 | 160 | f = self.f |
paul@89 | 161 | a = array('B') |
paul@67 | 162 | fromfile = a.fromfile |
paul@44 | 163 | |
paul@89 | 164 | fromfile(f, 1) |
paul@89 | 165 | csd = a[-1] |
paul@89 | 166 | if csd < 128: |
paul@89 | 167 | return csd |
paul@89 | 168 | else: |
paul@89 | 169 | while csd & 128: |
paul@89 | 170 | fromfile(f, 1) |
paul@89 | 171 | csd = a[-1] |
paul@89 | 172 | return vint_from_array(a) |
paul@89 | 173 | |
paul@89 | 174 | def read_number(self): |
paul@89 | 175 | |
paul@89 | 176 | "Read a number from the current record." |
paul@89 | 177 | |
paul@89 | 178 | n, self.start = vint_from_array_start(self.record, self.start) |
paul@89 | 179 | return n |
paul@44 | 180 | |
paul@44 | 181 | def read_string(self, decompress=0): |
paul@44 | 182 | |
paul@44 | 183 | """ |
paul@89 | 184 | Read a string from the current record, decompressing the stored data if |
paul@44 | 185 | 'decompress' is set to a true value. |
paul@44 | 186 | """ |
paul@44 | 187 | |
paul@44 | 188 | # Decompress the data if requested. |
paul@44 | 189 | |
paul@44 | 190 | if decompress: |
paul@89 | 191 | flag = chr(self.record[self.start]) |
paul@89 | 192 | self.start += 1 |
paul@44 | 193 | else: |
paul@44 | 194 | flag = "-" |
paul@44 | 195 | |
paul@44 | 196 | length = self.read_number() |
paul@89 | 197 | start = self.start |
paul@89 | 198 | self.start += length |
paul@89 | 199 | s = self.record[start:self.start].tostring() |
paul@44 | 200 | |
paul@89 | 201 | # Perform decompression if applicable. |
paul@44 | 202 | |
paul@89 | 203 | if flag == "z": |
paul@89 | 204 | s = zlib.decompress(s) |
paul@88 | 205 | |
paul@89 | 206 | # Convert strings to Unicode objects. |
paul@44 | 207 | |
paul@89 | 208 | return unicode(s, "utf-8") |
paul@44 | 209 | |
paul@89 | 210 | def read_sequence_value(self): |
paul@89 | 211 | value, self.start = sequence_from_array(self.record, self.start) |
paul@89 | 212 | return value |
paul@44 | 213 | |
paul@89 | 214 | def read_sequences(self): |
paul@89 | 215 | values = [] |
paul@89 | 216 | length = self.read_number() |
paul@89 | 217 | i = 0 |
paul@89 | 218 | while i < length: |
paul@89 | 219 | values.append(self.read_sequence_value()) |
paul@89 | 220 | i += 1 |
paul@89 | 221 | return values |
paul@87 | 222 | |
paul@89 | 223 | def read_delta_sequence(self): |
paul@89 | 224 | values = self.read_sequences() |
paul@89 | 225 | convert_sequence(values, get_adder(values[0])) |
paul@89 | 226 | return values |
paul@87 | 227 | |
paul@89 | 228 | def read_monotonic_sequence(self): |
paul@89 | 229 | values = self.read_sequences() |
paul@89 | 230 | convert_sequence(values, get_monotonic_adder(values[0])) |
paul@89 | 231 | return values |
paul@74 | 232 | |
paul@44 | 233 | # vim: tabstop=4 expandtab shiftwidth=4 |