paul@44 | 1 | #!/usr/bin/env python |
paul@44 | 2 | |
paul@44 | 3 | """ |
paul@44 | 4 | Generic file access. |
paul@44 | 5 | |
paul@44 | 6 | Copyright (C) 2009 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@44 | 41 | def rewind(self): |
paul@52 | 42 | self.f.seek(0) |
paul@44 | 43 | self.reset() |
paul@44 | 44 | |
paul@44 | 45 | def close(self): |
paul@44 | 46 | if self.f is not None: |
paul@44 | 47 | self.f.close() |
paul@44 | 48 | self.f = None |
paul@44 | 49 | |
paul@44 | 50 | class FileWriter(File): |
paul@44 | 51 | |
paul@44 | 52 | "Writing basic data types to files." |
paul@44 | 53 | |
paul@44 | 54 | def __init__(self, f): |
paul@44 | 55 | File.__init__(self, f) |
paul@44 | 56 | |
paul@44 | 57 | def write_number(self, number): |
paul@44 | 58 | |
paul@44 | 59 | "Write 'number' to the file using a variable length encoding." |
paul@44 | 60 | |
paul@52 | 61 | self.f.write(vint(number)) |
paul@44 | 62 | |
paul@44 | 63 | def write_string(self, s, compress=0): |
paul@44 | 64 | |
paul@44 | 65 | """ |
paul@44 | 66 | Write 's' to the file, recording its length and compressing the string |
paul@44 | 67 | if 'compress' is set to a true value. |
paul@44 | 68 | """ |
paul@44 | 69 | |
paul@44 | 70 | # Convert Unicode objects to strings. |
paul@44 | 71 | |
paul@44 | 72 | if isinstance(s, unicode): |
paul@44 | 73 | s = s.encode("utf-8") |
paul@44 | 74 | |
paul@44 | 75 | # Compress the string if requested. |
paul@44 | 76 | |
paul@44 | 77 | if compress: |
paul@50 | 78 | cs = zlib.compress(s) |
paul@50 | 79 | |
paul@50 | 80 | # Take any shorter than the original. |
paul@44 | 81 | |
paul@50 | 82 | if len(cs) < len(s): |
paul@50 | 83 | flag = "z" |
paul@50 | 84 | s = cs |
paul@44 | 85 | else: |
paul@44 | 86 | flag = "-" |
paul@44 | 87 | |
paul@44 | 88 | else: |
paul@44 | 89 | flag = "" |
paul@44 | 90 | |
paul@44 | 91 | # Write the length of the data before the data itself. |
paul@44 | 92 | |
paul@44 | 93 | length = len(s) |
paul@52 | 94 | self.f.write("".join([flag, vint(length), s])) |
paul@44 | 95 | |
paul@44 | 96 | class FileReader(File): |
paul@44 | 97 | |
paul@44 | 98 | "Reading basic data types from files." |
paul@44 | 99 | |
paul@44 | 100 | def __init__(self, f): |
paul@44 | 101 | File.__init__(self, f) |
paul@44 | 102 | |
paul@44 | 103 | def read_number(self): |
paul@44 | 104 | |
paul@44 | 105 | "Read a number from the file." |
paul@44 | 106 | |
paul@44 | 107 | # Read each byte, adding it to the number. |
paul@44 | 108 | |
paul@67 | 109 | a = array('B') |
paul@67 | 110 | fromfile = a.fromfile |
paul@67 | 111 | f = self.f |
paul@44 | 112 | |
paul@67 | 113 | fromfile(f, 1) |
paul@67 | 114 | csd = a[-1] |
paul@67 | 115 | if csd < 128: |
paul@67 | 116 | return csd |
paul@55 | 117 | else: |
paul@67 | 118 | while csd & 128: |
paul@67 | 119 | fromfile(f, 1) |
paul@67 | 120 | csd = a[-1] |
paul@67 | 121 | return sum([((csd & 127) << (number * 7)) for (number, csd) in enumerate(a)]) |
paul@44 | 122 | |
paul@44 | 123 | def read_string(self, decompress=0): |
paul@44 | 124 | |
paul@44 | 125 | """ |
paul@44 | 126 | Read a string from the file, decompressing the stored data if |
paul@44 | 127 | 'decompress' is set to a true value. |
paul@44 | 128 | """ |
paul@44 | 129 | |
paul@52 | 130 | read = self.f.read |
paul@52 | 131 | |
paul@44 | 132 | # Decompress the data if requested. |
paul@44 | 133 | |
paul@44 | 134 | if decompress: |
paul@52 | 135 | flag = read(1) |
paul@44 | 136 | else: |
paul@44 | 137 | flag = "-" |
paul@44 | 138 | |
paul@44 | 139 | length = self.read_number() |
paul@52 | 140 | s = read(length) |
paul@44 | 141 | |
paul@44 | 142 | # Perform decompression if applicable. |
paul@44 | 143 | |
paul@50 | 144 | if flag == "z": |
paul@50 | 145 | s = zlib.decompress(s) |
paul@44 | 146 | |
paul@44 | 147 | # Convert strings to Unicode objects. |
paul@44 | 148 | |
paul@44 | 149 | return unicode(s, "utf-8") |
paul@44 | 150 | |
paul@44 | 151 | # vim: tabstop=4 expandtab shiftwidth=4 |