tumble_input.c

changeset 141
752599b50ff3
child 142
cfa664f3129c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tumble_input.c	Thu Mar 20 06:54:08 2003 +0000
     1.3 @@ -0,0 +1,140 @@
     1.4 +/*
     1.5 + * tumble: build a PDF file from image files
     1.6 + *
     1.7 + * Input handler dispatch
     1.8 + * $Id: tumble_input.c,v 1.1 2003/03/19 22:54:08 eric Exp $
     1.9 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify
    1.12 + * it under the terms of the GNU General Public License version 2 as
    1.13 + * published by the Free Software Foundation.  Note that permission is
    1.14 + * not granted to redistribute this program under the terms of any
    1.15 + * other version of the General Public License.
    1.16 + *
    1.17 + * This program is distributed in the hope that it will be useful,
    1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.20 + * GNU General Public License for more details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License
    1.23 + * along with this program; if not, write to the Free Software
    1.24 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    1.25 + */
    1.26 +
    1.27 +
    1.28 +#include <stdbool.h>
    1.29 +#include <stdint.h>
    1.30 +#include <stdio.h>
    1.31 +#include <stdlib.h>
    1.32 +#include <string.h>
    1.33 +
    1.34 +
    1.35 +#include "semantics.h"
    1.36 +#include "tumble.h"
    1.37 +#include "bitblt.h"
    1.38 +#include "pdf.h"
    1.39 +#include "tumble_input.h"
    1.40 +
    1.41 +
    1.42 +#define MAX_INPUT_HANDLERS 10
    1.43 +
    1.44 +static int input_handler_count = 0;
    1.45 +
    1.46 +static input_handler_t *input_handlers [MAX_INPUT_HANDLERS];
    1.47 +
    1.48 +
    1.49 +static char *in_filename;
    1.50 +static FILE *in;
    1.51 +static input_handler_t *current_input_handler;
    1.52 +
    1.53 +
    1.54 +void install_input_handler (input_handler_t *handler)
    1.55 +{
    1.56 +  if (input_handler_count >= MAX_INPUT_HANDLERS)
    1.57 +    fprintf (stderr, "Too many input handlers, table only has room for %d\n", MAX_INPUT_HANDLERS);
    1.58 +  else
    1.59 +    input_handlers [input_handler_count++] = handler;
    1.60 +}
    1.61 +
    1.62 +
    1.63 +bool open_input_file (char *name)
    1.64 +{
    1.65 +  int i;
    1.66 +
    1.67 +  if (in)
    1.68 +    {
    1.69 +      if (strcmp (name, in_filename) == 0)
    1.70 +	return (1);
    1.71 +      close_input_file ();
    1.72 +    }
    1.73 +  in_filename = strdup (name);
    1.74 +  if (! in_filename)
    1.75 +    {
    1.76 +      fprintf (stderr, "can't strdup input filename '%s'\n", name);
    1.77 +      goto fail;
    1.78 +    }
    1.79 +
    1.80 +  in = fopen (name, "rb");
    1.81 +  if (! in)
    1.82 +    goto fail;
    1.83 +
    1.84 +  for (i = 0; i < input_handler_count; i++)
    1.85 +    {
    1.86 +      if (input_handlers [i]->open_input_file (in, name))
    1.87 +	break;
    1.88 +    }
    1.89 +  if (i >= input_handler_count)
    1.90 +    {
    1.91 +      fprintf (stderr, "unrecognized format for input file '%s'\n", name);
    1.92 +      goto fail;
    1.93 +    }
    1.94 +  current_input_handler = input_handlers [i];
    1.95 +  return (1);
    1.96 +
    1.97 + fail:
    1.98 +  if (in)
    1.99 +    fclose (in);
   1.100 +  in = NULL;
   1.101 +  return (0);
   1.102 +}
   1.103 +
   1.104 +
   1.105 +bool close_input_file (void)
   1.106 +{
   1.107 +  bool result;
   1.108 +
   1.109 +  result = current_input_handler->close_input_file ();
   1.110 +  if (in_filename)
   1.111 +    free (in_filename);
   1.112 +  fclose (in);
   1.113 +  in = NULL;
   1.114 +
   1.115 +  return (result);
   1.116 +}
   1.117 +
   1.118 +
   1.119 +bool last_input_page (void)
   1.120 +{
   1.121 +  return (current_input_handler->last_input_page ());
   1.122 +}
   1.123 +
   1.124 +
   1.125 +bool get_image_info (int image,
   1.126 +		     input_attributes_t input_attributes,
   1.127 +		     image_info_t *image_info)
   1.128 +{
   1.129 +  return (current_input_handler->get_image_info (image,
   1.130 +						 input_attributes,
   1.131 +						 image_info));
   1.132 +}
   1.133 +
   1.134 +bool process_image (int image,
   1.135 +		    input_attributes_t input_attributes,
   1.136 +		    image_info_t *image_info,
   1.137 +		    pdf_page_handle page)
   1.138 +{
   1.139 +  return (current_input_handler->process_image (image,
   1.140 +						input_attributes,
   1.141 +						image_info,
   1.142 +						page));
   1.143 +}