Index: pkpgcounter/trunk/pkpgpdls/postscript.py
===================================================================
--- pkpgcounter/trunk/pkpgpdls/postscript.py (revision 463)
+++ pkpgcounter/trunk/pkpgpdls/postscript.py (revision 491)
@@ -34,4 +34,5 @@
     """A parser for PostScript documents."""
     totiffcommands = [ 'gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -' ]
+    openmode = "rU"
     def isValid(self) :    
         """Returns True if data is PostScript, else False."""
@@ -52,28 +53,34 @@
         """Get the count through GhostScript, useful for non-DSC compliant PS files."""
         self.logdebug("Internal parser sucks, using GhostScript instead...")
-        self.infile.seek(0)
         command = 'gs -sDEVICE=bbox -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null'
-        child = popen2.Popen4(command)
+        pagecount = 0
+        # we need to reopen the input file in binary mode again, just in case
+        # otherwise we might break the original file's contents.
+        infile = open(self.filename, "rb")
         try :
-            data = self.infile.read(pdlparser.MEGABYTE)    
-            while data :
-                child.tochild.write(data)
-                data = self.infile.read(pdlparser.MEGABYTE)
-            child.tochild.flush()
-            child.tochild.close()    
-        except (IOError, OSError), msg :    
-            raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg
+            child = popen2.Popen4(command)
+            try :
+                data = infile.read(pdlparser.MEGABYTE)    
+                while data :
+                    child.tochild.write(data)
+                    data = infile.read(pdlparser.MEGABYTE)
+                child.tochild.flush()
+                child.tochild.close()    
+            except (IOError, OSError), msg :    
+                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg
+                
+            pagecount = 0
+            try :
+                pagecount = int(child.fromchild.readline().strip())
+            except (IOError, OSError, AttributeError, ValueError), msg :
+                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg
+            child.fromchild.close()
             
-        pagecount = 0
-        try :
-            pagecount = int(child.fromchild.readline().strip())
-        except (IOError, OSError, AttributeError, ValueError), msg :
-            raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg
-        child.fromchild.close()
-        
-        try :
-            child.wait()
-        except OSError, msg :    
-            raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg
+            try :
+                child.wait()
+            except OSError, msg :    
+                raise pdlparser.PDLParserError, "Problem during analysis of Binary PostScript document : %s" % msg
+        finally :        
+            infile.close()
         self.logdebug("GhostScript said : %s pages" % pagecount)    
         return pagecount * self.copies
@@ -81,5 +88,4 @@
     def natively(self) :
         """Count pages in a DSC compliant PostScript document."""
-        self.infile.seek(0)
         pagecount = 0
         self.pages = { 0 : { "copies" : 1 } }
@@ -90,5 +96,6 @@
         acrobatmarker = 0
         pagescomment = None
-        for line in self.infile : 
+        for line in self.infile :
+            line = line.strip()
             if (not prescribe) and line.startswith(r"%%BeginResource: procset pdf") \
                and not acrobatmarker :
@@ -121,5 +128,5 @@
             elif line.startswith(r"%%Requirements: numcopies(") :    
                 try :
-                    number = int(line.strip().split('(')[1].split(')')[0])
+                    number = int(line.split('(')[1].split(')')[0])
                 except :     
                     pass
@@ -130,5 +137,5 @@
                 # handle # of copies set by some Windows printer driver
                 try :
-                    number = int(line.strip().split()[2])
+                    number = int(line.split()[2])
                 except :     
                     pass
@@ -139,5 +146,5 @@
                 # handle # of copies set by mozilla/kprinter
                 try :
-                    number = int(line.strip().split()[4])
+                    number = int(line.split()[4])
                 except :     
                     pass
@@ -148,5 +155,5 @@
                 # handle # of copies set by firefox/kprinter/cups (alternate syntax)
                 try :
-                    number = int(line.strip().split()[6])
+                    number = int(line.split()[6])
                 except :
                     pass
@@ -156,5 +163,5 @@
             elif line.startswith("/languagelevel where{pop languagelevel}{1}ifelse 2 ge{1 dict dup/NumCopies") :
                 try :
-                    number = int(previousline.strip()[2:])
+                    number = int(previousline[2:])
                 except :
                     pass
@@ -164,5 +171,5 @@
             elif line.startswith("/#copies ") :
                 try :
-                    number = int(line.strip().split()[1])
+                    number = int(line.split()[1])
                 except :     
                     pass
@@ -172,5 +179,5 @@
             elif line.startswith(r"%RBINumCopies: ") :   
                 try :
-                    number = int(line.strip().split()[1])
+                    number = int(line.split()[1])
                 except :     
                     pass
@@ -207,5 +214,2 @@
                 self.logdebug(msg)
         return max(nbpages, newnbpages)    
-        
-if __name__ == "__main__" :    
-    pdlparser.test(Parser)
