1
0
mirror of https://github.com/pdewacht/brlaser synced 2024-12-26 15:38:20 +01:00

Be tolerant of zero-page jobs

https://github.com/OpenPrinting/cups-filters/issues/117
This commit is contained in:
Peter De Wachter 2020-04-18 10:58:27 +02:00
parent eae5f9ba98
commit de0d48dfdb
5 changed files with 59 additions and 13 deletions

View File

@ -116,15 +116,17 @@ add_executable(brdecode EXCLUDE_FROM_ALL src/brdecode.cc)
add_executable(test_lest test/test_lest.cc)
add_executable(test_line test/test_line.cc src/line.cc)
add_executable(test_block test/test_block.cc)
add_executable(test_job test/test_job.cc src/job.cc src/line.cc)
enable_testing()
add_test(test_lest test_lest)
add_test(test_line test_line)
add_test(test_block test_block)
add_test(test_job test_job)
# Autotools-style "make check" command
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
add_dependencies(check test_lest test_line test_block)
add_dependencies(check test_lest test_line test_block test_job)
# Installation

View File

@ -26,17 +26,18 @@
job::job(FILE *out, const std::string &job_name)
: out_(out),
job_name_(job_name),
page_params_() {
page_params_(),
pages_(0) {
// Delete dubious characters from job name
std::replace_if(job_name_.begin(), job_name_.end(), [](char c) {
return c < 32 || c >= 127 || c == '"' || c == '\\';
}, ' ');
begin_job();
}
job::~job() {
if (pages_ != 0) {
end_job();
}
}
void job::begin_job() {
@ -86,6 +87,11 @@ void job::encode_page(const page_params &page_params,
int lines,
int linesize,
nextline_fn nextline) {
if (pages_ == 0) {
begin_job();
}
++pages_;
if (!(page_params_ == page_params)) {
page_params_ = page_params;
write_page_header();

View File

@ -55,6 +55,10 @@ class job {
int linesize,
nextline_fn nextline);
int pages() const {
return pages_;
}
private:
void begin_job();
void end_job();
@ -63,6 +67,7 @@ class job {
FILE *out_;
std::string job_name_;
page_params page_params_;
int pages_;
};
#endif // JOB_H

View File

@ -170,7 +170,6 @@ int main(int argc, char *argv[]) {
return 1;
}
int pages = 0;
{
job job(stdout, ascii_job_name(job_id, job_user, job_name));
cups_page_header2_t header;
@ -179,11 +178,11 @@ int main(int argc, char *argv[]) {
|| header.cupsBitsPerColor != 1
|| header.cupsNumColors != 1
|| header.cupsBytesPerLine > 10000) {
fprintf(stderr, "ERROR: " PACKAGE ": Page %d: Bogus raster data.\n", pages + 1);
fprintf(stderr, "ERROR: " PACKAGE ": Page %d: Bogus raster data.\n", job.pages() + 1);
dump_page_header(header);
return 1;
}
if (pages == 0) {
if (job.pages() == 0) {
fprintf(stderr, "DEBUG: " PACKAGE ": Page header of first page\n");
dump_page_header(header);
}
@ -191,13 +190,12 @@ int main(int argc, char *argv[]) {
header.cupsHeight,
header.cupsBytesPerLine,
next_line);
fprintf(stderr, "PAGE: %d %d\n", ++pages, header.NumCopies);
}
fprintf(stderr, "PAGE: %d %d\n", job.pages(), header.NumCopies);
}
if (pages == 0) {
if (job.pages() == 0) {
fprintf(stderr, "ERROR: " PACKAGE ": No pages were found.\n");
return 1;
}
}
fflush(stdout);

35
test/test_job.cc Normal file
View File

@ -0,0 +1,35 @@
// This file is part of the brlaser printer driver.
//
// Copyright 2020 Peter De Wachter
//
// brlaser is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// brlaser 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 brlaser. If not, see <http://www.gnu.org/licenses/>.
#include "lest.hpp"
#include "tempfile.h"
#include "../src/job.h"
const lest::test specification[] = {
"An empty job produces no output",
[] {
tempfile f;
{
job j(f.file(), "name");
}
EXPECT(f.data().empty());
},
};
int main() {
return lest::run(specification);
}