Faster counting pages in PDF (Was: Re: [reportlab-users] Recounting pages in PDF)

Matej Pivoluska reportlab-users@reportlab.com
Sat, 19 Jun 2004 11:43:33 +0200


--Boundary-00=_FrA1AHeC/ZXk7Oi
Content-Type: text/plain;
  charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

D=F2a Sobota 19 J=FAn 2004 10:36 Jerome Alet nap=EDsal(a):
> > %%
> > "/Type /Page " ++num_pages;
> > [\n\t ]+
> > .
> > %%
>
> In fact from what I can see (not looked at the PDF spec), you can
> have \r and/or \n just after the /Page with no space between, so
> maybe you should add it (or replace \t, I don't know if \t is valid,
> at least I've never seen it)

OK, I fixed it. Enjoy!

Ad \t. It was for eating whitespaces in my first code.
I do not know if it is allowed in pdf spec after /Page token, neither.
Now I allow it. :-)

mP
=2D-=20
http://pivoluska.matfyz.cz/weblog

--Boundary-00=_FrA1AHeC/ZXk7Oi
Content-Type: text/x-csrc;
  charset="iso-8859-2";
  name="pdfcount.lex"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="pdfcount.lex"

%{
#define PROGRAMSTRING \
"PDFCount v0.2 by mP 2004\n\
Simple program that counts pages in PDF files."

/*
ChangeLog:

* 2004-06-19:
  fixed parsing rules -- added after [\n\r\t ] after "/Page", 
  thx to Jerome Alet for info

* 2004-06-18: 
  initial release
*/


/*
#include <std.disclaimer.h>
#include <Public.Domain.h>
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int num_pages = 0;
%}
%%
"/Type /Page"[ \n\r\t] ++num_pages;
\n /* eat up newlines */
. /* eat up everything else */
%%

void usage() {
  printf(PROGRAMSTRING "\nUsage:\npdfcount <file.pdf\npdfcount file.pdf\n");
}

int main( int argc, char** argv )
{
  FILE* in;
  char* filename = "STDIN";
  
  if( argc == 2 ) {
    if( !(strcmp(argv[1], "-h") )) {
      usage();
      exit( EXIT_SUCCESS );
    }

    if( !(in = fopen( argv[1], "r" ))) {
      fprintf( stderr, "Error: %s\n", strerror( errno ));
      exit( EXIT_FAILURE );
    }
    yyin = in;
    filename = argv[1];
  }
  yylex();
  printf( "%s: Number of pages is %d\n",filename, num_pages);
}

--Boundary-00=_FrA1AHeC/ZXk7Oi--