tumble_input.c

Thu, 20 Mar 2003 07:06:35 +0000

author
eric
date
Thu, 20 Mar 2003 07:06:35 +0000
changeset 144
f2b7f70a965d
parent 142
cfa664f3129c
child 151
83a99cc69861
permissions
-rw-r--r--

*** empty log message ***

     1 /*
     2  * tumble: build a PDF file from image files
     3  *
     4  * Input handler dispatch
     5  * $Id: tumble_input.c,v 1.2 2003/03/19 23:02:28 eric Exp $
     6  * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License version 2 as
    10  * published by the Free Software Foundation.  Note that permission is
    11  * not granted to redistribute this program under the terms of any
    12  * other version of the General Public License.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  *
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program; if not, write to the Free Software
    21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    22  */
    25 #include <stdbool.h>
    26 #include <stdint.h>
    27 #include <stdio.h>
    28 #include <stdlib.h>
    29 #include <string.h>
    32 #include "semantics.h"
    33 #include "tumble.h"
    34 #include "bitblt.h"
    35 #include "pdf.h"
    36 #include "tumble_input.h"
    39 #define MAX_INPUT_HANDLERS 10
    41 static int input_handler_count = 0;
    43 static input_handler_t *input_handlers [MAX_INPUT_HANDLERS];
    46 static char *in_filename;
    47 static FILE *in;
    48 static input_handler_t *current_input_handler;
    51 void install_input_handler (input_handler_t *handler)
    52 {
    53   if (input_handler_count >= MAX_INPUT_HANDLERS)
    54     fprintf (stderr, "Too many input handlers, table only has room for %d\n", MAX_INPUT_HANDLERS);
    55   else
    56     input_handlers [input_handler_count++] = handler;
    57 }
    60 bool open_input_file (char *name)
    61 {
    62   int i;
    64   if (in)
    65     {
    66       if (strcmp (name, in_filename) == 0)
    67 	return (1);
    68       close_input_file ();
    69     }
    70   in_filename = strdup (name);
    71   if (! in_filename)
    72     {
    73       fprintf (stderr, "can't strdup input filename '%s'\n", name);
    74       goto fail;
    75     }
    77   in = fopen (name, "rb");
    78   if (! in)
    79     goto fail;
    81   for (i = 0; i < input_handler_count; i++)
    82     {
    83       if (input_handlers [i]->open_input_file (in, name))
    84 	break;
    85     }
    86   if (i >= input_handler_count)
    87     {
    88       fprintf (stderr, "unrecognized format for input file '%s'\n", name);
    89       goto fail;
    90     }
    91   current_input_handler = input_handlers [i];
    92   return (1);
    94  fail:
    95   if (in)
    96     fclose (in);
    97   in = NULL;
    98   return (0);
    99 }
   102 bool close_input_file (void)
   103 {
   104   bool result;
   106   result = current_input_handler->close_input_file ();
   107   if (in_filename)
   108     free (in_filename);
   109   if (in)
   110     {
   111       fclose (in);
   112       in = NULL;
   113     }
   115   return (result);
   116 }
   119 bool last_input_page (void)
   120 {
   121   return (current_input_handler->last_input_page ());
   122 }
   125 bool get_image_info (int image,
   126 		     input_attributes_t input_attributes,
   127 		     image_info_t *image_info)
   128 {
   129   return (current_input_handler->get_image_info (image,
   130 						 input_attributes,
   131 						 image_info));
   132 }
   134 bool process_image (int image,
   135 		    input_attributes_t input_attributes,
   136 		    image_info_t *image_info,
   137 		    pdf_page_handle page)
   138 {
   139   return (current_input_handler->process_image (image,
   140 						input_attributes,
   141 						image_info,
   142 						page));
   143 }