Mon, 14 Dec 2009 16:18:21 +0000
remove erroneous 0.33-philpem1 tag
eric@141 | 1 | /* |
eric@141 | 2 | * tumble: build a PDF file from image files |
eric@141 | 3 | * |
eric@141 | 4 | * Input handler dispatch |
eric@156 | 5 | * $Id: tumble_input.c,v 1.4 2003/03/25 01:38:08 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@156 | 113 | bool result = 1; |
eric@141 | 114 | |
eric@156 | 115 | if (current_input_handler) |
eric@156 | 116 | { |
eric@156 | 117 | result = current_input_handler->close_input_file (); |
eric@156 | 118 | current_input_handler = NULL; |
eric@156 | 119 | } |
philpem@166 | 120 | if (in_filename) { |
eric@141 | 121 | free (in_filename); |
philpem@166 | 122 | in_filename = NULL; |
philpem@166 | 123 | } |
eric@142 | 124 | if (in) |
eric@142 | 125 | { |
eric@142 | 126 | fclose (in); |
eric@142 | 127 | in = NULL; |
eric@142 | 128 | } |
eric@141 | 129 | |
eric@141 | 130 | return (result); |
eric@141 | 131 | } |
eric@141 | 132 | |
eric@141 | 133 | |
eric@141 | 134 | bool last_input_page (void) |
eric@141 | 135 | { |
eric@156 | 136 | if (! current_input_handler) |
eric@156 | 137 | return (0); |
eric@141 | 138 | return (current_input_handler->last_input_page ()); |
eric@141 | 139 | } |
eric@141 | 140 | |
eric@141 | 141 | |
eric@141 | 142 | bool get_image_info (int image, |
eric@141 | 143 | input_attributes_t input_attributes, |
eric@141 | 144 | image_info_t *image_info) |
eric@141 | 145 | { |
eric@156 | 146 | if (! current_input_handler) |
eric@156 | 147 | return (0); |
eric@141 | 148 | return (current_input_handler->get_image_info (image, |
eric@141 | 149 | input_attributes, |
eric@141 | 150 | image_info)); |
eric@141 | 151 | } |
eric@141 | 152 | |
eric@141 | 153 | bool process_image (int image, |
eric@141 | 154 | input_attributes_t input_attributes, |
eric@141 | 155 | image_info_t *image_info, |
eric@141 | 156 | pdf_page_handle page) |
eric@141 | 157 | { |
eric@156 | 158 | if (! current_input_handler) |
eric@156 | 159 | return (0); |
eric@141 | 160 | return (current_input_handler->process_image (image, |
eric@141 | 161 | input_attributes, |
eric@141 | 162 | image_info, |
eric@141 | 163 | page)); |
eric@141 | 164 | } |