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