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 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 | |
---|
24 | import sys |
---|
25 | import os |
---|
26 | import mmap |
---|
27 | from struct import unpack |
---|
28 | |
---|
29 | import pdlparser |
---|
30 | |
---|
31 | class Parser(pdlparser.PDLParser) : |
---|
32 | """A parser for DVI documents.""" |
---|
33 | def isValid(self) : |
---|
34 | """Returns 1 if data is DVI, else 0.""" |
---|
35 | try : |
---|
36 | if (ord(self.firstblock[0]) == 0xf7) and (ord(self.lastblock[-1]) == 0xdf) : |
---|
37 | if self.debug : |
---|
38 | sys.stderr.write("DEBUG: Input file is in the DVI format.\n") |
---|
39 | return 1 |
---|
40 | else : |
---|
41 | return 0 |
---|
42 | except IndexError : |
---|
43 | return 0 |
---|
44 | |
---|
45 | def getJobSize(self) : |
---|
46 | """Counts pages in a DVI document. |
---|
47 | |
---|
48 | Algorithm by Jerome Alet. |
---|
49 | |
---|
50 | The documentation used for this was : |
---|
51 | |
---|
52 | http://www.math.umd.edu/~asnowden/comp-cont/dvi.html |
---|
53 | """ |
---|
54 | infileno = self.infile.fileno() |
---|
55 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
56 | pagecount = 0 |
---|
57 | pos = -1 |
---|
58 | eofchar = chr(0xdf) |
---|
59 | postchar = chr(0xf8) |
---|
60 | try : |
---|
61 | while minfile[pos] == eofchar : |
---|
62 | pos -= 1 |
---|
63 | idbyte = minfile[pos] |
---|
64 | if idbyte != minfile[1] : |
---|
65 | raise IndexError, "Invalid DVI file." |
---|
66 | pos = unpack(">I", minfile[pos - 4:pos])[0] |
---|
67 | if minfile[pos] != postchar : |
---|
68 | raise IndexError, "Invalid DVI file." |
---|
69 | pagecount = unpack(">H", minfile[pos + 27: pos + 29])[0] |
---|
70 | except IndexError : # EOF ? |
---|
71 | pass |
---|
72 | minfile.close() # reached EOF |
---|
73 | return pagecount |
---|
74 | |
---|
75 | def test() : |
---|
76 | """Test function.""" |
---|
77 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
78 | sys.argv.append("-") |
---|
79 | totalsize = 0 |
---|
80 | for arg in sys.argv[1:] : |
---|
81 | if arg == "-" : |
---|
82 | infile = sys.stdin |
---|
83 | mustclose = 0 |
---|
84 | else : |
---|
85 | infile = open(arg, "rb") |
---|
86 | mustclose = 1 |
---|
87 | try : |
---|
88 | parser = Parser(infile, debug=1) |
---|
89 | totalsize += parser.getJobSize() |
---|
90 | except pdlparser.PDLParserError, msg : |
---|
91 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
92 | sys.stderr.flush() |
---|
93 | if mustclose : |
---|
94 | infile.close() |
---|
95 | print "%s" % totalsize |
---|
96 | |
---|
97 | if __name__ == "__main__" : |
---|
98 | test() |
---|