]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/pwg-file.c
4 * PWG load/save API implementation for CUPS.
6 * Copyright 2010 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * _pwgCreateWithFile() - Create PWG mapping data from a written file.
19 * _pwgDestroy() - Free all memory used for PWG mapping data.
20 * _pwgWriteFile() - Write PWG mapping data to a file.
24 * Include necessary headers...
27 #include "pwg-private.h"
35 * '_pwgCreateWithFile()' - Create PWG mapping data from a written file.
37 * Use the @link _pwgWriteFile@ function to write PWG mapping data to a file.
40 _pwg_t
* /* O - PWG mapping data */
41 _pwgCreateWithFile(const char *filename
)/* I - File to read */
43 cups_file_t
*fp
; /* File */
44 _pwg_t
*pwg
; /* PWG mapping data */
45 _pwg_size_t
*size
; /* Current size */
46 _pwg_map_t
*map
; /* Current map */
47 int linenum
, /* Current line number */
48 num_sizes
, /* Number of sizes in file */
49 num_sources
, /* Number of sources in file */
50 num_types
; /* Number of types in file */
51 char line
[512], /* Current line */
52 *value
, /* Pointer to value in line */
53 pwg_keyword
[128], /* PWG keyword */
54 ppd_keyword
[PPD_MAX_NAME
];
58 DEBUG_printf(("_pwgCreateWithFile(filename=\"%s\")", filename
));
61 * Range check input...
66 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
74 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
76 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
81 * Allocate the mapping data structure...
84 if ((pwg
= calloc(1, sizeof(_pwg_t
))) == NULL
)
86 DEBUG_puts("_pwgCreateWithFile: Unable to allocate pwg_t.");
87 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
100 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
102 DEBUG_printf(("_pwgCreateWithFile: line=\"%s\", value=\"%s\", linenum=%d",
103 line
, value
, linenum
));
107 DEBUG_printf(("_pwgCreateWithFile: Missing value on line %d.", linenum
));
108 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
111 else if (!strcasecmp(line
, "NumSizes"))
115 DEBUG_puts("_pwgCreateWithFile: NumSizes listed multiple times.");
116 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
120 if ((num_sizes
= atoi(value
)) <= 0 || num_sizes
> 65536)
122 DEBUG_printf(("_pwgCreateWithFile: Bad NumSizes value %d on line %d.",
123 num_sizes
, linenum
));
124 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
128 if ((pwg
->sizes
= calloc(num_sizes
, sizeof(_pwg_size_t
))) == NULL
)
130 DEBUG_printf(("_pwgCreateWithFile: Unable to allocate %d sizes.",
132 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
136 else if (!strcasecmp(line
, "Size"))
138 if (pwg
->num_sizes
>= num_sizes
)
140 DEBUG_printf(("_pwgCreateWithFile: Too many Size's on line %d.",
142 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
146 size
= pwg
->sizes
+ pwg
->num_sizes
;
148 if (sscanf(value
, "%127s%40s%d%d%d%d%d%d", pwg_keyword
, ppd_keyword
,
149 &(size
->width
), &(size
->length
), &(size
->left
),
150 &(size
->bottom
), &(size
->right
), &(size
->top
)) != 8)
152 DEBUG_printf(("_pwgCreateWithFile: Bad Size on line %d.", linenum
));
153 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
157 size
->map
.pwg
= _cupsStrAlloc(pwg_keyword
);
158 size
->map
.ppd
= _cupsStrAlloc(ppd_keyword
);
162 else if (!strcasecmp(line
, "CustomSize"))
164 if (pwg
->custom_max_width
> 0)
166 DEBUG_printf(("_pwgCreateWithFile: Too many CustomSize's on line %d.",
168 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
172 if (sscanf(value
, "%d%d%d%d%d%d%d%d", &(pwg
->custom_max_width
),
173 &(pwg
->custom_max_length
), &(pwg
->custom_min_width
),
174 &(pwg
->custom_min_length
), &(pwg
->custom_size
.left
),
175 &(pwg
->custom_size
.bottom
), &(pwg
->custom_size
.right
),
176 &(pwg
->custom_size
.top
)) != 8)
178 DEBUG_printf(("_pwgCreateWithFile: Bad CustomSize on line %d.", linenum
));
179 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
183 _pwgGenerateSize(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
184 pwg
->custom_max_width
, pwg
->custom_max_length
);
185 pwg
->custom_max_keyword
= _cupsStrAlloc(pwg_keyword
);
187 _pwgGenerateSize(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
188 pwg
->custom_min_width
, pwg
->custom_min_length
);
189 pwg
->custom_min_keyword
= _cupsStrAlloc(pwg_keyword
);
191 else if (!strcasecmp(line
, "NumSources"))
195 DEBUG_puts("_pwgCreateWithFile: NumSources listed multiple times.");
196 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
200 if ((num_sources
= atoi(value
)) <= 0 || num_sources
> 65536)
202 DEBUG_printf(("_pwgCreateWithFile: Bad NumSources value %d on line %d.",
203 num_sources
, linenum
));
204 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
208 if ((pwg
->sources
= calloc(num_sources
, sizeof(_pwg_map_t
))) == NULL
)
210 DEBUG_printf(("_pwgCreateWithFile: Unable to allocate %d sources.",
212 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
216 else if (!strcasecmp(line
, "Source"))
218 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
220 DEBUG_printf(("_pwgCreateWithFile: Bad Source on line %d.", linenum
));
221 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
225 if (pwg
->num_sources
>= num_sources
)
227 DEBUG_printf(("_pwgCreateWithFile: Too many Source's on line %d.",
229 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
233 map
= pwg
->sources
+ pwg
->num_sources
;
234 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
235 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
239 else if (!strcasecmp(line
, "NumTypes"))
243 DEBUG_puts("_pwgCreateWithFile: NumTypes listed multiple times.");
244 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
248 if ((num_types
= atoi(value
)) <= 0 || num_types
> 65536)
250 DEBUG_printf(("_pwgCreateWithFile: Bad NumTypes value %d on line %d.",
251 num_types
, linenum
));
252 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
256 if ((pwg
->types
= calloc(num_types
, sizeof(_pwg_map_t
))) == NULL
)
258 DEBUG_printf(("_pwgCreateWithFile: Unable to allocate %d types.",
260 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
264 else if (!strcasecmp(line
, "Type"))
266 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
268 DEBUG_printf(("_pwgCreateWithFile: Bad Type on line %d.", linenum
));
269 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
273 if (pwg
->num_types
>= num_types
)
275 DEBUG_printf(("_pwgCreateWithFile: Too many Type's on line %d.",
277 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
281 map
= pwg
->types
+ pwg
->num_types
;
282 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
283 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
289 DEBUG_printf(("_pwgCreateWithFile: Unknown %s on line %d.", line
,
291 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
296 if (pwg
->num_sizes
< num_sizes
)
298 DEBUG_printf(("_pwgCreateWithFile: Not enough sizes (%d < %d).",
299 pwg
->num_sizes
, num_sizes
));
300 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
304 if (pwg
->num_sources
< num_sources
)
306 DEBUG_printf(("_pwgCreateWithFile: Not enough sources (%d < %d).",
307 pwg
->num_sources
, num_sources
));
308 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
312 if (pwg
->num_types
< num_types
)
314 DEBUG_printf(("_pwgCreateWithFile: Not enough types (%d < %d).",
315 pwg
->num_types
, num_types
));
316 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad PWG mapping file."), 1);
325 * If we get here the file was bad - free any data and return...
338 * '_pwgDestroy()' - Free all memory used for PWG mapping data.
342 _pwgDestroy(_pwg_t
*pwg
) /* I - PWG mapping data */
344 int i
; /* Looping var */
345 _pwg_map_t
*map
; /* Current map */
346 _pwg_size_t
*size
; /* Current size */
350 * Range check input...
357 * Free memory as needed...
362 for (i
= pwg
->num_sizes
, size
= pwg
->sizes
; i
> 0; i
--, size
++)
364 _cupsStrFree(size
->map
.pwg
);
365 _cupsStrFree(size
->map
.ppd
);
373 for (i
= pwg
->num_sources
, map
= pwg
->sources
; i
> 0; i
--, map
++)
375 _cupsStrFree(map
->pwg
);
376 _cupsStrFree(map
->ppd
);
384 for (i
= pwg
->num_types
, map
= pwg
->types
; i
> 0; i
--, map
++)
386 _cupsStrFree(map
->pwg
);
387 _cupsStrFree(map
->ppd
);
393 if (pwg
->custom_max_keyword
)
394 _cupsStrFree(pwg
->custom_max_keyword
);
396 if (pwg
->custom_min_keyword
)
397 _cupsStrFree(pwg
->custom_min_keyword
);
404 * '_pwgWriteFile()' - Write PWG mapping data to a file.
407 int /* O - 1 on success, 0 on failure */
408 _pwgWriteFile(_pwg_t
*pwg
, /* I - PWG mapping data */
409 const char *filename
) /* I - File to write */
411 int i
; /* Looping var */
412 cups_file_t
*fp
; /* Output file */
413 _pwg_size_t
*size
; /* Current size */
414 _pwg_map_t
*map
; /* Current map */
418 * Range check input...
421 if (!pwg
|| !filename
)
423 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
428 * Open the file and write with compression...
431 if ((fp
= cupsFileOpen(filename
, "w9")) == NULL
)
433 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
441 cupsFilePuts(fp
, "#CUPS-PWGPPD\n");
447 cupsFilePrintf(fp
, "NumSizes %d\n", pwg
->num_sizes
);
448 for (i
= pwg
->num_sizes
, size
= pwg
->sizes
; i
> 0; i
--, size
++)
449 cupsFilePrintf(fp
, "Size %s %s %d %d %d %d %d %d\n", size
->map
.pwg
,
450 size
->map
.ppd
, size
->width
, size
->length
, size
->left
,
451 size
->bottom
, size
->right
, size
->top
);
452 if (pwg
->custom_max_width
> 0)
453 cupsFilePrintf(fp
, "CustomSize %d %d %d %d %d %d %d %d\n",
454 pwg
->custom_max_width
, pwg
->custom_max_length
,
455 pwg
->custom_min_width
, pwg
->custom_min_length
,
456 pwg
->custom_size
.left
, pwg
->custom_size
.bottom
,
457 pwg
->custom_size
.right
, pwg
->custom_size
.top
);
463 if (pwg
->num_sources
> 0)
465 cupsFilePrintf(fp
, "NumSources %d\n", pwg
->num_sources
);
466 for (i
= pwg
->num_sources
, map
= pwg
->sources
; i
> 0; i
--, map
++)
467 cupsFilePrintf(fp
, "Source %s %s\n", map
->pwg
, map
->ppd
);
474 if (pwg
->num_types
> 0)
476 cupsFilePrintf(fp
, "NumTypes %d\n", pwg
->num_types
);
477 for (i
= pwg
->num_types
, map
= pwg
->types
; i
> 0; i
--, map
++)
478 cupsFilePrintf(fp
, "Type %s %s\n", map
->pwg
, map
->ppd
);
482 * Close and return...
485 return (!cupsFileClose(fp
));