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

Matej Pivoluska reportlab-users@reportlab.com
Sat, 19 Jun 2004 02:26:20 +0200


--Boundary-00=_sg40Ae0mQN611NO
Content-Type: text/plain;
  charset="iso-8859-2"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Ask for more speed :-)

compile with 
flex pdfcount.lex && gcc lex.yy.c -O2 -ll -o pdfcount

and use
./pdfcount <file.pdf
or
./pdfcount file.pdf


mP
-- 
http://pivoluska.matfyz.cz/weblog

--Boundary-00=_sg40Ae0mQN611NO
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 by mP 2004\n\
Simple program that counts pages in PDF files."

/*
#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 " ++num_pages;
[\n\t ]+
. 
%%

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=_sg40Ae0mQN611NO--