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