root / pkpgcounter / trunk / pdlanalyzer / dvi.py @ 216

Revision 216, 2.8 kB (checked in by jerome, 19 years ago)

Added support for the DVI format

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