Skip to content

Commit 6f58c5c

Browse files
committed
Auto-type the output file if necessary, using the filename extension.
Remove some unsupported input formats from the list of filename extensions. Use case-insensitive comparisons for MIME media types. Disable debug printfs in PDFio.
1 parent bed4837 commit 6f58c5c

2 files changed

Lines changed: 33 additions & 40 deletions

File tree

tools/ipptransform.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ main(int argc, // I - Number of command-line args
203203
*resolutions, // pwg-raster-document-resolution-supported
204204
*sheet_back, // pwg-raster-document-sheet-back
205205
*types, // pwg-raster-document-type-supported
206-
*opt; // Option character
206+
*opt, // Option character
207+
*ext; // Filename extension
207208
size_t num_files = 0; // Number of files
208209
xform_document_t files[1000]; // Files to convert
209210
size_t num_options = 0; // Number of options
@@ -304,6 +305,20 @@ main(int argc, // I - Number of command-line args
304305
usage(1);
305306
}
306307

308+
if (!output_type && (ext = strrchr(argv[i], '.')) != NULL)
309+
{
310+
if (!strcasecmp(ext, ".pcl"))
311+
output_type = "application/vnd.hp-PCL";
312+
else if (!strcasecmp(ext, ".pdf"))
313+
output_type = "application/pdf";
314+
else if (!strcasecmp(ext, ".ps"))
315+
output_type = "application/postscript";
316+
else if (!strcasecmp(ext, ".pwg"))
317+
output_type = "image/pwg-raster";
318+
else if (!strcasecmp(ext, ".urf"))
319+
output_type = "image/urf";
320+
}
321+
307322
if (!freopen(argv[i], "w", stdout))
308323
{
309324
cupsLangPrintf(stderr, _("%s: Unable to open '%s': %s"), Prefix, argv[i], strerror(errno));
@@ -412,23 +427,19 @@ main(int argc, // I - Number of command-line args
412427

413428
if (!files[num_files].format)
414429
{
415-
if ((opt = strrchr(argv[i], '.')) != NULL)
430+
if ((ext = strrchr(argv[i], '.')) != NULL)
416431
{
417-
if (!strcmp(opt, ".jpg") || !strcmp(opt, ".jpeg"))
432+
if (!strcasecmp(ext, ".jpg") || !strcasecmp(ext, ".jpeg"))
418433
files[num_files].format = "image/jpeg";
419-
else if (!strcmp(opt, ".pcl"))
420-
files[num_files].format = "application/vnd.hp-PCL";
421-
else if (!strcmp(opt, ".pdf"))
434+
else if (!strcasecmp(ext, ".pdf"))
422435
files[num_files].format = "application/pdf";
423-
else if (!strcmp(opt, ".png"))
436+
else if (!strcasecmp(ext, ".png"))
424437
files[num_files].format = "image/png";
425-
else if (!strcmp(opt, ".pxl"))
426-
files[num_files].format = "application/vnd.hp-PCLXL";
427-
else if (!strcmp(opt, ".pwg"))
438+
else if (!strcasecmp(ext, ".pwg"))
428439
files[num_files].format = "image/pwg-raster";
429-
else if (!strcmp(opt, ".c") || !strcmp(opt, ".c++") || !strcmp(opt, ".cpp") || !strcmp(opt, ".cxx") || !strcmp(opt, ".h") || !strcmp(opt, ".hpp") || !strcmp(opt, ".m") || !strcmp(opt, ".md") || !strcmp(opt, ".py") || !strcmp(opt, ".rst") || !strcmp(opt, ".swift") || !strcmp(opt, ".txt"))
440+
else if (!strcasecmp(ext, ".c") || !strcasecmp(ext, ".c++") || !strcasecmp(ext, ".cpp") || !strcasecmp(ext, ".cxx") || !strcasecmp(ext, ".h") || !strcasecmp(ext, ".hpp") || !strcasecmp(ext, ".m") || !strcasecmp(ext, ".md") || !strcasecmp(ext, ".pl") || !strcasecmp(ext, ".py") || !strcasecmp(ext, ".rst") || !strcasecmp(ext, ".swift") || !strcasecmp(ext, ".txt"))
430441
files[num_files].format = "text/plain";
431-
else if (!strcmp(opt, ".urf"))
442+
else if (!strcasecmp(ext, ".urf"))
432443
files[num_files].format = "image/urf";
433444
}
434445
}

tools/pdfio-private.h

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Private header file for PDFio.
33
//
4-
// Copyright © 2021-2022 by Michael R Sweet.
4+
// Copyright © 2021-2023 by Michael R Sweet.
55
//
66
// Licensed under Apache License v2.0. See the file "LICENSE" for more
77
// information.
@@ -10,18 +10,15 @@
1010
#ifndef PDFIO_PRIVATE_H
1111
# define PDFIO_PRIVATE_H
1212

13-
//
14-
// Include necessary headers...
15-
//
13+
# ifdef DEBUG // Disable PDFio debug printfs
14+
# undef DEBUG
15+
# endif // DEBUG
1616

1717
# ifdef _WIN32
18-
/*
19-
* Disable bogus VS warnings/errors...
20-
*/
21-
22-
# define _CRT_SECURE_NO_WARNINGS
18+
# define _CRT_SECURE_NO_WARNINGS // Disable bogus VS warnings/errors...
2319
# endif // _WIN32
2420

21+
2522
# include "pdfio.h"
2623
# include <stdarg.h>
2724
# include <stdint.h>
@@ -32,18 +29,7 @@
3229
# ifdef _WIN32
3330
# include <io.h>
3431
# include <direct.h>
35-
36-
/*
37-
* Microsoft renames the POSIX functions to _name, and introduces
38-
* a broken compatibility layer using the original names. As a result,
39-
* random crashes can occur when, for example, strdup() allocates memory
40-
* from a different heap than used by malloc() and free().
41-
*
42-
* To avoid moronic problems like this, we #define the POSIX function
43-
* names to the corresponding non-standard Microsoft names.
44-
*/
45-
46-
# define access _access
32+
# define access _access // Map standard POSIX/C99 names
4733
# define close _close
4834
# define fileno _fileno
4935
# define lseek _lseek
@@ -56,12 +42,7 @@
5642
# define unlink _unlink
5743
# define vsnprintf _vsnprintf
5844
# define write _write
59-
60-
/*
61-
* Map various parameters for POSIX...
62-
*/
63-
64-
# define F_OK 00
45+
# define F_OK 00 // POSIX parameters/flags
6546
# define W_OK 02
6647
# define R_OK 04
6748
# define O_RDONLY _O_RDONLY
@@ -71,7 +52,7 @@
7152
# define O_BINARY _O_BINARY
7253
# else // !_WIN32
7354
# include <unistd.h>
74-
# define O_BINARY 0
55+
# define O_BINARY 0 // Used on Windows for binary files...
7556
# endif // _WIN32
7657
# include <string.h>
7758
# include <ctype.h>
@@ -411,4 +392,5 @@ extern void _pdfioValueDelete(_pdfio_value_t *v) _PDFIO_INTERNAL;
411392
extern _pdfio_value_t *_pdfioValueRead(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_token_t *ts, _pdfio_value_t *v, size_t depth) _PDFIO_INTERNAL;
412393
extern bool _pdfioValueWrite(pdfio_file_t *pdf, pdfio_obj_t *obj, _pdfio_value_t *v, off_t *length) _PDFIO_INTERNAL;
413394

395+
414396
#endif // !PDFIO_PRIVATE_H

0 commit comments

Comments
 (0)