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

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

The input file was not always closed

  • 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            pass
60        minfile.close() # reached EOF
61        return pagecount
62       
63def test() :       
64    """Test function."""
65    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
66        sys.argv.append("-")
67    totalsize = 0   
68    for arg in sys.argv[1:] :
69        if arg == "-" :
70            infile = sys.stdin
71            mustclose = 0
72        else :   
73            infile = open(arg, "rb")
74            mustclose = 1
75        try :
76            parser = DVIParser(infile, debug=1)
77            totalsize += parser.getJobSize()
78        except pdlparser.PDLParserError, msg :   
79            sys.stderr.write("ERROR: %s\n" % msg)
80            sys.stderr.flush()
81        if mustclose :   
82            infile.close()
83    print "%s" % totalsize
84   
85if __name__ == "__main__" :   
86    test()
Note: See TracBrowser for help on using the browser.