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, 2007 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 3 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, see <http://www.gnu.org/licenses/>. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | |
---|
23 | """This modules implements a page counter for ZjStream documents.""" |
---|
24 | |
---|
25 | import sys |
---|
26 | import os |
---|
27 | import mmap |
---|
28 | from struct import unpack |
---|
29 | |
---|
30 | import pdlparser |
---|
31 | |
---|
32 | class Parser(pdlparser.PDLParser) : |
---|
33 | """A parser for ZjStream documents.""" |
---|
34 | def isValid(self) : |
---|
35 | """Returns True if data is ZjStream, else False.""" |
---|
36 | if self.firstblock[:4] == "ZJZJ" : |
---|
37 | self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.") |
---|
38 | self.littleEndian() |
---|
39 | return True |
---|
40 | elif self.firstblock[:4] == "JZJZ" : |
---|
41 | self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.") |
---|
42 | self.bigEndian() |
---|
43 | return True |
---|
44 | else : |
---|
45 | return False |
---|
46 | |
---|
47 | def littleEndian(self) : |
---|
48 | """Toggles to little endianness.""" |
---|
49 | self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" } |
---|
50 | self.unpackShort = self.unpackType[2] |
---|
51 | self.unpackLong = self.unpackType[4] |
---|
52 | return 0 |
---|
53 | |
---|
54 | def bigEndian(self) : |
---|
55 | """Toggles to big endianness.""" |
---|
56 | self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" } |
---|
57 | self.unpackShort = self.unpackType[2] |
---|
58 | self.unpackLong = self.unpackType[4] |
---|
59 | return 0 |
---|
60 | |
---|
61 | def getJobSize(self) : |
---|
62 | """Computes the number of pages in a ZjStream document.""" |
---|
63 | infileno = self.infile.fileno() |
---|
64 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
65 | pos = 4 |
---|
66 | startpagecount = endpagecount = 0 |
---|
67 | try : |
---|
68 | try : |
---|
69 | while 1 : |
---|
70 | header = minfile[pos:pos+16] |
---|
71 | if len(header) != 16 : |
---|
72 | break |
---|
73 | totalChunkSize = unpack(self.unpackLong, header[:4])[0] |
---|
74 | chunkType = unpack(self.unpackLong, header[4:8])[0] |
---|
75 | numberOfItems = unpack(self.unpackLong, header[8:12])[0] |
---|
76 | reserved = unpack(self.unpackShort, header[12:14])[0] |
---|
77 | signature = unpack(self.unpackShort, header[14:])[0] |
---|
78 | pos += totalChunkSize |
---|
79 | if chunkType == 0 : |
---|
80 | self.logdebug("startDoc") |
---|
81 | elif chunkType == 1 : |
---|
82 | self.logdebug("endDoc") |
---|
83 | elif chunkType == 2 : |
---|
84 | self.logdebug("startPage") |
---|
85 | startpagecount += 1 |
---|
86 | elif chunkType == 3 : |
---|
87 | self.logdebug("endPage") |
---|
88 | endpagecount += 1 |
---|
89 | |
---|
90 | #self.logdebug("Chunk size : %s" % totalChunkSize) |
---|
91 | #self.logdebug("Chunk type : 0x%08x" % chunkType) |
---|
92 | #self.logdebug("# items : %s" % numberOfItems) |
---|
93 | #self.logdebug("reserved : 0x%04x" % reserved) |
---|
94 | #self.logdebug("signature : 0x%04x" % signature) |
---|
95 | #self.logdebug("\n") |
---|
96 | except IndexError : # EOF ? |
---|
97 | pass |
---|
98 | finally : |
---|
99 | minfile.close() |
---|
100 | |
---|
101 | if startpagecount != endpagecount : |
---|
102 | sys.stderr.write("ERROR: Incorrect ZjStream datas.\n") |
---|
103 | return max(startpagecount, endpagecount) |
---|
104 | |
---|
105 | if __name__ == "__main__" : |
---|
106 | pdlparser.test(Parser) |
---|