[328] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
| 6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | |
---|
[357] | 24 | """This modules implements a page counter for ZjStream documents.""" |
---|
| 25 | |
---|
[328] | 26 | import sys |
---|
| 27 | import os |
---|
| 28 | import mmap |
---|
| 29 | from struct import unpack |
---|
| 30 | |
---|
| 31 | import pdlparser |
---|
| 32 | |
---|
| 33 | class Parser(pdlparser.PDLParser) : |
---|
| 34 | """A parser for ZjStream documents.""" |
---|
| 35 | def isValid(self) : |
---|
[329] | 36 | """Returns 1 if data is ZjStream, else 0.""" |
---|
[328] | 37 | if self.firstblock[:4] == "ZJZJ" : |
---|
[329] | 38 | self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.") |
---|
[328] | 39 | self.littleEndian() |
---|
| 40 | return 1 |
---|
| 41 | elif self.firstblock[:4] == "JZJZ" : |
---|
[329] | 42 | self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.") |
---|
[328] | 43 | self.bigEndian() |
---|
| 44 | return 1 |
---|
| 45 | else : |
---|
| 46 | return 0 |
---|
| 47 | |
---|
| 48 | def littleEndian(self) : |
---|
| 49 | """Toggles to little endianness.""" |
---|
| 50 | self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" } |
---|
| 51 | self.unpackShort = self.unpackType[2] |
---|
| 52 | self.unpackLong = self.unpackType[4] |
---|
| 53 | return 0 |
---|
| 54 | |
---|
| 55 | def bigEndian(self) : |
---|
| 56 | """Toggles to big endianness.""" |
---|
| 57 | self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" } |
---|
| 58 | self.unpackShort = self.unpackType[2] |
---|
| 59 | self.unpackLong = self.unpackType[4] |
---|
| 60 | return 0 |
---|
| 61 | |
---|
| 62 | def getJobSize(self) : |
---|
| 63 | """Computes the number of pages in a ZjStream document.""" |
---|
[329] | 64 | infileno = self.infile.fileno() |
---|
| 65 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
| 66 | pos = 4 |
---|
| 67 | startpagecount = endpagecount = 0 |
---|
| 68 | try : |
---|
| 69 | try : |
---|
| 70 | while 1 : |
---|
| 71 | header = minfile[pos:pos+16] |
---|
| 72 | if len(header) != 16 : |
---|
| 73 | break |
---|
| 74 | totalChunkSize = unpack(self.unpackLong, header[:4])[0] |
---|
| 75 | chunkType = unpack(self.unpackLong, header[4:8])[0] |
---|
| 76 | numberOfItems = unpack(self.unpackLong, header[8:12])[0] |
---|
| 77 | reserved = unpack(self.unpackShort, header[12:14])[0] |
---|
| 78 | signature = unpack(self.unpackShort, header[14:])[0] |
---|
| 79 | pos += totalChunkSize |
---|
| 80 | if chunkType == 0 : |
---|
| 81 | self.logdebug("startDoc") |
---|
| 82 | elif chunkType == 1 : |
---|
| 83 | self.logdebug("endDoc") |
---|
| 84 | elif chunkType == 2 : |
---|
| 85 | self.logdebug("startPage") |
---|
| 86 | startpagecount += 1 |
---|
| 87 | elif chunkType == 3 : |
---|
| 88 | self.logdebug("endPage") |
---|
| 89 | endpagecount += 1 |
---|
| 90 | |
---|
| 91 | #self.logdebug("Chunk size : %s" % totalChunkSize) |
---|
| 92 | #self.logdebug("Chunk type : 0x%08x" % chunkType) |
---|
| 93 | #self.logdebug("# items : %s" % numberOfItems) |
---|
| 94 | #self.logdebug("reserved : 0x%04x" % reserved) |
---|
| 95 | #self.logdebug("signature : 0x%04x" % signature) |
---|
| 96 | #self.logdebug("\n") |
---|
| 97 | except IndexError : # EOF ? |
---|
| 98 | pass |
---|
| 99 | finally : |
---|
| 100 | minfile.close() |
---|
| 101 | |
---|
| 102 | if startpagecount != endpagecount : |
---|
| 103 | sys.stderr.write("ERROR : Incorrect ZjStream datas.\n") |
---|
| 104 | return max(startpagecount, endpagecount) |
---|
[328] | 105 | |
---|
| 106 | def test() : |
---|
| 107 | """Test function.""" |
---|
| 108 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
| 109 | sys.argv.append("-") |
---|
| 110 | totalsize = 0 |
---|
| 111 | for arg in sys.argv[1:] : |
---|
| 112 | if arg == "-" : |
---|
| 113 | infile = sys.stdin |
---|
| 114 | mustclose = 0 |
---|
| 115 | else : |
---|
| 116 | infile = open(arg, "rb") |
---|
| 117 | mustclose = 1 |
---|
| 118 | try : |
---|
| 119 | parser = Parser(infile, debug=1) |
---|
| 120 | totalsize += parser.getJobSize() |
---|
| 121 | except pdlparser.PDLParserError, msg : |
---|
| 122 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 123 | sys.stderr.flush() |
---|
| 124 | if mustclose : |
---|
| 125 | infile.close() |
---|
| 126 | print "%s" % totalsize |
---|
| 127 | |
---|
| 128 | if __name__ == "__main__" : |
---|
| 129 | test() |
---|