tumble_input.c

Tue, 25 Mar 2003 09:36:56 +0000

author
eric
date
Tue, 25 Mar 2003 09:36:56 +0000
changeset 155
cbc23650d9ca
parent 151
83a99cc69861
child 156
745483d15215
permissions
-rw-r--r--

*** empty log message ***

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