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

Revision 463, 4.1 kB (checked in by jerome, 17 years ago)

Licensing terms changed to GNU GPL v3.0 or higher.
Removed old PCL3/4/5 parser which for a long time now wasn't used
anymore, and for which I was not the original copyright owner.
Version number bumped to 3.00alpha to reflect licensing changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[328]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
[443]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[463]7# This program is free software: you can redistribute it and/or modify
[328]8# it under the terms of the GNU General Public License as published by
[463]9# the Free Software Foundation, either version 3 of the License, or
[328]10# (at your option) any later version.
[463]11#
[328]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
[463]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[328]19#
20# $Id$
21#
22
[357]23"""This modules implements a page counter for ZjStream documents."""
24
[328]25import sys
26import os
27import mmap
28from struct import unpack
29
30import pdlparser
31
32class Parser(pdlparser.PDLParser) :
33    """A parser for ZjStream documents."""
34    def isValid(self) :   
[387]35        """Returns True if data is ZjStream, else False."""
[328]36        if self.firstblock[:4] == "ZJZJ" :
[329]37            self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.")
[328]38            self.littleEndian()
[387]39            return True
[328]40        elif self.firstblock[:4] == "JZJZ" :   
[329]41            self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.")
[328]42            self.bigEndian()
[387]43            return True
[328]44        else :   
[387]45            return False
[328]46       
47    def littleEndian(self) :
48        """Toggles to little endianness."""
49        self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" }
50        self.unpackShort = self.unpackType[2]
51        self.unpackLong = self.unpackType[4]
52        return 0
53       
54    def bigEndian(self) :
55        """Toggles to big endianness."""
56        self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" }
57        self.unpackShort = self.unpackType[2]
58        self.unpackLong = self.unpackType[4]
59        return 0
60       
61    def getJobSize(self) :
62        """Computes the number of pages in a ZjStream document."""
[329]63        infileno = self.infile.fileno()
64        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
65        pos = 4
66        startpagecount = endpagecount = 0
67        try :
68            try :
69                while 1 :
70                    header = minfile[pos:pos+16]
71                    if len(header) != 16 :
72                        break
73                    totalChunkSize = unpack(self.unpackLong, header[:4])[0]
74                    chunkType = unpack(self.unpackLong, header[4:8])[0]
75                    numberOfItems = unpack(self.unpackLong, header[8:12])[0]
76                    reserved = unpack(self.unpackShort, header[12:14])[0]
77                    signature = unpack(self.unpackShort, header[14:])[0]
78                    pos += totalChunkSize
79                    if chunkType == 0 :
80                        self.logdebug("startDoc")
81                    elif chunkType == 1 :   
82                        self.logdebug("endDoc")
83                    elif chunkType == 2 :   
84                        self.logdebug("startPage")
85                        startpagecount += 1
86                    elif chunkType == 3 :
87                        self.logdebug("endPage")
88                        endpagecount += 1
89                       
90                    #self.logdebug("Chunk size : %s" % totalChunkSize)
91                    #self.logdebug("Chunk type : 0x%08x" % chunkType)
92                    #self.logdebug("# items : %s" % numberOfItems)
93                    #self.logdebug("reserved : 0x%04x" % reserved)
94                    #self.logdebug("signature : 0x%04x" % signature)
95                    #self.logdebug("\n")
96            except IndexError : # EOF ?
97                pass 
98        finally :       
99            minfile.close()
100           
101        if startpagecount != endpagecount :   
[385]102            sys.stderr.write("ERROR: Incorrect ZjStream datas.\n")
[329]103        return max(startpagecount, endpagecount)
[328]104       
105if __name__ == "__main__" :   
[415]106    pdlparser.test(Parser)
Note: See TracBrowser for help on using the browser.