tumble_input.c

Mon, 14 Dec 2009 16:18:21 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 14 Dec 2009 16:18:21 +0000
changeset 172
2fae6df568f6
parent 166
301f6f17c364
permissions
-rw-r--r--

remove erroneous 0.33-philpem1 tag

     1 /*
     2  * tumble: build a PDF file from image files
     3  *
     4  * Input handler dispatch
     5  * $Id: tumble_input.c,v 1.4 2003/03/25 01:38:08 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 match_input_suffix (char *suffix)
    61 {
    62   int i;
    63   for (i = 0; i < input_handler_count; i++)
    64     if (input_handlers [i]->match_suffix (suffix))
    65       return (1);
    66   return (0);
    67 }
    69 bool open_input_file (char *name)
    70 {
    71   int i;
    73   if (in)
    74     {
    75       if (strcmp (name, in_filename) == 0)
    76 	return (1);
    77       close_input_file ();
    78     }
    79   in_filename = strdup (name);
    80   if (! in_filename)
    81     {
    82       fprintf (stderr, "can't strdup input filename '%s'\n", name);
    83       goto fail;
    84     }
    86   in = fopen (name, "rb");
    87   if (! in)
    88     goto fail;
    90   for (i = 0; i < input_handler_count; i++)
    91     {
    92       if (input_handlers [i]->open_input_file (in, name))
    93 	break;
    94     }
    95   if (i >= input_handler_count)
    96     {
    97       fprintf (stderr, "unrecognized format for input file '%s'\n", name);
    98       goto fail;
    99     }
   100   current_input_handler = input_handlers [i];
   101   return (1);
   103  fail:
   104   if (in)
   105     fclose (in);
   106   in = NULL;
   107   return (0);
   108 }
   111 bool close_input_file (void)
   112 {
   113   bool result = 1;
   115   if (current_input_handler)
   116     {
   117       result = current_input_handler->close_input_file ();
   118       current_input_handler = NULL;
   119     }
   120   if (in_filename) {
   121     free (in_filename);
   122     in_filename = NULL;
   123   }
   124   if (in)
   125     {
   126       fclose (in);
   127       in = NULL;
   128     }
   130   return (result);
   131 }
   134 bool last_input_page (void)
   135 {
   136   if (! current_input_handler)
   137     return (0);
   138   return (current_input_handler->last_input_page ());
   139 }
   142 bool get_image_info (int image,
   143 		     input_attributes_t input_attributes,
   144 		     image_info_t *image_info)
   145 {
   146   if (! current_input_handler)
   147     return (0);
   148   return (current_input_handler->get_image_info (image,
   149 						 input_attributes,
   150 						 image_info));
   151 }
   153 bool process_image (int image,
   154 		    input_attributes_t input_attributes,
   155 		    image_info_t *image_info,
   156 		    pdf_page_handle page)
   157 {
   158   if (! current_input_handler)
   159     return (0);
   160   return (current_input_handler->process_image (image,
   161 						input_attributes,
   162 						image_info,
   163 						page));
   164 }