root / pkpgcounter / trunk / pkpgpdls / zjstream.py @ 347

Revision 329, 4.8 kB (checked in by jerome, 18 years ago)

Added complete support for Zenographics ZjStream? input format.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id 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, 2006 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
29import pdlparser
30
31class Parser(pdlparser.PDLParser) :
32    """A parser for ZjStream documents."""
33    def isValid(self) :   
34        """Returns 1 if data is ZjStream, else 0."""
35        if self.firstblock[:4] == "ZJZJ" :
36            self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.")
37            self.littleEndian()
38            return 1
39        elif self.firstblock[:4] == "JZJZ" :   
40            self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.")
41            self.bigEndian()
42            return 1
43        else :   
44            return 0
45       
46    def littleEndian(self) :
47        """Toggles to little endianness."""
48        self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" }
49        self.unpackShort = self.unpackType[2]
50        self.unpackLong = self.unpackType[4]
51        return 0
52       
53    def bigEndian(self) :
54        """Toggles to big endianness."""
55        self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" }
56        self.unpackShort = self.unpackType[2]
57        self.unpackLong = self.unpackType[4]
58        return 0
59       
60    def getJobSize(self) :
61        """Computes the number of pages in a ZjStream document."""
62        infileno = self.infile.fileno()
63        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
64        pos = 4
65        startpagecount = endpagecount = 0
66        try :
67            try :
68                while 1 :
69                    header = minfile[pos:pos+16]
70                    if len(header) != 16 :
71                        break
72                    totalChunkSize = unpack(self.unpackLong, header[:4])[0]
73                    chunkType = unpack(self.unpackLong, header[4:8])[0]
74                    numberOfItems = unpack(self.unpackLong, header[8:12])[0]
75                    reserved = unpack(self.unpackShort, header[12:14])[0]
76                    signature = unpack(self.unpackShort, header[14:])[0]
77                    pos += totalChunkSize
78                    if chunkType == 0 :
79                        self.logdebug("startDoc")
80                    elif chunkType == 1 :   
81                        self.logdebug("endDoc")
82                    elif chunkType == 2 :   
83                        self.logdebug("startPage")
84                        startpagecount += 1
85                    elif chunkType == 3 :
86                        self.logdebug("endPage")
87                        endpagecount += 1
88                       
89                    #self.logdebug("Chunk size : %s" % totalChunkSize)
90                    #self.logdebug("Chunk type : 0x%08x" % chunkType)
91                    #self.logdebug("# items : %s" % numberOfItems)
92                    #self.logdebug("reserved : 0x%04x" % reserved)
93                    #self.logdebug("signature : 0x%04x" % signature)
94                    #self.logdebug("\n")
95            except IndexError : # EOF ?
96                pass 
97        finally :       
98            minfile.close()
99           
100        if startpagecount != endpagecount :   
101            sys.stderr.write("ERROR : Incorrect ZjStream datas.\n")
102        return max(startpagecount, endpagecount)
103       
104def test() :       
105    """Test function."""
106    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
107        sys.argv.append("-")
108    totalsize = 0   
109    for arg in sys.argv[1:] :
110        if arg == "-" :
111            infile = sys.stdin
112            mustclose = 0
113        else :   
114            infile = open(arg, "rb")
115            mustclose = 1
116        try :
117            parser = Parser(infile, debug=1)
118            totalsize += parser.getJobSize()
119        except pdlparser.PDLParserError, msg :   
120            sys.stderr.write("ERROR: %s\n" % msg)
121            sys.stderr.flush()
122        if mustclose :   
123            infile.close()
124    print "%s" % totalsize
125   
126if __name__ == "__main__" :   
127    test()
Note: See TracBrowser for help on using the browser.